blob: a06c656b01fd13f88b291548529311456e1bfd13 [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"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080047#include <linux/reservation.h>
48#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Matt Roper465c1202014-05-29 08:06:54 -070050/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010051static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010052 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070054 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010055 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070056};
57
58/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010059static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010060 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010064 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
72 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010073 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070076 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053077 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070081};
82
Matt Roper3d7d6512014-06-10 08:28:13 -070083/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020089 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030090static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020091 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030092
Jesse Barneseb1bfe82014-02-12 12:26:25 -080093static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020097static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +020099static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200100static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700101 struct intel_link_m_n *m_n,
102 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200103static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200104static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200105static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200106static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200107 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200108static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200109 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200110static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
111static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700112static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
113 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200114static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
115 int num_connectors);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200116static void skylake_pfit_enable(struct intel_crtc *crtc);
117static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
118static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200119static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200120static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100121
Jesse Barnes79e53942008-11-07 14:24:08 -0800122typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400123 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800124} intel_range_t;
125
126typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 int dot_limit;
128 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800129} intel_p2_t;
130
Ma Lingd4906092009-03-18 20:13:27 +0800131typedef struct intel_limit intel_limit_t;
132struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400133 intel_range_t dot, vco, n, m, m1, m2, p, p1;
134 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800135};
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300137/* returns HPLL frequency in kHz */
138static int valleyview_get_vco(struct drm_i915_private *dev_priv)
139{
140 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
141
142 /* Obtain SKU information */
143 mutex_lock(&dev_priv->sb_lock);
144 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
145 CCK_FUSE_HPLL_FREQ_MASK;
146 mutex_unlock(&dev_priv->sb_lock);
147
148 return vco_freq[hpll_freq] * 1000;
149}
150
151static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
152 const char *name, u32 reg)
153{
154 u32 val;
155 int divider;
156
157 if (dev_priv->hpll_freq == 0)
158 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
159
160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
170 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
171}
172
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200173static int
174intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200175{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200176 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200177}
178
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200179static int
180intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300181{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200182 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
183 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200184}
185
186static int
187intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
188{
Jani Nikula79e50a42015-08-26 10:58:20 +0300189 uint32_t clkcfg;
190
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200191 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300192 clkcfg = I915_READ(CLKCFG);
193 switch (clkcfg & CLKCFG_FSB_MASK) {
194 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300196 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200197 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200199 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 /* these two are just a guess; one of them might be right */
207 case CLKCFG_FSB_1600:
208 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 }
213}
214
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215static void intel_update_rawclk(struct drm_i915_private *dev_priv)
216{
217 if (HAS_PCH_SPLIT(dev_priv))
218 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
219 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
220 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
221 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
222 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
223 else
224 return; /* no rawclk on other platforms, or no need to know it */
225
226 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
227}
228
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300229static void intel_update_czclk(struct drm_i915_private *dev_priv)
230{
Wayne Boyer666a4532015-12-09 12:29:35 -0800231 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300232 return;
233
234 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
235 CCK_CZ_CLOCK_CONTROL);
236
237 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
238}
239
Chris Wilson021357a2010-09-07 20:54:59 +0100240static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200241intel_fdi_link_freq(struct drm_i915_private *dev_priv,
242 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100243{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200244 if (HAS_DDI(dev_priv))
245 return pipe_config->port_clock; /* SPLL */
246 else if (IS_GEN5(dev_priv))
247 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200248 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200249 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100250}
251
Daniel Vetter5d536e22013-07-06 12:52:06 +0200252static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400253 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200254 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200255 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .m = { .min = 96, .max = 140 },
257 .m1 = { .min = 18, .max = 26 },
258 .m2 = { .min = 6, .max = 16 },
259 .p = { .min = 4, .max = 128 },
260 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .p2 = { .dot_limit = 165000,
262 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700263};
264
Daniel Vetter5d536e22013-07-06 12:52:06 +0200265static const intel_limit_t intel_limits_i8xx_dvo = {
266 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200267 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200268 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200269 .m = { .min = 96, .max = 140 },
270 .m1 = { .min = 18, .max = 26 },
271 .m2 = { .min = 6, .max = 16 },
272 .p = { .min = 4, .max = 128 },
273 .p1 = { .min = 2, .max = 33 },
274 .p2 = { .dot_limit = 165000,
275 .p2_slow = 4, .p2_fast = 4 },
276};
277
Keith Packarde4b36692009-06-05 19:22:17 -0700278static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200280 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200281 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .m = { .min = 96, .max = 140 },
283 .m1 = { .min = 18, .max = 26 },
284 .m2 = { .min = 6, .max = 16 },
285 .p = { .min = 4, .max = 128 },
286 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .p2 = { .dot_limit = 165000,
288 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700289};
Eric Anholt273e27c2011-03-30 13:01:10 -0700290
Keith Packarde4b36692009-06-05 19:22:17 -0700291static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .dot = { .min = 20000, .max = 400000 },
293 .vco = { .min = 1400000, .max = 2800000 },
294 .n = { .min = 1, .max = 6 },
295 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100296 .m1 = { .min = 8, .max = 18 },
297 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400298 .p = { .min = 5, .max = 80 },
299 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .p2 = { .dot_limit = 200000,
301 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
304static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400305 .dot = { .min = 20000, .max = 400000 },
306 .vco = { .min = 1400000, .max = 2800000 },
307 .n = { .min = 1, .max = 6 },
308 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100309 .m1 = { .min = 8, .max = 18 },
310 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400311 .p = { .min = 7, .max = 98 },
312 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700313 .p2 = { .dot_limit = 112000,
314 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700315};
316
Eric Anholt273e27c2011-03-30 13:01:10 -0700317
Keith Packarde4b36692009-06-05 19:22:17 -0700318static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .dot = { .min = 25000, .max = 270000 },
320 .vco = { .min = 1750000, .max = 3500000},
321 .n = { .min = 1, .max = 4 },
322 .m = { .min = 104, .max = 138 },
323 .m1 = { .min = 17, .max = 23 },
324 .m2 = { .min = 5, .max = 11 },
325 .p = { .min = 10, .max = 30 },
326 .p1 = { .min = 1, .max = 3},
327 .p2 = { .dot_limit = 270000,
328 .p2_slow = 10,
329 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800330 },
Keith Packarde4b36692009-06-05 19:22:17 -0700331};
332
333static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 22000, .max = 400000 },
335 .vco = { .min = 1750000, .max = 3500000},
336 .n = { .min = 1, .max = 4 },
337 .m = { .min = 104, .max = 138 },
338 .m1 = { .min = 16, .max = 23 },
339 .m2 = { .min = 5, .max = 11 },
340 .p = { .min = 5, .max = 80 },
341 .p1 = { .min = 1, .max = 8},
342 .p2 = { .dot_limit = 165000,
343 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700344};
345
346static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700347 .dot = { .min = 20000, .max = 115000 },
348 .vco = { .min = 1750000, .max = 3500000 },
349 .n = { .min = 1, .max = 3 },
350 .m = { .min = 104, .max = 138 },
351 .m1 = { .min = 17, .max = 23 },
352 .m2 = { .min = 5, .max = 11 },
353 .p = { .min = 28, .max = 112 },
354 .p1 = { .min = 2, .max = 8 },
355 .p2 = { .dot_limit = 0,
356 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800357 },
Keith Packarde4b36692009-06-05 19:22:17 -0700358};
359
360static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700361 .dot = { .min = 80000, .max = 224000 },
362 .vco = { .min = 1750000, .max = 3500000 },
363 .n = { .min = 1, .max = 3 },
364 .m = { .min = 104, .max = 138 },
365 .m1 = { .min = 17, .max = 23 },
366 .m2 = { .min = 5, .max = 11 },
367 .p = { .min = 14, .max = 42 },
368 .p1 = { .min = 2, .max = 6 },
369 .p2 = { .dot_limit = 0,
370 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800371 },
Keith Packarde4b36692009-06-05 19:22:17 -0700372};
373
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500374static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400375 .dot = { .min = 20000, .max = 400000},
376 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700377 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400378 .n = { .min = 3, .max = 6 },
379 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700380 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400381 .m1 = { .min = 0, .max = 0 },
382 .m2 = { .min = 0, .max = 254 },
383 .p = { .min = 5, .max = 80 },
384 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700385 .p2 = { .dot_limit = 200000,
386 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700387};
388
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500389static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400390 .dot = { .min = 20000, .max = 400000 },
391 .vco = { .min = 1700000, .max = 3500000 },
392 .n = { .min = 3, .max = 6 },
393 .m = { .min = 2, .max = 256 },
394 .m1 = { .min = 0, .max = 0 },
395 .m2 = { .min = 0, .max = 254 },
396 .p = { .min = 7, .max = 112 },
397 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700398 .p2 = { .dot_limit = 112000,
399 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700400};
401
Eric Anholt273e27c2011-03-30 13:01:10 -0700402/* Ironlake / Sandybridge
403 *
404 * We calculate clock using (register_value + 2) for N/M1/M2, so here
405 * the range value for them is (actual_value - 2).
406 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800407static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .dot = { .min = 25000, .max = 350000 },
409 .vco = { .min = 1760000, .max = 3510000 },
410 .n = { .min = 1, .max = 5 },
411 .m = { .min = 79, .max = 127 },
412 .m1 = { .min = 12, .max = 22 },
413 .m2 = { .min = 5, .max = 9 },
414 .p = { .min = 5, .max = 80 },
415 .p1 = { .min = 1, .max = 8 },
416 .p2 = { .dot_limit = 225000,
417 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700418};
419
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800420static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700421 .dot = { .min = 25000, .max = 350000 },
422 .vco = { .min = 1760000, .max = 3510000 },
423 .n = { .min = 1, .max = 3 },
424 .m = { .min = 79, .max = 118 },
425 .m1 = { .min = 12, .max = 22 },
426 .m2 = { .min = 5, .max = 9 },
427 .p = { .min = 28, .max = 112 },
428 .p1 = { .min = 2, .max = 8 },
429 .p2 = { .dot_limit = 225000,
430 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800431};
432
433static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700434 .dot = { .min = 25000, .max = 350000 },
435 .vco = { .min = 1760000, .max = 3510000 },
436 .n = { .min = 1, .max = 3 },
437 .m = { .min = 79, .max = 127 },
438 .m1 = { .min = 12, .max = 22 },
439 .m2 = { .min = 5, .max = 9 },
440 .p = { .min = 14, .max = 56 },
441 .p1 = { .min = 2, .max = 8 },
442 .p2 = { .dot_limit = 225000,
443 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800444};
445
Eric Anholt273e27c2011-03-30 13:01:10 -0700446/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800447static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700448 .dot = { .min = 25000, .max = 350000 },
449 .vco = { .min = 1760000, .max = 3510000 },
450 .n = { .min = 1, .max = 2 },
451 .m = { .min = 79, .max = 126 },
452 .m1 = { .min = 12, .max = 22 },
453 .m2 = { .min = 5, .max = 9 },
454 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400455 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700456 .p2 = { .dot_limit = 225000,
457 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800458};
459
460static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700461 .dot = { .min = 25000, .max = 350000 },
462 .vco = { .min = 1760000, .max = 3510000 },
463 .n = { .min = 1, .max = 3 },
464 .m = { .min = 79, .max = 126 },
465 .m1 = { .min = 12, .max = 22 },
466 .m2 = { .min = 5, .max = 9 },
467 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400468 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700469 .p2 = { .dot_limit = 225000,
470 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800471};
472
Ville Syrjälädc730512013-09-24 21:26:30 +0300473static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300474 /*
475 * These are the data rate limits (measured in fast clocks)
476 * since those are the strictest limits we have. The fast
477 * clock and actual rate limits are more relaxed, so checking
478 * them would make no difference.
479 */
480 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200481 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700482 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700483 .m1 = { .min = 2, .max = 3 },
484 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300485 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300486 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700487};
488
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300489static const intel_limit_t intel_limits_chv = {
490 /*
491 * These are the data rate limits (measured in fast clocks)
492 * since those are the strictest limits we have. The fast
493 * clock and actual rate limits are more relaxed, so checking
494 * them would make no difference.
495 */
496 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200497 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300498 .n = { .min = 1, .max = 1 },
499 .m1 = { .min = 2, .max = 2 },
500 .m2 = { .min = 24 << 22, .max = 175 << 22 },
501 .p1 = { .min = 2, .max = 4 },
502 .p2 = { .p2_slow = 1, .p2_fast = 14 },
503};
504
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200505static const intel_limit_t intel_limits_bxt = {
506 /* FIXME: find real dot limits */
507 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530508 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 /* FIXME: find real m2 limits */
512 .m2 = { .min = 2 << 22, .max = 255 << 22 },
513 .p1 = { .min = 2, .max = 4 },
514 .p2 = { .p2_slow = 1, .p2_fast = 20 },
515};
516
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200517static bool
518needs_modeset(struct drm_crtc_state *state)
519{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200520 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200521}
522
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300523/**
524 * Returns whether any output on the specified pipe is of the specified type
525 */
Damien Lespiau40935612014-10-29 11:16:59 +0000526bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300527{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300528 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300529 struct intel_encoder *encoder;
530
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300531 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300532 if (encoder->type == type)
533 return true;
534
535 return false;
536}
537
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200538/**
539 * Returns whether any output on the specified pipe will have the specified
540 * type after a staged modeset is complete, i.e., the same as
541 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
542 * encoder->crtc.
543 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200544static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
545 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200546{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200547 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300548 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200550 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200552
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300553 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554 if (connector_state->crtc != crtc_state->base.crtc)
555 continue;
556
557 num_connectors++;
558
559 encoder = to_intel_encoder(connector_state->best_encoder);
560 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200561 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200562 }
563
564 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200565
566 return false;
567}
568
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200569static const intel_limit_t *
570intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800571{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200572 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800573 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800574
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200575 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100576 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000577 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800578 limit = &intel_limits_ironlake_dual_lvds_100m;
579 else
580 limit = &intel_limits_ironlake_dual_lvds;
581 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000582 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800583 limit = &intel_limits_ironlake_single_lvds_100m;
584 else
585 limit = &intel_limits_ironlake_single_lvds;
586 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200587 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800588 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800589
590 return limit;
591}
592
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200593static const intel_limit_t *
594intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800595{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200596 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800597 const intel_limit_t *limit;
598
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200599 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100600 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700601 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800602 else
Keith Packarde4b36692009-06-05 19:22:17 -0700603 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200604 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
605 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700606 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200607 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700608 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800609 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700610 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800611
612 return limit;
613}
614
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200615static const intel_limit_t *
616intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800617{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200618 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 const intel_limit_t *limit;
620
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200621 if (IS_BROXTON(dev))
622 limit = &intel_limits_bxt;
623 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200624 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800625 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200626 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500627 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200628 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500629 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800630 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500631 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300632 } else if (IS_CHERRYVIEW(dev)) {
633 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700634 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300635 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100636 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200637 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100638 limit = &intel_limits_i9xx_lvds;
639 else
640 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800641 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200642 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700643 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200644 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700645 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200646 else
647 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 }
649 return limit;
650}
651
Imre Deakdccbea32015-06-22 23:35:51 +0300652/*
653 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
654 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
655 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
656 * The helpers' return value is the rate of the clock that is fed to the
657 * display engine's pipe which can be the above fast dot clock rate or a
658 * divided-down version of it.
659 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500660/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300661static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800662{
Shaohua Li21778322009-02-23 15:19:16 +0800663 clock->m = clock->m2 + 2;
664 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200665 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300666 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300667 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
668 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300669
670 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800671}
672
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200673static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
674{
675 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
676}
677
Imre Deakdccbea32015-06-22 23:35:51 +0300678static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800679{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200680 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200682 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300683 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300684 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
685 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300686
687 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800688}
689
Imre Deakdccbea32015-06-22 23:35:51 +0300690static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300691{
692 clock->m = clock->m1 * clock->m2;
693 clock->p = clock->p1 * clock->p2;
694 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300695 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300696 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
697 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300698
699 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300700}
701
Imre Deakdccbea32015-06-22 23:35:51 +0300702int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300703{
704 clock->m = clock->m1 * clock->m2;
705 clock->p = clock->p1 * clock->p2;
706 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300707 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300708 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
709 clock->n << 22);
710 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300711
712 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300713}
714
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800715#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800716/**
717 * Returns whether the given set of divisors are valid for a given refclk with
718 * the given connectors.
719 */
720
Chris Wilson1b894b52010-12-14 20:04:54 +0000721static bool intel_PLL_is_valid(struct drm_device *dev,
722 const intel_limit_t *limit,
723 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800724{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300725 if (clock->n < limit->n.min || limit->n.max < clock->n)
726 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800727 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400728 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800729 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400730 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800731 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400732 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300733
Wayne Boyer666a4532015-12-09 12:29:35 -0800734 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
735 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300736 if (clock->m1 <= clock->m2)
737 INTELPllInvalid("m1 <= m2\n");
738
Wayne Boyer666a4532015-12-09 12:29:35 -0800739 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300740 if (clock->p < limit->p.min || limit->p.max < clock->p)
741 INTELPllInvalid("p out of range\n");
742 if (clock->m < limit->m.min || limit->m.max < clock->m)
743 INTELPllInvalid("m out of range\n");
744 }
745
Jesse Barnes79e53942008-11-07 14:24:08 -0800746 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400747 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
749 * connector, etc., rather than just a single range.
750 */
751 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400752 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800753
754 return true;
755}
756
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300757static int
758i9xx_select_p2_div(const intel_limit_t *limit,
759 const struct intel_crtc_state *crtc_state,
760 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800761{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300762 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800763
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200764 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800765 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100766 * For LVDS just rely on its current settings for dual-channel.
767 * We haven't figured out how to reliably set up different
768 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800769 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100770 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300771 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800772 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300773 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800774 } else {
775 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300776 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800777 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800779 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300780}
781
782static bool
783i9xx_find_best_dpll(const intel_limit_t *limit,
784 struct intel_crtc_state *crtc_state,
785 int target, int refclk, intel_clock_t *match_clock,
786 intel_clock_t *best_clock)
787{
788 struct drm_device *dev = crtc_state->base.crtc->dev;
789 intel_clock_t clock;
790 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800791
Akshay Joshi0206e352011-08-16 15:34:10 -0400792 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800793
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300794 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
795
Zhao Yakui42158662009-11-20 11:24:18 +0800796 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
797 clock.m1++) {
798 for (clock.m2 = limit->m2.min;
799 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200800 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800801 break;
802 for (clock.n = limit->n.min;
803 clock.n <= limit->n.max; clock.n++) {
804 for (clock.p1 = limit->p1.min;
805 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800806 int this_err;
807
Imre Deakdccbea32015-06-22 23:35:51 +0300808 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000809 if (!intel_PLL_is_valid(dev, limit,
810 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800811 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800812 if (match_clock &&
813 clock.p != match_clock->p)
814 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800815
816 this_err = abs(clock.dot - target);
817 if (this_err < err) {
818 *best_clock = clock;
819 err = this_err;
820 }
821 }
822 }
823 }
824 }
825
826 return (err != target);
827}
828
Ma Lingd4906092009-03-18 20:13:27 +0800829static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200830pnv_find_best_dpll(const intel_limit_t *limit,
831 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200832 int target, int refclk, intel_clock_t *match_clock,
833 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200834{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300835 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200836 intel_clock_t clock;
837 int err = target;
838
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200839 memset(best_clock, 0, sizeof(*best_clock));
840
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300841 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
842
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200843 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
844 clock.m1++) {
845 for (clock.m2 = limit->m2.min;
846 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200847 for (clock.n = limit->n.min;
848 clock.n <= limit->n.max; clock.n++) {
849 for (clock.p1 = limit->p1.min;
850 clock.p1 <= limit->p1.max; clock.p1++) {
851 int this_err;
852
Imre Deakdccbea32015-06-22 23:35:51 +0300853 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800854 if (!intel_PLL_is_valid(dev, limit,
855 &clock))
856 continue;
857 if (match_clock &&
858 clock.p != match_clock->p)
859 continue;
860
861 this_err = abs(clock.dot - target);
862 if (this_err < err) {
863 *best_clock = clock;
864 err = this_err;
865 }
866 }
867 }
868 }
869 }
870
871 return (err != target);
872}
873
Ma Lingd4906092009-03-18 20:13:27 +0800874static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200875g4x_find_best_dpll(const intel_limit_t *limit,
876 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200877 int target, int refclk, intel_clock_t *match_clock,
878 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800879{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300880 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800881 intel_clock_t clock;
882 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300883 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400884 /* approximately equals target * 0.00585 */
885 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800886
887 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300888
889 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
890
Ma Lingd4906092009-03-18 20:13:27 +0800891 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200892 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800893 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200894 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800895 for (clock.m1 = limit->m1.max;
896 clock.m1 >= limit->m1.min; clock.m1--) {
897 for (clock.m2 = limit->m2.max;
898 clock.m2 >= limit->m2.min; clock.m2--) {
899 for (clock.p1 = limit->p1.max;
900 clock.p1 >= limit->p1.min; clock.p1--) {
901 int this_err;
902
Imre Deakdccbea32015-06-22 23:35:51 +0300903 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000904 if (!intel_PLL_is_valid(dev, limit,
905 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800906 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000907
908 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800909 if (this_err < err_most) {
910 *best_clock = clock;
911 err_most = this_err;
912 max_n = clock.n;
913 found = true;
914 }
915 }
916 }
917 }
918 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800919 return found;
920}
Ma Lingd4906092009-03-18 20:13:27 +0800921
Imre Deakd5dd62b2015-03-17 11:40:03 +0200922/*
923 * Check if the calculated PLL configuration is more optimal compared to the
924 * best configuration and error found so far. Return the calculated error.
925 */
926static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
927 const intel_clock_t *calculated_clock,
928 const intel_clock_t *best_clock,
929 unsigned int best_error_ppm,
930 unsigned int *error_ppm)
931{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200932 /*
933 * For CHV ignore the error and consider only the P value.
934 * Prefer a bigger P value based on HW requirements.
935 */
936 if (IS_CHERRYVIEW(dev)) {
937 *error_ppm = 0;
938
939 return calculated_clock->p > best_clock->p;
940 }
941
Imre Deak24be4e42015-03-17 11:40:04 +0200942 if (WARN_ON_ONCE(!target_freq))
943 return false;
944
Imre Deakd5dd62b2015-03-17 11:40:03 +0200945 *error_ppm = div_u64(1000000ULL *
946 abs(target_freq - calculated_clock->dot),
947 target_freq);
948 /*
949 * Prefer a better P value over a better (smaller) error if the error
950 * is small. Ensure this preference for future configurations too by
951 * setting the error to 0.
952 */
953 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
954 *error_ppm = 0;
955
956 return true;
957 }
958
959 return *error_ppm + 10 < best_error_ppm;
960}
961
Zhenyu Wang2c072452009-06-05 15:38:42 +0800962static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200963vlv_find_best_dpll(const intel_limit_t *limit,
964 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200965 int target, int refclk, intel_clock_t *match_clock,
966 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700967{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200968 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300969 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300970 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300971 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300972 /* min update 19.2 MHz */
973 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300974 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700975
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300976 target *= 5; /* fast clock */
977
978 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700979
980 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300981 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300982 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300983 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300984 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300985 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700986 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300987 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200988 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300989
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300990 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
991 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300992
Imre Deakdccbea32015-06-22 23:35:51 +0300993 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300994
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300995 if (!intel_PLL_is_valid(dev, limit,
996 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300997 continue;
998
Imre Deakd5dd62b2015-03-17 11:40:03 +0200999 if (!vlv_PLL_is_optimal(dev, target,
1000 &clock,
1001 best_clock,
1002 bestppm, &ppm))
1003 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +03001004
Imre Deakd5dd62b2015-03-17 11:40:03 +02001005 *best_clock = clock;
1006 bestppm = ppm;
1007 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001008 }
1009 }
1010 }
1011 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001012
Ville Syrjälä49e497e2013-09-24 21:26:31 +03001013 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001014}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001015
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001016static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001017chv_find_best_dpll(const intel_limit_t *limit,
1018 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001019 int target, int refclk, intel_clock_t *match_clock,
1020 intel_clock_t *best_clock)
1021{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001022 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001023 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001024 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001025 intel_clock_t clock;
1026 uint64_t m2;
1027 int found = false;
1028
1029 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001030 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001031
1032 /*
1033 * Based on hardware doc, the n always set to 1, and m1 always
1034 * set to 2. If requires to support 200Mhz refclk, we need to
1035 * revisit this because n may not 1 anymore.
1036 */
1037 clock.n = 1, clock.m1 = 2;
1038 target *= 5; /* fast clock */
1039
1040 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1041 for (clock.p2 = limit->p2.p2_fast;
1042 clock.p2 >= limit->p2.p2_slow;
1043 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001044 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001045
1046 clock.p = clock.p1 * clock.p2;
1047
1048 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1049 clock.n) << 22, refclk * clock.m1);
1050
1051 if (m2 > INT_MAX/clock.m1)
1052 continue;
1053
1054 clock.m2 = m2;
1055
Imre Deakdccbea32015-06-22 23:35:51 +03001056 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001057
1058 if (!intel_PLL_is_valid(dev, limit, &clock))
1059 continue;
1060
Imre Deak9ca3ba02015-03-17 11:40:05 +02001061 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1062 best_error_ppm, &error_ppm))
1063 continue;
1064
1065 *best_clock = clock;
1066 best_error_ppm = error_ppm;
1067 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001068 }
1069 }
1070
1071 return found;
1072}
1073
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001074bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1075 intel_clock_t *best_clock)
1076{
1077 int refclk = i9xx_get_refclk(crtc_state, 0);
1078
1079 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1080 target_clock, refclk, NULL, best_clock);
1081}
1082
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001083bool intel_crtc_active(struct drm_crtc *crtc)
1084{
1085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1086
1087 /* Be paranoid as we can arrive here with only partial
1088 * state retrieved from the hardware during setup.
1089 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001090 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001091 * as Haswell has gained clock readout/fastboot support.
1092 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001093 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001094 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001095 *
1096 * FIXME: The intel_crtc->active here should be switched to
1097 * crtc->state->active once we have proper CRTC states wired up
1098 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001099 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001100 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001101 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001102}
1103
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001104enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1105 enum pipe pipe)
1106{
1107 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1109
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001110 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001111}
1112
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001113static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1114{
1115 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001116 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001117 u32 line1, line2;
1118 u32 line_mask;
1119
1120 if (IS_GEN2(dev))
1121 line_mask = DSL_LINEMASK_GEN2;
1122 else
1123 line_mask = DSL_LINEMASK_GEN3;
1124
1125 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001126 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001127 line2 = I915_READ(reg) & line_mask;
1128
1129 return line1 == line2;
1130}
1131
Keith Packardab7ad7f2010-10-03 00:33:06 -07001132/*
1133 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001134 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001135 *
1136 * After disabling a pipe, we can't wait for vblank in the usual way,
1137 * spinning on the vblank interrupt status bit, since we won't actually
1138 * see an interrupt when the pipe is disabled.
1139 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001140 * On Gen4 and above:
1141 * wait for the pipe register state bit to turn off
1142 *
1143 * Otherwise:
1144 * wait for the display line value to settle (it usually
1145 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001146 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001147 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001148static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001149{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001150 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001151 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001152 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001153 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001154
Keith Packardab7ad7f2010-10-03 00:33:06 -07001155 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001156 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001157
Keith Packardab7ad7f2010-10-03 00:33:06 -07001158 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001159 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1160 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001161 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001162 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001163 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001164 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001165 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001166 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001167}
1168
Jesse Barnesb24e7172011-01-04 15:09:30 -08001169/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001170void assert_pll(struct drm_i915_private *dev_priv,
1171 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001172{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001173 u32 val;
1174 bool cur_state;
1175
Ville Syrjälä649636e2015-09-22 19:50:01 +03001176 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001177 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001178 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001179 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001180 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001181}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001182
Jani Nikula23538ef2013-08-27 15:12:22 +03001183/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001184void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001185{
1186 u32 val;
1187 bool cur_state;
1188
Ville Syrjäläa5805162015-05-26 20:42:30 +03001189 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001190 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001191 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001192
1193 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001194 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001195 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001196 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001197}
Jani Nikula23538ef2013-08-27 15:12:22 +03001198
Jesse Barnes040484a2011-01-03 12:14:26 -08001199static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1200 enum pipe pipe, bool state)
1201{
Jesse Barnes040484a2011-01-03 12:14:26 -08001202 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001203 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1204 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001205
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001206 if (HAS_DDI(dev_priv->dev)) {
1207 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001208 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001209 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001210 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001211 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001212 cur_state = !!(val & FDI_TX_ENABLE);
1213 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001215 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001216 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001217}
1218#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1219#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1220
1221static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1222 enum pipe pipe, bool state)
1223{
Jesse Barnes040484a2011-01-03 12:14:26 -08001224 u32 val;
1225 bool cur_state;
1226
Ville Syrjälä649636e2015-09-22 19:50:01 +03001227 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001228 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001229 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001230 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001231 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001232}
1233#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1234#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1235
1236static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1237 enum pipe pipe)
1238{
Jesse Barnes040484a2011-01-03 12:14:26 -08001239 u32 val;
1240
1241 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001242 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001243 return;
1244
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001245 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001246 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001247 return;
1248
Ville Syrjälä649636e2015-09-22 19:50:01 +03001249 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001250 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 -08001251}
1252
Daniel Vetter55607e82013-06-16 21:42:39 +02001253void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1254 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001255{
Jesse Barnes040484a2011-01-03 12:14:26 -08001256 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001258
Ville Syrjälä649636e2015-09-22 19:50:01 +03001259 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001260 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001261 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001262 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001263 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001264}
1265
Daniel Vetterb680c372014-09-19 18:27:27 +02001266void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1267 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001269 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001270 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271 u32 val;
1272 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001273 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274
Jani Nikulabedd4db2014-08-22 15:04:13 +03001275 if (WARN_ON(HAS_DDI(dev)))
1276 return;
1277
1278 if (HAS_PCH_SPLIT(dev)) {
1279 u32 port_sel;
1280
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001282 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1283
1284 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1285 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
1287 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001288 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 /* presumably write lock depends on pipe, not port select */
1290 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1291 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 } else {
1293 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001294 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1295 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 }
1297
1298 val = I915_READ(pp_reg);
1299 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001300 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301 locked = false;
1302
Rob Clarke2c719b2014-12-15 13:56:32 -05001303 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001304 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001305 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001306}
1307
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308static void assert_cursor(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, bool state)
1310{
1311 struct drm_device *dev = dev_priv->dev;
1312 bool cur_state;
1313
Paulo Zanonid9d82082014-02-27 16:30:56 -03001314 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001315 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001316 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001317 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001318
Rob Clarke2c719b2014-12-15 13:56:32 -05001319 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001320 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001321 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001322}
1323#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1324#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1325
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001326void assert_pipe(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001329 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001330 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1331 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001332 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001334 /* if we need the pipe quirk it must be always on */
1335 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1336 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001337 state = true;
1338
Imre Deak4feed0e2016-02-12 18:55:14 +02001339 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1340 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001342 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001343
1344 intel_display_power_put(dev_priv, power_domain);
1345 } else {
1346 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001347 }
1348
Rob Clarke2c719b2014-12-15 13:56:32 -05001349 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001350 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001351 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352}
1353
Chris Wilson931872f2012-01-16 23:01:13 +00001354static void assert_plane(struct drm_i915_private *dev_priv,
1355 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001358 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001359
Ville Syrjälä649636e2015-09-22 19:50:01 +03001360 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001361 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001363 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001364 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365}
1366
Chris Wilson931872f2012-01-16 23:01:13 +00001367#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1368#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1369
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1371 enum pipe pipe)
1372{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001373 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001374 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001375
Ville Syrjälä653e1022013-06-04 13:49:05 +03001376 /* Primary planes are fixed to pipes on gen4+ */
1377 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001378 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001380 "plane %c assertion failure, should be disabled but not\n",
1381 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001383 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001384
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001386 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001387 u32 val = I915_READ(DSPCNTR(i));
1388 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001389 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001390 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001391 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1392 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001393 }
1394}
1395
Jesse Barnes19332d72013-03-28 09:55:38 -07001396static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1397 enum pipe pipe)
1398{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001399 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001400 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001401
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001402 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001403 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001404 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1407 sprite, pipe_name(pipe));
1408 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001409 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001410 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001411 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001412 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001413 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001414 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001415 }
1416 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001417 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001418 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001419 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001420 plane_name(pipe), pipe_name(pipe));
1421 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001422 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1425 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001426 }
1427}
1428
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001429static void assert_vblank_disabled(struct drm_crtc *crtc)
1430{
Rob Clarke2c719b2014-12-15 13:56:32 -05001431 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001432 drm_crtc_vblank_put(crtc);
1433}
1434
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001435void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1436 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001437{
Jesse Barnes92f25842011-01-04 15:09:34 -08001438 u32 val;
1439 bool enabled;
1440
Ville Syrjälä649636e2015-09-22 19:50:01 +03001441 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001442 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001443 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001444 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1445 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001446}
1447
Keith Packard4e634382011-08-06 10:39:45 -07001448static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1449 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001450{
1451 if ((val & DP_PORT_EN) == 0)
1452 return false;
1453
1454 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001455 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001456 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1457 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001458 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1459 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1460 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001461 } else {
1462 if ((val & DP_PIPE_MASK) != (pipe << 30))
1463 return false;
1464 }
1465 return true;
1466}
1467
Keith Packard1519b992011-08-06 10:35:34 -07001468static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe, u32 val)
1470{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001471 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001472 return false;
1473
1474 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001475 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001476 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001477 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1478 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1479 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001480 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001481 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001482 return false;
1483 }
1484 return true;
1485}
1486
1487static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1488 enum pipe pipe, u32 val)
1489{
1490 if ((val & LVDS_PORT_EN) == 0)
1491 return false;
1492
1493 if (HAS_PCH_CPT(dev_priv->dev)) {
1494 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1495 return false;
1496 } else {
1497 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1498 return false;
1499 }
1500 return true;
1501}
1502
1503static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1504 enum pipe pipe, u32 val)
1505{
1506 if ((val & ADPA_DAC_ENABLE) == 0)
1507 return false;
1508 if (HAS_PCH_CPT(dev_priv->dev)) {
1509 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1510 return false;
1511 } else {
1512 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1513 return false;
1514 }
1515 return true;
1516}
1517
Jesse Barnes291906f2011-02-02 12:28:03 -08001518static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001519 enum pipe pipe, i915_reg_t reg,
1520 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001521{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001522 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001523 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001524 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001525 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001526
Rob Clarke2c719b2014-12-15 13:56:32 -05001527 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001528 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001529 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001530}
1531
1532static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001533 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001534{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001535 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001536 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001537 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001538 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001539
Rob Clarke2c719b2014-12-15 13:56:32 -05001540 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001541 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001542 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001543}
1544
1545static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1546 enum pipe pipe)
1547{
Jesse Barnes291906f2011-02-02 12:28:03 -08001548 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001549
Keith Packardf0575e92011-07-25 22:12:43 -07001550 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1551 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1552 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001553
Ville Syrjälä649636e2015-09-22 19:50:01 +03001554 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001555 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001556 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001557 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001558
Ville Syrjälä649636e2015-09-22 19:50:01 +03001559 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001560 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001561 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001562 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001563
Paulo Zanonie2debe92013-02-18 19:00:27 -03001564 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1565 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1566 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001567}
1568
Ville Syrjäläd288f652014-10-28 13:20:22 +02001569static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001570 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001571{
Daniel Vetter426115c2013-07-11 22:13:42 +02001572 struct drm_device *dev = crtc->base.dev;
1573 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001574 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001575 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001576
Daniel Vetter426115c2013-07-11 22:13:42 +02001577 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001578
Daniel Vetter87442f72013-06-06 00:52:17 +02001579 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001580 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001581 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001582
Daniel Vetter426115c2013-07-11 22:13:42 +02001583 I915_WRITE(reg, dpll);
1584 POSTING_READ(reg);
1585 udelay(150);
1586
1587 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1588 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1589
Ville Syrjäläd288f652014-10-28 13:20:22 +02001590 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001591 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001592
1593 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001594 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001595 POSTING_READ(reg);
1596 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001597 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001598 POSTING_READ(reg);
1599 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001601 POSTING_READ(reg);
1602 udelay(150); /* wait for warmup */
1603}
1604
Ville Syrjäläd288f652014-10-28 13:20:22 +02001605static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001606 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607{
1608 struct drm_device *dev = crtc->base.dev;
1609 struct drm_i915_private *dev_priv = dev->dev_private;
1610 int pipe = crtc->pipe;
1611 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001612 u32 tmp;
1613
1614 assert_pipe_disabled(dev_priv, crtc->pipe);
1615
Ville Syrjäläa5805162015-05-26 20:42:30 +03001616 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001617
1618 /* Enable back the 10bit clock to display controller */
1619 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1620 tmp |= DPIO_DCLKP_EN;
1621 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1622
Ville Syrjälä54433e92015-05-26 20:42:31 +03001623 mutex_unlock(&dev_priv->sb_lock);
1624
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001625 /*
1626 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1627 */
1628 udelay(1);
1629
1630 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001631 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001632
1633 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001634 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001635 DRM_ERROR("PLL %d failed to lock\n", pipe);
1636
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001637 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001638 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001639 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001640}
1641
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001642static int intel_num_dvo_pipes(struct drm_device *dev)
1643{
1644 struct intel_crtc *crtc;
1645 int count = 0;
1646
1647 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001648 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001649 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001650
1651 return count;
1652}
1653
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001654static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001655{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 struct drm_device *dev = crtc->base.dev;
1657 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001658 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001659 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001660
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001661 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001662
1663 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001664 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665
1666 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001667 if (IS_MOBILE(dev) && !IS_I830(dev))
1668 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001669
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001670 /* Enable DVO 2x clock on both PLLs if necessary */
1671 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1672 /*
1673 * It appears to be important that we don't enable this
1674 * for the current pipe before otherwise configuring the
1675 * PLL. No idea how this should be handled if multiple
1676 * DVO outputs are enabled simultaneosly.
1677 */
1678 dpll |= DPLL_DVO_2X_MODE;
1679 I915_WRITE(DPLL(!crtc->pipe),
1680 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1681 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001682
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001683 /*
1684 * Apparently we need to have VGA mode enabled prior to changing
1685 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1686 * dividers, even though the register value does change.
1687 */
1688 I915_WRITE(reg, 0);
1689
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001690 I915_WRITE(reg, dpll);
1691
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001692 /* Wait for the clocks to stabilize. */
1693 POSTING_READ(reg);
1694 udelay(150);
1695
1696 if (INTEL_INFO(dev)->gen >= 4) {
1697 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001698 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001699 } else {
1700 /* The pixel multiplier can only be updated once the
1701 * DPLL is enabled and the clocks are stable.
1702 *
1703 * So write it again.
1704 */
1705 I915_WRITE(reg, dpll);
1706 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001707
1708 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710 POSTING_READ(reg);
1711 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001712 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713 POSTING_READ(reg);
1714 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001716 POSTING_READ(reg);
1717 udelay(150); /* wait for warmup */
1718}
1719
1720/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001721 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722 * @dev_priv: i915 private structure
1723 * @pipe: pipe PLL to disable
1724 *
1725 * Disable the PLL for @pipe, making sure the pipe is off first.
1726 *
1727 * Note! This is for pre-ILK only.
1728 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001729static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001730{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001731 struct drm_device *dev = crtc->base.dev;
1732 struct drm_i915_private *dev_priv = dev->dev_private;
1733 enum pipe pipe = crtc->pipe;
1734
1735 /* Disable DVO 2x clock on both PLLs if necessary */
1736 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001737 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001738 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001739 I915_WRITE(DPLL(PIPE_B),
1740 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1741 I915_WRITE(DPLL(PIPE_A),
1742 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1743 }
1744
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001745 /* Don't disable pipe or pipe PLLs if needed */
1746 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1747 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001748 return;
1749
1750 /* Make sure the pipe isn't still relying on us */
1751 assert_pipe_disabled(dev_priv, pipe);
1752
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001753 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001754 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755}
1756
Jesse Barnesf6071162013-10-01 10:41:38 -07001757static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1758{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001759 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001760
1761 /* Make sure the pipe isn't still relying on us */
1762 assert_pipe_disabled(dev_priv, pipe);
1763
Imre Deake5cbfbf2014-01-09 17:08:16 +02001764 /*
1765 * Leave integrated clock source and reference clock enabled for pipe B.
1766 * The latter is needed for VGA hotplug / manual detection.
1767 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001768 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001769 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001770 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001771 I915_WRITE(DPLL(pipe), val);
1772 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001773
1774}
1775
1776static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1777{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001778 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001779 u32 val;
1780
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001781 /* Make sure the pipe isn't still relying on us */
1782 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001783
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001784 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001785 val = DPLL_SSC_REF_CLK_CHV |
1786 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001787 if (pipe != PIPE_A)
1788 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1789 I915_WRITE(DPLL(pipe), val);
1790 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001791
Ville Syrjäläa5805162015-05-26 20:42:30 +03001792 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001793
1794 /* Disable 10bit clock to display controller */
1795 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1796 val &= ~DPIO_DCLKP_EN;
1797 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1798
Ville Syrjäläa5805162015-05-26 20:42:30 +03001799 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001800}
1801
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001802void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001803 struct intel_digital_port *dport,
1804 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001805{
1806 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001807 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001808
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001809 switch (dport->port) {
1810 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001811 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001812 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001813 break;
1814 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001816 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001817 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001818 break;
1819 case PORT_D:
1820 port_mask = DPLL_PORTD_READY_MASK;
1821 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001822 break;
1823 default:
1824 BUG();
1825 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001826
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001827 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1828 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1829 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001830}
1831
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001832static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1833 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001834{
Daniel Vetter23670b322012-11-01 09:15:30 +01001835 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001836 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001838 i915_reg_t reg;
1839 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001840
1841 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001842 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001843
1844 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001845 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001846
1847 /* FDI must be feeding us bits for PCH ports */
1848 assert_fdi_tx_enabled(dev_priv, pipe);
1849 assert_fdi_rx_enabled(dev_priv, pipe);
1850
Daniel Vetter23670b322012-11-01 09:15:30 +01001851 if (HAS_PCH_CPT(dev)) {
1852 /* Workaround: Set the timing override bit before enabling the
1853 * pch transcoder. */
1854 reg = TRANS_CHICKEN2(pipe);
1855 val = I915_READ(reg);
1856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1857 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001858 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001859
Daniel Vetterab9412b2013-05-03 11:49:46 +02001860 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001861 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001862 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001863
1864 if (HAS_PCH_IBX(dev_priv->dev)) {
1865 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001866 * Make the BPC in transcoder be consistent with
1867 * that in pipeconf reg. For HDMI we must use 8bpc
1868 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001869 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001870 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001871 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1872 val |= PIPECONF_8BPC;
1873 else
1874 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001875 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001876
1877 val &= ~TRANS_INTERLACE_MASK;
1878 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001879 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001880 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001881 val |= TRANS_LEGACY_INTERLACED_ILK;
1882 else
1883 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001884 else
1885 val |= TRANS_PROGRESSIVE;
1886
Jesse Barnes040484a2011-01-03 12:14:26 -08001887 I915_WRITE(reg, val | TRANS_ENABLE);
1888 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001889 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001890}
1891
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001892static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001893 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001894{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001895 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001896
1897 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001898 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001899
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001900 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001901 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001902 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001904 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001905 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001906 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001907 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001908
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001909 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001910 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001912 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1913 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001914 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915 else
1916 val |= TRANS_PROGRESSIVE;
1917
Daniel Vetterab9412b2013-05-03 11:49:46 +02001918 I915_WRITE(LPT_TRANSCONF, val);
1919 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001920 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001921}
1922
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001923static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1924 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001925{
Daniel Vetter23670b322012-11-01 09:15:30 +01001926 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001927 i915_reg_t reg;
1928 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001929
1930 /* FDI relies on the transcoder */
1931 assert_fdi_tx_disabled(dev_priv, pipe);
1932 assert_fdi_rx_disabled(dev_priv, pipe);
1933
Jesse Barnes291906f2011-02-02 12:28:03 -08001934 /* Ports must be off as well */
1935 assert_pch_ports_disabled(dev_priv, pipe);
1936
Daniel Vetterab9412b2013-05-03 11:49:46 +02001937 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001938 val = I915_READ(reg);
1939 val &= ~TRANS_ENABLE;
1940 I915_WRITE(reg, val);
1941 /* wait for PCH transcoder off, transcoder state */
1942 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001943 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001944
Ville Syrjäläc4656132015-10-29 21:25:56 +02001945 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001946 /* Workaround: Clear the timing override chicken bit again. */
1947 reg = TRANS_CHICKEN2(pipe);
1948 val = I915_READ(reg);
1949 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1950 I915_WRITE(reg, val);
1951 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001952}
1953
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001954static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001955{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001956 u32 val;
1957
Daniel Vetterab9412b2013-05-03 11:49:46 +02001958 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001959 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001960 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001961 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001962 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001963 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001964
1965 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001966 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001967 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001968 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001969}
1970
1971/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001972 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001973 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001974 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001975 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001978static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001979{
Paulo Zanoni03722642014-01-17 13:51:09 -02001980 struct drm_device *dev = crtc->base.dev;
1981 struct drm_i915_private *dev_priv = dev->dev_private;
1982 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001983 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001984 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001985 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001986 u32 val;
1987
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001988 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1989
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001990 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001991 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001992 assert_sprites_disabled(dev_priv, pipe);
1993
Paulo Zanoni681e5812012-12-06 11:12:38 -02001994 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001995 pch_transcoder = TRANSCODER_A;
1996 else
1997 pch_transcoder = pipe;
1998
Jesse Barnesb24e7172011-01-04 15:09:30 -08001999 /*
2000 * A pipe without a PLL won't actually be able to drive bits from
2001 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2002 * need the check.
2003 */
Imre Deak50360402015-01-16 00:55:16 -08002004 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002005 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002006 assert_dsi_pll_enabled(dev_priv);
2007 else
2008 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002009 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002010 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002011 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002012 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002013 assert_fdi_tx_pll_enabled(dev_priv,
2014 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002015 }
2016 /* FIXME: assert CPU port conditions for SNB+ */
2017 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002018
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002019 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002021 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002022 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2023 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002024 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002025 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002026
2027 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002028 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002029
2030 /*
2031 * Until the pipe starts DSL will read as 0, which would cause
2032 * an apparent vblank timestamp jump, which messes up also the
2033 * frame count when it's derived from the timestamps. So let's
2034 * wait for the pipe to start properly before we call
2035 * drm_crtc_vblank_on()
2036 */
2037 if (dev->max_vblank_count == 0 &&
2038 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2039 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002040}
2041
2042/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002043 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002044 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002045 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002046 * Disable the pipe of @crtc, making sure that various hardware
2047 * specific requirements are met, if applicable, e.g. plane
2048 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 *
2050 * Will wait until the pipe has shut down before returning.
2051 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002052static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002053{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002054 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002055 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002056 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002057 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002058 u32 val;
2059
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002060 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2061
Jesse Barnesb24e7172011-01-04 15:09:30 -08002062 /*
2063 * Make sure planes won't keep trying to pump pixels to us,
2064 * or we might hang the display.
2065 */
2066 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002067 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002068 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002069
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002070 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002071 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002072 if ((val & PIPECONF_ENABLE) == 0)
2073 return;
2074
Ville Syrjälä67adc642014-08-15 01:21:57 +03002075 /*
2076 * Double wide has implications for planes
2077 * so best keep it disabled when not needed.
2078 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002079 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002080 val &= ~PIPECONF_DOUBLE_WIDE;
2081
2082 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002083 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2084 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002085 val &= ~PIPECONF_ENABLE;
2086
2087 I915_WRITE(reg, val);
2088 if ((val & PIPECONF_ENABLE) == 0)
2089 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002090}
2091
Chris Wilson693db182013-03-05 14:52:39 +00002092static bool need_vtd_wa(struct drm_device *dev)
2093{
2094#ifdef CONFIG_INTEL_IOMMU
2095 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2096 return true;
2097#endif
2098 return false;
2099}
2100
Ville Syrjälä832be822016-01-12 21:08:33 +02002101static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2102{
2103 return IS_GEN2(dev_priv) ? 2048 : 4096;
2104}
2105
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002106static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2107 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002108{
2109 switch (fb_modifier) {
2110 case DRM_FORMAT_MOD_NONE:
2111 return cpp;
2112 case I915_FORMAT_MOD_X_TILED:
2113 if (IS_GEN2(dev_priv))
2114 return 128;
2115 else
2116 return 512;
2117 case I915_FORMAT_MOD_Y_TILED:
2118 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2119 return 128;
2120 else
2121 return 512;
2122 case I915_FORMAT_MOD_Yf_TILED:
2123 switch (cpp) {
2124 case 1:
2125 return 64;
2126 case 2:
2127 case 4:
2128 return 128;
2129 case 8:
2130 case 16:
2131 return 256;
2132 default:
2133 MISSING_CASE(cpp);
2134 return cpp;
2135 }
2136 break;
2137 default:
2138 MISSING_CASE(fb_modifier);
2139 return cpp;
2140 }
2141}
2142
Ville Syrjälä832be822016-01-12 21:08:33 +02002143unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2144 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002145{
Ville Syrjälä832be822016-01-12 21:08:33 +02002146 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2147 return 1;
2148 else
2149 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002150 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002151}
2152
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002153/* Return the tile dimensions in pixel units */
2154static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2155 unsigned int *tile_width,
2156 unsigned int *tile_height,
2157 uint64_t fb_modifier,
2158 unsigned int cpp)
2159{
2160 unsigned int tile_width_bytes =
2161 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2162
2163 *tile_width = tile_width_bytes / cpp;
2164 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2165}
2166
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002167unsigned int
2168intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002169 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002170{
Ville Syrjälä832be822016-01-12 21:08:33 +02002171 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2172 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2173
2174 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002175}
2176
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002177unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2178{
2179 unsigned int size = 0;
2180 int i;
2181
2182 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2183 size += rot_info->plane[i].width * rot_info->plane[i].height;
2184
2185 return size;
2186}
2187
Daniel Vetter75c82a52015-10-14 16:51:04 +02002188static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002189intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2190 const struct drm_framebuffer *fb,
2191 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002192{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002193 if (intel_rotation_90_or_270(rotation)) {
2194 *view = i915_ggtt_view_rotated;
2195 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2196 } else {
2197 *view = i915_ggtt_view_normal;
2198 }
2199}
2200
2201static void
2202intel_fill_fb_info(struct drm_i915_private *dev_priv,
2203 struct drm_framebuffer *fb)
2204{
2205 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002206 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002207
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002208 tile_size = intel_tile_size(dev_priv);
2209
2210 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002211 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2212 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002213
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002214 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2215 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002216
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002217 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002218 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002219 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2220 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002221
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002222 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002223 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2224 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002225 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002226}
2227
Ville Syrjälä603525d2016-01-12 21:08:37 +02002228static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002229{
2230 if (INTEL_INFO(dev_priv)->gen >= 9)
2231 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002232 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002233 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002234 return 128 * 1024;
2235 else if (INTEL_INFO(dev_priv)->gen >= 4)
2236 return 4 * 1024;
2237 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002238 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002239}
2240
Ville Syrjälä603525d2016-01-12 21:08:37 +02002241static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2242 uint64_t fb_modifier)
2243{
2244 switch (fb_modifier) {
2245 case DRM_FORMAT_MOD_NONE:
2246 return intel_linear_alignment(dev_priv);
2247 case I915_FORMAT_MOD_X_TILED:
2248 if (INTEL_INFO(dev_priv)->gen >= 9)
2249 return 256 * 1024;
2250 return 0;
2251 case I915_FORMAT_MOD_Y_TILED:
2252 case I915_FORMAT_MOD_Yf_TILED:
2253 return 1 * 1024 * 1024;
2254 default:
2255 MISSING_CASE(fb_modifier);
2256 return 0;
2257 }
2258}
2259
Chris Wilson127bd2a2010-07-23 23:32:05 +01002260int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002261intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2262 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002263{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002264 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002265 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002266 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002267 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002268 u32 alignment;
2269 int ret;
2270
Matt Roperebcdd392014-07-09 16:22:11 -07002271 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2272
Ville Syrjälä603525d2016-01-12 21:08:37 +02002273 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002274
Ville Syrjälä3465c582016-02-15 22:54:43 +02002275 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002276
Chris Wilson693db182013-03-05 14:52:39 +00002277 /* Note that the w/a also requires 64 PTE of padding following the
2278 * bo. We currently fill all unused PTE with the shadow page and so
2279 * we should always have valid PTE following the scanout preventing
2280 * the VT-d warning.
2281 */
2282 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2283 alignment = 256 * 1024;
2284
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002285 /*
2286 * Global gtt pte registers are special registers which actually forward
2287 * writes to a chunk of system memory. Which means that there is no risk
2288 * that the register values disappear as soon as we call
2289 * intel_runtime_pm_put(), so it is correct to wrap only the
2290 * pin/unpin/fence and not more.
2291 */
2292 intel_runtime_pm_get(dev_priv);
2293
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002294 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2295 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002296 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002297 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002298
2299 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2300 * fence, whereas 965+ only requires a fence if using
2301 * framebuffer compression. For simplicity, we always install
2302 * a fence as the cost is not that onerous.
2303 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002304 if (view.type == I915_GGTT_VIEW_NORMAL) {
2305 ret = i915_gem_object_get_fence(obj);
2306 if (ret == -EDEADLK) {
2307 /*
2308 * -EDEADLK means there are no free fences
2309 * no pending flips.
2310 *
2311 * This is propagated to atomic, but it uses
2312 * -EDEADLK to force a locking recovery, so
2313 * change the returned error to -EBUSY.
2314 */
2315 ret = -EBUSY;
2316 goto err_unpin;
2317 } else if (ret)
2318 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002319
Vivek Kasireddy98072162015-10-29 18:54:38 -07002320 i915_gem_object_pin_fence(obj);
2321 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002322
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002323 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002324 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002325
2326err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002327 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002328err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002329 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002330 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002331}
2332
Ville Syrjälä3465c582016-02-15 22:54:43 +02002333static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002334{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002335 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002336 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002337
Matt Roperebcdd392014-07-09 16:22:11 -07002338 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2339
Ville Syrjälä3465c582016-02-15 22:54:43 +02002340 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002341
Vivek Kasireddy98072162015-10-29 18:54:38 -07002342 if (view.type == I915_GGTT_VIEW_NORMAL)
2343 i915_gem_object_unpin_fence(obj);
2344
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002345 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002346}
2347
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002348/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002349 * Adjust the tile offset by moving the difference into
2350 * the x/y offsets.
2351 *
2352 * Input tile dimensions and pitch must already be
2353 * rotated to match x and y, and in pixel units.
2354 */
2355static u32 intel_adjust_tile_offset(int *x, int *y,
2356 unsigned int tile_width,
2357 unsigned int tile_height,
2358 unsigned int tile_size,
2359 unsigned int pitch_tiles,
2360 u32 old_offset,
2361 u32 new_offset)
2362{
2363 unsigned int tiles;
2364
2365 WARN_ON(old_offset & (tile_size - 1));
2366 WARN_ON(new_offset & (tile_size - 1));
2367 WARN_ON(new_offset > old_offset);
2368
2369 tiles = (old_offset - new_offset) / tile_size;
2370
2371 *y += tiles / pitch_tiles * tile_height;
2372 *x += tiles % pitch_tiles * tile_width;
2373
2374 return new_offset;
2375}
2376
2377/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002378 * Computes the linear offset to the base tile and adjusts
2379 * x, y. bytes per pixel is assumed to be a power-of-two.
2380 *
2381 * In the 90/270 rotated case, x and y are assumed
2382 * to be already rotated to match the rotated GTT view, and
2383 * pitch is the tile_height aligned framebuffer height.
2384 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002385u32 intel_compute_tile_offset(int *x, int *y,
2386 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002387 unsigned int pitch,
2388 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002389{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002390 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2391 uint64_t fb_modifier = fb->modifier[plane];
2392 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002393 u32 offset, offset_aligned, alignment;
2394
2395 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2396 if (alignment)
2397 alignment--;
2398
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002399 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002400 unsigned int tile_size, tile_width, tile_height;
2401 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002402
Ville Syrjäläd8433102016-01-12 21:08:35 +02002403 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002404 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2405 fb_modifier, cpp);
2406
2407 if (intel_rotation_90_or_270(rotation)) {
2408 pitch_tiles = pitch / tile_height;
2409 swap(tile_width, tile_height);
2410 } else {
2411 pitch_tiles = pitch / (tile_width * cpp);
2412 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002413
Ville Syrjäläd8433102016-01-12 21:08:35 +02002414 tile_rows = *y / tile_height;
2415 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002416
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002417 tiles = *x / tile_width;
2418 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002419
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002420 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2421 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002422
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002423 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2424 tile_size, pitch_tiles,
2425 offset, offset_aligned);
2426 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002427 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002428 offset_aligned = offset & ~alignment;
2429
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002430 *y = (offset & alignment) / pitch;
2431 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002432 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002433
2434 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002435}
2436
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002437static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002438{
2439 switch (format) {
2440 case DISPPLANE_8BPP:
2441 return DRM_FORMAT_C8;
2442 case DISPPLANE_BGRX555:
2443 return DRM_FORMAT_XRGB1555;
2444 case DISPPLANE_BGRX565:
2445 return DRM_FORMAT_RGB565;
2446 default:
2447 case DISPPLANE_BGRX888:
2448 return DRM_FORMAT_XRGB8888;
2449 case DISPPLANE_RGBX888:
2450 return DRM_FORMAT_XBGR8888;
2451 case DISPPLANE_BGRX101010:
2452 return DRM_FORMAT_XRGB2101010;
2453 case DISPPLANE_RGBX101010:
2454 return DRM_FORMAT_XBGR2101010;
2455 }
2456}
2457
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002458static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2459{
2460 switch (format) {
2461 case PLANE_CTL_FORMAT_RGB_565:
2462 return DRM_FORMAT_RGB565;
2463 default:
2464 case PLANE_CTL_FORMAT_XRGB_8888:
2465 if (rgb_order) {
2466 if (alpha)
2467 return DRM_FORMAT_ABGR8888;
2468 else
2469 return DRM_FORMAT_XBGR8888;
2470 } else {
2471 if (alpha)
2472 return DRM_FORMAT_ARGB8888;
2473 else
2474 return DRM_FORMAT_XRGB8888;
2475 }
2476 case PLANE_CTL_FORMAT_XRGB_2101010:
2477 if (rgb_order)
2478 return DRM_FORMAT_XBGR2101010;
2479 else
2480 return DRM_FORMAT_XRGB2101010;
2481 }
2482}
2483
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002484static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002485intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2486 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002487{
2488 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002489 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002490 struct drm_i915_gem_object *obj = NULL;
2491 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002492 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002493 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2494 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2495 PAGE_SIZE);
2496
2497 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002498
Chris Wilsonff2652e2014-03-10 08:07:02 +00002499 if (plane_config->size == 0)
2500 return false;
2501
Paulo Zanoni3badb492015-09-23 12:52:23 -03002502 /* If the FB is too big, just don't use it since fbdev is not very
2503 * important and we should probably use that space with FBC or other
2504 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002505 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002506 return false;
2507
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002508 mutex_lock(&dev->struct_mutex);
2509
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002510 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2511 base_aligned,
2512 base_aligned,
2513 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002514 if (!obj) {
2515 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002517 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518
Damien Lespiau49af4492015-01-20 12:51:44 +00002519 obj->tiling_mode = plane_config->tiling;
2520 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002521 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002523 mode_cmd.pixel_format = fb->pixel_format;
2524 mode_cmd.width = fb->width;
2525 mode_cmd.height = fb->height;
2526 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002527 mode_cmd.modifier[0] = fb->modifier[0];
2528 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002530 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002531 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532 DRM_DEBUG_KMS("intel fb init failed\n");
2533 goto out_unref_obj;
2534 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002535
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002537
Daniel Vetterf6936e22015-03-26 12:17:05 +01002538 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540
2541out_unref_obj:
2542 drm_gem_object_unreference(&obj->base);
2543 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544 return false;
2545}
2546
Matt Roperafd65eb2015-02-03 13:10:04 -08002547/* Update plane->state->fb to match plane->fb after driver-internal updates */
2548static void
2549update_state_fb(struct drm_plane *plane)
2550{
2551 if (plane->fb == plane->state->fb)
2552 return;
2553
2554 if (plane->state->fb)
2555 drm_framebuffer_unreference(plane->state->fb);
2556 plane->state->fb = plane->fb;
2557 if (plane->state->fb)
2558 drm_framebuffer_reference(plane->state->fb);
2559}
2560
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002561static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002562intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2563 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564{
2565 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002566 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 struct drm_crtc *c;
2568 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002569 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002570 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002571 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002572 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2573 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002574 struct intel_plane_state *intel_state =
2575 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002576 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002577
Damien Lespiau2d140302015-02-05 17:22:18 +00002578 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 return;
2580
Daniel Vetterf6936e22015-03-26 12:17:05 +01002581 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002582 fb = &plane_config->fb->base;
2583 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002584 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002585
Damien Lespiau2d140302015-02-05 17:22:18 +00002586 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587
2588 /*
2589 * Failed to alloc the obj, check to see if we should share
2590 * an fb with another CRTC instead
2591 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002592 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593 i = to_intel_crtc(c);
2594
2595 if (c == &intel_crtc->base)
2596 continue;
2597
Matt Roper2ff8fde2014-07-08 07:50:07 -07002598 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599 continue;
2600
Daniel Vetter88595ac2015-03-26 12:42:24 +01002601 fb = c->primary->fb;
2602 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002603 continue;
2604
Daniel Vetter88595ac2015-03-26 12:42:24 +01002605 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002606 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002607 drm_framebuffer_reference(fb);
2608 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 }
2610 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002611
Matt Roper200757f2015-12-03 11:37:36 -08002612 /*
2613 * We've failed to reconstruct the BIOS FB. Current display state
2614 * indicates that the primary plane is visible, but has a NULL FB,
2615 * which will lead to problems later if we don't fix it up. The
2616 * simplest solution is to just disable the primary plane now and
2617 * pretend the BIOS never had it enabled.
2618 */
2619 to_intel_plane_state(plane_state)->visible = false;
2620 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002621 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002622 intel_plane->disable_plane(primary, &intel_crtc->base);
2623
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 return;
2625
2626valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002627 plane_state->src_x = 0;
2628 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002629 plane_state->src_w = fb->width << 16;
2630 plane_state->src_h = fb->height << 16;
2631
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002632 plane_state->crtc_x = 0;
2633 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002634 plane_state->crtc_w = fb->width;
2635 plane_state->crtc_h = fb->height;
2636
Matt Roper0a8d8a82015-12-03 11:37:38 -08002637 intel_state->src.x1 = plane_state->src_x;
2638 intel_state->src.y1 = plane_state->src_y;
2639 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2640 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2641 intel_state->dst.x1 = plane_state->crtc_x;
2642 intel_state->dst.y1 = plane_state->crtc_y;
2643 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2644 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2645
Daniel Vetter88595ac2015-03-26 12:42:24 +01002646 obj = intel_fb_obj(fb);
2647 if (obj->tiling_mode != I915_TILING_NONE)
2648 dev_priv->preserve_bios_swizzle = true;
2649
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002650 drm_framebuffer_reference(fb);
2651 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002652 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002653 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002654 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002655}
2656
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002657static void i9xx_update_primary_plane(struct drm_plane *primary,
2658 const struct intel_crtc_state *crtc_state,
2659 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002660{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002661 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002662 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2664 struct drm_framebuffer *fb = plane_state->base.fb;
2665 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002666 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002667 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002668 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002669 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002670 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002671 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002672 int x = plane_state->src.x1 >> 16;
2673 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002674
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002675 dspcntr = DISPPLANE_GAMMA_ENABLE;
2676
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002677 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002678
2679 if (INTEL_INFO(dev)->gen < 4) {
2680 if (intel_crtc->pipe == PIPE_B)
2681 dspcntr |= DISPPLANE_SEL_PIPE_B;
2682
2683 /* pipesrc and dspsize control the size that is scaled from,
2684 * which should always be the user's requested size.
2685 */
2686 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002687 ((crtc_state->pipe_src_h - 1) << 16) |
2688 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002690 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2691 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002692 ((crtc_state->pipe_src_h - 1) << 16) |
2693 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002694 I915_WRITE(PRIMPOS(plane), 0);
2695 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002696 }
2697
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 switch (fb->pixel_format) {
2699 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002700 dspcntr |= DISPPLANE_8BPP;
2701 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002704 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 case DRM_FORMAT_RGB565:
2706 dspcntr |= DISPPLANE_BGRX565;
2707 break;
2708 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 dspcntr |= DISPPLANE_BGRX888;
2710 break;
2711 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 dspcntr |= DISPPLANE_RGBX888;
2713 break;
2714 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715 dspcntr |= DISPPLANE_BGRX101010;
2716 break;
2717 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002718 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002719 break;
2720 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002721 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002722 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002723
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002724 if (INTEL_INFO(dev)->gen >= 4 &&
2725 obj->tiling_mode != I915_TILING_NONE)
2726 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002727
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002728 if (IS_G4X(dev))
2729 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2730
Ville Syrjäläac484962016-01-20 21:05:26 +02002731 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002732
Daniel Vetterc2c75132012-07-05 12:17:30 +02002733 if (INTEL_INFO(dev)->gen >= 4) {
2734 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002735 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002736 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002737 linear_offset -= intel_crtc->dspaddr_offset;
2738 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002739 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002740 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002741
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002742 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302743 dspcntr |= DISPPLANE_ROTATE_180;
2744
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002745 x += (crtc_state->pipe_src_w - 1);
2746 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302747
2748 /* Finding the last pixel of the last line of the display
2749 data and adding to linear_offset*/
2750 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002752 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302753 }
2754
Paulo Zanoni2db33662015-09-14 15:20:03 -03002755 intel_crtc->adjusted_x = x;
2756 intel_crtc->adjusted_y = y;
2757
Sonika Jindal48404c12014-08-22 14:06:04 +05302758 I915_WRITE(reg, dspcntr);
2759
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002760 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002761 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002762 I915_WRITE(DSPSURF(plane),
2763 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002764 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002765 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002766 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002767 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002769}
2770
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002771static void i9xx_disable_primary_plane(struct drm_plane *primary,
2772 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773{
2774 struct drm_device *dev = crtc->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
2776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002777 int plane = intel_crtc->plane;
2778
2779 I915_WRITE(DSPCNTR(plane), 0);
2780 if (INTEL_INFO(dev_priv)->gen >= 4)
2781 I915_WRITE(DSPSURF(plane), 0);
2782 else
2783 I915_WRITE(DSPADDR(plane), 0);
2784 POSTING_READ(DSPCNTR(plane));
2785}
2786
2787static void ironlake_update_primary_plane(struct drm_plane *primary,
2788 const struct intel_crtc_state *crtc_state,
2789 const struct intel_plane_state *plane_state)
2790{
2791 struct drm_device *dev = primary->dev;
2792 struct drm_i915_private *dev_priv = dev->dev_private;
2793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2794 struct drm_framebuffer *fb = plane_state->base.fb;
2795 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002796 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002797 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002798 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002799 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002800 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002801 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002802 int x = plane_state->src.x1 >> 16;
2803 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002804
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002805 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002806 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002807
2808 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2809 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2810
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 switch (fb->pixel_format) {
2812 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 dspcntr |= DISPPLANE_8BPP;
2814 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 case DRM_FORMAT_RGB565:
2816 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 dspcntr |= DISPPLANE_BGRX888;
2820 break;
2821 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 dspcntr |= DISPPLANE_RGBX888;
2823 break;
2824 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002825 dspcntr |= DISPPLANE_BGRX101010;
2826 break;
2827 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002829 break;
2830 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002831 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 }
2833
2834 if (obj->tiling_mode != I915_TILING_NONE)
2835 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002837 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002838 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjäläac484962016-01-20 21:05:26 +02002840 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002841 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002842 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002843 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002844 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002845 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302846 dspcntr |= DISPPLANE_ROTATE_180;
2847
2848 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002849 x += (crtc_state->pipe_src_w - 1);
2850 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302851
2852 /* Finding the last pixel of the last line of the display
2853 data and adding to linear_offset*/
2854 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002855 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002856 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302857 }
2858 }
2859
Paulo Zanoni2db33662015-09-14 15:20:03 -03002860 intel_crtc->adjusted_x = x;
2861 intel_crtc->adjusted_y = y;
2862
Sonika Jindal48404c12014-08-22 14:06:04 +05302863 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002864
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002865 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002866 I915_WRITE(DSPSURF(plane),
2867 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002868 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002869 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2870 } else {
2871 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2872 I915_WRITE(DSPLINOFF(plane), linear_offset);
2873 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002874 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875}
2876
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002877u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2878 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002879{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002880 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2881 return 64;
2882 } else {
2883 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002884
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002885 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002886 }
2887}
2888
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002889u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2890 struct drm_i915_gem_object *obj,
2891 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002892{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002893 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002894 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002895 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002896
Ville Syrjäläe7941292016-01-19 18:23:17 +02002897 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002898 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899
Daniel Vetterce7f1722015-10-14 16:51:06 +02002900 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002901 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002902 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002903 return -1;
2904
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002905 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002906
2907 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002908 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002909 PAGE_SIZE;
2910 }
2911
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002912 WARN_ON(upper_32_bits(offset));
2913
2914 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002915}
2916
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002917static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2918{
2919 struct drm_device *dev = intel_crtc->base.dev;
2920 struct drm_i915_private *dev_priv = dev->dev_private;
2921
2922 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2923 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2924 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002925}
2926
Chandra Kondurua1b22782015-04-07 15:28:45 -07002927/*
2928 * This function detaches (aka. unbinds) unused scalers in hardware
2929 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002930static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002931{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002932 struct intel_crtc_scaler_state *scaler_state;
2933 int i;
2934
Chandra Kondurua1b22782015-04-07 15:28:45 -07002935 scaler_state = &intel_crtc->config->scaler_state;
2936
2937 /* loop through and disable scalers that aren't in use */
2938 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002939 if (!scaler_state->scalers[i].in_use)
2940 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002941 }
2942}
2943
Chandra Konduru6156a452015-04-27 13:48:39 -07002944u32 skl_plane_ctl_format(uint32_t pixel_format)
2945{
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002947 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 /*
2956 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2957 * to be already pre-multiplied. We need to add a knob (or a different
2958 * DRM_FORMAT) for user-space to configure that.
2959 */
2960 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002979 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002981
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983}
2984
2985u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2986{
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 switch (fb_modifier) {
2988 case DRM_FORMAT_MOD_NONE:
2989 break;
2990 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002993 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 default:
2997 MISSING_CASE(fb_modifier);
2998 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002999
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001}
3002
3003u32 skl_plane_ctl_rotation(unsigned int rotation)
3004{
Chandra Konduru6156a452015-04-27 13:48:39 -07003005 switch (rotation) {
3006 case BIT(DRM_ROTATE_0):
3007 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303008 /*
3009 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3010 * while i915 HW rotation is clockwise, thats why this swapping.
3011 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303013 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003015 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003016 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303017 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018 default:
3019 MISSING_CASE(rotation);
3020 }
3021
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003022 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023}
3024
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025static void skylake_update_primary_plane(struct drm_plane *plane,
3026 const struct intel_crtc_state *crtc_state,
3027 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003028{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003029 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003030 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3032 struct drm_framebuffer *fb = plane_state->base.fb;
3033 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 u32 plane_ctl, stride_div, stride;
3036 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003037 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303038 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003039 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003040 int scaler_id = plane_state->scaler_id;
3041 int src_x = plane_state->src.x1 >> 16;
3042 int src_y = plane_state->src.y1 >> 16;
3043 int src_w = drm_rect_width(&plane_state->src) >> 16;
3044 int src_h = drm_rect_height(&plane_state->src) >> 16;
3045 int dst_x = plane_state->dst.x1;
3046 int dst_y = plane_state->dst.y1;
3047 int dst_w = drm_rect_width(&plane_state->dst);
3048 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003049
3050 plane_ctl = PLANE_CTL_ENABLE |
3051 PLANE_CTL_PIPE_GAMMA_ENABLE |
3052 PLANE_CTL_PIPE_CSC_ENABLE;
3053
Chandra Konduru6156a452015-04-27 13:48:39 -07003054 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3055 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003056 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003058
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003059 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003060 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003061 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303062
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003063 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003064
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303065 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003066 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3067
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003069 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303070 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003071 x_offset = stride * tile_height - src_y - src_h;
3072 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303074 } else {
3075 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003076 x_offset = src_x;
3077 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003078 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303079 }
3080 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003081
Paulo Zanoni2db33662015-09-14 15:20:03 -03003082 intel_crtc->adjusted_x = x_offset;
3083 intel_crtc->adjusted_y = y_offset;
3084
Damien Lespiau70d21f02013-07-03 21:06:04 +01003085 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303086 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3087 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3088 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003089
3090 if (scaler_id >= 0) {
3091 uint32_t ps_ctrl = 0;
3092
3093 WARN_ON(!dst_w || !dst_h);
3094 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3095 crtc_state->scaler_state.scalers[scaler_id].mode;
3096 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3097 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3098 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3099 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3100 I915_WRITE(PLANE_POS(pipe, 0), 0);
3101 } else {
3102 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3103 }
3104
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003105 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003106
3107 POSTING_READ(PLANE_SURF(pipe, 0));
3108}
3109
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110static void skylake_disable_primary_plane(struct drm_plane *primary,
3111 struct drm_crtc *crtc)
3112{
3113 struct drm_device *dev = crtc->dev;
3114 struct drm_i915_private *dev_priv = dev->dev_private;
3115 int pipe = to_intel_crtc(crtc)->pipe;
3116
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003117 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3118 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3119 POSTING_READ(PLANE_SURF(pipe, 0));
3120}
3121
Jesse Barnes17638cd2011-06-24 12:19:23 -07003122/* Assume fb object is pinned & idle & fenced and just update base pointers */
3123static int
3124intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3125 int x, int y, enum mode_set_atomic state)
3126{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003127 /* Support for kgdboc is disabled, this needs a major rework. */
3128 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003129
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003130 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003131}
3132
Ville Syrjälä75147472014-11-24 18:28:11 +02003133static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003134{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003135 struct drm_crtc *crtc;
3136
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003137 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3139 enum plane plane = intel_crtc->plane;
3140
3141 intel_prepare_page_flip(dev, plane);
3142 intel_finish_page_flip_plane(dev, plane);
3143 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003144}
3145
3146static void intel_update_primary_planes(struct drm_device *dev)
3147{
Ville Syrjälä75147472014-11-24 18:28:11 +02003148 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003149
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003150 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003151 struct intel_plane *plane = to_intel_plane(crtc->primary);
3152 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003153
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003154 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003155 plane_state = to_intel_plane_state(plane->base.state);
3156
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003157 if (plane_state->visible)
3158 plane->update_plane(&plane->base,
3159 to_intel_crtc_state(crtc->state),
3160 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003161
3162 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003163 }
3164}
3165
Ville Syrjälä75147472014-11-24 18:28:11 +02003166void intel_prepare_reset(struct drm_device *dev)
3167{
3168 /* no reset support for gen2 */
3169 if (IS_GEN2(dev))
3170 return;
3171
3172 /* reset doesn't touch the display */
3173 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3174 return;
3175
3176 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003177 /*
3178 * Disabling the crtcs gracefully seems nicer. Also the
3179 * g33 docs say we should at least disable all the planes.
3180 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003181 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003182}
3183
3184void intel_finish_reset(struct drm_device *dev)
3185{
3186 struct drm_i915_private *dev_priv = to_i915(dev);
3187
3188 /*
3189 * Flips in the rings will be nuked by the reset,
3190 * so complete all pending flips so that user space
3191 * will get its events and not get stuck.
3192 */
3193 intel_complete_page_flips(dev);
3194
3195 /* no reset support for gen2 */
3196 if (IS_GEN2(dev))
3197 return;
3198
3199 /* reset doesn't touch the display */
3200 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3201 /*
3202 * Flips in the rings have been nuked by the reset,
3203 * so update the base address of all primary
3204 * planes to the the last fb to make sure we're
3205 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003206 *
3207 * FIXME: Atomic will make this obsolete since we won't schedule
3208 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003209 */
3210 intel_update_primary_planes(dev);
3211 return;
3212 }
3213
3214 /*
3215 * The display has been reset as well,
3216 * so need a full re-initialization.
3217 */
3218 intel_runtime_pm_disable_interrupts(dev_priv);
3219 intel_runtime_pm_enable_interrupts(dev_priv);
3220
3221 intel_modeset_init_hw(dev);
3222
3223 spin_lock_irq(&dev_priv->irq_lock);
3224 if (dev_priv->display.hpd_irq_setup)
3225 dev_priv->display.hpd_irq_setup(dev);
3226 spin_unlock_irq(&dev_priv->irq_lock);
3227
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003228 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003229
3230 intel_hpd_init(dev_priv);
3231
3232 drm_modeset_unlock_all(dev);
3233}
3234
Chris Wilson7d5e3792014-03-04 13:15:08 +00003235static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3236{
3237 struct drm_device *dev = crtc->dev;
3238 struct drm_i915_private *dev_priv = dev->dev_private;
3239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003240 bool pending;
3241
3242 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3243 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3244 return false;
3245
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003246 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003247 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003248 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003249
3250 return pending;
3251}
3252
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003253static void intel_update_pipe_config(struct intel_crtc *crtc,
3254 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255{
3256 struct drm_device *dev = crtc->base.dev;
3257 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003258 struct intel_crtc_state *pipe_config =
3259 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003260
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003261 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3262 crtc->base.mode = crtc->base.state->mode;
3263
3264 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3265 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3266 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003267
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003268 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003269 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003270
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003271 /*
3272 * Update pipe size and adjust fitter if needed: the reason for this is
3273 * that in compute_mode_changes we check the native mode (not the pfit
3274 * mode) to see if we can flip rather than do a full mode set. In the
3275 * fastboot case, we'll flip, but if we don't update the pipesrc and
3276 * pfit state, we'll end up with a big fb scanned out into the wrong
3277 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003278 */
3279
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003280 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003281 ((pipe_config->pipe_src_w - 1) << 16) |
3282 (pipe_config->pipe_src_h - 1));
3283
3284 /* on skylake this is done by detaching scalers */
3285 if (INTEL_INFO(dev)->gen >= 9) {
3286 skl_detach_scalers(crtc);
3287
3288 if (pipe_config->pch_pfit.enabled)
3289 skylake_pfit_enable(crtc);
3290 } else if (HAS_PCH_SPLIT(dev)) {
3291 if (pipe_config->pch_pfit.enabled)
3292 ironlake_pfit_enable(crtc);
3293 else if (old_crtc_state->pch_pfit.enabled)
3294 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003295 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003296}
3297
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003298static void intel_fdi_normal_train(struct drm_crtc *crtc)
3299{
3300 struct drm_device *dev = crtc->dev;
3301 struct drm_i915_private *dev_priv = dev->dev_private;
3302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3303 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003304 i915_reg_t reg;
3305 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003306
3307 /* enable normal train */
3308 reg = FDI_TX_CTL(pipe);
3309 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003310 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003311 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3312 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003313 } else {
3314 temp &= ~FDI_LINK_TRAIN_NONE;
3315 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003316 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003317 I915_WRITE(reg, temp);
3318
3319 reg = FDI_RX_CTL(pipe);
3320 temp = I915_READ(reg);
3321 if (HAS_PCH_CPT(dev)) {
3322 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3323 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3324 } else {
3325 temp &= ~FDI_LINK_TRAIN_NONE;
3326 temp |= FDI_LINK_TRAIN_NONE;
3327 }
3328 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3329
3330 /* wait one idle pattern time */
3331 POSTING_READ(reg);
3332 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003333
3334 /* IVB wants error correction enabled */
3335 if (IS_IVYBRIDGE(dev))
3336 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3337 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003338}
3339
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003340/* The FDI link training functions for ILK/Ibexpeak. */
3341static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3342{
3343 struct drm_device *dev = crtc->dev;
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3346 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003347 i915_reg_t reg;
3348 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003350 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003351 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003352
Adam Jacksone1a44742010-06-25 15:32:14 -04003353 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3354 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 reg = FDI_RX_IMR(pipe);
3356 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003357 temp &= ~FDI_RX_SYMBOL_LOCK;
3358 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 I915_WRITE(reg, temp);
3360 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 udelay(150);
3362
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 reg = FDI_TX_CTL(pipe);
3365 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003366 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003367 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368 temp &= ~FDI_LINK_TRAIN_NONE;
3369 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003370 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 reg = FDI_RX_CTL(pipe);
3373 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 temp &= ~FDI_LINK_TRAIN_NONE;
3375 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3377
3378 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 udelay(150);
3380
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003381 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003382 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3383 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3384 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003385
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003387 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3390
3391 if ((temp & FDI_RX_BIT_LOCK)) {
3392 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 break;
3395 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003397 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399
3400 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 reg = FDI_TX_CTL(pipe);
3402 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 temp &= ~FDI_LINK_TRAIN_NONE;
3404 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 reg = FDI_RX_CTL(pipe);
3408 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409 temp &= ~FDI_LINK_TRAIN_NONE;
3410 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp);
3412
3413 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414 udelay(150);
3415
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3420
3421 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003422 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 DRM_DEBUG_KMS("FDI train 2 done.\n");
3424 break;
3425 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003427 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429
3430 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003431
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432}
3433
Akshay Joshi0206e352011-08-16 15:34:10 -04003434static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3436 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3437 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3438 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3439};
3440
3441/* The FDI link training functions for SNB/Cougarpoint. */
3442static void gen6_fdi_link_train(struct drm_crtc *crtc)
3443{
3444 struct drm_device *dev = crtc->dev;
3445 struct drm_i915_private *dev_priv = dev->dev_private;
3446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3447 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003448 i915_reg_t reg;
3449 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450
Adam Jacksone1a44742010-06-25 15:32:14 -04003451 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3452 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 reg = FDI_RX_IMR(pipe);
3454 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003455 temp &= ~FDI_RX_SYMBOL_LOCK;
3456 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp);
3458
3459 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003460 udelay(150);
3461
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 reg = FDI_TX_CTL(pipe);
3464 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003465 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003466 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_1;
3469 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3470 /* SNB-B */
3471 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473
Daniel Vetterd74cf322012-10-26 10:58:13 +02003474 I915_WRITE(FDI_RX_MISC(pipe),
3475 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3476
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 reg = FDI_RX_CTL(pipe);
3478 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 if (HAS_PCH_CPT(dev)) {
3480 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3481 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3482 } else {
3483 temp &= ~FDI_LINK_TRAIN_NONE;
3484 temp |= FDI_LINK_TRAIN_PATTERN_1;
3485 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3487
3488 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 udelay(150);
3490
Akshay Joshi0206e352011-08-16 15:34:10 -04003491 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 reg = FDI_TX_CTL(pipe);
3493 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3495 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 I915_WRITE(reg, temp);
3497
3498 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 udelay(500);
3500
Sean Paulfa37d392012-03-02 12:53:39 -05003501 for (retry = 0; retry < 5; retry++) {
3502 reg = FDI_RX_IIR(pipe);
3503 temp = I915_READ(reg);
3504 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3505 if (temp & FDI_RX_BIT_LOCK) {
3506 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3507 DRM_DEBUG_KMS("FDI train 1 done.\n");
3508 break;
3509 }
3510 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 }
Sean Paulfa37d392012-03-02 12:53:39 -05003512 if (retry < 5)
3513 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514 }
3515 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517
3518 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 reg = FDI_TX_CTL(pipe);
3520 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 temp &= ~FDI_LINK_TRAIN_NONE;
3522 temp |= FDI_LINK_TRAIN_PATTERN_2;
3523 if (IS_GEN6(dev)) {
3524 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3525 /* SNB-B */
3526 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3527 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003528 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 reg = FDI_RX_CTL(pipe);
3531 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 if (HAS_PCH_CPT(dev)) {
3533 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3534 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3535 } else {
3536 temp &= ~FDI_LINK_TRAIN_NONE;
3537 temp |= FDI_LINK_TRAIN_PATTERN_2;
3538 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 I915_WRITE(reg, temp);
3540
3541 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 udelay(150);
3543
Akshay Joshi0206e352011-08-16 15:34:10 -04003544 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 reg = FDI_TX_CTL(pipe);
3546 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3548 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 I915_WRITE(reg, temp);
3550
3551 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 udelay(500);
3553
Sean Paulfa37d392012-03-02 12:53:39 -05003554 for (retry = 0; retry < 5; retry++) {
3555 reg = FDI_RX_IIR(pipe);
3556 temp = I915_READ(reg);
3557 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3558 if (temp & FDI_RX_SYMBOL_LOCK) {
3559 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3560 DRM_DEBUG_KMS("FDI train 2 done.\n");
3561 break;
3562 }
3563 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 }
Sean Paulfa37d392012-03-02 12:53:39 -05003565 if (retry < 5)
3566 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003567 }
3568 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003569 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570
3571 DRM_DEBUG_KMS("FDI train done.\n");
3572}
3573
Jesse Barnes357555c2011-04-28 15:09:55 -07003574/* Manual link training for Ivy Bridge A0 parts */
3575static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3576{
3577 struct drm_device *dev = crtc->dev;
3578 struct drm_i915_private *dev_priv = dev->dev_private;
3579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3580 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003581 i915_reg_t reg;
3582 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003583
3584 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3585 for train result */
3586 reg = FDI_RX_IMR(pipe);
3587 temp = I915_READ(reg);
3588 temp &= ~FDI_RX_SYMBOL_LOCK;
3589 temp &= ~FDI_RX_BIT_LOCK;
3590 I915_WRITE(reg, temp);
3591
3592 POSTING_READ(reg);
3593 udelay(150);
3594
Daniel Vetter01a415f2012-10-27 15:58:40 +02003595 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3596 I915_READ(FDI_RX_IIR(pipe)));
3597
Jesse Barnes139ccd32013-08-19 11:04:55 -07003598 /* Try each vswing and preemphasis setting twice before moving on */
3599 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3600 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003601 reg = FDI_TX_CTL(pipe);
3602 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003603 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3604 temp &= ~FDI_TX_ENABLE;
3605 I915_WRITE(reg, temp);
3606
3607 reg = FDI_RX_CTL(pipe);
3608 temp = I915_READ(reg);
3609 temp &= ~FDI_LINK_TRAIN_AUTO;
3610 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3611 temp &= ~FDI_RX_ENABLE;
3612 I915_WRITE(reg, temp);
3613
3614 /* enable CPU FDI TX and PCH FDI RX */
3615 reg = FDI_TX_CTL(pipe);
3616 temp = I915_READ(reg);
3617 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003618 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003619 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003620 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003621 temp |= snb_b_fdi_train_param[j/2];
3622 temp |= FDI_COMPOSITE_SYNC;
3623 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3624
3625 I915_WRITE(FDI_RX_MISC(pipe),
3626 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3627
3628 reg = FDI_RX_CTL(pipe);
3629 temp = I915_READ(reg);
3630 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3631 temp |= FDI_COMPOSITE_SYNC;
3632 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3633
3634 POSTING_READ(reg);
3635 udelay(1); /* should be 0.5us */
3636
3637 for (i = 0; i < 4; i++) {
3638 reg = FDI_RX_IIR(pipe);
3639 temp = I915_READ(reg);
3640 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3641
3642 if (temp & FDI_RX_BIT_LOCK ||
3643 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3644 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3645 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3646 i);
3647 break;
3648 }
3649 udelay(1); /* should be 0.5us */
3650 }
3651 if (i == 4) {
3652 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3653 continue;
3654 }
3655
3656 /* Train 2 */
3657 reg = FDI_TX_CTL(pipe);
3658 temp = I915_READ(reg);
3659 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3660 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3661 I915_WRITE(reg, temp);
3662
3663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3666 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003667 I915_WRITE(reg, temp);
3668
3669 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003671
Jesse Barnes139ccd32013-08-19 11:04:55 -07003672 for (i = 0; i < 4; i++) {
3673 reg = FDI_RX_IIR(pipe);
3674 temp = I915_READ(reg);
3675 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003676
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 if (temp & FDI_RX_SYMBOL_LOCK ||
3678 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3679 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3680 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3681 i);
3682 goto train_done;
3683 }
3684 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003685 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003686 if (i == 4)
3687 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003688 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003689
Jesse Barnes139ccd32013-08-19 11:04:55 -07003690train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003691 DRM_DEBUG_KMS("FDI train done.\n");
3692}
3693
Daniel Vetter88cefb62012-08-12 19:27:14 +02003694static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003695{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003696 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003697 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003698 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003699 i915_reg_t reg;
3700 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003701
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003705 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003706 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003707 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003708 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3709
3710 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003711 udelay(200);
3712
3713 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp | FDI_PCDCLK);
3716
3717 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003718 udelay(200);
3719
Paulo Zanoni20749732012-11-23 15:30:38 -02003720 /* Enable CPU FDI TX PLL, always on for Ironlake */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3724 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003725
Paulo Zanoni20749732012-11-23 15:30:38 -02003726 POSTING_READ(reg);
3727 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003728 }
3729}
3730
Daniel Vetter88cefb62012-08-12 19:27:14 +02003731static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3732{
3733 struct drm_device *dev = intel_crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3735 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003736 i915_reg_t reg;
3737 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003738
3739 /* Switch from PCDclk to Rawclk */
3740 reg = FDI_RX_CTL(pipe);
3741 temp = I915_READ(reg);
3742 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3743
3744 /* Disable CPU FDI TX PLL */
3745 reg = FDI_TX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751
3752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3755
3756 /* Wait for the clocks to turn off. */
3757 POSTING_READ(reg);
3758 udelay(100);
3759}
3760
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003761static void ironlake_fdi_disable(struct drm_crtc *crtc)
3762{
3763 struct drm_device *dev = crtc->dev;
3764 struct drm_i915_private *dev_priv = dev->dev_private;
3765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3766 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003767 i915_reg_t reg;
3768 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003769
3770 /* disable CPU FDI tx and PCH FDI rx */
3771 reg = FDI_TX_CTL(pipe);
3772 temp = I915_READ(reg);
3773 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3774 POSTING_READ(reg);
3775
3776 reg = FDI_RX_CTL(pipe);
3777 temp = I915_READ(reg);
3778 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003779 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003780 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3781
3782 POSTING_READ(reg);
3783 udelay(100);
3784
3785 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003786 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003787 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003788
3789 /* still set train pattern 1 */
3790 reg = FDI_TX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 temp &= ~FDI_LINK_TRAIN_NONE;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1;
3794 I915_WRITE(reg, temp);
3795
3796 reg = FDI_RX_CTL(pipe);
3797 temp = I915_READ(reg);
3798 if (HAS_PCH_CPT(dev)) {
3799 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3800 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3801 } else {
3802 temp &= ~FDI_LINK_TRAIN_NONE;
3803 temp |= FDI_LINK_TRAIN_PATTERN_1;
3804 }
3805 /* BPC in FDI rx is consistent with that in PIPECONF */
3806 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003807 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003808 I915_WRITE(reg, temp);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812}
3813
Chris Wilson5dce5b932014-01-20 10:17:36 +00003814bool intel_has_pending_fb_unpin(struct drm_device *dev)
3815{
3816 struct intel_crtc *crtc;
3817
3818 /* Note that we don't need to be called with mode_config.lock here
3819 * as our list of CRTC objects is static for the lifetime of the
3820 * device and so cannot disappear as we iterate. Similarly, we can
3821 * happily treat the predicates as racy, atomic checks as userspace
3822 * cannot claim and pin a new fb without at least acquring the
3823 * struct_mutex and so serialising with us.
3824 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003825 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003826 if (atomic_read(&crtc->unpin_work_count) == 0)
3827 continue;
3828
3829 if (crtc->unpin_work)
3830 intel_wait_for_vblank(dev, crtc->pipe);
3831
3832 return true;
3833 }
3834
3835 return false;
3836}
3837
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003838static void page_flip_completed(struct intel_crtc *intel_crtc)
3839{
3840 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3841 struct intel_unpin_work *work = intel_crtc->unpin_work;
3842
3843 /* ensure that the unpin work is consistent wrt ->pending. */
3844 smp_rmb();
3845 intel_crtc->unpin_work = NULL;
3846
3847 if (work->event)
3848 drm_send_vblank_event(intel_crtc->base.dev,
3849 intel_crtc->pipe,
3850 work->event);
3851
3852 drm_crtc_vblank_put(&intel_crtc->base);
3853
3854 wake_up_all(&dev_priv->pending_flip_queue);
3855 queue_work(dev_priv->wq, &work->work);
3856
3857 trace_i915_flip_complete(intel_crtc->plane,
3858 work->pending_flip_obj);
3859}
3860
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003861static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003862{
Chris Wilson0f911282012-04-17 10:05:38 +01003863 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003864 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003865 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003866
Daniel Vetter2c10d572012-12-20 21:24:07 +01003867 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003868
3869 ret = wait_event_interruptible_timeout(
3870 dev_priv->pending_flip_queue,
3871 !intel_crtc_has_pending_flip(crtc),
3872 60*HZ);
3873
3874 if (ret < 0)
3875 return ret;
3876
3877 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003879
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003880 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003881 if (intel_crtc->unpin_work) {
3882 WARN_ONCE(1, "Removing stuck page flip\n");
3883 page_flip_completed(intel_crtc);
3884 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003885 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003886 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003887
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003888 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003889}
3890
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003891static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3892{
3893 u32 temp;
3894
3895 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3896
3897 mutex_lock(&dev_priv->sb_lock);
3898
3899 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3900 temp |= SBI_SSCCTL_DISABLE;
3901 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3902
3903 mutex_unlock(&dev_priv->sb_lock);
3904}
3905
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906/* Program iCLKIP clock to the desired frequency */
3907static void lpt_program_iclkip(struct drm_crtc *crtc)
3908{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003909 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003910 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003911 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3912 u32 temp;
3913
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003914 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003916 /* The iCLK virtual clock root frequency is in MHz,
3917 * but the adjusted_mode->crtc_clock in in KHz. To get the
3918 * divisors, it is necessary to divide one by another, so we
3919 * convert the virtual clock precision to KHz here for higher
3920 * precision.
3921 */
3922 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 u32 iclk_virtual_root_freq = 172800 * 1000;
3924 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003925 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003927 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3928 clock << auxdiv);
3929 divsel = (desired_divisor / iclk_pi_range) - 2;
3930 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003932 /*
3933 * Near 20MHz is a corner case which is
3934 * out of range for the 7-bit divisor
3935 */
3936 if (divsel <= 0x7f)
3937 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 }
3939
3940 /* This should not happen with any sane values */
3941 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3942 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3943 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3944 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3945
3946 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 +03003947 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 auxdiv,
3949 divsel,
3950 phasedir,
3951 phaseinc);
3952
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003953 mutex_lock(&dev_priv->sb_lock);
3954
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003956 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3958 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3959 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3960 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3961 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3962 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003963 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003964
3965 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003966 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003967 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3968 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003969 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970
3971 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003972 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003973 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003974 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003975
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003976 mutex_unlock(&dev_priv->sb_lock);
3977
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003978 /* Wait for initialization time */
3979 udelay(24);
3980
3981 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3982}
3983
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003984int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3985{
3986 u32 divsel, phaseinc, auxdiv;
3987 u32 iclk_virtual_root_freq = 172800 * 1000;
3988 u32 iclk_pi_range = 64;
3989 u32 desired_divisor;
3990 u32 temp;
3991
3992 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3993 return 0;
3994
3995 mutex_lock(&dev_priv->sb_lock);
3996
3997 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3998 if (temp & SBI_SSCCTL_DISABLE) {
3999 mutex_unlock(&dev_priv->sb_lock);
4000 return 0;
4001 }
4002
4003 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4004 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4005 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4006 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4007 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4008
4009 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4010 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4011 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4012
4013 mutex_unlock(&dev_priv->sb_lock);
4014
4015 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4016
4017 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4018 desired_divisor << auxdiv);
4019}
4020
Daniel Vetter275f01b22013-05-03 11:49:47 +02004021static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4022 enum pipe pch_transcoder)
4023{
4024 struct drm_device *dev = crtc->base.dev;
4025 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004026 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004027
4028 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4029 I915_READ(HTOTAL(cpu_transcoder)));
4030 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4031 I915_READ(HBLANK(cpu_transcoder)));
4032 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4033 I915_READ(HSYNC(cpu_transcoder)));
4034
4035 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4036 I915_READ(VTOTAL(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4038 I915_READ(VBLANK(cpu_transcoder)));
4039 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4040 I915_READ(VSYNC(cpu_transcoder)));
4041 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4042 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4043}
4044
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004045static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004046{
4047 struct drm_i915_private *dev_priv = dev->dev_private;
4048 uint32_t temp;
4049
4050 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004051 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004052 return;
4053
4054 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4055 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4056
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004057 temp &= ~FDI_BC_BIFURCATION_SELECT;
4058 if (enable)
4059 temp |= FDI_BC_BIFURCATION_SELECT;
4060
4061 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004062 I915_WRITE(SOUTH_CHICKEN1, temp);
4063 POSTING_READ(SOUTH_CHICKEN1);
4064}
4065
4066static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4067{
4068 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004069
4070 switch (intel_crtc->pipe) {
4071 case PIPE_A:
4072 break;
4073 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004074 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004075 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004077 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004078
4079 break;
4080 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004081 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004082
4083 break;
4084 default:
4085 BUG();
4086 }
4087}
4088
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004089/* Return which DP Port should be selected for Transcoder DP control */
4090static enum port
4091intel_trans_dp_port_sel(struct drm_crtc *crtc)
4092{
4093 struct drm_device *dev = crtc->dev;
4094 struct intel_encoder *encoder;
4095
4096 for_each_encoder_on_crtc(dev, crtc, encoder) {
4097 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4098 encoder->type == INTEL_OUTPUT_EDP)
4099 return enc_to_dig_port(&encoder->base)->port;
4100 }
4101
4102 return -1;
4103}
4104
Jesse Barnesf67a5592011-01-05 10:31:48 -08004105/*
4106 * Enable PCH resources required for PCH ports:
4107 * - PCH PLLs
4108 * - FDI training & RX/TX
4109 * - update transcoder timings
4110 * - DP transcoding bits
4111 * - transcoder
4112 */
4113static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004114{
4115 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004116 struct drm_i915_private *dev_priv = dev->dev_private;
4117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4118 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004119 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004120
Daniel Vetterab9412b2013-05-03 11:49:46 +02004121 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004122
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004123 if (IS_IVYBRIDGE(dev))
4124 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4125
Daniel Vettercd986ab2012-10-26 10:58:12 +02004126 /* Write the TU size bits before fdi link training, so that error
4127 * detection works. */
4128 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4129 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4130
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004131 /*
4132 * Sometimes spurious CPU pipe underruns happen during FDI
4133 * training, at least with VGA+HDMI cloning. Suppress them.
4134 */
4135 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4136
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004138 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004139
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004140 /* We need to program the right clock selection before writing the pixel
4141 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004142 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004143 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004144
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004146 temp |= TRANS_DPLL_ENABLE(pipe);
4147 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004148 if (intel_crtc->config->shared_dpll ==
4149 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004150 temp |= sel;
4151 else
4152 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004156 /* XXX: pch pll's can be enabled any time before we enable the PCH
4157 * transcoder, and we actually should do this to not upset any PCH
4158 * transcoder that already use the clock when we share it.
4159 *
4160 * Note that enable_shared_dpll tries to do the right thing, but
4161 * get_shared_dpll unconditionally resets the pll - we need that to have
4162 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004163 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004164
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004165 /* set transcoder timing, panel must allow it */
4166 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004167 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004169 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004170
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004171 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4172
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004174 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004175 const struct drm_display_mode *adjusted_mode =
4176 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004177 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004178 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 temp = I915_READ(reg);
4180 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004181 TRANS_DP_SYNC_MASK |
4182 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004183 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004184 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004185
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004186 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004187 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004188 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004189 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004190
4191 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004192 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004194 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004195 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004196 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004197 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004198 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004199 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004200 break;
4201 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004202 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004203 }
4204
Chris Wilson5eddb702010-09-11 13:48:45 +01004205 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004206 }
4207
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004208 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004209}
4210
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004211static void lpt_pch_enable(struct drm_crtc *crtc)
4212{
4213 struct drm_device *dev = crtc->dev;
4214 struct drm_i915_private *dev_priv = dev->dev_private;
4215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004216 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004217
Daniel Vetterab9412b2013-05-03 11:49:46 +02004218 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004219
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004220 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004221
Paulo Zanoni0540e482012-10-31 18:12:40 -02004222 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004223 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004224
Paulo Zanoni937bb612012-10-31 18:12:47 -02004225 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004226}
4227
Daniel Vettera1520312013-05-03 11:49:50 +02004228static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004229{
4230 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004231 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004232 u32 temp;
4233
4234 temp = I915_READ(dslreg);
4235 udelay(500);
4236 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004237 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004238 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004239 }
4240}
4241
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242static int
4243skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4244 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4245 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004246{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004247 struct intel_crtc_scaler_state *scaler_state =
4248 &crtc_state->scaler_state;
4249 struct intel_crtc *intel_crtc =
4250 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004251 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004252
4253 need_scaling = intel_rotation_90_or_270(rotation) ?
4254 (src_h != dst_w || src_w != dst_h):
4255 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004256
4257 /*
4258 * if plane is being disabled or scaler is no more required or force detach
4259 * - free scaler binded to this plane/crtc
4260 * - in order to do this, update crtc->scaler_usage
4261 *
4262 * Here scaler state in crtc_state is set free so that
4263 * scaler can be assigned to other user. Actual register
4264 * update to free the scaler is done in plane/panel-fit programming.
4265 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4266 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004267 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004268 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004270 scaler_state->scalers[*scaler_id].in_use = 0;
4271
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4273 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4274 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004275 scaler_state->scaler_users);
4276 *scaler_id = -1;
4277 }
4278 return 0;
4279 }
4280
4281 /* range checks */
4282 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4283 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4284
4285 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4286 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004287 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004288 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004290 return -EINVAL;
4291 }
4292
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293 /* mark this plane as a scaler user in crtc_state */
4294 scaler_state->scaler_users |= (1 << scaler_user);
4295 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4296 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4297 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4298 scaler_state->scaler_users);
4299
4300 return 0;
4301}
4302
4303/**
4304 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4305 *
4306 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004307 *
4308 * Return
4309 * 0 - scaler_usage updated successfully
4310 * error - requested scaling cannot be supported or other error condition
4311 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004312int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004313{
4314 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004315 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004316
4317 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4318 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4319
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004320 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004321 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004323 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004324}
4325
4326/**
4327 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4328 *
4329 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004330 * @plane_state: atomic plane state to update
4331 *
4332 * Return
4333 * 0 - scaler_usage updated successfully
4334 * error - requested scaling cannot be supported or other error condition
4335 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004336static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4337 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004338{
4339
4340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004341 struct intel_plane *intel_plane =
4342 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004343 struct drm_framebuffer *fb = plane_state->base.fb;
4344 int ret;
4345
4346 bool force_detach = !fb || !plane_state->visible;
4347
4348 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4349 intel_plane->base.base.id, intel_crtc->pipe,
4350 drm_plane_index(&intel_plane->base));
4351
4352 ret = skl_update_scaler(crtc_state, force_detach,
4353 drm_plane_index(&intel_plane->base),
4354 &plane_state->scaler_id,
4355 plane_state->base.rotation,
4356 drm_rect_width(&plane_state->src) >> 16,
4357 drm_rect_height(&plane_state->src) >> 16,
4358 drm_rect_width(&plane_state->dst),
4359 drm_rect_height(&plane_state->dst));
4360
4361 if (ret || plane_state->scaler_id < 0)
4362 return ret;
4363
Chandra Kondurua1b22782015-04-07 15:28:45 -07004364 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004365 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004366 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004367 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004368 return -EINVAL;
4369 }
4370
4371 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004372 switch (fb->pixel_format) {
4373 case DRM_FORMAT_RGB565:
4374 case DRM_FORMAT_XBGR8888:
4375 case DRM_FORMAT_XRGB8888:
4376 case DRM_FORMAT_ABGR8888:
4377 case DRM_FORMAT_ARGB8888:
4378 case DRM_FORMAT_XRGB2101010:
4379 case DRM_FORMAT_XBGR2101010:
4380 case DRM_FORMAT_YUYV:
4381 case DRM_FORMAT_YVYU:
4382 case DRM_FORMAT_UYVY:
4383 case DRM_FORMAT_VYUY:
4384 break;
4385 default:
4386 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4387 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4388 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004389 }
4390
Chandra Kondurua1b22782015-04-07 15:28:45 -07004391 return 0;
4392}
4393
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004394static void skylake_scaler_disable(struct intel_crtc *crtc)
4395{
4396 int i;
4397
4398 for (i = 0; i < crtc->num_scalers; i++)
4399 skl_detach_scaler(crtc, i);
4400}
4401
4402static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004403{
4404 struct drm_device *dev = crtc->base.dev;
4405 struct drm_i915_private *dev_priv = dev->dev_private;
4406 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004407 struct intel_crtc_scaler_state *scaler_state =
4408 &crtc->config->scaler_state;
4409
4410 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004412 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004413 int id;
4414
4415 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4416 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4417 return;
4418 }
4419
4420 id = scaler_state->scaler_id;
4421 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4422 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4423 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4424 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4425
4426 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004427 }
4428}
4429
Jesse Barnesb074cec2013-04-25 12:55:02 -07004430static void ironlake_pfit_enable(struct intel_crtc *crtc)
4431{
4432 struct drm_device *dev = crtc->base.dev;
4433 struct drm_i915_private *dev_priv = dev->dev_private;
4434 int pipe = crtc->pipe;
4435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004436 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004437 /* Force use of hard-coded filter coefficients
4438 * as some pre-programmed values are broken,
4439 * e.g. x201.
4440 */
4441 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4442 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4443 PF_PIPE_SEL_IVB(pipe));
4444 else
4445 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004446 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4447 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004448 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004449}
4450
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004451void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004453 struct drm_device *dev = crtc->base.dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004456 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004457 return;
4458
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004459 /* We can only enable IPS after we enable a plane and wait for a vblank */
4460 intel_wait_for_vblank(dev, crtc->pipe);
4461
Paulo Zanonid77e4532013-09-24 13:52:55 -03004462 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004463 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004464 mutex_lock(&dev_priv->rps.hw_lock);
4465 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4466 mutex_unlock(&dev_priv->rps.hw_lock);
4467 /* Quoting Art Runyan: "its not safe to expect any particular
4468 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004469 * mailbox." Moreover, the mailbox may return a bogus state,
4470 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004471 */
4472 } else {
4473 I915_WRITE(IPS_CTL, IPS_ENABLE);
4474 /* The bit only becomes 1 in the next vblank, so this wait here
4475 * is essentially intel_wait_for_vblank. If we don't have this
4476 * and don't wait for vblanks until the end of crtc_enable, then
4477 * the HW state readout code will complain that the expected
4478 * IPS_CTL value is not the one we read. */
4479 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4480 DRM_ERROR("Timed out waiting for IPS enable\n");
4481 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004482}
4483
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004484void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004485{
4486 struct drm_device *dev = crtc->base.dev;
4487 struct drm_i915_private *dev_priv = dev->dev_private;
4488
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004489 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004490 return;
4491
4492 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004493 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004494 mutex_lock(&dev_priv->rps.hw_lock);
4495 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4496 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004497 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4498 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4499 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004500 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004501 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004502 POSTING_READ(IPS_CTL);
4503 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004504
4505 /* We need to wait for a vblank before we can disable the plane. */
4506 intel_wait_for_vblank(dev, crtc->pipe);
4507}
4508
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004509static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004510{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004511 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004512 struct drm_device *dev = intel_crtc->base.dev;
4513 struct drm_i915_private *dev_priv = dev->dev_private;
4514
4515 mutex_lock(&dev->struct_mutex);
4516 dev_priv->mm.interruptible = false;
4517 (void) intel_overlay_switch_off(intel_crtc->overlay);
4518 dev_priv->mm.interruptible = true;
4519 mutex_unlock(&dev->struct_mutex);
4520 }
4521
4522 /* Let userspace switch the overlay on again. In most cases userspace
4523 * has to recompute where to put it anyway.
4524 */
4525}
4526
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004527/**
4528 * intel_post_enable_primary - Perform operations after enabling primary plane
4529 * @crtc: the CRTC whose primary plane was just enabled
4530 *
4531 * Performs potentially sleeping operations that must be done after the primary
4532 * plane is enabled, such as updating FBC and IPS. Note that this may be
4533 * called due to an explicit primary plane update, or due to an implicit
4534 * re-enable that is caused when a sprite plane is updated to no longer
4535 * completely hide the primary plane.
4536 */
4537static void
4538intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004539{
4540 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004541 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4543 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004544
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004545 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004546 * FIXME IPS should be fine as long as one plane is
4547 * enabled, but in practice it seems to have problems
4548 * when going from primary only to sprite only and vice
4549 * versa.
4550 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004551 hsw_enable_ips(intel_crtc);
4552
Daniel Vetterf99d7062014-06-19 16:01:59 +02004553 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004554 * Gen2 reports pipe underruns whenever all planes are disabled.
4555 * So don't enable underrun reporting before at least some planes
4556 * are enabled.
4557 * FIXME: Need to fix the logic to work when we turn off all planes
4558 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004559 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004560 if (IS_GEN2(dev))
4561 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4562
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004563 /* Underruns don't always raise interrupts, so check manually. */
4564 intel_check_cpu_fifo_underruns(dev_priv);
4565 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004566}
4567
Ville Syrjälä2622a082016-03-09 19:07:26 +02004568/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004569static void
4570intel_pre_disable_primary(struct drm_crtc *crtc)
4571{
4572 struct drm_device *dev = crtc->dev;
4573 struct drm_i915_private *dev_priv = dev->dev_private;
4574 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4575 int pipe = intel_crtc->pipe;
4576
4577 /*
4578 * Gen2 reports pipe underruns whenever all planes are disabled.
4579 * So diasble underrun reporting before all the planes get disabled.
4580 * FIXME: Need to fix the logic to work when we turn off all planes
4581 * but leave the pipe running.
4582 */
4583 if (IS_GEN2(dev))
4584 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4585
4586 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004587 * FIXME IPS should be fine as long as one plane is
4588 * enabled, but in practice it seems to have problems
4589 * when going from primary only to sprite only and vice
4590 * versa.
4591 */
4592 hsw_disable_ips(intel_crtc);
4593}
4594
4595/* FIXME get rid of this and use pre_plane_update */
4596static void
4597intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4598{
4599 struct drm_device *dev = crtc->dev;
4600 struct drm_i915_private *dev_priv = dev->dev_private;
4601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4602 int pipe = intel_crtc->pipe;
4603
4604 intel_pre_disable_primary(crtc);
4605
4606 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004607 * Vblank time updates from the shadow to live plane control register
4608 * are blocked if the memory self-refresh mode is active at that
4609 * moment. So to make sure the plane gets truly disabled, disable
4610 * first the self-refresh mode. The self-refresh enable bit in turn
4611 * will be checked/applied by the HW only at the next frame start
4612 * event which is after the vblank start event, so we need to have a
4613 * wait-for-vblank between disabling the plane and the pipe.
4614 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004615 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004616 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004617 dev_priv->wm.vlv.cxsr = false;
4618 intel_wait_for_vblank(dev, pipe);
4619 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004620}
4621
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004622static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004623{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004624 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4625 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004626 struct intel_crtc_state *pipe_config =
4627 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004628 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004629 struct drm_plane *primary = crtc->base.primary;
4630 struct drm_plane_state *old_pri_state =
4631 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004632
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004633 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004634
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004635 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004636
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004637 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004638 intel_update_watermarks(&crtc->base);
4639
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004640 if (old_pri_state) {
4641 struct intel_plane_state *primary_state =
4642 to_intel_plane_state(primary->state);
4643 struct intel_plane_state *old_primary_state =
4644 to_intel_plane_state(old_pri_state);
4645
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004646 intel_fbc_post_update(crtc);
4647
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004648 if (primary_state->visible &&
4649 (needs_modeset(&pipe_config->base) ||
4650 !old_primary_state->visible))
4651 intel_post_enable_primary(&crtc->base);
4652 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004653}
4654
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004655static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004656{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004657 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004658 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004659 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004660 struct intel_crtc_state *pipe_config =
4661 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004662 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4663 struct drm_plane *primary = crtc->base.primary;
4664 struct drm_plane_state *old_pri_state =
4665 drm_atomic_get_existing_plane_state(old_state, primary);
4666 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004667
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004668 if (old_pri_state) {
4669 struct intel_plane_state *primary_state =
4670 to_intel_plane_state(primary->state);
4671 struct intel_plane_state *old_primary_state =
4672 to_intel_plane_state(old_pri_state);
4673
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004674 intel_fbc_pre_update(crtc);
4675
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004676 if (old_primary_state->visible &&
4677 (modeset || !primary_state->visible))
4678 intel_pre_disable_primary(&crtc->base);
4679 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004680
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004681 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004682 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004683
Ville Syrjälä2622a082016-03-09 19:07:26 +02004684 /*
4685 * Vblank time updates from the shadow to live plane control register
4686 * are blocked if the memory self-refresh mode is active at that
4687 * moment. So to make sure the plane gets truly disabled, disable
4688 * first the self-refresh mode. The self-refresh enable bit in turn
4689 * will be checked/applied by the HW only at the next frame start
4690 * event which is after the vblank start event, so we need to have a
4691 * wait-for-vblank between disabling the plane and the pipe.
4692 */
4693 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004694 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004695 dev_priv->wm.vlv.cxsr = false;
4696 intel_wait_for_vblank(dev, crtc->pipe);
4697 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004698 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004699
Matt Ropered4a6a72016-02-23 17:20:13 -08004700 /*
4701 * IVB workaround: must disable low power watermarks for at least
4702 * one frame before enabling scaling. LP watermarks can be re-enabled
4703 * when scaling is disabled.
4704 *
4705 * WaCxSRDisabledForSpriteScaling:ivb
4706 */
4707 if (pipe_config->disable_lp_wm) {
4708 ilk_disable_lp_wm(dev);
4709 intel_wait_for_vblank(dev, crtc->pipe);
4710 }
4711
4712 /*
4713 * If we're doing a modeset, we're done. No need to do any pre-vblank
4714 * watermark programming here.
4715 */
4716 if (needs_modeset(&pipe_config->base))
4717 return;
4718
4719 /*
4720 * For platforms that support atomic watermarks, program the
4721 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4722 * will be the intermediate values that are safe for both pre- and
4723 * post- vblank; when vblank happens, the 'active' values will be set
4724 * to the final 'target' values and we'll do this again to get the
4725 * optimal watermarks. For gen9+ platforms, the values we program here
4726 * will be the final target values which will get automatically latched
4727 * at vblank time; no further programming will be necessary.
4728 *
4729 * If a platform hasn't been transitioned to atomic watermarks yet,
4730 * we'll continue to update watermarks the old way, if flags tell
4731 * us to.
4732 */
4733 if (dev_priv->display.initial_watermarks != NULL)
4734 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004735 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004736 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004737}
4738
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004739static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004740{
4741 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004743 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004744 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004745
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004746 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004747
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004748 drm_for_each_plane_mask(p, dev, plane_mask)
4749 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004750
Daniel Vetterf99d7062014-06-19 16:01:59 +02004751 /*
4752 * FIXME: Once we grow proper nuclear flip support out of this we need
4753 * to compute the mask of flip planes precisely. For the time being
4754 * consider this a flip to a NULL plane.
4755 */
4756 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004757}
4758
Jesse Barnesf67a5592011-01-05 10:31:48 -08004759static void ironlake_crtc_enable(struct drm_crtc *crtc)
4760{
4761 struct drm_device *dev = crtc->dev;
4762 struct drm_i915_private *dev_priv = dev->dev_private;
4763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004764 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004765 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004767 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004768 return;
4769
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004770 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004771 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4772
4773 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004774 intel_prepare_shared_dpll(intel_crtc);
4775
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004776 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304777 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004778
4779 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004780 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004782 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004783 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004784 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004785 }
4786
4787 ironlake_set_pipeconf(crtc);
4788
Jesse Barnesf67a5592011-01-05 10:31:48 -08004789 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004790
Daniel Vettera72e4c92014-09-30 10:56:47 +02004791 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004792
Daniel Vetterf6736a12013-06-05 13:34:30 +02004793 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004794 if (encoder->pre_enable)
4795 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004796
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004797 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004798 /* Note: FDI PLL enabling _must_ be done before we enable the
4799 * cpu pipes, hence this is separate from all the other fdi/pch
4800 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004801 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004802 } else {
4803 assert_fdi_tx_disabled(dev_priv, pipe);
4804 assert_fdi_rx_disabled(dev_priv, pipe);
4805 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004806
Jesse Barnesb074cec2013-04-25 12:55:02 -07004807 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004808
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004809 /*
4810 * On ILK+ LUT must be loaded before the pipe is running but with
4811 * clocks enabled
4812 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004813 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004814
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004815 if (dev_priv->display.initial_watermarks != NULL)
4816 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004817 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004818
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004819 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004820 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004821
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004822 assert_vblank_disabled(crtc);
4823 drm_crtc_vblank_on(crtc);
4824
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004825 for_each_encoder_on_crtc(dev, crtc, encoder)
4826 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004827
4828 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004829 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004830
4831 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4832 if (intel_crtc->config->has_pch_encoder)
4833 intel_wait_for_vblank(dev, pipe);
4834 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004835}
4836
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004837/* IPS only exists on ULT machines and is tied to pipe A. */
4838static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4839{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004840 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004841}
4842
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004843static void haswell_crtc_enable(struct drm_crtc *crtc)
4844{
4845 struct drm_device *dev = crtc->dev;
4846 struct drm_i915_private *dev_priv = dev->dev_private;
4847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4848 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004849 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004850 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004851 struct intel_crtc_state *pipe_config =
4852 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004853
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004854 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004855 return;
4856
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004857 if (intel_crtc->config->has_pch_encoder)
4858 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4859 false);
4860
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004861 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004862 intel_enable_shared_dpll(intel_crtc);
4863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004864 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304865 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004866
Jani Nikula4d1de972016-03-18 17:05:42 +02004867 if (!intel_crtc->config->has_dsi_encoder)
4868 intel_set_pipe_timings(intel_crtc);
4869
Jani Nikulabc58be62016-03-18 17:05:39 +02004870 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004871
Jani Nikula4d1de972016-03-18 17:05:42 +02004872 if (cpu_transcoder != TRANSCODER_EDP &&
4873 !transcoder_is_dsi(cpu_transcoder)) {
4874 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004875 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004876 }
4877
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004878 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004879 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004880 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004881 }
4882
Jani Nikula4d1de972016-03-18 17:05:42 +02004883 if (!intel_crtc->config->has_dsi_encoder)
4884 haswell_set_pipeconf(crtc);
4885
Jani Nikula391bf042016-03-18 17:05:40 +02004886 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004887
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004888 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004889
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004890 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004891
Daniel Vetter6b698512015-11-28 11:05:39 +01004892 if (intel_crtc->config->has_pch_encoder)
4893 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4894 else
4895 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4896
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304897 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004898 if (encoder->pre_enable)
4899 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304900 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004902 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004903 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004904
Jani Nikulaa65347b2015-11-27 12:21:46 +02004905 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304906 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004907
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004908 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004909 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004910 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004911 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004912
4913 /*
4914 * On ILK+ LUT must be loaded before the pipe is running but with
4915 * clocks enabled
4916 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004917 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004918
Paulo Zanoni1f544382012-10-24 11:32:00 -02004919 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004920 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304921 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004922
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004923 if (dev_priv->display.initial_watermarks != NULL)
4924 dev_priv->display.initial_watermarks(pipe_config);
4925 else
4926 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004927
4928 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4929 if (!intel_crtc->config->has_dsi_encoder)
4930 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004932 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004933 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004934
Jani Nikulaa65347b2015-11-27 12:21:46 +02004935 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004936 intel_ddi_set_vc_payload_alloc(crtc, true);
4937
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004938 assert_vblank_disabled(crtc);
4939 drm_crtc_vblank_on(crtc);
4940
Jani Nikula8807e552013-08-30 19:40:32 +03004941 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004942 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004943 intel_opregion_notify_encoder(encoder, true);
4944 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004945
Daniel Vetter6b698512015-11-28 11:05:39 +01004946 if (intel_crtc->config->has_pch_encoder) {
4947 intel_wait_for_vblank(dev, pipe);
4948 intel_wait_for_vblank(dev, pipe);
4949 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004950 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4951 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004952 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004953
Paulo Zanonie4916942013-09-20 16:21:19 -03004954 /* If we change the relative order between pipe/planes enabling, we need
4955 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004956 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4957 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4958 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4959 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4960 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961}
4962
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004963static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004964{
4965 struct drm_device *dev = crtc->base.dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 int pipe = crtc->pipe;
4968
4969 /* To avoid upsetting the power well on haswell only disable the pfit if
4970 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004971 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004972 I915_WRITE(PF_CTL(pipe), 0);
4973 I915_WRITE(PF_WIN_POS(pipe), 0);
4974 I915_WRITE(PF_WIN_SZ(pipe), 0);
4975 }
4976}
4977
Jesse Barnes6be4a602010-09-10 10:26:01 -07004978static void ironlake_crtc_disable(struct drm_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->dev;
4981 struct drm_i915_private *dev_priv = dev->dev_private;
4982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004983 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004985
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004986 if (intel_crtc->config->has_pch_encoder)
4987 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4988
Daniel Vetterea9d7582012-07-10 10:42:52 +02004989 for_each_encoder_on_crtc(dev, crtc, encoder)
4990 encoder->disable(encoder);
4991
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004992 drm_crtc_vblank_off(crtc);
4993 assert_vblank_disabled(crtc);
4994
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004995 /*
4996 * Sometimes spurious CPU pipe underruns happen when the
4997 * pipe is already disabled, but FDI RX/TX is still enabled.
4998 * Happens at least with VGA+HDMI cloning. Suppress them.
4999 */
5000 if (intel_crtc->config->has_pch_encoder)
5001 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5002
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005003 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005004
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005005 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005006
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005007 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005008 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005009 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5010 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005011
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005012 for_each_encoder_on_crtc(dev, crtc, encoder)
5013 if (encoder->post_disable)
5014 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005015
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005016 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018
Daniel Vetterd925c592013-06-05 13:34:04 +02005019 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005020 i915_reg_t reg;
5021 u32 temp;
5022
Daniel Vetterd925c592013-06-05 13:34:04 +02005023 /* disable TRANS_DP_CTL */
5024 reg = TRANS_DP_CTL(pipe);
5025 temp = I915_READ(reg);
5026 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5027 TRANS_DP_PORT_SEL_MASK);
5028 temp |= TRANS_DP_PORT_SEL_NONE;
5029 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005030
Daniel Vetterd925c592013-06-05 13:34:04 +02005031 /* disable DPLL_SEL */
5032 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005033 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005034 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005035 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005036
Daniel Vetterd925c592013-06-05 13:34:04 +02005037 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005038 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005039
5040 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005041}
5042
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043static void haswell_crtc_disable(struct drm_crtc *crtc)
5044{
5045 struct drm_device *dev = crtc->dev;
5046 struct drm_i915_private *dev_priv = dev->dev_private;
5047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5048 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005049 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005051 if (intel_crtc->config->has_pch_encoder)
5052 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5053 false);
5054
Jani Nikula8807e552013-08-30 19:40:32 +03005055 for_each_encoder_on_crtc(dev, crtc, encoder) {
5056 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005058 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005060 drm_crtc_vblank_off(crtc);
5061 assert_vblank_disabled(crtc);
5062
Jani Nikula4d1de972016-03-18 17:05:42 +02005063 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5064 if (!intel_crtc->config->has_dsi_encoder)
5065 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005067 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005068 intel_ddi_set_vc_payload_alloc(crtc, false);
5069
Jani Nikulaa65347b2015-11-27 12:21:46 +02005070 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305071 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005072
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005073 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005074 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005075 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005076 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005077
Jani Nikulaa65347b2015-11-27 12:21:46 +02005078 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305079 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080
Imre Deak97b040a2014-06-25 22:01:50 +03005081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 if (encoder->post_disable)
5083 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005084
Ville Syrjälä92966a32015-12-08 16:05:48 +02005085 if (intel_crtc->config->has_pch_encoder) {
5086 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005087 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005088 intel_ddi_fdi_disable(crtc);
5089
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005090 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5091 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005092 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005093}
5094
Jesse Barnes2dd24552013-04-25 12:55:01 -07005095static void i9xx_pfit_enable(struct intel_crtc *crtc)
5096{
5097 struct drm_device *dev = crtc->base.dev;
5098 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005099 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005100
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005101 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005102 return;
5103
Daniel Vetterc0b03412013-05-28 12:05:54 +02005104 /*
5105 * The panel fitter should only be adjusted whilst the pipe is disabled,
5106 * according to register description and PRM.
5107 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005108 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5109 assert_pipe_disabled(dev_priv, crtc->pipe);
5110
Jesse Barnesb074cec2013-04-25 12:55:02 -07005111 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5112 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005113
5114 /* Border color in case we don't scale up to the full screen. Black by
5115 * default, change to something else for debugging. */
5116 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005117}
5118
Dave Airlied05410f2014-06-05 13:22:59 +10005119static enum intel_display_power_domain port_to_power_domain(enum port port)
5120{
5121 switch (port) {
5122 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005123 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005124 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005125 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005126 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005127 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005128 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005129 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005130 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005131 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005132 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005133 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005134 return POWER_DOMAIN_PORT_OTHER;
5135 }
5136}
5137
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005138static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5139{
5140 switch (port) {
5141 case PORT_A:
5142 return POWER_DOMAIN_AUX_A;
5143 case PORT_B:
5144 return POWER_DOMAIN_AUX_B;
5145 case PORT_C:
5146 return POWER_DOMAIN_AUX_C;
5147 case PORT_D:
5148 return POWER_DOMAIN_AUX_D;
5149 case PORT_E:
5150 /* FIXME: Check VBT for actual wiring of PORT E */
5151 return POWER_DOMAIN_AUX_D;
5152 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005153 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005154 return POWER_DOMAIN_AUX_A;
5155 }
5156}
5157
Imre Deak319be8a2014-03-04 19:22:57 +02005158enum intel_display_power_domain
5159intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005160{
Imre Deak319be8a2014-03-04 19:22:57 +02005161 struct drm_device *dev = intel_encoder->base.dev;
5162 struct intel_digital_port *intel_dig_port;
5163
5164 switch (intel_encoder->type) {
5165 case INTEL_OUTPUT_UNKNOWN:
5166 /* Only DDI platforms should ever use this output type */
5167 WARN_ON_ONCE(!HAS_DDI(dev));
5168 case INTEL_OUTPUT_DISPLAYPORT:
5169 case INTEL_OUTPUT_HDMI:
5170 case INTEL_OUTPUT_EDP:
5171 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005172 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005173 case INTEL_OUTPUT_DP_MST:
5174 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5175 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005176 case INTEL_OUTPUT_ANALOG:
5177 return POWER_DOMAIN_PORT_CRT;
5178 case INTEL_OUTPUT_DSI:
5179 return POWER_DOMAIN_PORT_DSI;
5180 default:
5181 return POWER_DOMAIN_PORT_OTHER;
5182 }
5183}
5184
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005185enum intel_display_power_domain
5186intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5187{
5188 struct drm_device *dev = intel_encoder->base.dev;
5189 struct intel_digital_port *intel_dig_port;
5190
5191 switch (intel_encoder->type) {
5192 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005193 case INTEL_OUTPUT_HDMI:
5194 /*
5195 * Only DDI platforms should ever use these output types.
5196 * We can get here after the HDMI detect code has already set
5197 * the type of the shared encoder. Since we can't be sure
5198 * what's the status of the given connectors, play safe and
5199 * run the DP detection too.
5200 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005201 WARN_ON_ONCE(!HAS_DDI(dev));
5202 case INTEL_OUTPUT_DISPLAYPORT:
5203 case INTEL_OUTPUT_EDP:
5204 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5205 return port_to_aux_power_domain(intel_dig_port->port);
5206 case INTEL_OUTPUT_DP_MST:
5207 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5208 return port_to_aux_power_domain(intel_dig_port->port);
5209 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005210 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005211 return POWER_DOMAIN_AUX_A;
5212 }
5213}
5214
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5216 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005217{
5218 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005219 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5221 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005222 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005223 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005224
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005225 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005226 return 0;
5227
Imre Deak77d22dc2014-03-05 16:20:52 +02005228 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5229 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005230 if (crtc_state->pch_pfit.enabled ||
5231 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005232 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5233
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005234 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5235 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5236
Imre Deak319be8a2014-03-04 19:22:57 +02005237 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005238 }
Imre Deak319be8a2014-03-04 19:22:57 +02005239
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005240 if (crtc_state->shared_dpll)
5241 mask |= BIT(POWER_DOMAIN_PLLS);
5242
Imre Deak77d22dc2014-03-05 16:20:52 +02005243 return mask;
5244}
5245
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005246static unsigned long
5247modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5248 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005249{
5250 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5252 enum intel_display_power_domain domain;
5253 unsigned long domains, new_domains, old_domains;
5254
5255 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005256 intel_crtc->enabled_power_domains = new_domains =
5257 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005258
5259 domains = new_domains & ~old_domains;
5260
5261 for_each_power_domain(domain, domains)
5262 intel_display_power_get(dev_priv, domain);
5263
5264 return old_domains & ~new_domains;
5265}
5266
5267static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5268 unsigned long domains)
5269{
5270 enum intel_display_power_domain domain;
5271
5272 for_each_power_domain(domain, domains)
5273 intel_display_power_put(dev_priv, domain);
5274}
5275
Mika Kaholaadafdc62015-08-18 14:36:59 +03005276static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5277{
5278 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5279
5280 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5281 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5282 return max_cdclk_freq;
5283 else if (IS_CHERRYVIEW(dev_priv))
5284 return max_cdclk_freq*95/100;
5285 else if (INTEL_INFO(dev_priv)->gen < 4)
5286 return 2*max_cdclk_freq*90/100;
5287 else
5288 return max_cdclk_freq*90/100;
5289}
5290
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005291static void intel_update_max_cdclk(struct drm_device *dev)
5292{
5293 struct drm_i915_private *dev_priv = dev->dev_private;
5294
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005295 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005296 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5297
5298 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5299 dev_priv->max_cdclk_freq = 675000;
5300 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5301 dev_priv->max_cdclk_freq = 540000;
5302 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5303 dev_priv->max_cdclk_freq = 450000;
5304 else
5305 dev_priv->max_cdclk_freq = 337500;
5306 } else if (IS_BROADWELL(dev)) {
5307 /*
5308 * FIXME with extra cooling we can allow
5309 * 540 MHz for ULX and 675 Mhz for ULT.
5310 * How can we know if extra cooling is
5311 * available? PCI ID, VTB, something else?
5312 */
5313 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5314 dev_priv->max_cdclk_freq = 450000;
5315 else if (IS_BDW_ULX(dev))
5316 dev_priv->max_cdclk_freq = 450000;
5317 else if (IS_BDW_ULT(dev))
5318 dev_priv->max_cdclk_freq = 540000;
5319 else
5320 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005321 } else if (IS_CHERRYVIEW(dev)) {
5322 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005323 } else if (IS_VALLEYVIEW(dev)) {
5324 dev_priv->max_cdclk_freq = 400000;
5325 } else {
5326 /* otherwise assume cdclk is fixed */
5327 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5328 }
5329
Mika Kaholaadafdc62015-08-18 14:36:59 +03005330 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5331
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005332 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5333 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005334
5335 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5336 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005337}
5338
5339static void intel_update_cdclk(struct drm_device *dev)
5340{
5341 struct drm_i915_private *dev_priv = dev->dev_private;
5342
5343 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5344 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5345 dev_priv->cdclk_freq);
5346
5347 /*
5348 * Program the gmbus_freq based on the cdclk frequency.
5349 * BSpec erroneously claims we should aim for 4MHz, but
5350 * in fact 1MHz is the correct frequency.
5351 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005352 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005353 /*
5354 * Program the gmbus_freq based on the cdclk frequency.
5355 * BSpec erroneously claims we should aim for 4MHz, but
5356 * in fact 1MHz is the correct frequency.
5357 */
5358 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5359 }
5360
5361 if (dev_priv->max_cdclk_freq == 0)
5362 intel_update_max_cdclk(dev);
5363}
5364
Damien Lespiau70d0c572015-06-04 18:21:29 +01005365static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305366{
5367 struct drm_i915_private *dev_priv = dev->dev_private;
5368 uint32_t divider;
5369 uint32_t ratio;
5370 uint32_t current_freq;
5371 int ret;
5372
5373 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5374 switch (frequency) {
5375 case 144000:
5376 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5377 ratio = BXT_DE_PLL_RATIO(60);
5378 break;
5379 case 288000:
5380 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5381 ratio = BXT_DE_PLL_RATIO(60);
5382 break;
5383 case 384000:
5384 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5385 ratio = BXT_DE_PLL_RATIO(60);
5386 break;
5387 case 576000:
5388 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5389 ratio = BXT_DE_PLL_RATIO(60);
5390 break;
5391 case 624000:
5392 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5393 ratio = BXT_DE_PLL_RATIO(65);
5394 break;
5395 case 19200:
5396 /*
5397 * Bypass frequency with DE PLL disabled. Init ratio, divider
5398 * to suppress GCC warning.
5399 */
5400 ratio = 0;
5401 divider = 0;
5402 break;
5403 default:
5404 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5405
5406 return;
5407 }
5408
5409 mutex_lock(&dev_priv->rps.hw_lock);
5410 /* Inform power controller of upcoming frequency change */
5411 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5412 0x80000000);
5413 mutex_unlock(&dev_priv->rps.hw_lock);
5414
5415 if (ret) {
5416 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5417 ret, frequency);
5418 return;
5419 }
5420
5421 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5422 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5423 current_freq = current_freq * 500 + 1000;
5424
5425 /*
5426 * DE PLL has to be disabled when
5427 * - setting to 19.2MHz (bypass, PLL isn't used)
5428 * - before setting to 624MHz (PLL needs toggling)
5429 * - before setting to any frequency from 624MHz (PLL needs toggling)
5430 */
5431 if (frequency == 19200 || frequency == 624000 ||
5432 current_freq == 624000) {
5433 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5434 /* Timeout 200us */
5435 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5436 1))
5437 DRM_ERROR("timout waiting for DE PLL unlock\n");
5438 }
5439
5440 if (frequency != 19200) {
5441 uint32_t val;
5442
5443 val = I915_READ(BXT_DE_PLL_CTL);
5444 val &= ~BXT_DE_PLL_RATIO_MASK;
5445 val |= ratio;
5446 I915_WRITE(BXT_DE_PLL_CTL, val);
5447
5448 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5449 /* Timeout 200us */
5450 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5451 DRM_ERROR("timeout waiting for DE PLL lock\n");
5452
5453 val = I915_READ(CDCLK_CTL);
5454 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5455 val |= divider;
5456 /*
5457 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5458 * enable otherwise.
5459 */
5460 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5461 if (frequency >= 500000)
5462 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5463
5464 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5465 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5466 val |= (frequency - 1000) / 500;
5467 I915_WRITE(CDCLK_CTL, val);
5468 }
5469
5470 mutex_lock(&dev_priv->rps.hw_lock);
5471 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5472 DIV_ROUND_UP(frequency, 25000));
5473 mutex_unlock(&dev_priv->rps.hw_lock);
5474
5475 if (ret) {
5476 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5477 ret, frequency);
5478 return;
5479 }
5480
Damien Lespiaua47871b2015-06-04 18:21:34 +01005481 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305482}
5483
5484void broxton_init_cdclk(struct drm_device *dev)
5485{
5486 struct drm_i915_private *dev_priv = dev->dev_private;
5487 uint32_t val;
5488
5489 /*
5490 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5491 * or else the reset will hang because there is no PCH to respond.
5492 * Move the handshake programming to initialization sequence.
5493 * Previously was left up to BIOS.
5494 */
5495 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5496 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5497 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5498
5499 /* Enable PG1 for cdclk */
5500 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5501
5502 /* check if cd clock is enabled */
5503 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5504 DRM_DEBUG_KMS("Display already initialized\n");
5505 return;
5506 }
5507
5508 /*
5509 * FIXME:
5510 * - The initial CDCLK needs to be read from VBT.
5511 * Need to make this change after VBT has changes for BXT.
5512 * - check if setting the max (or any) cdclk freq is really necessary
5513 * here, it belongs to modeset time
5514 */
5515 broxton_set_cdclk(dev, 624000);
5516
5517 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005518 POSTING_READ(DBUF_CTL);
5519
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305520 udelay(10);
5521
5522 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5523 DRM_ERROR("DBuf power enable timeout!\n");
5524}
5525
5526void broxton_uninit_cdclk(struct drm_device *dev)
5527{
5528 struct drm_i915_private *dev_priv = dev->dev_private;
5529
5530 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005531 POSTING_READ(DBUF_CTL);
5532
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305533 udelay(10);
5534
5535 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5536 DRM_ERROR("DBuf power disable timeout!\n");
5537
5538 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5539 broxton_set_cdclk(dev, 19200);
5540
5541 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5542}
5543
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005544static const struct skl_cdclk_entry {
5545 unsigned int freq;
5546 unsigned int vco;
5547} skl_cdclk_frequencies[] = {
5548 { .freq = 308570, .vco = 8640 },
5549 { .freq = 337500, .vco = 8100 },
5550 { .freq = 432000, .vco = 8640 },
5551 { .freq = 450000, .vco = 8100 },
5552 { .freq = 540000, .vco = 8100 },
5553 { .freq = 617140, .vco = 8640 },
5554 { .freq = 675000, .vco = 8100 },
5555};
5556
5557static unsigned int skl_cdclk_decimal(unsigned int freq)
5558{
5559 return (freq - 1000) / 500;
5560}
5561
5562static unsigned int skl_cdclk_get_vco(unsigned int freq)
5563{
5564 unsigned int i;
5565
5566 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5567 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5568
5569 if (e->freq == freq)
5570 return e->vco;
5571 }
5572
5573 return 8100;
5574}
5575
5576static void
5577skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5578{
5579 unsigned int min_freq;
5580 u32 val;
5581
5582 /* select the minimum CDCLK before enabling DPLL 0 */
5583 val = I915_READ(CDCLK_CTL);
5584 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5585 val |= CDCLK_FREQ_337_308;
5586
5587 if (required_vco == 8640)
5588 min_freq = 308570;
5589 else
5590 min_freq = 337500;
5591
5592 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5593
5594 I915_WRITE(CDCLK_CTL, val);
5595 POSTING_READ(CDCLK_CTL);
5596
5597 /*
5598 * We always enable DPLL0 with the lowest link rate possible, but still
5599 * taking into account the VCO required to operate the eDP panel at the
5600 * desired frequency. The usual DP link rates operate with a VCO of
5601 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5602 * The modeset code is responsible for the selection of the exact link
5603 * rate later on, with the constraint of choosing a frequency that
5604 * works with required_vco.
5605 */
5606 val = I915_READ(DPLL_CTRL1);
5607
5608 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5609 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5610 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5611 if (required_vco == 8640)
5612 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5613 SKL_DPLL0);
5614 else
5615 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5616 SKL_DPLL0);
5617
5618 I915_WRITE(DPLL_CTRL1, val);
5619 POSTING_READ(DPLL_CTRL1);
5620
5621 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5622
5623 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5624 DRM_ERROR("DPLL0 not locked\n");
5625}
5626
5627static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5628{
5629 int ret;
5630 u32 val;
5631
5632 /* inform PCU we want to change CDCLK */
5633 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5634 mutex_lock(&dev_priv->rps.hw_lock);
5635 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5636 mutex_unlock(&dev_priv->rps.hw_lock);
5637
5638 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5639}
5640
5641static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5642{
5643 unsigned int i;
5644
5645 for (i = 0; i < 15; i++) {
5646 if (skl_cdclk_pcu_ready(dev_priv))
5647 return true;
5648 udelay(10);
5649 }
5650
5651 return false;
5652}
5653
5654static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5655{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005656 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005657 u32 freq_select, pcu_ack;
5658
5659 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5660
5661 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5662 DRM_ERROR("failed to inform PCU about cdclk change\n");
5663 return;
5664 }
5665
5666 /* set CDCLK_CTL */
5667 switch(freq) {
5668 case 450000:
5669 case 432000:
5670 freq_select = CDCLK_FREQ_450_432;
5671 pcu_ack = 1;
5672 break;
5673 case 540000:
5674 freq_select = CDCLK_FREQ_540;
5675 pcu_ack = 2;
5676 break;
5677 case 308570:
5678 case 337500:
5679 default:
5680 freq_select = CDCLK_FREQ_337_308;
5681 pcu_ack = 0;
5682 break;
5683 case 617140:
5684 case 675000:
5685 freq_select = CDCLK_FREQ_675_617;
5686 pcu_ack = 3;
5687 break;
5688 }
5689
5690 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5691 POSTING_READ(CDCLK_CTL);
5692
5693 /* inform PCU of the change */
5694 mutex_lock(&dev_priv->rps.hw_lock);
5695 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5696 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005697
5698 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699}
5700
5701void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5702{
5703 /* disable DBUF power */
5704 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5705 POSTING_READ(DBUF_CTL);
5706
5707 udelay(10);
5708
5709 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5710 DRM_ERROR("DBuf power disable timeout\n");
5711
Imre Deakab96c1ee2015-11-04 19:24:18 +02005712 /* disable DPLL0 */
5713 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5714 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5715 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005716}
5717
5718void skl_init_cdclk(struct drm_i915_private *dev_priv)
5719{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005720 unsigned int required_vco;
5721
Gary Wang39d9b852015-08-28 16:40:34 +08005722 /* DPLL0 not enabled (happens on early BIOS versions) */
5723 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5724 /* enable DPLL0 */
5725 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5726 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005727 }
5728
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005729 /* set CDCLK to the frequency the BIOS chose */
5730 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5731
5732 /* enable DBUF power */
5733 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5734 POSTING_READ(DBUF_CTL);
5735
5736 udelay(10);
5737
5738 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5739 DRM_ERROR("DBuf power enable timeout\n");
5740}
5741
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305742int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5743{
5744 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5745 uint32_t cdctl = I915_READ(CDCLK_CTL);
5746 int freq = dev_priv->skl_boot_cdclk;
5747
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305748 /*
5749 * check if the pre-os intialized the display
5750 * There is SWF18 scratchpad register defined which is set by the
5751 * pre-os which can be used by the OS drivers to check the status
5752 */
5753 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5754 goto sanitize;
5755
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305756 /* Is PLL enabled and locked ? */
5757 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5758 goto sanitize;
5759
5760 /* DPLL okay; verify the cdclock
5761 *
5762 * Noticed in some instances that the freq selection is correct but
5763 * decimal part is programmed wrong from BIOS where pre-os does not
5764 * enable display. Verify the same as well.
5765 */
5766 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5767 /* All well; nothing to sanitize */
5768 return false;
5769sanitize:
5770 /*
5771 * As of now initialize with max cdclk till
5772 * we get dynamic cdclk support
5773 * */
5774 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5775 skl_init_cdclk(dev_priv);
5776
5777 /* we did have to sanitize */
5778 return true;
5779}
5780
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781/* Adjust CDclk dividers to allow high res or save power if possible */
5782static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5783{
5784 struct drm_i915_private *dev_priv = dev->dev_private;
5785 u32 val, cmd;
5786
Vandana Kannan164dfd22014-11-24 13:37:41 +05305787 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5788 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005789
Ville Syrjälädfcab172014-06-13 13:37:47 +03005790 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005791 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005792 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 cmd = 1;
5794 else
5795 cmd = 0;
5796
5797 mutex_lock(&dev_priv->rps.hw_lock);
5798 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5799 val &= ~DSPFREQGUAR_MASK;
5800 val |= (cmd << DSPFREQGUAR_SHIFT);
5801 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5802 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5803 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5804 50)) {
5805 DRM_ERROR("timed out waiting for CDclk change\n");
5806 }
5807 mutex_unlock(&dev_priv->rps.hw_lock);
5808
Ville Syrjälä54433e92015-05-26 20:42:31 +03005809 mutex_lock(&dev_priv->sb_lock);
5810
Ville Syrjälädfcab172014-06-13 13:37:47 +03005811 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005812 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005813
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005814 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005815
Jesse Barnes30a970c2013-11-04 13:48:12 -08005816 /* adjust cdclk divider */
5817 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005818 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819 val |= divider;
5820 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005821
5822 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005823 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005824 50))
5825 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005826 }
5827
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828 /* adjust self-refresh exit latency value */
5829 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5830 val &= ~0x7f;
5831
5832 /*
5833 * For high bandwidth configs, we set a higher latency in the bunit
5834 * so that the core display fetch happens in time to avoid underruns.
5835 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005836 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005837 val |= 4500 / 250; /* 4.5 usec */
5838 else
5839 val |= 3000 / 250; /* 3.0 usec */
5840 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005841
Ville Syrjäläa5805162015-05-26 20:42:30 +03005842 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005843
Ville Syrjäläb6283052015-06-03 15:45:07 +03005844 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845}
5846
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5848{
5849 struct drm_i915_private *dev_priv = dev->dev_private;
5850 u32 val, cmd;
5851
Vandana Kannan164dfd22014-11-24 13:37:41 +05305852 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5853 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005854
5855 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005856 case 333333:
5857 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005858 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005859 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005860 break;
5861 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005862 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005863 return;
5864 }
5865
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005866 /*
5867 * Specs are full of misinformation, but testing on actual
5868 * hardware has shown that we just need to write the desired
5869 * CCK divider into the Punit register.
5870 */
5871 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5872
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005873 mutex_lock(&dev_priv->rps.hw_lock);
5874 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5875 val &= ~DSPFREQGUAR_MASK_CHV;
5876 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5877 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5878 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5879 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5880 50)) {
5881 DRM_ERROR("timed out waiting for CDclk change\n");
5882 }
5883 mutex_unlock(&dev_priv->rps.hw_lock);
5884
Ville Syrjäläb6283052015-06-03 15:45:07 +03005885 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005886}
5887
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5889 int max_pixclk)
5890{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005891 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005892 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005893
Jesse Barnes30a970c2013-11-04 13:48:12 -08005894 /*
5895 * Really only a few cases to deal with, as only 4 CDclks are supported:
5896 * 200MHz
5897 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005898 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005899 * 400MHz (VLV only)
5900 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5901 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005902 *
5903 * We seem to get an unstable or solid color picture at 200MHz.
5904 * Not sure what's wrong. For now use 200MHz only when all pipes
5905 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005907 if (!IS_CHERRYVIEW(dev_priv) &&
5908 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005909 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005910 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005911 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005912 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005913 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005914 else
5915 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916}
5917
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305918static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5919 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305921 /*
5922 * FIXME:
5923 * - remove the guardband, it's not needed on BXT
5924 * - set 19.2MHz bypass frequency if there are no active pipes
5925 */
5926 if (max_pixclk > 576000*9/10)
5927 return 624000;
5928 else if (max_pixclk > 384000*9/10)
5929 return 576000;
5930 else if (max_pixclk > 288000*9/10)
5931 return 384000;
5932 else if (max_pixclk > 144000*9/10)
5933 return 288000;
5934 else
5935 return 144000;
5936}
5937
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005938/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005939static int intel_mode_max_pixclk(struct drm_device *dev,
5940 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005941{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005942 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5943 struct drm_i915_private *dev_priv = dev->dev_private;
5944 struct drm_crtc *crtc;
5945 struct drm_crtc_state *crtc_state;
5946 unsigned max_pixclk = 0, i;
5947 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005948
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005949 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5950 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005951
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005952 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5953 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005954
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005955 if (crtc_state->enable)
5956 pixclk = crtc_state->adjusted_mode.crtc_clock;
5957
5958 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959 }
5960
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005961 for_each_pipe(dev_priv, pipe)
5962 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5963
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964 return max_pixclk;
5965}
5966
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005967static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005968{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005969 struct drm_device *dev = state->dev;
5970 struct drm_i915_private *dev_priv = dev->dev_private;
5971 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005972 struct intel_atomic_state *intel_state =
5973 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005974
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005975 if (max_pixclk < 0)
5976 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005977
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005978 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005979 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305980
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005981 if (!intel_state->active_crtcs)
5982 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5983
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005984 return 0;
5985}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005987static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5988{
5989 struct drm_device *dev = state->dev;
5990 struct drm_i915_private *dev_priv = dev->dev_private;
5991 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005992 struct intel_atomic_state *intel_state =
5993 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005994
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005995 if (max_pixclk < 0)
5996 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005997
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005998 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005999 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006000
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006001 if (!intel_state->active_crtcs)
6002 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6003
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006004 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006005}
6006
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006007static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6008{
6009 unsigned int credits, default_credits;
6010
6011 if (IS_CHERRYVIEW(dev_priv))
6012 default_credits = PFI_CREDIT(12);
6013 else
6014 default_credits = PFI_CREDIT(8);
6015
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006016 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006017 /* CHV suggested value is 31 or 63 */
6018 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006019 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006020 else
6021 credits = PFI_CREDIT(15);
6022 } else {
6023 credits = default_credits;
6024 }
6025
6026 /*
6027 * WA - write default credits before re-programming
6028 * FIXME: should we also set the resend bit here?
6029 */
6030 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6031 default_credits);
6032
6033 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6034 credits | PFI_CREDIT_RESEND);
6035
6036 /*
6037 * FIXME is this guaranteed to clear
6038 * immediately or should we poll for it?
6039 */
6040 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6041}
6042
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006043static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006045 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006046 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006047 struct intel_atomic_state *old_intel_state =
6048 to_intel_atomic_state(old_state);
6049 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006050
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006051 /*
6052 * FIXME: We can end up here with all power domains off, yet
6053 * with a CDCLK frequency other than the minimum. To account
6054 * for this take the PIPE-A power domain, which covers the HW
6055 * blocks needed for the following programming. This can be
6056 * removed once it's guaranteed that we get here either with
6057 * the minimum CDCLK set, or the required power domains
6058 * enabled.
6059 */
6060 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006061
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006062 if (IS_CHERRYVIEW(dev))
6063 cherryview_set_cdclk(dev, req_cdclk);
6064 else
6065 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006066
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006067 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006068
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006069 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006070}
6071
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072static void valleyview_crtc_enable(struct drm_crtc *crtc)
6073{
6074 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006075 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6077 struct intel_encoder *encoder;
6078 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006079
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006080 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006081 return;
6082
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006083 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306084 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006085
6086 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006087 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006088
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006089 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6090 struct drm_i915_private *dev_priv = dev->dev_private;
6091
6092 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6093 I915_WRITE(CHV_CANVAS(pipe), 0);
6094 }
6095
Daniel Vetter5b18e572014-04-24 23:55:06 +02006096 i9xx_set_pipeconf(intel_crtc);
6097
Jesse Barnes89b667f2013-04-18 14:51:36 -07006098 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006099
Daniel Vettera72e4c92014-09-30 10:56:47 +02006100 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006101
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102 for_each_encoder_on_crtc(dev, crtc, encoder)
6103 if (encoder->pre_pll_enable)
6104 encoder->pre_pll_enable(encoder);
6105
Jani Nikulaa65347b2015-11-27 12:21:46 +02006106 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006107 if (IS_CHERRYVIEW(dev)) {
6108 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006109 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006110 } else {
6111 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006112 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006113 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006114 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006115
6116 for_each_encoder_on_crtc(dev, crtc, encoder)
6117 if (encoder->pre_enable)
6118 encoder->pre_enable(encoder);
6119
Jesse Barnes2dd24552013-04-25 12:55:01 -07006120 i9xx_pfit_enable(intel_crtc);
6121
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006122 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006123
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006124 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006125 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006126
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006127 assert_vblank_disabled(crtc);
6128 drm_crtc_vblank_on(crtc);
6129
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006130 for_each_encoder_on_crtc(dev, crtc, encoder)
6131 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006132}
6133
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006134static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6135{
6136 struct drm_device *dev = crtc->base.dev;
6137 struct drm_i915_private *dev_priv = dev->dev_private;
6138
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006139 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6140 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006141}
6142
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006143static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006144{
6145 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006146 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006148 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006149 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006150
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006151 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006152 return;
6153
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006154 i9xx_set_pll_dividers(intel_crtc);
6155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006156 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306157 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006158
6159 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006160 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006161
Daniel Vetter5b18e572014-04-24 23:55:06 +02006162 i9xx_set_pipeconf(intel_crtc);
6163
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006164 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006165
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006166 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006167 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006168
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006169 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006170 if (encoder->pre_enable)
6171 encoder->pre_enable(encoder);
6172
Daniel Vetterf6736a12013-06-05 13:34:30 +02006173 i9xx_enable_pll(intel_crtc);
6174
Jesse Barnes2dd24552013-04-25 12:55:01 -07006175 i9xx_pfit_enable(intel_crtc);
6176
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006177 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006178
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006179 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006180 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006181
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006182 assert_vblank_disabled(crtc);
6183 drm_crtc_vblank_on(crtc);
6184
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006185 for_each_encoder_on_crtc(dev, crtc, encoder)
6186 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006187}
6188
Daniel Vetter87476d62013-04-11 16:29:06 +02006189static void i9xx_pfit_disable(struct intel_crtc *crtc)
6190{
6191 struct drm_device *dev = crtc->base.dev;
6192 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006193
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006194 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006195 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006196
6197 assert_pipe_disabled(dev_priv, crtc->pipe);
6198
Daniel Vetter328d8e82013-05-08 10:36:31 +02006199 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6200 I915_READ(PFIT_CONTROL));
6201 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006202}
6203
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006204static void i9xx_crtc_disable(struct drm_crtc *crtc)
6205{
6206 struct drm_device *dev = crtc->dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
6208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006209 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006210 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006211
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006212 /*
6213 * On gen2 planes are double buffered but the pipe isn't, so we must
6214 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006215 * We also need to wait on all gmch platforms because of the
6216 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006217 */
Imre Deak564ed192014-06-13 14:54:21 +03006218 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006219
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006220 for_each_encoder_on_crtc(dev, crtc, encoder)
6221 encoder->disable(encoder);
6222
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006223 drm_crtc_vblank_off(crtc);
6224 assert_vblank_disabled(crtc);
6225
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006226 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006227
Daniel Vetter87476d62013-04-11 16:29:06 +02006228 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006229
Jesse Barnes89b667f2013-04-18 14:51:36 -07006230 for_each_encoder_on_crtc(dev, crtc, encoder)
6231 if (encoder->post_disable)
6232 encoder->post_disable(encoder);
6233
Jani Nikulaa65347b2015-11-27 12:21:46 +02006234 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006235 if (IS_CHERRYVIEW(dev))
6236 chv_disable_pll(dev_priv, pipe);
6237 else if (IS_VALLEYVIEW(dev))
6238 vlv_disable_pll(dev_priv, pipe);
6239 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006240 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006241 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006242
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006243 for_each_encoder_on_crtc(dev, crtc, encoder)
6244 if (encoder->post_pll_disable)
6245 encoder->post_pll_disable(encoder);
6246
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006247 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006248 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006249}
6250
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006251static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006252{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006253 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006255 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006256 enum intel_display_power_domain domain;
6257 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006258
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006259 if (!intel_crtc->active)
6260 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006261
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006262 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006263 WARN_ON(intel_crtc->unpin_work);
6264
Ville Syrjälä2622a082016-03-09 19:07:26 +02006265 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006266
6267 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6268 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006269 }
6270
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006271 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006272
6273 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6274 crtc->base.id);
6275
6276 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6277 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006278 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006279 crtc->enabled = false;
6280 crtc->state->connector_mask = 0;
6281 crtc->state->encoder_mask = 0;
6282
6283 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6284 encoder->base.crtc = NULL;
6285
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006286 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006287 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006288 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006289
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006290 domains = intel_crtc->enabled_power_domains;
6291 for_each_power_domain(domain, domains)
6292 intel_display_power_put(dev_priv, domain);
6293 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006294
6295 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6296 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006297}
6298
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006299/*
6300 * turn all crtc's off, but do not adjust state
6301 * This has to be paired with a call to intel_modeset_setup_hw_state.
6302 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006303int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006304{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006305 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006306 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006307 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006308
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006309 state = drm_atomic_helper_suspend(dev);
6310 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006311 if (ret)
6312 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006313 else
6314 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006315 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006316}
6317
Chris Wilsonea5b2132010-08-04 13:50:23 +01006318void intel_encoder_destroy(struct drm_encoder *encoder)
6319{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006320 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006321
Chris Wilsonea5b2132010-08-04 13:50:23 +01006322 drm_encoder_cleanup(encoder);
6323 kfree(intel_encoder);
6324}
6325
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326/* Cross check the actual hw state with our own modeset state tracking (and it's
6327 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006328static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006329{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006330 struct drm_crtc *crtc = connector->base.state->crtc;
6331
6332 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6333 connector->base.base.id,
6334 connector->base.name);
6335
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006337 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006338 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006339
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006340 I915_STATE_WARN(!crtc,
6341 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006342
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006343 if (!crtc)
6344 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006346 I915_STATE_WARN(!crtc->state->active,
6347 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006348
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006349 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006350 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006351
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006352 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006353 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006354
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006355 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006356 "attached encoder crtc differs from connector crtc\n");
6357 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006358 I915_STATE_WARN(crtc && crtc->state->active,
6359 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006360 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6361 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006362 }
6363}
6364
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006365int intel_connector_init(struct intel_connector *connector)
6366{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006367 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006368
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006369 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006370 return -ENOMEM;
6371
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006372 return 0;
6373}
6374
6375struct intel_connector *intel_connector_alloc(void)
6376{
6377 struct intel_connector *connector;
6378
6379 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6380 if (!connector)
6381 return NULL;
6382
6383 if (intel_connector_init(connector) < 0) {
6384 kfree(connector);
6385 return NULL;
6386 }
6387
6388 return connector;
6389}
6390
Daniel Vetterf0947c32012-07-02 13:10:34 +02006391/* Simple connector->get_hw_state implementation for encoders that support only
6392 * one connector and no cloning and hence the encoder state determines the state
6393 * of the connector. */
6394bool intel_connector_get_hw_state(struct intel_connector *connector)
6395{
Daniel Vetter24929352012-07-02 20:28:59 +02006396 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006397 struct intel_encoder *encoder = connector->encoder;
6398
6399 return encoder->get_hw_state(encoder, &pipe);
6400}
6401
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006403{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006404 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6405 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006406
6407 return 0;
6408}
6409
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006411 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006412{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006413 struct drm_atomic_state *state = pipe_config->base.state;
6414 struct intel_crtc *other_crtc;
6415 struct intel_crtc_state *other_crtc_state;
6416
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6418 pipe_name(pipe), pipe_config->fdi_lanes);
6419 if (pipe_config->fdi_lanes > 4) {
6420 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6421 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 }
6424
Paulo Zanonibafb6552013-11-02 21:07:44 -07006425 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006426 if (pipe_config->fdi_lanes > 2) {
6427 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6428 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006431 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 }
6433 }
6434
6435 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437
6438 /* Ivybridge 3 pipe is really complicated */
6439 switch (pipe) {
6440 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006441 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006442 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443 if (pipe_config->fdi_lanes <= 2)
6444 return 0;
6445
6446 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6447 other_crtc_state =
6448 intel_atomic_get_crtc_state(state, other_crtc);
6449 if (IS_ERR(other_crtc_state))
6450 return PTR_ERR(other_crtc_state);
6451
6452 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6454 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006455 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006456 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006457 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006458 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006459 if (pipe_config->fdi_lanes > 2) {
6460 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6461 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006463 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006464
6465 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6466 other_crtc_state =
6467 intel_atomic_get_crtc_state(state, other_crtc);
6468 if (IS_ERR(other_crtc_state))
6469 return PTR_ERR(other_crtc_state);
6470
6471 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006472 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006475 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006476 default:
6477 BUG();
6478 }
6479}
6480
Daniel Vettere29c22c2013-02-21 00:00:16 +01006481#define RETRY 1
6482static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006483 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006484{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006485 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006486 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006487 int lane, link_bw, fdi_dotclock, ret;
6488 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006489
Daniel Vettere29c22c2013-02-21 00:00:16 +01006490retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006491 /* FDI is a binary signal running at ~2.7GHz, encoding
6492 * each output octet as 10 bits. The actual frequency
6493 * is stored as a divider into a 100MHz clock, and the
6494 * mode pixel clock is stored in units of 1KHz.
6495 * Hence the bw of each lane in terms of the mode signal
6496 * is:
6497 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006498 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006499
Damien Lespiau241bfc32013-09-25 16:45:37 +01006500 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006501
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006502 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006503 pipe_config->pipe_bpp);
6504
6505 pipe_config->fdi_lanes = lane;
6506
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006507 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006508 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006509
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006510 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006511 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006512 pipe_config->pipe_bpp -= 2*3;
6513 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6514 pipe_config->pipe_bpp);
6515 needs_recompute = true;
6516 pipe_config->bw_constrained = true;
6517
6518 goto retry;
6519 }
6520
6521 if (needs_recompute)
6522 return RETRY;
6523
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006524 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006525}
6526
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006527static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6528 struct intel_crtc_state *pipe_config)
6529{
6530 if (pipe_config->pipe_bpp > 24)
6531 return false;
6532
6533 /* HSW can handle pixel rate up to cdclk? */
6534 if (IS_HASWELL(dev_priv->dev))
6535 return true;
6536
6537 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006538 * We compare against max which means we must take
6539 * the increased cdclk requirement into account when
6540 * calculating the new cdclk.
6541 *
6542 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006543 */
6544 return ilk_pipe_pixel_rate(pipe_config) <=
6545 dev_priv->max_cdclk_freq * 95 / 100;
6546}
6547
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006548static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006549 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006550{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006551 struct drm_device *dev = crtc->base.dev;
6552 struct drm_i915_private *dev_priv = dev->dev_private;
6553
Jani Nikulad330a952014-01-21 11:24:25 +02006554 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006555 hsw_crtc_supports_ips(crtc) &&
6556 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006557}
6558
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006559static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6560{
6561 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6562
6563 /* GDG double wide on either pipe, otherwise pipe A only */
6564 return INTEL_INFO(dev_priv)->gen < 4 &&
6565 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6566}
6567
Daniel Vettera43f6e02013-06-07 23:10:32 +02006568static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006569 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006570{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006571 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006572 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006573 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006574
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006575 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006576 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006577 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006578
6579 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006580 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006581 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006582 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006583 if (intel_crtc_supports_double_wide(crtc) &&
6584 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006585 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006586 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006587 }
6588
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006589 if (adjusted_mode->crtc_clock > clock_limit) {
6590 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6591 adjusted_mode->crtc_clock, clock_limit,
6592 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006593 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006594 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006595 }
Chris Wilson89749352010-09-12 18:25:19 +01006596
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006597 /*
6598 * Pipe horizontal size must be even in:
6599 * - DVO ganged mode
6600 * - LVDS dual channel mode
6601 * - Double wide pipe
6602 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006603 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006604 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6605 pipe_config->pipe_src_w &= ~1;
6606
Damien Lespiau8693a822013-05-03 18:48:11 +01006607 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6608 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006609 */
6610 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006611 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006612 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006613
Damien Lespiauf5adf942013-06-24 18:29:34 +01006614 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006615 hsw_compute_ips_config(crtc, pipe_config);
6616
Daniel Vetter877d48d2013-04-19 11:24:43 +02006617 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006618 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006619
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006620 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006621}
6622
Ville Syrjälä1652d192015-03-31 14:12:01 +03006623static int skylake_get_display_clock_speed(struct drm_device *dev)
6624{
6625 struct drm_i915_private *dev_priv = to_i915(dev);
6626 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6627 uint32_t cdctl = I915_READ(CDCLK_CTL);
6628 uint32_t linkrate;
6629
Damien Lespiau414355a2015-06-04 18:21:31 +01006630 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006631 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006632
6633 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6634 return 540000;
6635
6636 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006637 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006638
Damien Lespiau71cd8422015-04-30 16:39:17 +01006639 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6640 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006641 /* vco 8640 */
6642 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6643 case CDCLK_FREQ_450_432:
6644 return 432000;
6645 case CDCLK_FREQ_337_308:
6646 return 308570;
6647 case CDCLK_FREQ_675_617:
6648 return 617140;
6649 default:
6650 WARN(1, "Unknown cd freq selection\n");
6651 }
6652 } else {
6653 /* vco 8100 */
6654 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6655 case CDCLK_FREQ_450_432:
6656 return 450000;
6657 case CDCLK_FREQ_337_308:
6658 return 337500;
6659 case CDCLK_FREQ_675_617:
6660 return 675000;
6661 default:
6662 WARN(1, "Unknown cd freq selection\n");
6663 }
6664 }
6665
6666 /* error case, do as if DPLL0 isn't enabled */
6667 return 24000;
6668}
6669
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006670static int broxton_get_display_clock_speed(struct drm_device *dev)
6671{
6672 struct drm_i915_private *dev_priv = to_i915(dev);
6673 uint32_t cdctl = I915_READ(CDCLK_CTL);
6674 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6675 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6676 int cdclk;
6677
6678 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6679 return 19200;
6680
6681 cdclk = 19200 * pll_ratio / 2;
6682
6683 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6684 case BXT_CDCLK_CD2X_DIV_SEL_1:
6685 return cdclk; /* 576MHz or 624MHz */
6686 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6687 return cdclk * 2 / 3; /* 384MHz */
6688 case BXT_CDCLK_CD2X_DIV_SEL_2:
6689 return cdclk / 2; /* 288MHz */
6690 case BXT_CDCLK_CD2X_DIV_SEL_4:
6691 return cdclk / 4; /* 144MHz */
6692 }
6693
6694 /* error case, do as if DE PLL isn't enabled */
6695 return 19200;
6696}
6697
Ville Syrjälä1652d192015-03-31 14:12:01 +03006698static int broadwell_get_display_clock_speed(struct drm_device *dev)
6699{
6700 struct drm_i915_private *dev_priv = dev->dev_private;
6701 uint32_t lcpll = I915_READ(LCPLL_CTL);
6702 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6703
6704 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6705 return 800000;
6706 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6707 return 450000;
6708 else if (freq == LCPLL_CLK_FREQ_450)
6709 return 450000;
6710 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6711 return 540000;
6712 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6713 return 337500;
6714 else
6715 return 675000;
6716}
6717
6718static int haswell_get_display_clock_speed(struct drm_device *dev)
6719{
6720 struct drm_i915_private *dev_priv = dev->dev_private;
6721 uint32_t lcpll = I915_READ(LCPLL_CTL);
6722 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6723
6724 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6725 return 800000;
6726 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6727 return 450000;
6728 else if (freq == LCPLL_CLK_FREQ_450)
6729 return 450000;
6730 else if (IS_HSW_ULT(dev))
6731 return 337500;
6732 else
6733 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006734}
6735
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006736static int valleyview_get_display_clock_speed(struct drm_device *dev)
6737{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006738 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6739 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006740}
6741
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006742static int ilk_get_display_clock_speed(struct drm_device *dev)
6743{
6744 return 450000;
6745}
6746
Jesse Barnese70236a2009-09-21 10:42:27 -07006747static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006748{
Jesse Barnese70236a2009-09-21 10:42:27 -07006749 return 400000;
6750}
Jesse Barnes79e53942008-11-07 14:24:08 -08006751
Jesse Barnese70236a2009-09-21 10:42:27 -07006752static int i915_get_display_clock_speed(struct drm_device *dev)
6753{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006754 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006755}
Jesse Barnes79e53942008-11-07 14:24:08 -08006756
Jesse Barnese70236a2009-09-21 10:42:27 -07006757static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6758{
6759 return 200000;
6760}
Jesse Barnes79e53942008-11-07 14:24:08 -08006761
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006762static int pnv_get_display_clock_speed(struct drm_device *dev)
6763{
6764 u16 gcfgc = 0;
6765
6766 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6767
6768 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6769 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006770 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006771 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006773 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006775 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6776 return 200000;
6777 default:
6778 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6779 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006780 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006781 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006782 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006783 }
6784}
6785
Jesse Barnese70236a2009-09-21 10:42:27 -07006786static int i915gm_get_display_clock_speed(struct drm_device *dev)
6787{
6788 u16 gcfgc = 0;
6789
6790 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6791
6792 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006793 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006794 else {
6795 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6796 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006797 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006798 default:
6799 case GC_DISPLAY_CLOCK_190_200_MHZ:
6800 return 190000;
6801 }
6802 }
6803}
Jesse Barnes79e53942008-11-07 14:24:08 -08006804
Jesse Barnese70236a2009-09-21 10:42:27 -07006805static int i865_get_display_clock_speed(struct drm_device *dev)
6806{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006807 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006808}
6809
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006810static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006811{
6812 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006813
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006814 /*
6815 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6816 * encoding is different :(
6817 * FIXME is this the right way to detect 852GM/852GMV?
6818 */
6819 if (dev->pdev->revision == 0x1)
6820 return 133333;
6821
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006822 pci_bus_read_config_word(dev->pdev->bus,
6823 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6824
Jesse Barnese70236a2009-09-21 10:42:27 -07006825 /* Assume that the hardware is in the high speed state. This
6826 * should be the default.
6827 */
6828 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6829 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006830 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006831 case GC_CLOCK_100_200:
6832 return 200000;
6833 case GC_CLOCK_166_250:
6834 return 250000;
6835 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006836 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006837 case GC_CLOCK_133_266:
6838 case GC_CLOCK_133_266_2:
6839 case GC_CLOCK_166_266:
6840 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006841 }
6842
6843 /* Shouldn't happen */
6844 return 0;
6845}
6846
6847static int i830_get_display_clock_speed(struct drm_device *dev)
6848{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006849 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006850}
6851
Ville Syrjälä34edce22015-05-22 11:22:33 +03006852static unsigned int intel_hpll_vco(struct drm_device *dev)
6853{
6854 struct drm_i915_private *dev_priv = dev->dev_private;
6855 static const unsigned int blb_vco[8] = {
6856 [0] = 3200000,
6857 [1] = 4000000,
6858 [2] = 5333333,
6859 [3] = 4800000,
6860 [4] = 6400000,
6861 };
6862 static const unsigned int pnv_vco[8] = {
6863 [0] = 3200000,
6864 [1] = 4000000,
6865 [2] = 5333333,
6866 [3] = 4800000,
6867 [4] = 2666667,
6868 };
6869 static const unsigned int cl_vco[8] = {
6870 [0] = 3200000,
6871 [1] = 4000000,
6872 [2] = 5333333,
6873 [3] = 6400000,
6874 [4] = 3333333,
6875 [5] = 3566667,
6876 [6] = 4266667,
6877 };
6878 static const unsigned int elk_vco[8] = {
6879 [0] = 3200000,
6880 [1] = 4000000,
6881 [2] = 5333333,
6882 [3] = 4800000,
6883 };
6884 static const unsigned int ctg_vco[8] = {
6885 [0] = 3200000,
6886 [1] = 4000000,
6887 [2] = 5333333,
6888 [3] = 6400000,
6889 [4] = 2666667,
6890 [5] = 4266667,
6891 };
6892 const unsigned int *vco_table;
6893 unsigned int vco;
6894 uint8_t tmp = 0;
6895
6896 /* FIXME other chipsets? */
6897 if (IS_GM45(dev))
6898 vco_table = ctg_vco;
6899 else if (IS_G4X(dev))
6900 vco_table = elk_vco;
6901 else if (IS_CRESTLINE(dev))
6902 vco_table = cl_vco;
6903 else if (IS_PINEVIEW(dev))
6904 vco_table = pnv_vco;
6905 else if (IS_G33(dev))
6906 vco_table = blb_vco;
6907 else
6908 return 0;
6909
6910 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6911
6912 vco = vco_table[tmp & 0x7];
6913 if (vco == 0)
6914 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6915 else
6916 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6917
6918 return vco;
6919}
6920
6921static int gm45_get_display_clock_speed(struct drm_device *dev)
6922{
6923 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6924 uint16_t tmp = 0;
6925
6926 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6927
6928 cdclk_sel = (tmp >> 12) & 0x1;
6929
6930 switch (vco) {
6931 case 2666667:
6932 case 4000000:
6933 case 5333333:
6934 return cdclk_sel ? 333333 : 222222;
6935 case 3200000:
6936 return cdclk_sel ? 320000 : 228571;
6937 default:
6938 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6939 return 222222;
6940 }
6941}
6942
6943static int i965gm_get_display_clock_speed(struct drm_device *dev)
6944{
6945 static const uint8_t div_3200[] = { 16, 10, 8 };
6946 static const uint8_t div_4000[] = { 20, 12, 10 };
6947 static const uint8_t div_5333[] = { 24, 16, 14 };
6948 const uint8_t *div_table;
6949 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6950 uint16_t tmp = 0;
6951
6952 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6953
6954 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6955
6956 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6957 goto fail;
6958
6959 switch (vco) {
6960 case 3200000:
6961 div_table = div_3200;
6962 break;
6963 case 4000000:
6964 div_table = div_4000;
6965 break;
6966 case 5333333:
6967 div_table = div_5333;
6968 break;
6969 default:
6970 goto fail;
6971 }
6972
6973 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6974
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006975fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006976 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6977 return 200000;
6978}
6979
6980static int g33_get_display_clock_speed(struct drm_device *dev)
6981{
6982 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6983 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6984 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6985 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6986 const uint8_t *div_table;
6987 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6988 uint16_t tmp = 0;
6989
6990 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6991
6992 cdclk_sel = (tmp >> 4) & 0x7;
6993
6994 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6995 goto fail;
6996
6997 switch (vco) {
6998 case 3200000:
6999 div_table = div_3200;
7000 break;
7001 case 4000000:
7002 div_table = div_4000;
7003 break;
7004 case 4800000:
7005 div_table = div_4800;
7006 break;
7007 case 5333333:
7008 div_table = div_5333;
7009 break;
7010 default:
7011 goto fail;
7012 }
7013
7014 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7015
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007016fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007017 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7018 return 190476;
7019}
7020
Zhenyu Wang2c072452009-06-05 15:38:42 +08007021static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007022intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007023{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007024 while (*num > DATA_LINK_M_N_MASK ||
7025 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007026 *num >>= 1;
7027 *den >>= 1;
7028 }
7029}
7030
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007031static void compute_m_n(unsigned int m, unsigned int n,
7032 uint32_t *ret_m, uint32_t *ret_n)
7033{
7034 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7035 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7036 intel_reduce_m_n_ratio(ret_m, ret_n);
7037}
7038
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007039void
7040intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7041 int pixel_clock, int link_clock,
7042 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007043{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007044 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007045
7046 compute_m_n(bits_per_pixel * pixel_clock,
7047 link_clock * nlanes * 8,
7048 &m_n->gmch_m, &m_n->gmch_n);
7049
7050 compute_m_n(pixel_clock, link_clock,
7051 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007052}
7053
Chris Wilsona7615032011-01-12 17:04:08 +00007054static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7055{
Jani Nikulad330a952014-01-21 11:24:25 +02007056 if (i915.panel_use_ssc >= 0)
7057 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007058 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007059 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007060}
7061
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007062static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7063 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007064{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007065 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007066 struct drm_i915_private *dev_priv = dev->dev_private;
7067 int refclk;
7068
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007069 WARN_ON(!crtc_state->base.state);
7070
Wayne Boyer666a4532015-12-09 12:29:35 -08007071 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007072 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007073 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007074 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007075 refclk = dev_priv->vbt.lvds_ssc_freq;
7076 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007077 } else if (!IS_GEN2(dev)) {
7078 refclk = 96000;
7079 } else {
7080 refclk = 48000;
7081 }
7082
7083 return refclk;
7084}
7085
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007086static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007087{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007088 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007089}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007090
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007091static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7092{
7093 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007094}
7095
Daniel Vetterf47709a2013-03-28 10:42:02 +01007096static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007097 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007098 intel_clock_t *reduced_clock)
7099{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007100 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007101 u32 fp, fp2 = 0;
7102
7103 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007104 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007105 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007106 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007107 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007108 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007109 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007110 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 }
7112
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007113 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007114
Daniel Vetterf47709a2013-03-28 10:42:02 +01007115 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007116 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007117 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007118 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007119 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007120 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007121 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007122 }
7123}
7124
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007125static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7126 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007127{
7128 u32 reg_val;
7129
7130 /*
7131 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7132 * and set it to a reasonable value instead.
7133 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007134 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007135 reg_val &= 0xffffff00;
7136 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007137 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007138
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007139 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007140 reg_val &= 0x8cffffff;
7141 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007142 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007143
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007144 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007145 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007146 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007148 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007149 reg_val &= 0x00ffffff;
7150 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007151 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007152}
7153
Daniel Vetterb5518422013-05-03 11:49:48 +02007154static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7155 struct intel_link_m_n *m_n)
7156{
7157 struct drm_device *dev = crtc->base.dev;
7158 struct drm_i915_private *dev_priv = dev->dev_private;
7159 int pipe = crtc->pipe;
7160
Daniel Vettere3b95f12013-05-03 11:49:49 +02007161 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7162 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7163 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7164 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007165}
7166
7167static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007168 struct intel_link_m_n *m_n,
7169 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007170{
7171 struct drm_device *dev = crtc->base.dev;
7172 struct drm_i915_private *dev_priv = dev->dev_private;
7173 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007174 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007175
7176 if (INTEL_INFO(dev)->gen >= 5) {
7177 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7178 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7179 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7180 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007181 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7182 * for gen < 8) and if DRRS is supported (to make sure the
7183 * registers are not unnecessarily accessed).
7184 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307185 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007186 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007187 I915_WRITE(PIPE_DATA_M2(transcoder),
7188 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7189 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7190 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7191 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7192 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007193 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007194 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7195 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7196 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7197 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007198 }
7199}
7200
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307201void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007202{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307203 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7204
7205 if (m_n == M1_N1) {
7206 dp_m_n = &crtc->config->dp_m_n;
7207 dp_m2_n2 = &crtc->config->dp_m2_n2;
7208 } else if (m_n == M2_N2) {
7209
7210 /*
7211 * M2_N2 registers are not supported. Hence m2_n2 divider value
7212 * needs to be programmed into M1_N1.
7213 */
7214 dp_m_n = &crtc->config->dp_m2_n2;
7215 } else {
7216 DRM_ERROR("Unsupported divider value\n");
7217 return;
7218 }
7219
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007220 if (crtc->config->has_pch_encoder)
7221 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007222 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307223 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007224}
7225
Daniel Vetter251ac862015-06-18 10:30:24 +02007226static void vlv_compute_dpll(struct intel_crtc *crtc,
7227 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007228{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007229 u32 dpll, dpll_md;
7230
7231 /*
7232 * Enable DPIO clock input. We should never disable the reference
7233 * clock for pipe B, since VGA hotplug / manual detection depends
7234 * on it.
7235 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007236 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7237 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007238 /* We should never disable this, set it here for state tracking */
7239 if (crtc->pipe == PIPE_B)
7240 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7241 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007242 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007243
Ville Syrjäläd288f652014-10-28 13:20:22 +02007244 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007245 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007246 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007247}
7248
Ville Syrjäläd288f652014-10-28 13:20:22 +02007249static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007250 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007251{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007252 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007253 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007254 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007255 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007256 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007258
Ville Syrjäläa5805162015-05-26 20:42:30 +03007259 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007260
Ville Syrjäläd288f652014-10-28 13:20:22 +02007261 bestn = pipe_config->dpll.n;
7262 bestm1 = pipe_config->dpll.m1;
7263 bestm2 = pipe_config->dpll.m2;
7264 bestp1 = pipe_config->dpll.p1;
7265 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007266
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 /* See eDP HDMI DPIO driver vbios notes doc */
7268
7269 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007271 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272
7273 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275
7276 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007277 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007278 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007279 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280
7281 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007282 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283
7284 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007285 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7286 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7287 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007288 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007289
7290 /*
7291 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7292 * but we don't support that).
7293 * Note: don't use the DAC post divider as it seems unstable.
7294 */
7295 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007296 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007297
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007298 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007299 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007300
Jesse Barnes89b667f2013-04-18 14:51:36 -07007301 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007302 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007303 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7304 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007306 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007307 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007308 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007309 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007310
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007311 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007312 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007313 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007314 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007315 0x0df40000);
7316 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007317 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007318 0x0df70000);
7319 } else { /* HDMI or VGA */
7320 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007321 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007322 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007323 0x0df70000);
7324 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326 0x0df40000);
7327 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007328
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007329 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007330 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007331 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7332 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007333 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007334 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007337 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007338}
7339
Daniel Vetter251ac862015-06-18 10:30:24 +02007340static void chv_compute_dpll(struct intel_crtc *crtc,
7341 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007342{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007343 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7344 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007345 DPLL_VCO_ENABLE;
7346 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007347 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007348
Ville Syrjäläd288f652014-10-28 13:20:22 +02007349 pipe_config->dpll_hw_state.dpll_md =
7350 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007351}
7352
Ville Syrjäläd288f652014-10-28 13:20:22 +02007353static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007354 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007355{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007356 struct drm_device *dev = crtc->base.dev;
7357 struct drm_i915_private *dev_priv = dev->dev_private;
7358 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007359 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007360 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307361 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007362 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307363 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307364 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007365
Ville Syrjäläd288f652014-10-28 13:20:22 +02007366 bestn = pipe_config->dpll.n;
7367 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7368 bestm1 = pipe_config->dpll.m1;
7369 bestm2 = pipe_config->dpll.m2 >> 22;
7370 bestp1 = pipe_config->dpll.p1;
7371 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307372 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307373 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307374 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007375
7376 /*
7377 * Enable Refclk and SSC
7378 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007379 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007380 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007381
Ville Syrjäläa5805162015-05-26 20:42:30 +03007382 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007383
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007384 /* p1 and p2 divider */
7385 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7386 5 << DPIO_CHV_S1_DIV_SHIFT |
7387 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7388 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7389 1 << DPIO_CHV_K_DIV_SHIFT);
7390
7391 /* Feedback post-divider - m2 */
7392 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7393
7394 /* Feedback refclk divider - n and m1 */
7395 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7396 DPIO_CHV_M1_DIV_BY_2 |
7397 1 << DPIO_CHV_N_DIV_SHIFT);
7398
7399 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007400 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007401
7402 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307403 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7404 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7405 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7406 if (bestm2_frac)
7407 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7408 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007409
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307410 /* Program digital lock detect threshold */
7411 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7412 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7413 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7414 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7415 if (!bestm2_frac)
7416 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7417 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7418
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007419 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307420 if (vco == 5400000) {
7421 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7422 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7423 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7424 tribuf_calcntr = 0x9;
7425 } else if (vco <= 6200000) {
7426 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7427 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7428 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7429 tribuf_calcntr = 0x9;
7430 } else if (vco <= 6480000) {
7431 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7432 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7433 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7434 tribuf_calcntr = 0x8;
7435 } else {
7436 /* Not supported. Apply the same limits as in the max case */
7437 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7438 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7439 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7440 tribuf_calcntr = 0;
7441 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007442 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7443
Ville Syrjälä968040b2015-03-11 22:52:08 +02007444 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307445 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7446 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7447 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7448
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007449 /* AFC Recal */
7450 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7451 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7452 DPIO_AFC_RECAL);
7453
Ville Syrjäläa5805162015-05-26 20:42:30 +03007454 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007455}
7456
Ville Syrjäläd288f652014-10-28 13:20:22 +02007457/**
7458 * vlv_force_pll_on - forcibly enable just the PLL
7459 * @dev_priv: i915 private structure
7460 * @pipe: pipe PLL to enable
7461 * @dpll: PLL configuration
7462 *
7463 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7464 * in cases where we need the PLL enabled even when @pipe is not going to
7465 * be enabled.
7466 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007467int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7468 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007469{
7470 struct intel_crtc *crtc =
7471 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007472 struct intel_crtc_state *pipe_config;
7473
7474 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7475 if (!pipe_config)
7476 return -ENOMEM;
7477
7478 pipe_config->base.crtc = &crtc->base;
7479 pipe_config->pixel_multiplier = 1;
7480 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007481
7482 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007483 chv_compute_dpll(crtc, pipe_config);
7484 chv_prepare_pll(crtc, pipe_config);
7485 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007486 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007487 vlv_compute_dpll(crtc, pipe_config);
7488 vlv_prepare_pll(crtc, pipe_config);
7489 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007490 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007491
7492 kfree(pipe_config);
7493
7494 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007495}
7496
7497/**
7498 * vlv_force_pll_off - forcibly disable just the PLL
7499 * @dev_priv: i915 private structure
7500 * @pipe: pipe PLL to disable
7501 *
7502 * Disable the PLL for @pipe. To be used in cases where we need
7503 * the PLL enabled even when @pipe is not going to be enabled.
7504 */
7505void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7506{
7507 if (IS_CHERRYVIEW(dev))
7508 chv_disable_pll(to_i915(dev), pipe);
7509 else
7510 vlv_disable_pll(to_i915(dev), pipe);
7511}
7512
Daniel Vetter251ac862015-06-18 10:30:24 +02007513static void i9xx_compute_dpll(struct intel_crtc *crtc,
7514 struct intel_crtc_state *crtc_state,
7515 intel_clock_t *reduced_clock,
7516 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007518 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007519 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007520 u32 dpll;
7521 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007522 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007524 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007526 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7527 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528
7529 dpll = DPLL_VGA_MODE_DIS;
7530
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007531 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 dpll |= DPLLB_MODE_LVDS;
7533 else
7534 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007535
Daniel Vetteref1b4602013-06-01 17:17:04 +02007536 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007537 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007538 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007540
7541 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007542 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007543
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007545 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007546
7547 /* compute bitmask from p1 value */
7548 if (IS_PINEVIEW(dev))
7549 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7550 else {
7551 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7552 if (IS_G4X(dev) && reduced_clock)
7553 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7554 }
7555 switch (clock->p2) {
7556 case 5:
7557 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7558 break;
7559 case 7:
7560 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7561 break;
7562 case 10:
7563 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7564 break;
7565 case 14:
7566 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7567 break;
7568 }
7569 if (INTEL_INFO(dev)->gen >= 4)
7570 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7571
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007572 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007574 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007575 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7576 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7577 else
7578 dpll |= PLL_REF_INPUT_DREFCLK;
7579
7580 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007581 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007582
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007583 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007584 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007585 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007586 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587 }
7588}
7589
Daniel Vetter251ac862015-06-18 10:30:24 +02007590static void i8xx_compute_dpll(struct intel_crtc *crtc,
7591 struct intel_crtc_state *crtc_state,
7592 intel_clock_t *reduced_clock,
7593 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007595 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007596 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007598 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007599
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007600 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307601
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007602 dpll = DPLL_VGA_MODE_DIS;
7603
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007604 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007605 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7606 } else {
7607 if (clock->p1 == 2)
7608 dpll |= PLL_P1_DIVIDE_BY_TWO;
7609 else
7610 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7611 if (clock->p2 == 4)
7612 dpll |= PLL_P2_DIVIDE_BY_4;
7613 }
7614
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007615 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007616 dpll |= DPLL_DVO_2X_MODE;
7617
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007618 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007619 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7620 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7621 else
7622 dpll |= PLL_REF_INPUT_DREFCLK;
7623
7624 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007625 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007626}
7627
Daniel Vetter8a654f32013-06-01 17:16:22 +02007628static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007629{
7630 struct drm_device *dev = intel_crtc->base.dev;
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007633 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007634 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007635 uint32_t crtc_vtotal, crtc_vblank_end;
7636 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007637
7638 /* We need to be careful not to changed the adjusted mode, for otherwise
7639 * the hw state checker will get angry at the mismatch. */
7640 crtc_vtotal = adjusted_mode->crtc_vtotal;
7641 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007642
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007643 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007644 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007645 crtc_vtotal -= 1;
7646 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007647
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007648 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007649 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7650 else
7651 vsyncshift = adjusted_mode->crtc_hsync_start -
7652 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007653 if (vsyncshift < 0)
7654 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007655 }
7656
7657 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007658 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007659
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007660 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007661 (adjusted_mode->crtc_hdisplay - 1) |
7662 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007663 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007664 (adjusted_mode->crtc_hblank_start - 1) |
7665 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007666 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007667 (adjusted_mode->crtc_hsync_start - 1) |
7668 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7669
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007670 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007671 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007672 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007673 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007674 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007675 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007676 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007677 (adjusted_mode->crtc_vsync_start - 1) |
7678 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7679
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007680 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7681 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7682 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7683 * bits. */
7684 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7685 (pipe == PIPE_B || pipe == PIPE_C))
7686 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7687
Jani Nikulabc58be62016-03-18 17:05:39 +02007688}
7689
7690static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7691{
7692 struct drm_device *dev = intel_crtc->base.dev;
7693 struct drm_i915_private *dev_priv = dev->dev_private;
7694 enum pipe pipe = intel_crtc->pipe;
7695
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007696 /* pipesrc controls the size that is scaled from, which should
7697 * always be the user's requested size.
7698 */
7699 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007700 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7701 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007702}
7703
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007704static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007705 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007706{
7707 struct drm_device *dev = crtc->base.dev;
7708 struct drm_i915_private *dev_priv = dev->dev_private;
7709 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7710 uint32_t tmp;
7711
7712 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007713 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7714 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007715 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007716 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7717 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007718 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007719 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7720 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007721
7722 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7724 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007725 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007726 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7727 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007728 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007729 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7730 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007731
7732 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007733 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7734 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7735 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007736 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007737}
7738
7739static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7740 struct intel_crtc_state *pipe_config)
7741{
7742 struct drm_device *dev = crtc->base.dev;
7743 struct drm_i915_private *dev_priv = dev->dev_private;
7744 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007745
7746 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007747 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7748 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7749
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007750 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7751 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007752}
7753
Daniel Vetterf6a83282014-02-11 15:28:57 -08007754void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007755 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007756{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007757 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7758 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7759 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7760 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007761
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007762 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7763 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7764 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7765 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007766
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007767 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007768 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007769
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007770 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7771 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007772
7773 mode->hsync = drm_mode_hsync(mode);
7774 mode->vrefresh = drm_mode_vrefresh(mode);
7775 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007776}
7777
Daniel Vetter84b046f2013-02-19 18:48:54 +01007778static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7779{
7780 struct drm_device *dev = intel_crtc->base.dev;
7781 struct drm_i915_private *dev_priv = dev->dev_private;
7782 uint32_t pipeconf;
7783
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007784 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007785
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007786 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7787 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7788 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007789
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007790 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007791 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007792
Daniel Vetterff9ce462013-04-24 14:57:17 +02007793 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007794 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007795 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007796 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007797 pipeconf |= PIPECONF_DITHER_EN |
7798 PIPECONF_DITHER_TYPE_SP;
7799
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007800 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007801 case 18:
7802 pipeconf |= PIPECONF_6BPC;
7803 break;
7804 case 24:
7805 pipeconf |= PIPECONF_8BPC;
7806 break;
7807 case 30:
7808 pipeconf |= PIPECONF_10BPC;
7809 break;
7810 default:
7811 /* Case prevented by intel_choose_pipe_bpp_dither. */
7812 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007813 }
7814 }
7815
7816 if (HAS_PIPE_CXSR(dev)) {
7817 if (intel_crtc->lowfreq_avail) {
7818 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7819 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7820 } else {
7821 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007822 }
7823 }
7824
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007825 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007826 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007827 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007828 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7829 else
7830 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7831 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007832 pipeconf |= PIPECONF_PROGRESSIVE;
7833
Wayne Boyer666a4532015-12-09 12:29:35 -08007834 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7835 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007836 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007837
Daniel Vetter84b046f2013-02-19 18:48:54 +01007838 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7839 POSTING_READ(PIPECONF(intel_crtc->pipe));
7840}
7841
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007842static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7843 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007844{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007845 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007846 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007847 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007848 intel_clock_t clock;
7849 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007850 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007851 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007852 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007853 struct drm_connector_state *connector_state;
7854 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007855
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007856 memset(&crtc_state->dpll_hw_state, 0,
7857 sizeof(crtc_state->dpll_hw_state));
7858
Jani Nikulaa65347b2015-11-27 12:21:46 +02007859 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007860 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861
Jani Nikulaa65347b2015-11-27 12:21:46 +02007862 for_each_connector_in_state(state, connector, connector_state, i) {
7863 if (connector_state->crtc == &crtc->base)
7864 num_connectors++;
7865 }
7866
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007867 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007868 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007869
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007870 /*
7871 * Returns a set of divisors for the desired target clock with
7872 * the given refclk, or FALSE. The returned values represent
7873 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7874 * 2) / p1 / p2.
7875 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007876 limit = intel_limit(crtc_state, refclk);
7877 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007878 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007879 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007880 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007881 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7882 return -EINVAL;
7883 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007884
Jani Nikulaf2335332013-09-13 11:03:09 +03007885 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007886 crtc_state->dpll.n = clock.n;
7887 crtc_state->dpll.m1 = clock.m1;
7888 crtc_state->dpll.m2 = clock.m2;
7889 crtc_state->dpll.p1 = clock.p1;
7890 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007891 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007892
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007893 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007894 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007895 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007896 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007897 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007898 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007899 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007900 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007901 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007902 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007903 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007904
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007905 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007906}
7907
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007908static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007909 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007910{
7911 struct drm_device *dev = crtc->base.dev;
7912 struct drm_i915_private *dev_priv = dev->dev_private;
7913 uint32_t tmp;
7914
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007915 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7916 return;
7917
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007918 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007919 if (!(tmp & PFIT_ENABLE))
7920 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007921
Daniel Vetter06922822013-07-11 13:35:40 +02007922 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007923 if (INTEL_INFO(dev)->gen < 4) {
7924 if (crtc->pipe != PIPE_B)
7925 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007926 } else {
7927 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7928 return;
7929 }
7930
Daniel Vetter06922822013-07-11 13:35:40 +02007931 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007932 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7933 if (INTEL_INFO(dev)->gen < 5)
7934 pipe_config->gmch_pfit.lvds_border_bits =
7935 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7936}
7937
Jesse Barnesacbec812013-09-20 11:29:32 -07007938static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007939 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007940{
7941 struct drm_device *dev = crtc->base.dev;
7942 struct drm_i915_private *dev_priv = dev->dev_private;
7943 int pipe = pipe_config->cpu_transcoder;
7944 intel_clock_t clock;
7945 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007946 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007947
Shobhit Kumarf573de52014-07-30 20:32:37 +05307948 /* In case of MIPI DPLL will not even be used */
7949 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7950 return;
7951
Ville Syrjäläa5805162015-05-26 20:42:30 +03007952 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007953 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007954 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007955
7956 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7957 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7958 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7959 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7960 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7961
Imre Deakdccbea32015-06-22 23:35:51 +03007962 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007963}
7964
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007965static void
7966i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7967 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007968{
7969 struct drm_device *dev = crtc->base.dev;
7970 struct drm_i915_private *dev_priv = dev->dev_private;
7971 u32 val, base, offset;
7972 int pipe = crtc->pipe, plane = crtc->plane;
7973 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007974 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007975 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007976 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007977
Damien Lespiau42a7b082015-02-05 19:35:13 +00007978 val = I915_READ(DSPCNTR(plane));
7979 if (!(val & DISPLAY_PLANE_ENABLE))
7980 return;
7981
Damien Lespiaud9806c92015-01-21 14:07:19 +00007982 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007983 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007984 DRM_DEBUG_KMS("failed to alloc fb\n");
7985 return;
7986 }
7987
Damien Lespiau1b842c82015-01-21 13:50:54 +00007988 fb = &intel_fb->base;
7989
Daniel Vetter18c52472015-02-10 17:16:09 +00007990 if (INTEL_INFO(dev)->gen >= 4) {
7991 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007992 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007993 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7994 }
7995 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007996
7997 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007998 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007999 fb->pixel_format = fourcc;
8000 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008001
8002 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008003 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008004 offset = I915_READ(DSPTILEOFF(plane));
8005 else
8006 offset = I915_READ(DSPLINOFF(plane));
8007 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8008 } else {
8009 base = I915_READ(DSPADDR(plane));
8010 }
8011 plane_config->base = base;
8012
8013 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008014 fb->width = ((val >> 16) & 0xfff) + 1;
8015 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008016
8017 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008018 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008019
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008020 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008021 fb->pixel_format,
8022 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008023
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008024 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008025
Damien Lespiau2844a922015-01-20 12:51:48 +00008026 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8027 pipe_name(pipe), plane, fb->width, fb->height,
8028 fb->bits_per_pixel, base, fb->pitches[0],
8029 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008030
Damien Lespiau2d140302015-02-05 17:22:18 +00008031 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008032}
8033
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008034static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008035 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008036{
8037 struct drm_device *dev = crtc->base.dev;
8038 struct drm_i915_private *dev_priv = dev->dev_private;
8039 int pipe = pipe_config->cpu_transcoder;
8040 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8041 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008042 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008043 int refclk = 100000;
8044
Ville Syrjäläa5805162015-05-26 20:42:30 +03008045 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008046 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8047 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8048 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8049 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008050 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008051 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008052
8053 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008054 clock.m2 = (pll_dw0 & 0xff) << 22;
8055 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8056 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008057 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8058 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8059 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8060
Imre Deakdccbea32015-06-22 23:35:51 +03008061 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008062}
8063
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008064static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008065 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008066{
8067 struct drm_device *dev = crtc->base.dev;
8068 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008069 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008070 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008071 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008072
Imre Deak17290502016-02-12 18:55:11 +02008073 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8074 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008075 return false;
8076
Daniel Vettere143a212013-07-04 12:01:15 +02008077 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008078 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008079
Imre Deak17290502016-02-12 18:55:11 +02008080 ret = false;
8081
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008082 tmp = I915_READ(PIPECONF(crtc->pipe));
8083 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008084 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008085
Wayne Boyer666a4532015-12-09 12:29:35 -08008086 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008087 switch (tmp & PIPECONF_BPC_MASK) {
8088 case PIPECONF_6BPC:
8089 pipe_config->pipe_bpp = 18;
8090 break;
8091 case PIPECONF_8BPC:
8092 pipe_config->pipe_bpp = 24;
8093 break;
8094 case PIPECONF_10BPC:
8095 pipe_config->pipe_bpp = 30;
8096 break;
8097 default:
8098 break;
8099 }
8100 }
8101
Wayne Boyer666a4532015-12-09 12:29:35 -08008102 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8103 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008104 pipe_config->limited_color_range = true;
8105
Ville Syrjälä282740f2013-09-04 18:30:03 +03008106 if (INTEL_INFO(dev)->gen < 4)
8107 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8108
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008109 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008110 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008111
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008112 i9xx_get_pfit_config(crtc, pipe_config);
8113
Daniel Vetter6c49f242013-06-06 12:45:25 +02008114 if (INTEL_INFO(dev)->gen >= 4) {
8115 tmp = I915_READ(DPLL_MD(crtc->pipe));
8116 pipe_config->pixel_multiplier =
8117 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8118 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008119 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008120 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8121 tmp = I915_READ(DPLL(crtc->pipe));
8122 pipe_config->pixel_multiplier =
8123 ((tmp & SDVO_MULTIPLIER_MASK)
8124 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8125 } else {
8126 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8127 * port and will be fixed up in the encoder->get_config
8128 * function. */
8129 pipe_config->pixel_multiplier = 1;
8130 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008131 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008132 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008133 /*
8134 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8135 * on 830. Filter it out here so that we don't
8136 * report errors due to that.
8137 */
8138 if (IS_I830(dev))
8139 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8140
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008141 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8142 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008143 } else {
8144 /* Mask out read-only status bits. */
8145 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8146 DPLL_PORTC_READY_MASK |
8147 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008148 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008149
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008150 if (IS_CHERRYVIEW(dev))
8151 chv_crtc_clock_get(crtc, pipe_config);
8152 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008153 vlv_crtc_clock_get(crtc, pipe_config);
8154 else
8155 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008156
Ville Syrjälä0f646142015-08-26 19:39:18 +03008157 /*
8158 * Normally the dotclock is filled in by the encoder .get_config()
8159 * but in case the pipe is enabled w/o any ports we need a sane
8160 * default.
8161 */
8162 pipe_config->base.adjusted_mode.crtc_clock =
8163 pipe_config->port_clock / pipe_config->pixel_multiplier;
8164
Imre Deak17290502016-02-12 18:55:11 +02008165 ret = true;
8166
8167out:
8168 intel_display_power_put(dev_priv, power_domain);
8169
8170 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008171}
8172
Paulo Zanonidde86e22012-12-01 12:04:25 -02008173static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008174{
8175 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008176 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008177 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008178 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008179 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008180 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008181 bool has_ck505 = false;
8182 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008183
8184 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008185 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008186 switch (encoder->type) {
8187 case INTEL_OUTPUT_LVDS:
8188 has_panel = true;
8189 has_lvds = true;
8190 break;
8191 case INTEL_OUTPUT_EDP:
8192 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008193 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008194 has_cpu_edp = true;
8195 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008196 default:
8197 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008198 }
8199 }
8200
Keith Packard99eb6a02011-09-26 14:29:12 -07008201 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008202 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008203 can_ssc = has_ck505;
8204 } else {
8205 has_ck505 = false;
8206 can_ssc = true;
8207 }
8208
Imre Deak2de69052013-05-08 13:14:04 +03008209 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8210 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008211
8212 /* Ironlake: try to setup display ref clock before DPLL
8213 * enabling. This is only under driver's control after
8214 * PCH B stepping, previous chipset stepping should be
8215 * ignoring this setting.
8216 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008217 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008218
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008219 /* As we must carefully and slowly disable/enable each source in turn,
8220 * compute the final state we want first and check if we need to
8221 * make any changes at all.
8222 */
8223 final = val;
8224 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008225 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008226 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008227 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008228 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8229
8230 final &= ~DREF_SSC_SOURCE_MASK;
8231 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8232 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008233
Keith Packard199e5d72011-09-22 12:01:57 -07008234 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008235 final |= DREF_SSC_SOURCE_ENABLE;
8236
8237 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8238 final |= DREF_SSC1_ENABLE;
8239
8240 if (has_cpu_edp) {
8241 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8242 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8243 else
8244 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8245 } else
8246 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8247 } else {
8248 final |= DREF_SSC_SOURCE_DISABLE;
8249 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8250 }
8251
8252 if (final == val)
8253 return;
8254
8255 /* Always enable nonspread source */
8256 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8257
8258 if (has_ck505)
8259 val |= DREF_NONSPREAD_CK505_ENABLE;
8260 else
8261 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8262
8263 if (has_panel) {
8264 val &= ~DREF_SSC_SOURCE_MASK;
8265 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008266
Keith Packard199e5d72011-09-22 12:01:57 -07008267 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008268 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008269 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008270 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008271 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008272 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008273
8274 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008276 POSTING_READ(PCH_DREF_CONTROL);
8277 udelay(200);
8278
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008279 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008280
8281 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008282 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008283 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008284 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008285 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008286 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008288 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008289 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008290
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008291 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008292 POSTING_READ(PCH_DREF_CONTROL);
8293 udelay(200);
8294 } else {
8295 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8296
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008297 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008298
8299 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008300 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008301
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008303 POSTING_READ(PCH_DREF_CONTROL);
8304 udelay(200);
8305
8306 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008307 val &= ~DREF_SSC_SOURCE_MASK;
8308 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008309
8310 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008311 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008312
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008313 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008314 POSTING_READ(PCH_DREF_CONTROL);
8315 udelay(200);
8316 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008317
8318 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008319}
8320
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008321static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008322{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008323 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008324
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008325 tmp = I915_READ(SOUTH_CHICKEN2);
8326 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8327 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008328
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008329 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8330 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8331 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008332
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008333 tmp = I915_READ(SOUTH_CHICKEN2);
8334 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8335 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008337 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8338 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8339 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008340}
8341
8342/* WaMPhyProgramming:hsw */
8343static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8344{
8345 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008346
8347 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8348 tmp &= ~(0xFF << 24);
8349 tmp |= (0x12 << 24);
8350 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8351
Paulo Zanonidde86e22012-12-01 12:04:25 -02008352 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8353 tmp |= (1 << 11);
8354 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8355
8356 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8357 tmp |= (1 << 11);
8358 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8359
Paulo Zanonidde86e22012-12-01 12:04:25 -02008360 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8361 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8362 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8363
8364 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8365 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8366 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8367
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008368 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8369 tmp &= ~(7 << 13);
8370 tmp |= (5 << 13);
8371 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008372
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008373 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8374 tmp &= ~(7 << 13);
8375 tmp |= (5 << 13);
8376 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008377
8378 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8379 tmp &= ~0xFF;
8380 tmp |= 0x1C;
8381 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8382
8383 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8384 tmp &= ~0xFF;
8385 tmp |= 0x1C;
8386 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8387
8388 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8389 tmp &= ~(0xFF << 16);
8390 tmp |= (0x1C << 16);
8391 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8392
8393 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8394 tmp &= ~(0xFF << 16);
8395 tmp |= (0x1C << 16);
8396 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8397
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008398 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8399 tmp |= (1 << 27);
8400 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008401
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008402 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8403 tmp |= (1 << 27);
8404 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008405
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008406 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8407 tmp &= ~(0xF << 28);
8408 tmp |= (4 << 28);
8409 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008410
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008411 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8412 tmp &= ~(0xF << 28);
8413 tmp |= (4 << 28);
8414 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008415}
8416
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008417/* Implements 3 different sequences from BSpec chapter "Display iCLK
8418 * Programming" based on the parameters passed:
8419 * - Sequence to enable CLKOUT_DP
8420 * - Sequence to enable CLKOUT_DP without spread
8421 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8422 */
8423static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8424 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008425{
8426 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008427 uint32_t reg, tmp;
8428
8429 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8430 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008431 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008432 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008433
Ville Syrjäläa5805162015-05-26 20:42:30 +03008434 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008435
8436 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8437 tmp &= ~SBI_SSCCTL_DISABLE;
8438 tmp |= SBI_SSCCTL_PATHALT;
8439 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8440
8441 udelay(24);
8442
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008443 if (with_spread) {
8444 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8445 tmp &= ~SBI_SSCCTL_PATHALT;
8446 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008447
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008448 if (with_fdi) {
8449 lpt_reset_fdi_mphy(dev_priv);
8450 lpt_program_fdi_mphy(dev_priv);
8451 }
8452 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008453
Ville Syrjäläc2699522015-08-27 23:55:59 +03008454 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008455 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8456 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8457 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008458
Ville Syrjäläa5805162015-05-26 20:42:30 +03008459 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008460}
8461
Paulo Zanoni47701c32013-07-23 11:19:25 -03008462/* Sequence to disable CLKOUT_DP */
8463static void lpt_disable_clkout_dp(struct drm_device *dev)
8464{
8465 struct drm_i915_private *dev_priv = dev->dev_private;
8466 uint32_t reg, tmp;
8467
Ville Syrjäläa5805162015-05-26 20:42:30 +03008468 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008469
Ville Syrjäläc2699522015-08-27 23:55:59 +03008470 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008471 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8472 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8473 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8474
8475 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8476 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8477 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8478 tmp |= SBI_SSCCTL_PATHALT;
8479 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8480 udelay(32);
8481 }
8482 tmp |= SBI_SSCCTL_DISABLE;
8483 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8484 }
8485
Ville Syrjäläa5805162015-05-26 20:42:30 +03008486 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008487}
8488
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008489#define BEND_IDX(steps) ((50 + (steps)) / 5)
8490
8491static const uint16_t sscdivintphase[] = {
8492 [BEND_IDX( 50)] = 0x3B23,
8493 [BEND_IDX( 45)] = 0x3B23,
8494 [BEND_IDX( 40)] = 0x3C23,
8495 [BEND_IDX( 35)] = 0x3C23,
8496 [BEND_IDX( 30)] = 0x3D23,
8497 [BEND_IDX( 25)] = 0x3D23,
8498 [BEND_IDX( 20)] = 0x3E23,
8499 [BEND_IDX( 15)] = 0x3E23,
8500 [BEND_IDX( 10)] = 0x3F23,
8501 [BEND_IDX( 5)] = 0x3F23,
8502 [BEND_IDX( 0)] = 0x0025,
8503 [BEND_IDX( -5)] = 0x0025,
8504 [BEND_IDX(-10)] = 0x0125,
8505 [BEND_IDX(-15)] = 0x0125,
8506 [BEND_IDX(-20)] = 0x0225,
8507 [BEND_IDX(-25)] = 0x0225,
8508 [BEND_IDX(-30)] = 0x0325,
8509 [BEND_IDX(-35)] = 0x0325,
8510 [BEND_IDX(-40)] = 0x0425,
8511 [BEND_IDX(-45)] = 0x0425,
8512 [BEND_IDX(-50)] = 0x0525,
8513};
8514
8515/*
8516 * Bend CLKOUT_DP
8517 * steps -50 to 50 inclusive, in steps of 5
8518 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8519 * change in clock period = -(steps / 10) * 5.787 ps
8520 */
8521static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8522{
8523 uint32_t tmp;
8524 int idx = BEND_IDX(steps);
8525
8526 if (WARN_ON(steps % 5 != 0))
8527 return;
8528
8529 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8530 return;
8531
8532 mutex_lock(&dev_priv->sb_lock);
8533
8534 if (steps % 10 != 0)
8535 tmp = 0xAAAAAAAB;
8536 else
8537 tmp = 0x00000000;
8538 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8539
8540 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8541 tmp &= 0xffff0000;
8542 tmp |= sscdivintphase[idx];
8543 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8544
8545 mutex_unlock(&dev_priv->sb_lock);
8546}
8547
8548#undef BEND_IDX
8549
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008550static void lpt_init_pch_refclk(struct drm_device *dev)
8551{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008552 struct intel_encoder *encoder;
8553 bool has_vga = false;
8554
Damien Lespiaub2784e12014-08-05 11:29:37 +01008555 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008556 switch (encoder->type) {
8557 case INTEL_OUTPUT_ANALOG:
8558 has_vga = true;
8559 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008560 default:
8561 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008562 }
8563 }
8564
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008565 if (has_vga) {
8566 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008567 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008568 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008569 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008570 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008571}
8572
Paulo Zanonidde86e22012-12-01 12:04:25 -02008573/*
8574 * Initialize reference clocks when the driver loads
8575 */
8576void intel_init_pch_refclk(struct drm_device *dev)
8577{
8578 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8579 ironlake_init_pch_refclk(dev);
8580 else if (HAS_PCH_LPT(dev))
8581 lpt_init_pch_refclk(dev);
8582}
8583
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008584static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008585{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008586 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008587 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008588 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008589 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008590 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008591 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008592 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008593 bool is_lvds = false;
8594
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008595 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008596 if (connector_state->crtc != crtc_state->base.crtc)
8597 continue;
8598
8599 encoder = to_intel_encoder(connector_state->best_encoder);
8600
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008601 switch (encoder->type) {
8602 case INTEL_OUTPUT_LVDS:
8603 is_lvds = true;
8604 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008605 default:
8606 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008607 }
8608 num_connectors++;
8609 }
8610
8611 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008612 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008613 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008614 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008615 }
8616
8617 return 120000;
8618}
8619
Daniel Vetter6ff93602013-04-19 11:24:36 +02008620static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008621{
8622 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8624 int pipe = intel_crtc->pipe;
8625 uint32_t val;
8626
Daniel Vetter78114072013-06-13 00:54:57 +02008627 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008628
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008629 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008630 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008631 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008632 break;
8633 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008634 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008635 break;
8636 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008637 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008638 break;
8639 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008640 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008641 break;
8642 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008643 /* Case prevented by intel_choose_pipe_bpp_dither. */
8644 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008645 }
8646
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008647 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008648 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8649
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008650 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008651 val |= PIPECONF_INTERLACED_ILK;
8652 else
8653 val |= PIPECONF_PROGRESSIVE;
8654
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008655 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008656 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008657
Paulo Zanonic8203562012-09-12 10:06:29 -03008658 I915_WRITE(PIPECONF(pipe), val);
8659 POSTING_READ(PIPECONF(pipe));
8660}
8661
Daniel Vetter6ff93602013-04-19 11:24:36 +02008662static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008663{
Jani Nikula391bf042016-03-18 17:05:40 +02008664 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008666 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008667 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008668
Jani Nikula391bf042016-03-18 17:05:40 +02008669 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008670 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8671
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008672 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008673 val |= PIPECONF_INTERLACED_ILK;
8674 else
8675 val |= PIPECONF_PROGRESSIVE;
8676
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008677 I915_WRITE(PIPECONF(cpu_transcoder), val);
8678 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008679}
8680
Jani Nikula391bf042016-03-18 17:05:40 +02008681static void haswell_set_pipemisc(struct drm_crtc *crtc)
8682{
8683 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8685
8686 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8687 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008688
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008689 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008690 case 18:
8691 val |= PIPEMISC_DITHER_6_BPC;
8692 break;
8693 case 24:
8694 val |= PIPEMISC_DITHER_8_BPC;
8695 break;
8696 case 30:
8697 val |= PIPEMISC_DITHER_10_BPC;
8698 break;
8699 case 36:
8700 val |= PIPEMISC_DITHER_12_BPC;
8701 break;
8702 default:
8703 /* Case prevented by pipe_config_set_bpp. */
8704 BUG();
8705 }
8706
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008707 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008708 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8709
Jani Nikula391bf042016-03-18 17:05:40 +02008710 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008711 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008712}
8713
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008714static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008715 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008716 intel_clock_t *clock,
8717 bool *has_reduced_clock,
8718 intel_clock_t *reduced_clock)
8719{
8720 struct drm_device *dev = crtc->dev;
8721 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008722 int refclk;
8723 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008724 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008725
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008726 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008727
8728 /*
8729 * Returns a set of divisors for the desired target clock with the given
8730 * refclk, or FALSE. The returned values represent the clock equation:
8731 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8732 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008733 limit = intel_limit(crtc_state, refclk);
8734 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008735 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008736 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008737 if (!ret)
8738 return false;
8739
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008740 return true;
8741}
8742
Paulo Zanonid4b19312012-11-29 11:29:32 -02008743int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8744{
8745 /*
8746 * Account for spread spectrum to avoid
8747 * oversubscribing the link. Max center spread
8748 * is 2.5%; use 5% for safety's sake.
8749 */
8750 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008751 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008752}
8753
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008754static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008755{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008756 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008757}
8758
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008759static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008760 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008761 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008762 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008763{
8764 struct drm_crtc *crtc = &intel_crtc->base;
8765 struct drm_device *dev = crtc->dev;
8766 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008767 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008768 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008769 struct drm_connector_state *connector_state;
8770 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008771 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008772 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008773 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008774
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008775 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008776 if (connector_state->crtc != crtc_state->base.crtc)
8777 continue;
8778
8779 encoder = to_intel_encoder(connector_state->best_encoder);
8780
8781 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008782 case INTEL_OUTPUT_LVDS:
8783 is_lvds = true;
8784 break;
8785 case INTEL_OUTPUT_SDVO:
8786 case INTEL_OUTPUT_HDMI:
8787 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008788 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008789 default:
8790 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008791 }
8792
8793 num_connectors++;
8794 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008795
Chris Wilsonc1858122010-12-03 21:35:48 +00008796 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008797 factor = 21;
8798 if (is_lvds) {
8799 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008800 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008801 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008802 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008803 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008804 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008805
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008806 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008807 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008808
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008809 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8810 *fp2 |= FP_CB_TUNE;
8811
Chris Wilson5eddb702010-09-11 13:48:45 +01008812 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008813
Eric Anholta07d6782011-03-30 13:01:08 -07008814 if (is_lvds)
8815 dpll |= DPLLB_MODE_LVDS;
8816 else
8817 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008818
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008819 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008820 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008821
8822 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008823 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008824 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008825 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008826
Eric Anholta07d6782011-03-30 13:01:08 -07008827 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008828 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008829 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008831
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008832 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008833 case 5:
8834 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8835 break;
8836 case 7:
8837 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8838 break;
8839 case 10:
8840 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8841 break;
8842 case 14:
8843 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8844 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008845 }
8846
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008847 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008848 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008849 else
8850 dpll |= PLL_REF_INPUT_DREFCLK;
8851
Daniel Vetter959e16d2013-06-05 13:34:21 +02008852 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008853}
8854
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008855static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8856 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008857{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008858 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008859 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008860 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008861 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008862 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008863 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008864
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008865 memset(&crtc_state->dpll_hw_state, 0,
8866 sizeof(crtc_state->dpll_hw_state));
8867
Ville Syrjälä7905df22015-11-25 16:35:30 +02008868 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008869
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008870 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8871 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8872
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008873 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008874 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008875 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008876 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8877 return -EINVAL;
8878 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008879 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008880 if (!crtc_state->clock_set) {
8881 crtc_state->dpll.n = clock.n;
8882 crtc_state->dpll.m1 = clock.m1;
8883 crtc_state->dpll.m2 = clock.m2;
8884 crtc_state->dpll.p1 = clock.p1;
8885 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008886 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008887
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008888 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008889 if (crtc_state->has_pch_encoder) {
8890 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008891 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008892 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008893
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008894 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008895 &fp, &reduced_clock,
8896 has_reduced_clock ? &fp2 : NULL);
8897
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008898 crtc_state->dpll_hw_state.dpll = dpll;
8899 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008900 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008901 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008902 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008903 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008904
Ander Conselvan de Oliveiradaedf202016-03-08 17:46:23 +02008905 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008906 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008907 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008908 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008909 return -EINVAL;
8910 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008911 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008912
Rodrigo Viviab585de2015-03-24 12:40:09 -07008913 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008914 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008915 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008916 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008917
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008918 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008919}
8920
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008921static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8922 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008923{
8924 struct drm_device *dev = crtc->base.dev;
8925 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008926 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008927
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008928 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8929 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8930 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8931 & ~TU_SIZE_MASK;
8932 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8933 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8934 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8935}
8936
8937static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8938 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008939 struct intel_link_m_n *m_n,
8940 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008941{
8942 struct drm_device *dev = crtc->base.dev;
8943 struct drm_i915_private *dev_priv = dev->dev_private;
8944 enum pipe pipe = crtc->pipe;
8945
8946 if (INTEL_INFO(dev)->gen >= 5) {
8947 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8948 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8949 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8950 & ~TU_SIZE_MASK;
8951 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8952 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8953 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008954 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8955 * gen < 8) and if DRRS is supported (to make sure the
8956 * registers are not unnecessarily read).
8957 */
8958 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008959 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008960 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8961 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8962 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8963 & ~TU_SIZE_MASK;
8964 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8965 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8966 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8967 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008968 } else {
8969 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8970 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8971 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8972 & ~TU_SIZE_MASK;
8973 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8974 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8975 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8976 }
8977}
8978
8979void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008980 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008981{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008982 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008983 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8984 else
8985 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008986 &pipe_config->dp_m_n,
8987 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008988}
8989
Daniel Vetter72419202013-04-04 13:28:53 +02008990static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008991 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008992{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008993 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008994 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008995}
8996
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008997static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008998 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008999{
9000 struct drm_device *dev = crtc->base.dev;
9001 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009002 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9003 uint32_t ps_ctrl = 0;
9004 int id = -1;
9005 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009006
Chandra Kondurua1b22782015-04-07 15:28:45 -07009007 /* find scaler attached to this pipe */
9008 for (i = 0; i < crtc->num_scalers; i++) {
9009 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9010 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9011 id = i;
9012 pipe_config->pch_pfit.enabled = true;
9013 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9014 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9015 break;
9016 }
9017 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009018
Chandra Kondurua1b22782015-04-07 15:28:45 -07009019 scaler_state->scaler_id = id;
9020 if (id >= 0) {
9021 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9022 } else {
9023 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009024 }
9025}
9026
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009027static void
9028skylake_get_initial_plane_config(struct intel_crtc *crtc,
9029 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009030{
9031 struct drm_device *dev = crtc->base.dev;
9032 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009033 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009034 int pipe = crtc->pipe;
9035 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009036 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009037 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009038 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009039
Damien Lespiaud9806c92015-01-21 14:07:19 +00009040 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009041 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009042 DRM_DEBUG_KMS("failed to alloc fb\n");
9043 return;
9044 }
9045
Damien Lespiau1b842c82015-01-21 13:50:54 +00009046 fb = &intel_fb->base;
9047
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009048 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009049 if (!(val & PLANE_CTL_ENABLE))
9050 goto error;
9051
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009052 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9053 fourcc = skl_format_to_fourcc(pixel_format,
9054 val & PLANE_CTL_ORDER_RGBX,
9055 val & PLANE_CTL_ALPHA_MASK);
9056 fb->pixel_format = fourcc;
9057 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9058
Damien Lespiau40f46282015-02-27 11:15:21 +00009059 tiling = val & PLANE_CTL_TILED_MASK;
9060 switch (tiling) {
9061 case PLANE_CTL_TILED_LINEAR:
9062 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9063 break;
9064 case PLANE_CTL_TILED_X:
9065 plane_config->tiling = I915_TILING_X;
9066 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9067 break;
9068 case PLANE_CTL_TILED_Y:
9069 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9070 break;
9071 case PLANE_CTL_TILED_YF:
9072 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9073 break;
9074 default:
9075 MISSING_CASE(tiling);
9076 goto error;
9077 }
9078
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009079 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9080 plane_config->base = base;
9081
9082 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9083
9084 val = I915_READ(PLANE_SIZE(pipe, 0));
9085 fb->height = ((val >> 16) & 0xfff) + 1;
9086 fb->width = ((val >> 0) & 0x1fff) + 1;
9087
9088 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009089 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009090 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009091 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9092
9093 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009094 fb->pixel_format,
9095 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009096
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009097 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009098
9099 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9100 pipe_name(pipe), fb->width, fb->height,
9101 fb->bits_per_pixel, base, fb->pitches[0],
9102 plane_config->size);
9103
Damien Lespiau2d140302015-02-05 17:22:18 +00009104 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009105 return;
9106
9107error:
9108 kfree(fb);
9109}
9110
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009111static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009112 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009113{
9114 struct drm_device *dev = crtc->base.dev;
9115 struct drm_i915_private *dev_priv = dev->dev_private;
9116 uint32_t tmp;
9117
9118 tmp = I915_READ(PF_CTL(crtc->pipe));
9119
9120 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009121 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009122 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9123 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009124
9125 /* We currently do not free assignements of panel fitters on
9126 * ivb/hsw (since we don't use the higher upscaling modes which
9127 * differentiates them) so just WARN about this case for now. */
9128 if (IS_GEN7(dev)) {
9129 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9130 PF_PIPE_SEL_IVB(crtc->pipe));
9131 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009132 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009133}
9134
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009135static void
9136ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9137 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009138{
9139 struct drm_device *dev = crtc->base.dev;
9140 struct drm_i915_private *dev_priv = dev->dev_private;
9141 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009142 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009143 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009144 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009145 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009146 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009147
Damien Lespiau42a7b082015-02-05 19:35:13 +00009148 val = I915_READ(DSPCNTR(pipe));
9149 if (!(val & DISPLAY_PLANE_ENABLE))
9150 return;
9151
Damien Lespiaud9806c92015-01-21 14:07:19 +00009152 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009153 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009154 DRM_DEBUG_KMS("failed to alloc fb\n");
9155 return;
9156 }
9157
Damien Lespiau1b842c82015-01-21 13:50:54 +00009158 fb = &intel_fb->base;
9159
Daniel Vetter18c52472015-02-10 17:16:09 +00009160 if (INTEL_INFO(dev)->gen >= 4) {
9161 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009162 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009163 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9164 }
9165 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009166
9167 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009168 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009169 fb->pixel_format = fourcc;
9170 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009171
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009172 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009173 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009174 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009175 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009176 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009177 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009178 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009179 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009180 }
9181 plane_config->base = base;
9182
9183 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009184 fb->width = ((val >> 16) & 0xfff) + 1;
9185 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186
9187 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009188 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009189
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009190 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009191 fb->pixel_format,
9192 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009193
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009194 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
Damien Lespiau2844a922015-01-20 12:51:48 +00009196 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9197 pipe_name(pipe), fb->width, fb->height,
9198 fb->bits_per_pixel, base, fb->pitches[0],
9199 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009200
Damien Lespiau2d140302015-02-05 17:22:18 +00009201 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009202}
9203
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009204static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009205 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009206{
9207 struct drm_device *dev = crtc->base.dev;
9208 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009209 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009210 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009211 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009212
Imre Deak17290502016-02-12 18:55:11 +02009213 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9214 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009215 return false;
9216
Daniel Vettere143a212013-07-04 12:01:15 +02009217 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009218 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009219
Imre Deak17290502016-02-12 18:55:11 +02009220 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009221 tmp = I915_READ(PIPECONF(crtc->pipe));
9222 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009223 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009224
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009225 switch (tmp & PIPECONF_BPC_MASK) {
9226 case PIPECONF_6BPC:
9227 pipe_config->pipe_bpp = 18;
9228 break;
9229 case PIPECONF_8BPC:
9230 pipe_config->pipe_bpp = 24;
9231 break;
9232 case PIPECONF_10BPC:
9233 pipe_config->pipe_bpp = 30;
9234 break;
9235 case PIPECONF_12BPC:
9236 pipe_config->pipe_bpp = 36;
9237 break;
9238 default:
9239 break;
9240 }
9241
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009242 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9243 pipe_config->limited_color_range = true;
9244
Daniel Vetterab9412b2013-05-03 11:49:46 +02009245 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009246 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009247 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009248
Daniel Vetter88adfff2013-03-28 10:42:01 +01009249 pipe_config->has_pch_encoder = true;
9250
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009251 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9252 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9253 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009254
9255 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009256
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009257 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009258 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009259 } else {
9260 tmp = I915_READ(PCH_DPLL_SEL);
9261 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009262 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009263 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009264 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009265 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009266
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009267 pipe_config->shared_dpll =
9268 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9269 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009270
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009271 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9272 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009273
9274 tmp = pipe_config->dpll_hw_state.dpll;
9275 pipe_config->pixel_multiplier =
9276 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9277 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009278
9279 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009280 } else {
9281 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009282 }
9283
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009284 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009285 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009286
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009287 ironlake_get_pfit_config(crtc, pipe_config);
9288
Imre Deak17290502016-02-12 18:55:11 +02009289 ret = true;
9290
9291out:
9292 intel_display_power_put(dev_priv, power_domain);
9293
9294 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009295}
9296
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009297static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9298{
9299 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009300 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009301
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009302 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009303 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009304 pipe_name(crtc->pipe));
9305
Rob Clarke2c719b2014-12-15 13:56:32 -05009306 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9307 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009308 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9309 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009310 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9311 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009312 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009313 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009314 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009315 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009316 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009317 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009318 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009319 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009320 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009321
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009322 /*
9323 * In theory we can still leave IRQs enabled, as long as only the HPD
9324 * interrupts remain enabled. We used to check for that, but since it's
9325 * gen-specific and since we only disable LCPLL after we fully disable
9326 * the interrupts, the check below should be enough.
9327 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009328 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009329}
9330
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009331static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9332{
9333 struct drm_device *dev = dev_priv->dev;
9334
9335 if (IS_HASWELL(dev))
9336 return I915_READ(D_COMP_HSW);
9337 else
9338 return I915_READ(D_COMP_BDW);
9339}
9340
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009341static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9342{
9343 struct drm_device *dev = dev_priv->dev;
9344
9345 if (IS_HASWELL(dev)) {
9346 mutex_lock(&dev_priv->rps.hw_lock);
9347 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9348 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009349 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009350 mutex_unlock(&dev_priv->rps.hw_lock);
9351 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009352 I915_WRITE(D_COMP_BDW, val);
9353 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009354 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009355}
9356
9357/*
9358 * This function implements pieces of two sequences from BSpec:
9359 * - Sequence for display software to disable LCPLL
9360 * - Sequence for display software to allow package C8+
9361 * The steps implemented here are just the steps that actually touch the LCPLL
9362 * register. Callers should take care of disabling all the display engine
9363 * functions, doing the mode unset, fixing interrupts, etc.
9364 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009365static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9366 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367{
9368 uint32_t val;
9369
9370 assert_can_disable_lcpll(dev_priv);
9371
9372 val = I915_READ(LCPLL_CTL);
9373
9374 if (switch_to_fclk) {
9375 val |= LCPLL_CD_SOURCE_FCLK;
9376 I915_WRITE(LCPLL_CTL, val);
9377
9378 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9379 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9380 DRM_ERROR("Switching to FCLK failed\n");
9381
9382 val = I915_READ(LCPLL_CTL);
9383 }
9384
9385 val |= LCPLL_PLL_DISABLE;
9386 I915_WRITE(LCPLL_CTL, val);
9387 POSTING_READ(LCPLL_CTL);
9388
9389 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9390 DRM_ERROR("LCPLL still locked\n");
9391
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009392 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009393 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009394 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009395 ndelay(100);
9396
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009397 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9398 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009399 DRM_ERROR("D_COMP RCOMP still in progress\n");
9400
9401 if (allow_power_down) {
9402 val = I915_READ(LCPLL_CTL);
9403 val |= LCPLL_POWER_DOWN_ALLOW;
9404 I915_WRITE(LCPLL_CTL, val);
9405 POSTING_READ(LCPLL_CTL);
9406 }
9407}
9408
9409/*
9410 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9411 * source.
9412 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009413static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009414{
9415 uint32_t val;
9416
9417 val = I915_READ(LCPLL_CTL);
9418
9419 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9420 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9421 return;
9422
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009423 /*
9424 * Make sure we're not on PC8 state before disabling PC8, otherwise
9425 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009426 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009427 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009428
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009429 if (val & LCPLL_POWER_DOWN_ALLOW) {
9430 val &= ~LCPLL_POWER_DOWN_ALLOW;
9431 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009432 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009433 }
9434
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009435 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009436 val |= D_COMP_COMP_FORCE;
9437 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009438 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439
9440 val = I915_READ(LCPLL_CTL);
9441 val &= ~LCPLL_PLL_DISABLE;
9442 I915_WRITE(LCPLL_CTL, val);
9443
9444 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9445 DRM_ERROR("LCPLL not locked yet\n");
9446
9447 if (val & LCPLL_CD_SOURCE_FCLK) {
9448 val = I915_READ(LCPLL_CTL);
9449 val &= ~LCPLL_CD_SOURCE_FCLK;
9450 I915_WRITE(LCPLL_CTL, val);
9451
9452 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9453 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9454 DRM_ERROR("Switching back to LCPLL failed\n");
9455 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009456
Mika Kuoppala59bad942015-01-16 11:34:40 +02009457 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009458 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009459}
9460
Paulo Zanoni765dab672014-03-07 20:08:18 -03009461/*
9462 * Package states C8 and deeper are really deep PC states that can only be
9463 * reached when all the devices on the system allow it, so even if the graphics
9464 * device allows PC8+, it doesn't mean the system will actually get to these
9465 * states. Our driver only allows PC8+ when going into runtime PM.
9466 *
9467 * The requirements for PC8+ are that all the outputs are disabled, the power
9468 * well is disabled and most interrupts are disabled, and these are also
9469 * requirements for runtime PM. When these conditions are met, we manually do
9470 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9471 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9472 * hang the machine.
9473 *
9474 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9475 * the state of some registers, so when we come back from PC8+ we need to
9476 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9477 * need to take care of the registers kept by RC6. Notice that this happens even
9478 * if we don't put the device in PCI D3 state (which is what currently happens
9479 * because of the runtime PM support).
9480 *
9481 * For more, read "Display Sequences for Package C8" on the hardware
9482 * documentation.
9483 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009484void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009485{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009486 struct drm_device *dev = dev_priv->dev;
9487 uint32_t val;
9488
Paulo Zanonic67a4702013-08-19 13:18:09 -03009489 DRM_DEBUG_KMS("Enabling package C8+\n");
9490
Ville Syrjäläc2699522015-08-27 23:55:59 +03009491 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009492 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9493 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9494 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9495 }
9496
9497 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009498 hsw_disable_lcpll(dev_priv, true, true);
9499}
9500
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009501void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009502{
9503 struct drm_device *dev = dev_priv->dev;
9504 uint32_t val;
9505
Paulo Zanonic67a4702013-08-19 13:18:09 -03009506 DRM_DEBUG_KMS("Disabling package C8+\n");
9507
9508 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009509 lpt_init_pch_refclk(dev);
9510
Ville Syrjäläc2699522015-08-27 23:55:59 +03009511 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009512 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9513 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9514 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9515 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009516}
9517
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009518static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309519{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009520 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009521 struct intel_atomic_state *old_intel_state =
9522 to_intel_atomic_state(old_state);
9523 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309524
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009525 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309526}
9527
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009528/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009529static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009530{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009531 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9532 struct drm_i915_private *dev_priv = state->dev->dev_private;
9533 struct drm_crtc *crtc;
9534 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009535 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009536 unsigned max_pixel_rate = 0, i;
9537 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009538
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009539 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9540 sizeof(intel_state->min_pixclk));
9541
9542 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009543 int pixel_rate;
9544
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009545 crtc_state = to_intel_crtc_state(cstate);
9546 if (!crtc_state->base.enable) {
9547 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009548 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009549 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009550
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009551 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009552
9553 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009554 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009555 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9556
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009557 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009558 }
9559
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009560 for_each_pipe(dev_priv, pipe)
9561 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9562
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009563 return max_pixel_rate;
9564}
9565
9566static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9567{
9568 struct drm_i915_private *dev_priv = dev->dev_private;
9569 uint32_t val, data;
9570 int ret;
9571
9572 if (WARN((I915_READ(LCPLL_CTL) &
9573 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9574 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9575 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9576 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9577 "trying to change cdclk frequency with cdclk not enabled\n"))
9578 return;
9579
9580 mutex_lock(&dev_priv->rps.hw_lock);
9581 ret = sandybridge_pcode_write(dev_priv,
9582 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9583 mutex_unlock(&dev_priv->rps.hw_lock);
9584 if (ret) {
9585 DRM_ERROR("failed to inform pcode about cdclk change\n");
9586 return;
9587 }
9588
9589 val = I915_READ(LCPLL_CTL);
9590 val |= LCPLL_CD_SOURCE_FCLK;
9591 I915_WRITE(LCPLL_CTL, val);
9592
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009593 if (wait_for_us(I915_READ(LCPLL_CTL) &
9594 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009595 DRM_ERROR("Switching to FCLK failed\n");
9596
9597 val = I915_READ(LCPLL_CTL);
9598 val &= ~LCPLL_CLK_FREQ_MASK;
9599
9600 switch (cdclk) {
9601 case 450000:
9602 val |= LCPLL_CLK_FREQ_450;
9603 data = 0;
9604 break;
9605 case 540000:
9606 val |= LCPLL_CLK_FREQ_54O_BDW;
9607 data = 1;
9608 break;
9609 case 337500:
9610 val |= LCPLL_CLK_FREQ_337_5_BDW;
9611 data = 2;
9612 break;
9613 case 675000:
9614 val |= LCPLL_CLK_FREQ_675_BDW;
9615 data = 3;
9616 break;
9617 default:
9618 WARN(1, "invalid cdclk frequency\n");
9619 return;
9620 }
9621
9622 I915_WRITE(LCPLL_CTL, val);
9623
9624 val = I915_READ(LCPLL_CTL);
9625 val &= ~LCPLL_CD_SOURCE_FCLK;
9626 I915_WRITE(LCPLL_CTL, val);
9627
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009628 if (wait_for_us((I915_READ(LCPLL_CTL) &
9629 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009630 DRM_ERROR("Switching back to LCPLL failed\n");
9631
9632 mutex_lock(&dev_priv->rps.hw_lock);
9633 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9634 mutex_unlock(&dev_priv->rps.hw_lock);
9635
9636 intel_update_cdclk(dev);
9637
9638 WARN(cdclk != dev_priv->cdclk_freq,
9639 "cdclk requested %d kHz but got %d kHz\n",
9640 cdclk, dev_priv->cdclk_freq);
9641}
9642
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009643static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009644{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009645 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009646 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009647 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009648 int cdclk;
9649
9650 /*
9651 * FIXME should also account for plane ratio
9652 * once 64bpp pixel formats are supported.
9653 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009654 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009655 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009656 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009657 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009658 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009659 cdclk = 450000;
9660 else
9661 cdclk = 337500;
9662
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009663 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009664 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9665 cdclk, dev_priv->max_cdclk_freq);
9666 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009667 }
9668
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009669 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9670 if (!intel_state->active_crtcs)
9671 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009672
9673 return 0;
9674}
9675
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009676static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009677{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009678 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009679 struct intel_atomic_state *old_intel_state =
9680 to_intel_atomic_state(old_state);
9681 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009683 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009684}
9685
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009686static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9687 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009688{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009689 struct intel_encoder *intel_encoder =
9690 intel_ddi_get_crtc_new_encoder(crtc_state);
9691
9692 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9693 if (!intel_ddi_pll_select(crtc, crtc_state))
9694 return -EINVAL;
9695 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009696
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009697 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009698
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009699 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009700}
9701
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309702static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9703 enum port port,
9704 struct intel_crtc_state *pipe_config)
9705{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009706 enum intel_dpll_id id;
9707
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309708 switch (port) {
9709 case PORT_A:
9710 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009711 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309712 break;
9713 case PORT_B:
9714 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009715 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309716 break;
9717 case PORT_C:
9718 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009719 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309720 break;
9721 default:
9722 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009723 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309724 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009725
9726 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309727}
9728
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009729static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9730 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009731 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009732{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009733 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009734 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009735
9736 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9737 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9738
9739 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009740 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009741 id = DPLL_ID_SKL_DPLL0;
9742 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009743 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009744 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009745 break;
9746 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009747 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009748 break;
9749 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009750 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009751 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009752 default:
9753 MISSING_CASE(pipe_config->ddi_pll_sel);
9754 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009755 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009756
9757 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009758}
9759
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009760static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9761 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009762 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009763{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009764 enum intel_dpll_id id;
9765
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009766 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9767
9768 switch (pipe_config->ddi_pll_sel) {
9769 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009770 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009771 break;
9772 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009773 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009774 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009775 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009776 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009777 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009778 case PORT_CLK_SEL_LCPLL_810:
9779 id = DPLL_ID_LCPLL_810;
9780 break;
9781 case PORT_CLK_SEL_LCPLL_1350:
9782 id = DPLL_ID_LCPLL_1350;
9783 break;
9784 case PORT_CLK_SEL_LCPLL_2700:
9785 id = DPLL_ID_LCPLL_2700;
9786 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009787 default:
9788 MISSING_CASE(pipe_config->ddi_pll_sel);
9789 /* fall through */
9790 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009791 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009792 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009793
9794 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009795}
9796
Jani Nikulacf304292016-03-18 17:05:41 +02009797static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9798 struct intel_crtc_state *pipe_config,
9799 unsigned long *power_domain_mask)
9800{
9801 struct drm_device *dev = crtc->base.dev;
9802 struct drm_i915_private *dev_priv = dev->dev_private;
9803 enum intel_display_power_domain power_domain;
9804 u32 tmp;
9805
9806 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9807
9808 /*
9809 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9810 * consistency and less surprising code; it's in always on power).
9811 */
9812 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9813 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9814 enum pipe trans_edp_pipe;
9815 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9816 default:
9817 WARN(1, "unknown pipe linked to edp transcoder\n");
9818 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9819 case TRANS_DDI_EDP_INPUT_A_ON:
9820 trans_edp_pipe = PIPE_A;
9821 break;
9822 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9823 trans_edp_pipe = PIPE_B;
9824 break;
9825 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9826 trans_edp_pipe = PIPE_C;
9827 break;
9828 }
9829
9830 if (trans_edp_pipe == crtc->pipe)
9831 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9832 }
9833
9834 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9835 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9836 return false;
9837 *power_domain_mask |= BIT(power_domain);
9838
9839 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9840
9841 return tmp & PIPECONF_ENABLE;
9842}
9843
Jani Nikula4d1de972016-03-18 17:05:42 +02009844static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9845 struct intel_crtc_state *pipe_config,
9846 unsigned long *power_domain_mask)
9847{
9848 struct drm_device *dev = crtc->base.dev;
9849 struct drm_i915_private *dev_priv = dev->dev_private;
9850 enum intel_display_power_domain power_domain;
9851 enum port port;
9852 enum transcoder cpu_transcoder;
9853 u32 tmp;
9854
9855 pipe_config->has_dsi_encoder = false;
9856
9857 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9858 if (port == PORT_A)
9859 cpu_transcoder = TRANSCODER_DSI_A;
9860 else
9861 cpu_transcoder = TRANSCODER_DSI_C;
9862
9863 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9864 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9865 continue;
9866 *power_domain_mask |= BIT(power_domain);
9867
9868 /* XXX: this works for video mode only */
9869 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9870 if (!(tmp & DPI_ENABLE))
9871 continue;
9872
9873 tmp = I915_READ(MIPI_CTRL(port));
9874 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9875 continue;
9876
9877 pipe_config->cpu_transcoder = cpu_transcoder;
9878 pipe_config->has_dsi_encoder = true;
9879 break;
9880 }
9881
9882 return pipe_config->has_dsi_encoder;
9883}
9884
Daniel Vetter26804af2014-06-25 22:01:55 +03009885static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009886 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009887{
9888 struct drm_device *dev = crtc->base.dev;
9889 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009890 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009891 enum port port;
9892 uint32_t tmp;
9893
9894 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9895
9896 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9897
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009898 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009899 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309900 else if (IS_BROXTON(dev))
9901 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009902 else
9903 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009904
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009905 pll = pipe_config->shared_dpll;
9906 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009907 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9908 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009909 }
9910
Daniel Vetter26804af2014-06-25 22:01:55 +03009911 /*
9912 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9913 * DDI E. So just check whether this pipe is wired to DDI E and whether
9914 * the PCH transcoder is on.
9915 */
Damien Lespiauca370452013-12-03 13:56:24 +00009916 if (INTEL_INFO(dev)->gen < 9 &&
9917 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009918 pipe_config->has_pch_encoder = true;
9919
9920 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9921 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9922 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9923
9924 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9925 }
9926}
9927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009928static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009929 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009930{
9931 struct drm_device *dev = crtc->base.dev;
9932 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009933 enum intel_display_power_domain power_domain;
9934 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009935 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009936
Imre Deak17290502016-02-12 18:55:11 +02009937 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9938 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009939 return false;
Imre Deak17290502016-02-12 18:55:11 +02009940 power_domain_mask = BIT(power_domain);
9941
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009942 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009943
Jani Nikulacf304292016-03-18 17:05:41 +02009944 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009945
Jani Nikula4d1de972016-03-18 17:05:42 +02009946 if (IS_BROXTON(dev_priv)) {
9947 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9948 &power_domain_mask);
9949 WARN_ON(active && pipe_config->has_dsi_encoder);
9950 if (pipe_config->has_dsi_encoder)
9951 active = true;
9952 }
9953
Jani Nikulacf304292016-03-18 17:05:41 +02009954 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009955 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009956
Jani Nikula4d1de972016-03-18 17:05:42 +02009957 if (!pipe_config->has_dsi_encoder) {
9958 haswell_get_ddi_port_state(crtc, pipe_config);
9959 intel_get_pipe_timings(crtc, pipe_config);
9960 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009961
Jani Nikulabc58be62016-03-18 17:05:39 +02009962 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009963
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009964 pipe_config->gamma_mode =
9965 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9966
Chandra Kondurua1b22782015-04-07 15:28:45 -07009967 if (INTEL_INFO(dev)->gen >= 9) {
9968 skl_init_scalers(dev, crtc, pipe_config);
9969 }
9970
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009971 if (INTEL_INFO(dev)->gen >= 9) {
9972 pipe_config->scaler_state.scaler_id = -1;
9973 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9974 }
9975
Imre Deak17290502016-02-12 18:55:11 +02009976 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9977 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9978 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009979 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009980 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009981 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009982 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009983 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009984
Jesse Barnese59150d2014-01-07 13:30:45 -08009985 if (IS_HASWELL(dev))
9986 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9987 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009988
Jani Nikula4d1de972016-03-18 17:05:42 +02009989 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9990 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009991 pipe_config->pixel_multiplier =
9992 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9993 } else {
9994 pipe_config->pixel_multiplier = 1;
9995 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009996
Imre Deak17290502016-02-12 18:55:11 +02009997out:
9998 for_each_power_domain(power_domain, power_domain_mask)
9999 intel_display_power_put(dev_priv, power_domain);
10000
Jani Nikulacf304292016-03-18 17:05:41 +020010001 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010002}
10003
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010004static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10005 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010006{
10007 struct drm_device *dev = crtc->dev;
10008 struct drm_i915_private *dev_priv = dev->dev_private;
10009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010010 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010011
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010012 if (plane_state && plane_state->visible) {
10013 unsigned int width = plane_state->base.crtc_w;
10014 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010015 unsigned int stride = roundup_pow_of_two(width) * 4;
10016
10017 switch (stride) {
10018 default:
10019 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10020 width, stride);
10021 stride = 256;
10022 /* fallthrough */
10023 case 256:
10024 case 512:
10025 case 1024:
10026 case 2048:
10027 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010028 }
10029
Ville Syrjälädc41c152014-08-13 11:57:05 +030010030 cntl |= CURSOR_ENABLE |
10031 CURSOR_GAMMA_ENABLE |
10032 CURSOR_FORMAT_ARGB |
10033 CURSOR_STRIDE(stride);
10034
10035 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010036 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010037
Ville Syrjälädc41c152014-08-13 11:57:05 +030010038 if (intel_crtc->cursor_cntl != 0 &&
10039 (intel_crtc->cursor_base != base ||
10040 intel_crtc->cursor_size != size ||
10041 intel_crtc->cursor_cntl != cntl)) {
10042 /* On these chipsets we can only modify the base/size/stride
10043 * whilst the cursor is disabled.
10044 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010045 I915_WRITE(CURCNTR(PIPE_A), 0);
10046 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010047 intel_crtc->cursor_cntl = 0;
10048 }
10049
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010050 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010051 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010052 intel_crtc->cursor_base = base;
10053 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010054
10055 if (intel_crtc->cursor_size != size) {
10056 I915_WRITE(CURSIZE, size);
10057 intel_crtc->cursor_size = size;
10058 }
10059
Chris Wilson4b0e3332014-05-30 16:35:26 +030010060 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010061 I915_WRITE(CURCNTR(PIPE_A), cntl);
10062 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010063 intel_crtc->cursor_cntl = cntl;
10064 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010065}
10066
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010067static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10068 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010069{
10070 struct drm_device *dev = crtc->dev;
10071 struct drm_i915_private *dev_priv = dev->dev_private;
10072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10073 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010074 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010075
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010076 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010077 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010078 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010079 case 64:
10080 cntl |= CURSOR_MODE_64_ARGB_AX;
10081 break;
10082 case 128:
10083 cntl |= CURSOR_MODE_128_ARGB_AX;
10084 break;
10085 case 256:
10086 cntl |= CURSOR_MODE_256_ARGB_AX;
10087 break;
10088 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010089 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010090 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010091 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010092 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010093
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010094 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010095 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010096
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010097 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10098 cntl |= CURSOR_ROTATE_180;
10099 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010100
Chris Wilson4b0e3332014-05-30 16:35:26 +030010101 if (intel_crtc->cursor_cntl != cntl) {
10102 I915_WRITE(CURCNTR(pipe), cntl);
10103 POSTING_READ(CURCNTR(pipe));
10104 intel_crtc->cursor_cntl = cntl;
10105 }
10106
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010107 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010108 I915_WRITE(CURBASE(pipe), base);
10109 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010110
10111 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010112}
10113
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010114/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010115static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010116 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010117{
10118 struct drm_device *dev = crtc->dev;
10119 struct drm_i915_private *dev_priv = dev->dev_private;
10120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10121 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010122 u32 base = intel_crtc->cursor_addr;
10123 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010124
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010125 if (plane_state) {
10126 int x = plane_state->base.crtc_x;
10127 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010128
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010129 if (x < 0) {
10130 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10131 x = -x;
10132 }
10133 pos |= x << CURSOR_X_SHIFT;
10134
10135 if (y < 0) {
10136 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10137 y = -y;
10138 }
10139 pos |= y << CURSOR_Y_SHIFT;
10140
10141 /* ILK+ do this automagically */
10142 if (HAS_GMCH_DISPLAY(dev) &&
10143 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10144 base += (plane_state->base.crtc_h *
10145 plane_state->base.crtc_w - 1) * 4;
10146 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010147 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010148
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010149 I915_WRITE(CURPOS(pipe), pos);
10150
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010151 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010152 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010153 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010154 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010155}
10156
Ville Syrjälädc41c152014-08-13 11:57:05 +030010157static bool cursor_size_ok(struct drm_device *dev,
10158 uint32_t width, uint32_t height)
10159{
10160 if (width == 0 || height == 0)
10161 return false;
10162
10163 /*
10164 * 845g/865g are special in that they are only limited by
10165 * the width of their cursors, the height is arbitrary up to
10166 * the precision of the register. Everything else requires
10167 * square cursors, limited to a few power-of-two sizes.
10168 */
10169 if (IS_845G(dev) || IS_I865G(dev)) {
10170 if ((width & 63) != 0)
10171 return false;
10172
10173 if (width > (IS_845G(dev) ? 64 : 512))
10174 return false;
10175
10176 if (height > 1023)
10177 return false;
10178 } else {
10179 switch (width | height) {
10180 case 256:
10181 case 128:
10182 if (IS_GEN2(dev))
10183 return false;
10184 case 64:
10185 break;
10186 default:
10187 return false;
10188 }
10189 }
10190
10191 return true;
10192}
10193
Jesse Barnes79e53942008-11-07 14:24:08 -080010194/* VESA 640x480x72Hz mode to set on the pipe */
10195static struct drm_display_mode load_detect_mode = {
10196 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10197 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10198};
10199
Daniel Vettera8bb6812014-02-10 18:00:39 +010010200struct drm_framebuffer *
10201__intel_framebuffer_create(struct drm_device *dev,
10202 struct drm_mode_fb_cmd2 *mode_cmd,
10203 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010204{
10205 struct intel_framebuffer *intel_fb;
10206 int ret;
10207
10208 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010209 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010210 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010211
10212 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010213 if (ret)
10214 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010215
10216 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010217
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010218err:
10219 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010220 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010221}
10222
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010223static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010224intel_framebuffer_create(struct drm_device *dev,
10225 struct drm_mode_fb_cmd2 *mode_cmd,
10226 struct drm_i915_gem_object *obj)
10227{
10228 struct drm_framebuffer *fb;
10229 int ret;
10230
10231 ret = i915_mutex_lock_interruptible(dev);
10232 if (ret)
10233 return ERR_PTR(ret);
10234 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10235 mutex_unlock(&dev->struct_mutex);
10236
10237 return fb;
10238}
10239
Chris Wilsond2dff872011-04-19 08:36:26 +010010240static u32
10241intel_framebuffer_pitch_for_width(int width, int bpp)
10242{
10243 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10244 return ALIGN(pitch, 64);
10245}
10246
10247static u32
10248intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10249{
10250 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010251 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010252}
10253
10254static struct drm_framebuffer *
10255intel_framebuffer_create_for_mode(struct drm_device *dev,
10256 struct drm_display_mode *mode,
10257 int depth, int bpp)
10258{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010259 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010260 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010261 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010262
10263 obj = i915_gem_alloc_object(dev,
10264 intel_framebuffer_size_for_mode(mode, bpp));
10265 if (obj == NULL)
10266 return ERR_PTR(-ENOMEM);
10267
10268 mode_cmd.width = mode->hdisplay;
10269 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010270 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10271 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010272 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010273
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010274 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10275 if (IS_ERR(fb))
10276 drm_gem_object_unreference_unlocked(&obj->base);
10277
10278 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010279}
10280
10281static struct drm_framebuffer *
10282mode_fits_in_fbdev(struct drm_device *dev,
10283 struct drm_display_mode *mode)
10284{
Daniel Vetter06957262015-08-10 13:34:08 +020010285#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010286 struct drm_i915_private *dev_priv = dev->dev_private;
10287 struct drm_i915_gem_object *obj;
10288 struct drm_framebuffer *fb;
10289
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010290 if (!dev_priv->fbdev)
10291 return NULL;
10292
10293 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010294 return NULL;
10295
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010296 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010297 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010298
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010299 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010300 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10301 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010302 return NULL;
10303
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010304 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010305 return NULL;
10306
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010307 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010308 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010309#else
10310 return NULL;
10311#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010312}
10313
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010314static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10315 struct drm_crtc *crtc,
10316 struct drm_display_mode *mode,
10317 struct drm_framebuffer *fb,
10318 int x, int y)
10319{
10320 struct drm_plane_state *plane_state;
10321 int hdisplay, vdisplay;
10322 int ret;
10323
10324 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10325 if (IS_ERR(plane_state))
10326 return PTR_ERR(plane_state);
10327
10328 if (mode)
10329 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10330 else
10331 hdisplay = vdisplay = 0;
10332
10333 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10334 if (ret)
10335 return ret;
10336 drm_atomic_set_fb_for_plane(plane_state, fb);
10337 plane_state->crtc_x = 0;
10338 plane_state->crtc_y = 0;
10339 plane_state->crtc_w = hdisplay;
10340 plane_state->crtc_h = vdisplay;
10341 plane_state->src_x = x << 16;
10342 plane_state->src_y = y << 16;
10343 plane_state->src_w = hdisplay << 16;
10344 plane_state->src_h = vdisplay << 16;
10345
10346 return 0;
10347}
10348
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010349bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010350 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010351 struct intel_load_detect_pipe *old,
10352 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010353{
10354 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010355 struct intel_encoder *intel_encoder =
10356 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010357 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010358 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010359 struct drm_crtc *crtc = NULL;
10360 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010361 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010362 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010363 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010364 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010365 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010366 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010367
Chris Wilsond2dff872011-04-19 08:36:26 +010010368 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010369 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010370 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010371
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010372 old->restore_state = NULL;
10373
Rob Clark51fd3712013-11-19 12:10:12 -050010374retry:
10375 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10376 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010377 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010378
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 /*
10380 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010381 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010382 * - if the connector already has an assigned crtc, use it (but make
10383 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010384 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010385 * - try to find the first unused crtc that can drive this connector,
10386 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010387 */
10388
10389 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010390 if (connector->state->crtc) {
10391 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010392
Rob Clark51fd3712013-11-19 12:10:12 -050010393 ret = drm_modeset_lock(&crtc->mutex, ctx);
10394 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010395 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010396
10397 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010398 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010399 }
10400
10401 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010402 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 i++;
10404 if (!(encoder->possible_crtcs & (1 << i)))
10405 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010406
10407 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10408 if (ret)
10409 goto fail;
10410
10411 if (possible_crtc->state->enable) {
10412 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010413 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010414 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010415
10416 crtc = possible_crtc;
10417 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010418 }
10419
10420 /*
10421 * If we didn't find an unused CRTC, don't use any.
10422 */
10423 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010424 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010425 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010426 }
10427
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010428found:
10429 intel_crtc = to_intel_crtc(crtc);
10430
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010431 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10432 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010433 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010434
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010435 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010436 restore_state = drm_atomic_state_alloc(dev);
10437 if (!state || !restore_state) {
10438 ret = -ENOMEM;
10439 goto fail;
10440 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010441
10442 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010443 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010444
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010445 connector_state = drm_atomic_get_connector_state(state, connector);
10446 if (IS_ERR(connector_state)) {
10447 ret = PTR_ERR(connector_state);
10448 goto fail;
10449 }
10450
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010451 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10452 if (ret)
10453 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010454
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010455 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10456 if (IS_ERR(crtc_state)) {
10457 ret = PTR_ERR(crtc_state);
10458 goto fail;
10459 }
10460
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010461 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010462
Chris Wilson64927112011-04-20 07:25:26 +010010463 if (!mode)
10464 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010465
Chris Wilsond2dff872011-04-19 08:36:26 +010010466 /* We need a framebuffer large enough to accommodate all accesses
10467 * that the plane may generate whilst we perform load detection.
10468 * We can not rely on the fbcon either being present (we get called
10469 * during its initialisation to detect all boot displays, or it may
10470 * not even exist) or that it is large enough to satisfy the
10471 * requested mode.
10472 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010473 fb = mode_fits_in_fbdev(dev, mode);
10474 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010475 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010476 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010477 } else
10478 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010479 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010480 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010481 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010482 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010483
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010484 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10485 if (ret)
10486 goto fail;
10487
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010488 drm_framebuffer_unreference(fb);
10489
10490 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10491 if (ret)
10492 goto fail;
10493
10494 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10495 if (!ret)
10496 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10497 if (!ret)
10498 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10499 if (ret) {
10500 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10501 goto fail;
10502 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010503
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010504 ret = drm_atomic_commit(state);
10505 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010506 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010507 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010508 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010509
10510 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010511
Jesse Barnes79e53942008-11-07 14:24:08 -080010512 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010513 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010514 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010515
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010516fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010517 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010518 drm_atomic_state_free(restore_state);
10519 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010520
Rob Clark51fd3712013-11-19 12:10:12 -050010521 if (ret == -EDEADLK) {
10522 drm_modeset_backoff(ctx);
10523 goto retry;
10524 }
10525
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010526 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010527}
10528
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010529void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010530 struct intel_load_detect_pipe *old,
10531 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010532{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010533 struct intel_encoder *intel_encoder =
10534 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010535 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010536 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010537 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010538
Chris Wilsond2dff872011-04-19 08:36:26 +010010539 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010540 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010541 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010542
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010543 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010544 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010545
10546 ret = drm_atomic_commit(state);
10547 if (ret) {
10548 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10549 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010550 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010551}
10552
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010553static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010554 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010555{
10556 struct drm_i915_private *dev_priv = dev->dev_private;
10557 u32 dpll = pipe_config->dpll_hw_state.dpll;
10558
10559 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010560 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010561 else if (HAS_PCH_SPLIT(dev))
10562 return 120000;
10563 else if (!IS_GEN2(dev))
10564 return 96000;
10565 else
10566 return 48000;
10567}
10568
Jesse Barnes79e53942008-11-07 14:24:08 -080010569/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010570static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010571 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010572{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010573 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010574 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010575 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010576 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010577 u32 fp;
10578 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010579 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010580 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010581
10582 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010583 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010584 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010585 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010586
10587 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010588 if (IS_PINEVIEW(dev)) {
10589 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10590 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010591 } else {
10592 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10593 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10594 }
10595
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010596 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010597 if (IS_PINEVIEW(dev))
10598 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10599 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010600 else
10601 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010602 DPLL_FPA01_P1_POST_DIV_SHIFT);
10603
10604 switch (dpll & DPLL_MODE_MASK) {
10605 case DPLLB_MODE_DAC_SERIAL:
10606 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10607 5 : 10;
10608 break;
10609 case DPLLB_MODE_LVDS:
10610 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10611 7 : 14;
10612 break;
10613 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010614 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010616 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010617 }
10618
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010619 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010620 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010621 else
Imre Deakdccbea32015-06-22 23:35:51 +030010622 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010623 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010624 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010625 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010626
10627 if (is_lvds) {
10628 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10629 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010630
10631 if (lvds & LVDS_CLKB_POWER_UP)
10632 clock.p2 = 7;
10633 else
10634 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 } else {
10636 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10637 clock.p1 = 2;
10638 else {
10639 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10640 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10641 }
10642 if (dpll & PLL_P2_DIVIDE_BY_4)
10643 clock.p2 = 4;
10644 else
10645 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010646 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010647
Imre Deakdccbea32015-06-22 23:35:51 +030010648 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010649 }
10650
Ville Syrjälä18442d02013-09-13 16:00:08 +030010651 /*
10652 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010653 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010654 * encoder's get_config() function.
10655 */
Imre Deakdccbea32015-06-22 23:35:51 +030010656 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010657}
10658
Ville Syrjälä6878da02013-09-13 15:59:11 +030010659int intel_dotclock_calculate(int link_freq,
10660 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010661{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010662 /*
10663 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010664 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010665 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010666 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010667 *
10668 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010669 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010670 */
10671
Ville Syrjälä6878da02013-09-13 15:59:11 +030010672 if (!m_n->link_n)
10673 return 0;
10674
10675 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10676}
10677
Ville Syrjälä18442d02013-09-13 16:00:08 +030010678static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010679 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010680{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010682
10683 /* read out port_clock from the DPLL */
10684 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010685
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010686 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010687 * In case there is an active pipe without active ports,
10688 * we may need some idea for the dotclock anyway.
10689 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010690 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010691 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010692 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010693 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010694}
10695
10696/** Returns the currently programmed mode of the given pipe. */
10697struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10698 struct drm_crtc *crtc)
10699{
Jesse Barnes548f2452011-02-17 10:40:53 -080010700 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010702 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010703 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010704 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010705 int htot = I915_READ(HTOTAL(cpu_transcoder));
10706 int hsync = I915_READ(HSYNC(cpu_transcoder));
10707 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10708 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010709 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010710
10711 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10712 if (!mode)
10713 return NULL;
10714
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010715 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10716 if (!pipe_config) {
10717 kfree(mode);
10718 return NULL;
10719 }
10720
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010721 /*
10722 * Construct a pipe_config sufficient for getting the clock info
10723 * back out of crtc_clock_get.
10724 *
10725 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10726 * to use a real value here instead.
10727 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010728 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10729 pipe_config->pixel_multiplier = 1;
10730 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10731 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10732 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10733 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010734
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010735 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010736 mode->hdisplay = (htot & 0xffff) + 1;
10737 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10738 mode->hsync_start = (hsync & 0xffff) + 1;
10739 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10740 mode->vdisplay = (vtot & 0xffff) + 1;
10741 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10742 mode->vsync_start = (vsync & 0xffff) + 1;
10743 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10744
10745 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010746
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010747 kfree(pipe_config);
10748
Jesse Barnes79e53942008-11-07 14:24:08 -080010749 return mode;
10750}
10751
Chris Wilsonf047e392012-07-21 12:31:41 +010010752void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010753{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010754 struct drm_i915_private *dev_priv = dev->dev_private;
10755
Chris Wilsonf62a0072014-02-21 17:55:39 +000010756 if (dev_priv->mm.busy)
10757 return;
10758
Paulo Zanoni43694d62014-03-07 20:08:08 -030010759 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010760 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010761 if (INTEL_INFO(dev)->gen >= 6)
10762 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010763 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010764}
10765
10766void intel_mark_idle(struct drm_device *dev)
10767{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010768 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010769
Chris Wilsonf62a0072014-02-21 17:55:39 +000010770 if (!dev_priv->mm.busy)
10771 return;
10772
10773 dev_priv->mm.busy = false;
10774
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010775 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010776 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010777
Paulo Zanoni43694d62014-03-07 20:08:08 -030010778 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010779}
10780
Jesse Barnes79e53942008-11-07 14:24:08 -080010781static void intel_crtc_destroy(struct drm_crtc *crtc)
10782{
10783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010784 struct drm_device *dev = crtc->dev;
10785 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010786
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010787 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010788 work = intel_crtc->unpin_work;
10789 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010790 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010791
10792 if (work) {
10793 cancel_work_sync(&work->work);
10794 kfree(work);
10795 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010796
10797 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010798
Jesse Barnes79e53942008-11-07 14:24:08 -080010799 kfree(intel_crtc);
10800}
10801
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010802static void intel_unpin_work_fn(struct work_struct *__work)
10803{
10804 struct intel_unpin_work *work =
10805 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010806 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10807 struct drm_device *dev = crtc->base.dev;
10808 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010809
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010810 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010811 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010812 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010813
John Harrisonf06cc1b2014-11-24 18:49:37 +000010814 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010815 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010816 mutex_unlock(&dev->struct_mutex);
10817
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010818 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010819 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010820 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010821
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010822 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10823 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010824
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010825 kfree(work);
10826}
10827
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010828static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010829 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010830{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10832 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010833 unsigned long flags;
10834
10835 /* Ignore early vblank irqs */
10836 if (intel_crtc == NULL)
10837 return;
10838
Daniel Vetterf3260382014-09-15 14:55:23 +020010839 /*
10840 * This is called both by irq handlers and the reset code (to complete
10841 * lost pageflips) so needs the full irqsave spinlocks.
10842 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010843 spin_lock_irqsave(&dev->event_lock, flags);
10844 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010845
10846 /* Ensure we don't miss a work->pending update ... */
10847 smp_rmb();
10848
10849 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010850 spin_unlock_irqrestore(&dev->event_lock, flags);
10851 return;
10852 }
10853
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010854 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010855
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010856 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010857}
10858
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010859void intel_finish_page_flip(struct drm_device *dev, int pipe)
10860{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010861 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010862 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10863
Mario Kleiner49b14a52010-12-09 07:00:07 +010010864 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010865}
10866
10867void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10868{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010869 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010870 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10871
Mario Kleiner49b14a52010-12-09 07:00:07 +010010872 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010873}
10874
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010875/* Is 'a' after or equal to 'b'? */
10876static bool g4x_flip_count_after_eq(u32 a, u32 b)
10877{
10878 return !((a - b) & 0x80000000);
10879}
10880
10881static bool page_flip_finished(struct intel_crtc *crtc)
10882{
10883 struct drm_device *dev = crtc->base.dev;
10884 struct drm_i915_private *dev_priv = dev->dev_private;
10885
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010886 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10887 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10888 return true;
10889
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010890 /*
10891 * The relevant registers doen't exist on pre-ctg.
10892 * As the flip done interrupt doesn't trigger for mmio
10893 * flips on gmch platforms, a flip count check isn't
10894 * really needed there. But since ctg has the registers,
10895 * include it in the check anyway.
10896 */
10897 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10898 return true;
10899
10900 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010901 * BDW signals flip done immediately if the plane
10902 * is disabled, even if the plane enable is already
10903 * armed to occur at the next vblank :(
10904 */
10905
10906 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010907 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10908 * used the same base address. In that case the mmio flip might
10909 * have completed, but the CS hasn't even executed the flip yet.
10910 *
10911 * A flip count check isn't enough as the CS might have updated
10912 * the base address just after start of vblank, but before we
10913 * managed to process the interrupt. This means we'd complete the
10914 * CS flip too soon.
10915 *
10916 * Combining both checks should get us a good enough result. It may
10917 * still happen that the CS flip has been executed, but has not
10918 * yet actually completed. But in case the base address is the same
10919 * anyway, we don't really care.
10920 */
10921 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10922 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010923 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010924 crtc->unpin_work->flip_count);
10925}
10926
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010927void intel_prepare_page_flip(struct drm_device *dev, int plane)
10928{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010929 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930 struct intel_crtc *intel_crtc =
10931 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10932 unsigned long flags;
10933
Daniel Vetterf3260382014-09-15 14:55:23 +020010934
10935 /*
10936 * This is called both by irq handlers and the reset code (to complete
10937 * lost pageflips) so needs the full irqsave spinlocks.
10938 *
10939 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010940 * generate a page-flip completion irq, i.e. every modeset
10941 * is also accompanied by a spurious intel_prepare_page_flip().
10942 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010943 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010944 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010945 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946 spin_unlock_irqrestore(&dev->event_lock, flags);
10947}
10948
Chris Wilson60426392015-10-10 10:44:32 +010010949static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010950{
10951 /* Ensure that the work item is consistent when activating it ... */
10952 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010953 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010954 /* and that it is marked active as soon as the irq could fire. */
10955 smp_wmb();
10956}
10957
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010958static int intel_gen2_queue_flip(struct drm_device *dev,
10959 struct drm_crtc *crtc,
10960 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010961 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010962 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010963 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010964{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010965 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010967 u32 flip_mask;
10968 int ret;
10969
John Harrison5fb9de12015-05-29 17:44:07 +010010970 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010972 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010973
10974 /* Can't queue multiple flips, so wait for the previous
10975 * one to finish before executing the next.
10976 */
10977 if (intel_crtc->plane)
10978 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10979 else
10980 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010981 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10982 intel_ring_emit(engine, MI_NOOP);
10983 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010984 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010985 intel_ring_emit(engine, fb->pitches[0]);
10986 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10987 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010988
Chris Wilson60426392015-10-10 10:44:32 +010010989 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010990 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010991}
10992
10993static int intel_gen3_queue_flip(struct drm_device *dev,
10994 struct drm_crtc *crtc,
10995 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010996 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010997 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010998 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010999{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011000 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011002 u32 flip_mask;
11003 int ret;
11004
John Harrison5fb9de12015-05-29 17:44:07 +010011005 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011006 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011007 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011008
11009 if (intel_crtc->plane)
11010 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11011 else
11012 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011013 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11014 intel_ring_emit(engine, MI_NOOP);
11015 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011016 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011017 intel_ring_emit(engine, fb->pitches[0]);
11018 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11019 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020
Chris Wilson60426392015-10-10 10:44:32 +010011021 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011022 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023}
11024
11025static int intel_gen4_queue_flip(struct drm_device *dev,
11026 struct drm_crtc *crtc,
11027 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011028 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011029 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011030 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011031{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011032 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011033 struct drm_i915_private *dev_priv = dev->dev_private;
11034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11035 uint32_t pf, pipesrc;
11036 int ret;
11037
John Harrison5fb9de12015-05-29 17:44:07 +010011038 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011040 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041
11042 /* i965+ uses the linear or tiled offsets from the
11043 * Display Registers (which do not change across a page-flip)
11044 * so we need only reprogram the base address.
11045 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011046 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011047 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011048 intel_ring_emit(engine, fb->pitches[0]);
11049 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011050 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011051
11052 /* XXX Enabling the panel-fitter across page-flip is so far
11053 * untested on non-native modes, so ignore it for now.
11054 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11055 */
11056 pf = 0;
11057 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011058 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011059
Chris Wilson60426392015-10-10 10:44:32 +010011060 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011061 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011062}
11063
11064static int intel_gen6_queue_flip(struct drm_device *dev,
11065 struct drm_crtc *crtc,
11066 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011067 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011068 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011069 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011070{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011071 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072 struct drm_i915_private *dev_priv = dev->dev_private;
11073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11074 uint32_t pf, pipesrc;
11075 int ret;
11076
John Harrison5fb9de12015-05-29 17:44:07 +010011077 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011078 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011079 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011080
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011081 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011082 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011083 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11084 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011085
Chris Wilson99d9acd2012-04-17 20:37:00 +010011086 /* Contrary to the suggestions in the documentation,
11087 * "Enable Panel Fitter" does not seem to be required when page
11088 * flipping with a non-native mode, and worse causes a normal
11089 * modeset to fail.
11090 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11091 */
11092 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011094 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011095
Chris Wilson60426392015-10-10 10:44:32 +010011096 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011097 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011098}
11099
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011100static int intel_gen7_queue_flip(struct drm_device *dev,
11101 struct drm_crtc *crtc,
11102 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011103 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011104 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011105 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011106{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011107 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011109 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011110 int len, ret;
11111
Robin Schroereba905b2014-05-18 02:24:50 +020011112 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011113 case PLANE_A:
11114 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11115 break;
11116 case PLANE_B:
11117 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11118 break;
11119 case PLANE_C:
11120 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11121 break;
11122 default:
11123 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011124 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011125 }
11126
Chris Wilsonffe74d72013-08-26 20:58:12 +010011127 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011128 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011129 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011130 /*
11131 * On Gen 8, SRM is now taking an extra dword to accommodate
11132 * 48bits addresses, and we need a NOOP for the batch size to
11133 * stay even.
11134 */
11135 if (IS_GEN8(dev))
11136 len += 2;
11137 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011138
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011139 /*
11140 * BSpec MI_DISPLAY_FLIP for IVB:
11141 * "The full packet must be contained within the same cache line."
11142 *
11143 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11144 * cacheline, if we ever start emitting more commands before
11145 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11146 * then do the cacheline alignment, and finally emit the
11147 * MI_DISPLAY_FLIP.
11148 */
John Harrisonbba09b12015-05-29 17:44:06 +010011149 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011150 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011151 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011152
John Harrison5fb9de12015-05-29 17:44:07 +010011153 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011154 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011155 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011156
Chris Wilsonffe74d72013-08-26 20:58:12 +010011157 /* Unmask the flip-done completion message. Note that the bspec says that
11158 * we should do this for both the BCS and RCS, and that we must not unmask
11159 * more than one flip event at any time (or ensure that one flip message
11160 * can be sent by waiting for flip-done prior to queueing new flips).
11161 * Experimentation says that BCS works despite DERRMR masking all
11162 * flip-done completion events and that unmasking all planes at once
11163 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11164 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11165 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011166 if (engine->id == RCS) {
11167 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11168 intel_ring_emit_reg(engine, DERRMR);
11169 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11170 DERRMR_PIPEB_PRI_FLIP_DONE |
11171 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011172 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011173 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011174 MI_SRM_LRM_GLOBAL_GTT);
11175 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011176 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011177 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011178 intel_ring_emit_reg(engine, DERRMR);
11179 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011180 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011181 intel_ring_emit(engine, 0);
11182 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011183 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011184 }
11185
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011186 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11187 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11188 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11189 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011190
Chris Wilson60426392015-10-10 10:44:32 +010011191 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011192 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011193}
11194
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011195static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011196 struct drm_i915_gem_object *obj)
11197{
11198 /*
11199 * This is not being used for older platforms, because
11200 * non-availability of flip done interrupt forces us to use
11201 * CS flips. Older platforms derive flip done using some clever
11202 * tricks involving the flip_pending status bits and vblank irqs.
11203 * So using MMIO flips there would disrupt this mechanism.
11204 */
11205
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011206 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011207 return true;
11208
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011209 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011210 return false;
11211
11212 if (i915.use_mmio_flip < 0)
11213 return false;
11214 else if (i915.use_mmio_flip > 0)
11215 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011216 else if (i915.enable_execlists)
11217 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011218 else if (obj->base.dma_buf &&
11219 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11220 false))
11221 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011222 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011223 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011224}
11225
Chris Wilson60426392015-10-10 10:44:32 +010011226static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011227 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011228 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011229{
11230 struct drm_device *dev = intel_crtc->base.dev;
11231 struct drm_i915_private *dev_priv = dev->dev_private;
11232 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011233 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011234 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011235
11236 ctl = I915_READ(PLANE_CTL(pipe, 0));
11237 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011238 switch (fb->modifier[0]) {
11239 case DRM_FORMAT_MOD_NONE:
11240 break;
11241 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011242 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011243 break;
11244 case I915_FORMAT_MOD_Y_TILED:
11245 ctl |= PLANE_CTL_TILED_Y;
11246 break;
11247 case I915_FORMAT_MOD_Yf_TILED:
11248 ctl |= PLANE_CTL_TILED_YF;
11249 break;
11250 default:
11251 MISSING_CASE(fb->modifier[0]);
11252 }
Damien Lespiauff944562014-11-20 14:58:16 +000011253
11254 /*
11255 * The stride is either expressed as a multiple of 64 bytes chunks for
11256 * linear buffers or in number of tiles for tiled buffers.
11257 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011258 if (intel_rotation_90_or_270(rotation)) {
11259 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011260 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011261 stride = DIV_ROUND_UP(fb->height, tile_height);
11262 } else {
11263 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011264 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11265 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011266 }
Damien Lespiauff944562014-11-20 14:58:16 +000011267
11268 /*
11269 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11270 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11271 */
11272 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11273 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11274
Chris Wilson60426392015-10-10 10:44:32 +010011275 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011276 POSTING_READ(PLANE_SURF(pipe, 0));
11277}
11278
Chris Wilson60426392015-10-10 10:44:32 +010011279static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11280 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011281{
11282 struct drm_device *dev = intel_crtc->base.dev;
11283 struct drm_i915_private *dev_priv = dev->dev_private;
11284 struct intel_framebuffer *intel_fb =
11285 to_intel_framebuffer(intel_crtc->base.primary->fb);
11286 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011287 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011288 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011289
Sourab Gupta84c33a62014-06-02 16:47:17 +053011290 dspcntr = I915_READ(reg);
11291
Damien Lespiauc5d97472014-10-25 00:11:11 +010011292 if (obj->tiling_mode != I915_TILING_NONE)
11293 dspcntr |= DISPPLANE_TILED;
11294 else
11295 dspcntr &= ~DISPPLANE_TILED;
11296
Sourab Gupta84c33a62014-06-02 16:47:17 +053011297 I915_WRITE(reg, dspcntr);
11298
Chris Wilson60426392015-10-10 10:44:32 +010011299 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011300 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011301}
11302
11303/*
11304 * XXX: This is the temporary way to update the plane registers until we get
11305 * around to using the usual plane update functions for MMIO flips
11306 */
Chris Wilson60426392015-10-10 10:44:32 +010011307static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011308{
Chris Wilson60426392015-10-10 10:44:32 +010011309 struct intel_crtc *crtc = mmio_flip->crtc;
11310 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011311
Chris Wilson60426392015-10-10 10:44:32 +010011312 spin_lock_irq(&crtc->base.dev->event_lock);
11313 work = crtc->unpin_work;
11314 spin_unlock_irq(&crtc->base.dev->event_lock);
11315 if (work == NULL)
11316 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011317
Chris Wilson60426392015-10-10 10:44:32 +010011318 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011319
Chris Wilson60426392015-10-10 10:44:32 +010011320 intel_pipe_update_start(crtc);
11321
11322 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011323 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011324 else
11325 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011326 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011327
Chris Wilson60426392015-10-10 10:44:32 +010011328 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011329}
11330
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011331static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011332{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011333 struct intel_mmio_flip *mmio_flip =
11334 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011335 struct intel_framebuffer *intel_fb =
11336 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11337 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338
Chris Wilson60426392015-10-10 10:44:32 +010011339 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011340 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011341 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011342 false, NULL,
11343 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011344 i915_gem_request_unreference__unlocked(mmio_flip->req);
11345 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011346
Alex Goinsfd8e0582015-11-25 18:43:38 -080011347 /* For framebuffer backed by dmabuf, wait for fence */
11348 if (obj->base.dma_buf)
11349 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11350 false, false,
11351 MAX_SCHEDULE_TIMEOUT) < 0);
11352
Chris Wilson60426392015-10-10 10:44:32 +010011353 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011354 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011355}
11356
11357static int intel_queue_mmio_flip(struct drm_device *dev,
11358 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011359 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011360{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011361 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011362
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011363 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11364 if (mmio_flip == NULL)
11365 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011366
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011367 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011368 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011369 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011370 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011371
11372 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11373 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011374
Sourab Gupta84c33a62014-06-02 16:47:17 +053011375 return 0;
11376}
11377
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011378static int intel_default_queue_flip(struct drm_device *dev,
11379 struct drm_crtc *crtc,
11380 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011381 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011382 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011383 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011384{
11385 return -ENODEV;
11386}
11387
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011388static bool __intel_pageflip_stall_check(struct drm_device *dev,
11389 struct drm_crtc *crtc)
11390{
11391 struct drm_i915_private *dev_priv = dev->dev_private;
11392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11393 struct intel_unpin_work *work = intel_crtc->unpin_work;
11394 u32 addr;
11395
11396 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11397 return true;
11398
Chris Wilson908565c2015-08-12 13:08:22 +010011399 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11400 return false;
11401
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011402 if (!work->enable_stall_check)
11403 return false;
11404
11405 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011406 if (work->flip_queued_req &&
11407 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011408 return false;
11409
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011410 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011411 }
11412
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011413 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011414 return false;
11415
11416 /* Potential stall - if we see that the flip has happened,
11417 * assume a missed interrupt. */
11418 if (INTEL_INFO(dev)->gen >= 4)
11419 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11420 else
11421 addr = I915_READ(DSPADDR(intel_crtc->plane));
11422
11423 /* There is a potential issue here with a false positive after a flip
11424 * to the same address. We could address this by checking for a
11425 * non-incrementing frame counter.
11426 */
11427 return addr == work->gtt_offset;
11428}
11429
11430void intel_check_page_flip(struct drm_device *dev, int pipe)
11431{
11432 struct drm_i915_private *dev_priv = dev->dev_private;
11433 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11434 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011435 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011436
Dave Gordon6c51d462015-03-06 15:34:26 +000011437 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011438
11439 if (crtc == NULL)
11440 return;
11441
Daniel Vetterf3260382014-09-15 14:55:23 +020011442 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011443 work = intel_crtc->unpin_work;
11444 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011445 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011446 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011447 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011448 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011449 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011450 if (work != NULL &&
11451 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11452 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011453 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011454}
11455
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011456static int intel_crtc_page_flip(struct drm_crtc *crtc,
11457 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011458 struct drm_pending_vblank_event *event,
11459 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011460{
11461 struct drm_device *dev = crtc->dev;
11462 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011463 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011464 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011466 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011467 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011468 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011469 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011470 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011471 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011472 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011473
Matt Roper2ff8fde2014-07-08 07:50:07 -070011474 /*
11475 * drm_mode_page_flip_ioctl() should already catch this, but double
11476 * check to be safe. In the future we may enable pageflipping from
11477 * a disabled primary plane.
11478 */
11479 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11480 return -EBUSY;
11481
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011482 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011483 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011484 return -EINVAL;
11485
11486 /*
11487 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11488 * Note that pitch changes could also affect these register.
11489 */
11490 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011491 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11492 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011493 return -EINVAL;
11494
Chris Wilsonf900db42014-02-20 09:26:13 +000011495 if (i915_terminally_wedged(&dev_priv->gpu_error))
11496 goto out_hang;
11497
Daniel Vetterb14c5672013-09-19 12:18:32 +020011498 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011499 if (work == NULL)
11500 return -ENOMEM;
11501
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011502 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011503 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011504 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011505 INIT_WORK(&work->work, intel_unpin_work_fn);
11506
Daniel Vetter87b6b102014-05-15 15:33:46 +020011507 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011508 if (ret)
11509 goto free_work;
11510
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011511 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011512 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011513 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011514 /* Before declaring the flip queue wedged, check if
11515 * the hardware completed the operation behind our backs.
11516 */
11517 if (__intel_pageflip_stall_check(dev, crtc)) {
11518 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11519 page_flip_completed(intel_crtc);
11520 } else {
11521 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011522 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011523
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011524 drm_crtc_vblank_put(crtc);
11525 kfree(work);
11526 return -EBUSY;
11527 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011528 }
11529 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011530 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011531
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011532 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11533 flush_workqueue(dev_priv->wq);
11534
Jesse Barnes75dfca82010-02-10 15:09:44 -080011535 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011536 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011537 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011538
Matt Roperf4510a22014-04-01 15:22:40 -070011539 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011540 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011541 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011542
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011543 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011544
Chris Wilson89ed88b2015-02-16 14:31:49 +000011545 ret = i915_mutex_lock_interruptible(dev);
11546 if (ret)
11547 goto cleanup;
11548
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011549 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011550 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011551
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011552 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011553 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011554
Wayne Boyer666a4532015-12-09 12:29:35 -080011555 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011556 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011557 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011558 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011559 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011560 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011561 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011562 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011563 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011564 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011565 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011566 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011567 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011568 }
11569
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011570 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011571
11572 /* When using CS flips, we want to emit semaphores between rings.
11573 * However, when using mmio flips we will create a task to do the
11574 * synchronisation, so all we want here is to pin the framebuffer
11575 * into the display plane and skip any waits.
11576 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011577 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011578 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011579 if (ret)
11580 goto cleanup_pending;
11581 }
11582
Ville Syrjälä3465c582016-02-15 22:54:43 +020011583 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011584 if (ret)
11585 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011586
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011587 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11588 obj, 0);
11589 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011590
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011591 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011592 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011593 if (ret)
11594 goto cleanup_unpin;
11595
John Harrisonf06cc1b2014-11-24 18:49:37 +000011596 i915_gem_request_assign(&work->flip_queued_req,
11597 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011598 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011599 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011600 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011601 if (IS_ERR(request)) {
11602 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011603 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011604 }
John Harrison6258fbe2015-05-29 17:43:48 +010011605 }
11606
11607 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011608 page_flip_flags);
11609 if (ret)
11610 goto cleanup_unpin;
11611
John Harrison6258fbe2015-05-29 17:43:48 +010011612 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011613 }
11614
John Harrison91af1272015-06-18 13:14:56 +010011615 if (request)
John Harrison75289872015-05-29 17:43:49 +010011616 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011617
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011618 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011619 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011620
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011621 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011622 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011623 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011624
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011625 intel_frontbuffer_flip_prepare(dev,
11626 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011627
Jesse Barnese5510fa2010-07-01 16:48:37 -070011628 trace_i915_flip_request(intel_crtc->plane, obj);
11629
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011630 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011631
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011632cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011633 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011634cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011635 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011636 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011637 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011638 mutex_unlock(&dev->struct_mutex);
11639cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011640 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011641 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011642
Chris Wilson89ed88b2015-02-16 14:31:49 +000011643 drm_gem_object_unreference_unlocked(&obj->base);
11644 drm_framebuffer_unreference(work->old_fb);
11645
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011646 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011647 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011648 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011649
Daniel Vetter87b6b102014-05-15 15:33:46 +020011650 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011651free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011652 kfree(work);
11653
Chris Wilsonf900db42014-02-20 09:26:13 +000011654 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011655 struct drm_atomic_state *state;
11656 struct drm_plane_state *plane_state;
11657
Chris Wilsonf900db42014-02-20 09:26:13 +000011658out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011659 state = drm_atomic_state_alloc(dev);
11660 if (!state)
11661 return -ENOMEM;
11662 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11663
11664retry:
11665 plane_state = drm_atomic_get_plane_state(state, primary);
11666 ret = PTR_ERR_OR_ZERO(plane_state);
11667 if (!ret) {
11668 drm_atomic_set_fb_for_plane(plane_state, fb);
11669
11670 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11671 if (!ret)
11672 ret = drm_atomic_commit(state);
11673 }
11674
11675 if (ret == -EDEADLK) {
11676 drm_modeset_backoff(state->acquire_ctx);
11677 drm_atomic_state_clear(state);
11678 goto retry;
11679 }
11680
11681 if (ret)
11682 drm_atomic_state_free(state);
11683
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011684 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011685 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011686 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011687 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011688 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011689 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011690 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011691}
11692
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011693
11694/**
11695 * intel_wm_need_update - Check whether watermarks need updating
11696 * @plane: drm plane
11697 * @state: new plane state
11698 *
11699 * Check current plane state versus the new one to determine whether
11700 * watermarks need to be recalculated.
11701 *
11702 * Returns true or false.
11703 */
11704static bool intel_wm_need_update(struct drm_plane *plane,
11705 struct drm_plane_state *state)
11706{
Matt Roperd21fbe82015-09-24 15:53:12 -070011707 struct intel_plane_state *new = to_intel_plane_state(state);
11708 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11709
11710 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011711 if (new->visible != cur->visible)
11712 return true;
11713
11714 if (!cur->base.fb || !new->base.fb)
11715 return false;
11716
11717 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11718 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011719 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11720 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11721 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11722 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011723 return true;
11724
11725 return false;
11726}
11727
Matt Roperd21fbe82015-09-24 15:53:12 -070011728static bool needs_scaling(struct intel_plane_state *state)
11729{
11730 int src_w = drm_rect_width(&state->src) >> 16;
11731 int src_h = drm_rect_height(&state->src) >> 16;
11732 int dst_w = drm_rect_width(&state->dst);
11733 int dst_h = drm_rect_height(&state->dst);
11734
11735 return (src_w != dst_w || src_h != dst_h);
11736}
11737
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011738int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11739 struct drm_plane_state *plane_state)
11740{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011741 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011742 struct drm_crtc *crtc = crtc_state->crtc;
11743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11744 struct drm_plane *plane = plane_state->plane;
11745 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011746 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011747 struct intel_plane_state *old_plane_state =
11748 to_intel_plane_state(plane->state);
11749 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011750 bool mode_changed = needs_modeset(crtc_state);
11751 bool was_crtc_enabled = crtc->state->active;
11752 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011753 bool turn_off, turn_on, visible, was_visible;
11754 struct drm_framebuffer *fb = plane_state->fb;
11755
11756 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11757 plane->type != DRM_PLANE_TYPE_CURSOR) {
11758 ret = skl_update_scaler_plane(
11759 to_intel_crtc_state(crtc_state),
11760 to_intel_plane_state(plane_state));
11761 if (ret)
11762 return ret;
11763 }
11764
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011765 was_visible = old_plane_state->visible;
11766 visible = to_intel_plane_state(plane_state)->visible;
11767
11768 if (!was_crtc_enabled && WARN_ON(was_visible))
11769 was_visible = false;
11770
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011771 /*
11772 * Visibility is calculated as if the crtc was on, but
11773 * after scaler setup everything depends on it being off
11774 * when the crtc isn't active.
11775 */
11776 if (!is_crtc_enabled)
11777 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011778
11779 if (!was_visible && !visible)
11780 return 0;
11781
Maarten Lankhorste8861672016-02-24 11:24:26 +010011782 if (fb != old_plane_state->base.fb)
11783 pipe_config->fb_changed = true;
11784
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011785 turn_off = was_visible && (!visible || mode_changed);
11786 turn_on = visible && (!was_visible || mode_changed);
11787
11788 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11789 plane->base.id, fb ? fb->base.id : -1);
11790
11791 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11792 plane->base.id, was_visible, visible,
11793 turn_off, turn_on, mode_changed);
11794
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011795 if (turn_on) {
11796 pipe_config->update_wm_pre = true;
11797
11798 /* must disable cxsr around plane enable/disable */
11799 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11800 pipe_config->disable_cxsr = true;
11801 } else if (turn_off) {
11802 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011803
Ville Syrjälä852eb002015-06-24 22:00:07 +030011804 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011805 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011806 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011807 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011808 /* FIXME bollocks */
11809 pipe_config->update_wm_pre = true;
11810 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011811 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011812
Matt Ropered4a6a72016-02-23 17:20:13 -080011813 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011814 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11815 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011816 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11817
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011818 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011819 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011820
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011821 /*
11822 * WaCxSRDisabledForSpriteScaling:ivb
11823 *
11824 * cstate->update_wm was already set above, so this flag will
11825 * take effect when we commit and program watermarks.
11826 */
11827 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11828 needs_scaling(to_intel_plane_state(plane_state)) &&
11829 !needs_scaling(old_plane_state))
11830 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011831
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011832 return 0;
11833}
11834
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011835static bool encoders_cloneable(const struct intel_encoder *a,
11836 const struct intel_encoder *b)
11837{
11838 /* masks could be asymmetric, so check both ways */
11839 return a == b || (a->cloneable & (1 << b->type) &&
11840 b->cloneable & (1 << a->type));
11841}
11842
11843static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11844 struct intel_crtc *crtc,
11845 struct intel_encoder *encoder)
11846{
11847 struct intel_encoder *source_encoder;
11848 struct drm_connector *connector;
11849 struct drm_connector_state *connector_state;
11850 int i;
11851
11852 for_each_connector_in_state(state, connector, connector_state, i) {
11853 if (connector_state->crtc != &crtc->base)
11854 continue;
11855
11856 source_encoder =
11857 to_intel_encoder(connector_state->best_encoder);
11858 if (!encoders_cloneable(encoder, source_encoder))
11859 return false;
11860 }
11861
11862 return true;
11863}
11864
11865static bool check_encoder_cloning(struct drm_atomic_state *state,
11866 struct intel_crtc *crtc)
11867{
11868 struct intel_encoder *encoder;
11869 struct drm_connector *connector;
11870 struct drm_connector_state *connector_state;
11871 int i;
11872
11873 for_each_connector_in_state(state, connector, connector_state, i) {
11874 if (connector_state->crtc != &crtc->base)
11875 continue;
11876
11877 encoder = to_intel_encoder(connector_state->best_encoder);
11878 if (!check_single_encoder_cloning(state, crtc, encoder))
11879 return false;
11880 }
11881
11882 return true;
11883}
11884
11885static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11886 struct drm_crtc_state *crtc_state)
11887{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011888 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011889 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011891 struct intel_crtc_state *pipe_config =
11892 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011893 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011894 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011895 bool mode_changed = needs_modeset(crtc_state);
11896
11897 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11898 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11899 return -EINVAL;
11900 }
11901
Ville Syrjälä852eb002015-06-24 22:00:07 +030011902 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011903 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011904
Maarten Lankhorstad421372015-06-15 12:33:42 +020011905 if (mode_changed && crtc_state->enable &&
11906 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011907 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011908 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11909 pipe_config);
11910 if (ret)
11911 return ret;
11912 }
11913
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011914 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011915 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011916 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011917 if (ret) {
11918 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011919 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011920 }
11921 }
11922
11923 if (dev_priv->display.compute_intermediate_wm &&
11924 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11925 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11926 return 0;
11927
11928 /*
11929 * Calculate 'intermediate' watermarks that satisfy both the
11930 * old state and the new state. We can program these
11931 * immediately.
11932 */
11933 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11934 intel_crtc,
11935 pipe_config);
11936 if (ret) {
11937 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11938 return ret;
11939 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011940 }
11941
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011942 if (INTEL_INFO(dev)->gen >= 9) {
11943 if (mode_changed)
11944 ret = skl_update_scaler_crtc(pipe_config);
11945
11946 if (!ret)
11947 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11948 pipe_config);
11949 }
11950
11951 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011952}
11953
Jani Nikula65b38e02015-04-13 11:26:56 +030011954static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011955 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000011956 .load_lut = intel_color_load_luts,
Matt Roperea2c67b2014-12-23 10:41:52 -080011957 .atomic_begin = intel_begin_crtc_commit,
11958 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011959 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011960};
11961
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011962static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11963{
11964 struct intel_connector *connector;
11965
11966 for_each_intel_connector(dev, connector) {
11967 if (connector->base.encoder) {
11968 connector->base.state->best_encoder =
11969 connector->base.encoder;
11970 connector->base.state->crtc =
11971 connector->base.encoder->crtc;
11972 } else {
11973 connector->base.state->best_encoder = NULL;
11974 connector->base.state->crtc = NULL;
11975 }
11976 }
11977}
11978
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011979static void
Robin Schroereba905b2014-05-18 02:24:50 +020011980connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011981 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011982{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011983 int bpp = pipe_config->pipe_bpp;
11984
11985 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11986 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011987 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011988
11989 /* Don't use an invalid EDID bpc value */
11990 if (connector->base.display_info.bpc &&
11991 connector->base.display_info.bpc * 3 < bpp) {
11992 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11993 bpp, connector->base.display_info.bpc*3);
11994 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11995 }
11996
Jani Nikula013dd9e2016-01-13 16:35:20 +020011997 /* Clamp bpp to default limit on screens without EDID 1.4 */
11998 if (connector->base.display_info.bpc == 0) {
11999 int type = connector->base.connector_type;
12000 int clamp_bpp = 24;
12001
12002 /* Fall back to 18 bpp when DP sink capability is unknown. */
12003 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12004 type == DRM_MODE_CONNECTOR_eDP)
12005 clamp_bpp = 18;
12006
12007 if (bpp > clamp_bpp) {
12008 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12009 bpp, clamp_bpp);
12010 pipe_config->pipe_bpp = clamp_bpp;
12011 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012012 }
12013}
12014
12015static int
12016compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012017 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012018{
12019 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012020 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012021 struct drm_connector *connector;
12022 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012023 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012024
Wayne Boyer666a4532015-12-09 12:29:35 -080012025 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012026 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012027 else if (INTEL_INFO(dev)->gen >= 5)
12028 bpp = 12*3;
12029 else
12030 bpp = 8*3;
12031
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012032
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012033 pipe_config->pipe_bpp = bpp;
12034
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012035 state = pipe_config->base.state;
12036
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012037 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012038 for_each_connector_in_state(state, connector, connector_state, i) {
12039 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012040 continue;
12041
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012042 connected_sink_compute_bpp(to_intel_connector(connector),
12043 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012044 }
12045
12046 return bpp;
12047}
12048
Daniel Vetter644db712013-09-19 14:53:58 +020012049static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12050{
12051 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12052 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012053 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012054 mode->crtc_hdisplay, mode->crtc_hsync_start,
12055 mode->crtc_hsync_end, mode->crtc_htotal,
12056 mode->crtc_vdisplay, mode->crtc_vsync_start,
12057 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12058}
12059
Daniel Vetterc0b03412013-05-28 12:05:54 +020012060static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012061 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012062 const char *context)
12063{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012064 struct drm_device *dev = crtc->base.dev;
12065 struct drm_plane *plane;
12066 struct intel_plane *intel_plane;
12067 struct intel_plane_state *state;
12068 struct drm_framebuffer *fb;
12069
12070 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12071 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012072
Jani Nikulada205632016-03-15 21:51:10 +020012073 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012074 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12075 pipe_config->pipe_bpp, pipe_config->dither);
12076 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12077 pipe_config->has_pch_encoder,
12078 pipe_config->fdi_lanes,
12079 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12080 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12081 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012082 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 +030012083 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012084 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012085 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12086 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12087 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012088
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012089 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 -070012090 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012091 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012092 pipe_config->dp_m2_n2.gmch_m,
12093 pipe_config->dp_m2_n2.gmch_n,
12094 pipe_config->dp_m2_n2.link_m,
12095 pipe_config->dp_m2_n2.link_n,
12096 pipe_config->dp_m2_n2.tu);
12097
Daniel Vetter55072d12014-11-20 16:10:28 +010012098 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12099 pipe_config->has_audio,
12100 pipe_config->has_infoframe);
12101
Daniel Vetterc0b03412013-05-28 12:05:54 +020012102 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012103 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012104 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012105 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12106 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012107 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012108 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12109 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012110 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12111 crtc->num_scalers,
12112 pipe_config->scaler_state.scaler_users,
12113 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012114 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12115 pipe_config->gmch_pfit.control,
12116 pipe_config->gmch_pfit.pgm_ratios,
12117 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012118 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012119 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012120 pipe_config->pch_pfit.size,
12121 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012122 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012123 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012124
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012125 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012126 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012127 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012128 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012129 pipe_config->ddi_pll_sel,
12130 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012131 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012132 pipe_config->dpll_hw_state.pll0,
12133 pipe_config->dpll_hw_state.pll1,
12134 pipe_config->dpll_hw_state.pll2,
12135 pipe_config->dpll_hw_state.pll3,
12136 pipe_config->dpll_hw_state.pll6,
12137 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012138 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012139 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012140 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012141 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012142 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12143 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12144 pipe_config->ddi_pll_sel,
12145 pipe_config->dpll_hw_state.ctrl1,
12146 pipe_config->dpll_hw_state.cfgcr1,
12147 pipe_config->dpll_hw_state.cfgcr2);
12148 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012149 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 +010012150 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012151 pipe_config->dpll_hw_state.wrpll,
12152 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012153 } else {
12154 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12155 "fp0: 0x%x, fp1: 0x%x\n",
12156 pipe_config->dpll_hw_state.dpll,
12157 pipe_config->dpll_hw_state.dpll_md,
12158 pipe_config->dpll_hw_state.fp0,
12159 pipe_config->dpll_hw_state.fp1);
12160 }
12161
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012162 DRM_DEBUG_KMS("planes on this crtc\n");
12163 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12164 intel_plane = to_intel_plane(plane);
12165 if (intel_plane->pipe != crtc->pipe)
12166 continue;
12167
12168 state = to_intel_plane_state(plane->state);
12169 fb = state->base.fb;
12170 if (!fb) {
12171 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12172 "disabled, scaler_id = %d\n",
12173 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12174 plane->base.id, intel_plane->pipe,
12175 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12176 drm_plane_index(plane), state->scaler_id);
12177 continue;
12178 }
12179
12180 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12181 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12182 plane->base.id, intel_plane->pipe,
12183 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12184 drm_plane_index(plane));
12185 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12186 fb->base.id, fb->width, fb->height, fb->pixel_format);
12187 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12188 state->scaler_id,
12189 state->src.x1 >> 16, state->src.y1 >> 16,
12190 drm_rect_width(&state->src) >> 16,
12191 drm_rect_height(&state->src) >> 16,
12192 state->dst.x1, state->dst.y1,
12193 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12194 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012195}
12196
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012197static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012198{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012199 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012200 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012201 unsigned int used_ports = 0;
12202
12203 /*
12204 * Walk the connector list instead of the encoder
12205 * list to detect the problem on ddi platforms
12206 * where there's just one encoder per digital port.
12207 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012208 drm_for_each_connector(connector, dev) {
12209 struct drm_connector_state *connector_state;
12210 struct intel_encoder *encoder;
12211
12212 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12213 if (!connector_state)
12214 connector_state = connector->state;
12215
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012216 if (!connector_state->best_encoder)
12217 continue;
12218
12219 encoder = to_intel_encoder(connector_state->best_encoder);
12220
12221 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012222
12223 switch (encoder->type) {
12224 unsigned int port_mask;
12225 case INTEL_OUTPUT_UNKNOWN:
12226 if (WARN_ON(!HAS_DDI(dev)))
12227 break;
12228 case INTEL_OUTPUT_DISPLAYPORT:
12229 case INTEL_OUTPUT_HDMI:
12230 case INTEL_OUTPUT_EDP:
12231 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12232
12233 /* the same port mustn't appear more than once */
12234 if (used_ports & port_mask)
12235 return false;
12236
12237 used_ports |= port_mask;
12238 default:
12239 break;
12240 }
12241 }
12242
12243 return true;
12244}
12245
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012246static void
12247clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12248{
12249 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012250 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012251 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012252 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012253 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012254 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012255
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012256 /* FIXME: before the switch to atomic started, a new pipe_config was
12257 * kzalloc'd. Code that depends on any field being zero should be
12258 * fixed, so that the crtc_state can be safely duplicated. For now,
12259 * only fields that are know to not cause problems are preserved. */
12260
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012261 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012262 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012263 shared_dpll = crtc_state->shared_dpll;
12264 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012265 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012266 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012267
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012268 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012269
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012270 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012271 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012272 crtc_state->shared_dpll = shared_dpll;
12273 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012274 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012275 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012276}
12277
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012278static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012279intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012280 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012281{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012282 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012283 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012284 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012285 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012286 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012287 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012288 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012289
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012290 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012291
Daniel Vettere143a212013-07-04 12:01:15 +020012292 pipe_config->cpu_transcoder =
12293 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012294
Imre Deak2960bc92013-07-30 13:36:32 +030012295 /*
12296 * Sanitize sync polarity flags based on requested ones. If neither
12297 * positive or negative polarity is requested, treat this as meaning
12298 * negative polarity.
12299 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012300 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012301 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012302 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012303
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012304 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012305 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012306 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012307
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012308 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12309 pipe_config);
12310 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012311 goto fail;
12312
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012313 /*
12314 * Determine the real pipe dimensions. Note that stereo modes can
12315 * increase the actual pipe size due to the frame doubling and
12316 * insertion of additional space for blanks between the frame. This
12317 * is stored in the crtc timings. We use the requested mode to do this
12318 * computation to clearly distinguish it from the adjusted mode, which
12319 * can be changed by the connectors in the below retry loop.
12320 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012321 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012322 &pipe_config->pipe_src_w,
12323 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012324
Daniel Vettere29c22c2013-02-21 00:00:16 +010012325encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012326 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012327 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012328 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012329
Daniel Vetter135c81b2013-07-21 21:37:09 +020012330 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012331 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12332 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012333
Daniel Vetter7758a112012-07-08 19:40:39 +020012334 /* Pass our mode to the connectors and the CRTC to give them a chance to
12335 * adjust it according to limitations or connector properties, and also
12336 * a chance to reject the mode entirely.
12337 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012338 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012339 if (connector_state->crtc != crtc)
12340 continue;
12341
12342 encoder = to_intel_encoder(connector_state->best_encoder);
12343
Daniel Vetterefea6e82013-07-21 21:36:59 +020012344 if (!(encoder->compute_config(encoder, pipe_config))) {
12345 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012346 goto fail;
12347 }
12348 }
12349
Daniel Vetterff9a6752013-06-01 17:16:21 +020012350 /* Set default port clock if not overwritten by the encoder. Needs to be
12351 * done afterwards in case the encoder adjusts the mode. */
12352 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012353 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012354 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012355
Daniel Vettera43f6e02013-06-07 23:10:32 +020012356 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012357 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012358 DRM_DEBUG_KMS("CRTC fixup failed\n");
12359 goto fail;
12360 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012361
12362 if (ret == RETRY) {
12363 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12364 ret = -EINVAL;
12365 goto fail;
12366 }
12367
12368 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12369 retry = false;
12370 goto encoder_retry;
12371 }
12372
Daniel Vettere8fa4272015-08-12 11:43:34 +020012373 /* Dithering seems to not pass-through bits correctly when it should, so
12374 * only enable it on 6bpc panels. */
12375 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012376 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012377 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012378
Daniel Vetter7758a112012-07-08 19:40:39 +020012379fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012380 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012381}
12382
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012383static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012384intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012385{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012386 struct drm_crtc *crtc;
12387 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012388 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012389
Ville Syrjälä76688512014-01-10 11:28:06 +020012390 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012391 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012392 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012393
12394 /* Update hwmode for vblank functions */
12395 if (crtc->state->active)
12396 crtc->hwmode = crtc->state->adjusted_mode;
12397 else
12398 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012399
12400 /*
12401 * Update legacy state to satisfy fbc code. This can
12402 * be removed when fbc uses the atomic state.
12403 */
12404 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12405 struct drm_plane_state *plane_state = crtc->primary->state;
12406
12407 crtc->primary->fb = plane_state->fb;
12408 crtc->x = plane_state->src_x >> 16;
12409 crtc->y = plane_state->src_y >> 16;
12410 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012411 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012412}
12413
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012414static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012415{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012416 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012417
12418 if (clock1 == clock2)
12419 return true;
12420
12421 if (!clock1 || !clock2)
12422 return false;
12423
12424 diff = abs(clock1 - clock2);
12425
12426 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12427 return true;
12428
12429 return false;
12430}
12431
Daniel Vetter25c5b262012-07-08 22:08:04 +020012432#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12433 list_for_each_entry((intel_crtc), \
12434 &(dev)->mode_config.crtc_list, \
12435 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012436 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012437
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012438static bool
12439intel_compare_m_n(unsigned int m, unsigned int n,
12440 unsigned int m2, unsigned int n2,
12441 bool exact)
12442{
12443 if (m == m2 && n == n2)
12444 return true;
12445
12446 if (exact || !m || !n || !m2 || !n2)
12447 return false;
12448
12449 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12450
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012451 if (n > n2) {
12452 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012453 m2 <<= 1;
12454 n2 <<= 1;
12455 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012456 } else if (n < n2) {
12457 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012458 m <<= 1;
12459 n <<= 1;
12460 }
12461 }
12462
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012463 if (n != n2)
12464 return false;
12465
12466 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012467}
12468
12469static bool
12470intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12471 struct intel_link_m_n *m2_n2,
12472 bool adjust)
12473{
12474 if (m_n->tu == m2_n2->tu &&
12475 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12476 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12477 intel_compare_m_n(m_n->link_m, m_n->link_n,
12478 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12479 if (adjust)
12480 *m2_n2 = *m_n;
12481
12482 return true;
12483 }
12484
12485 return false;
12486}
12487
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012488static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012489intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012490 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012491 struct intel_crtc_state *pipe_config,
12492 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012493{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012494 bool ret = true;
12495
12496#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12497 do { \
12498 if (!adjust) \
12499 DRM_ERROR(fmt, ##__VA_ARGS__); \
12500 else \
12501 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12502 } while (0)
12503
Daniel Vetter66e985c2013-06-05 13:34:20 +020012504#define PIPE_CONF_CHECK_X(name) \
12505 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012506 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012507 "(expected 0x%08x, found 0x%08x)\n", \
12508 current_config->name, \
12509 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012510 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012511 }
12512
Daniel Vetter08a24032013-04-19 11:25:34 +020012513#define PIPE_CONF_CHECK_I(name) \
12514 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012515 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012516 "(expected %i, found %i)\n", \
12517 current_config->name, \
12518 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012519 ret = false; \
12520 }
12521
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012522#define PIPE_CONF_CHECK_P(name) \
12523 if (current_config->name != pipe_config->name) { \
12524 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12525 "(expected %p, found %p)\n", \
12526 current_config->name, \
12527 pipe_config->name); \
12528 ret = false; \
12529 }
12530
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531#define PIPE_CONF_CHECK_M_N(name) \
12532 if (!intel_compare_link_m_n(&current_config->name, \
12533 &pipe_config->name,\
12534 adjust)) { \
12535 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12536 "(expected tu %i gmch %i/%i link %i/%i, " \
12537 "found tu %i, gmch %i/%i link %i/%i)\n", \
12538 current_config->name.tu, \
12539 current_config->name.gmch_m, \
12540 current_config->name.gmch_n, \
12541 current_config->name.link_m, \
12542 current_config->name.link_n, \
12543 pipe_config->name.tu, \
12544 pipe_config->name.gmch_m, \
12545 pipe_config->name.gmch_n, \
12546 pipe_config->name.link_m, \
12547 pipe_config->name.link_n); \
12548 ret = false; \
12549 }
12550
12551#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12552 if (!intel_compare_link_m_n(&current_config->name, \
12553 &pipe_config->name, adjust) && \
12554 !intel_compare_link_m_n(&current_config->alt_name, \
12555 &pipe_config->name, adjust)) { \
12556 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12557 "(expected tu %i gmch %i/%i link %i/%i, " \
12558 "or tu %i gmch %i/%i link %i/%i, " \
12559 "found tu %i, gmch %i/%i link %i/%i)\n", \
12560 current_config->name.tu, \
12561 current_config->name.gmch_m, \
12562 current_config->name.gmch_n, \
12563 current_config->name.link_m, \
12564 current_config->name.link_n, \
12565 current_config->alt_name.tu, \
12566 current_config->alt_name.gmch_m, \
12567 current_config->alt_name.gmch_n, \
12568 current_config->alt_name.link_m, \
12569 current_config->alt_name.link_n, \
12570 pipe_config->name.tu, \
12571 pipe_config->name.gmch_m, \
12572 pipe_config->name.gmch_n, \
12573 pipe_config->name.link_m, \
12574 pipe_config->name.link_n); \
12575 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012576 }
12577
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012578/* This is required for BDW+ where there is only one set of registers for
12579 * switching between high and low RR.
12580 * This macro can be used whenever a comparison has to be made between one
12581 * hw state and multiple sw state variables.
12582 */
12583#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12584 if ((current_config->name != pipe_config->name) && \
12585 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012586 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012587 "(expected %i or %i, found %i)\n", \
12588 current_config->name, \
12589 current_config->alt_name, \
12590 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012591 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012592 }
12593
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012594#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12595 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012596 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012597 "(expected %i, found %i)\n", \
12598 current_config->name & (mask), \
12599 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012600 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012601 }
12602
Ville Syrjälä5e550652013-09-06 23:29:07 +030012603#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12604 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012605 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012606 "(expected %i, found %i)\n", \
12607 current_config->name, \
12608 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012609 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012610 }
12611
Daniel Vetterbb760062013-06-06 14:55:52 +020012612#define PIPE_CONF_QUIRK(quirk) \
12613 ((current_config->quirks | pipe_config->quirks) & (quirk))
12614
Daniel Vettereccb1402013-05-22 00:50:22 +020012615 PIPE_CONF_CHECK_I(cpu_transcoder);
12616
Daniel Vetter08a24032013-04-19 11:25:34 +020012617 PIPE_CONF_CHECK_I(has_pch_encoder);
12618 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012619 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012620
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012621 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012622 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012623
12624 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012625 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012626
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012627 if (current_config->has_drrs)
12628 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12629 } else
12630 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012631
Jani Nikulaa65347b2015-11-27 12:21:46 +020012632 PIPE_CONF_CHECK_I(has_dsi_encoder);
12633
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012634 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12635 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12636 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12637 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12638 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12639 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012640
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012641 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12642 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12643 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012647
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012648 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012649 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012650 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012651 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012652 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012653 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012654
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012655 PIPE_CONF_CHECK_I(has_audio);
12656
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012657 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012658 DRM_MODE_FLAG_INTERLACE);
12659
Daniel Vetterbb760062013-06-06 14:55:52 +020012660 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012661 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012662 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012663 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012664 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012665 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012666 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012668 DRM_MODE_FLAG_NVSYNC);
12669 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012670
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012671 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012672 /* pfit ratios are autocomputed by the hw on gen4+ */
12673 if (INTEL_INFO(dev)->gen < 4)
12674 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012675 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012676
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012677 if (!adjust) {
12678 PIPE_CONF_CHECK_I(pipe_src_w);
12679 PIPE_CONF_CHECK_I(pipe_src_h);
12680
12681 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12682 if (current_config->pch_pfit.enabled) {
12683 PIPE_CONF_CHECK_X(pch_pfit.pos);
12684 PIPE_CONF_CHECK_X(pch_pfit.size);
12685 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012686
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012687 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12688 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012689
Jesse Barnese59150d2014-01-07 13:30:45 -080012690 /* BDW+ don't expose a synchronous way to read the state */
12691 if (IS_HASWELL(dev))
12692 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012693
Ville Syrjälä282740f2013-09-04 18:30:03 +030012694 PIPE_CONF_CHECK_I(double_wide);
12695
Daniel Vetter26804af2014-06-25 22:01:55 +030012696 PIPE_CONF_CHECK_X(ddi_pll_sel);
12697
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012698 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012699 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012700 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012701 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12702 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012703 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012704 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012705 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12706 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12707 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012708
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012709 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12710 PIPE_CONF_CHECK_I(pipe_bpp);
12711
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012712 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012713 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012714
Daniel Vetter66e985c2013-06-05 13:34:20 +020012715#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012716#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012717#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012718#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012719#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012720#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012721#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012722#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012723
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012724 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012725}
12726
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012727static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12728 const struct intel_crtc_state *pipe_config)
12729{
12730 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012731 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012732 &pipe_config->fdi_m_n);
12733 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12734
12735 /*
12736 * FDI already provided one idea for the dotclock.
12737 * Yell if the encoder disagrees.
12738 */
12739 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12740 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12741 fdi_dotclock, dotclock);
12742 }
12743}
12744
Damien Lespiau08db6652014-11-04 17:06:52 +000012745static void check_wm_state(struct drm_device *dev)
12746{
12747 struct drm_i915_private *dev_priv = dev->dev_private;
12748 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12749 struct intel_crtc *intel_crtc;
12750 int plane;
12751
12752 if (INTEL_INFO(dev)->gen < 9)
12753 return;
12754
12755 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12756 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12757
12758 for_each_intel_crtc(dev, intel_crtc) {
12759 struct skl_ddb_entry *hw_entry, *sw_entry;
12760 const enum pipe pipe = intel_crtc->pipe;
12761
12762 if (!intel_crtc->active)
12763 continue;
12764
12765 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012766 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012767 hw_entry = &hw_ddb.plane[pipe][plane];
12768 sw_entry = &sw_ddb->plane[pipe][plane];
12769
12770 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12771 continue;
12772
12773 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12774 "(expected (%u,%u), found (%u,%u))\n",
12775 pipe_name(pipe), plane + 1,
12776 sw_entry->start, sw_entry->end,
12777 hw_entry->start, hw_entry->end);
12778 }
12779
12780 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012781 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12782 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012783
12784 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12785 continue;
12786
12787 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12788 "(expected (%u,%u), found (%u,%u))\n",
12789 pipe_name(pipe),
12790 sw_entry->start, sw_entry->end,
12791 hw_entry->start, hw_entry->end);
12792 }
12793}
12794
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012795static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012796check_connector_state(struct drm_device *dev,
12797 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012798{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012799 struct drm_connector_state *old_conn_state;
12800 struct drm_connector *connector;
12801 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012802
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012803 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12804 struct drm_encoder *encoder = connector->encoder;
12805 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012806
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807 /* This also checks the encoder/connector hw state with the
12808 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012809 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012810
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012811 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012812 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012813 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012814}
12815
12816static void
12817check_encoder_state(struct drm_device *dev)
12818{
12819 struct intel_encoder *encoder;
12820 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012821
Damien Lespiaub2784e12014-08-05 11:29:37 +010012822 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012823 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012824 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012825
12826 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12827 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012828 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012829
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012830 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012831 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012832 continue;
12833 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012834
12835 I915_STATE_WARN(connector->base.state->crtc !=
12836 encoder->base.crtc,
12837 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012838 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012839
Rob Clarke2c719b2014-12-15 13:56:32 -050012840 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012841 "encoder's enabled state mismatch "
12842 "(expected %i, found %i)\n",
12843 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012844
12845 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012846 bool active;
12847
12848 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012849 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012850 "encoder detached but still enabled on pipe %c.\n",
12851 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012852 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012853 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012854}
12855
12856static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012857check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012858{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012860 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012861 struct drm_crtc_state *old_crtc_state;
12862 struct drm_crtc *crtc;
12863 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012864
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012865 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12867 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012868 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012869
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012870 if (!needs_modeset(crtc->state) &&
12871 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012872 continue;
12873
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012874 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12875 pipe_config = to_intel_crtc_state(old_crtc_state);
12876 memset(pipe_config, 0, sizeof(*pipe_config));
12877 pipe_config->base.crtc = crtc;
12878 pipe_config->base.state = old_state;
12879
12880 DRM_DEBUG_KMS("[CRTC:%d]\n",
12881 crtc->base.id);
12882
12883 active = dev_priv->display.get_pipe_config(intel_crtc,
12884 pipe_config);
12885
12886 /* hw state is inconsistent with the pipe quirk */
12887 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12888 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12889 active = crtc->state->active;
12890
12891 I915_STATE_WARN(crtc->state->active != active,
12892 "crtc active state doesn't match with hw state "
12893 "(expected %i, found %i)\n", crtc->state->active, active);
12894
12895 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12896 "transitional active state does not match atomic hw state "
12897 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12898
12899 for_each_encoder_on_crtc(dev, crtc, encoder) {
12900 enum pipe pipe;
12901
12902 active = encoder->get_hw_state(encoder, &pipe);
12903 I915_STATE_WARN(active != crtc->state->active,
12904 "[ENCODER:%i] active %i with crtc active %i\n",
12905 encoder->base.base.id, active, crtc->state->active);
12906
12907 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12908 "Encoder connected to wrong pipe %c\n",
12909 pipe_name(pipe));
12910
12911 if (active)
12912 encoder->get_config(encoder, pipe_config);
12913 }
12914
12915 if (!crtc->state->active)
12916 continue;
12917
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012918 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12919
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012920 sw_config = to_intel_crtc_state(crtc->state);
12921 if (!intel_pipe_config_compare(dev, sw_config,
12922 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012923 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012924 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012925 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012926 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012927 "[sw state]");
12928 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012929 }
12930}
12931
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012932static void
12933check_shared_dpll_state(struct drm_device *dev)
12934{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012935 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012936 struct intel_crtc *crtc;
12937 struct intel_dpll_hw_state dpll_hw_state;
12938 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012939
12940 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012941 struct intel_shared_dpll *pll =
12942 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012943 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012944 bool active;
12945
12946 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12947
12948 DRM_DEBUG_KMS("%s\n", pll->name);
12949
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012950 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012951
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012952 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12953 "more active pll users than references: %x vs %x\n",
12954 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012955
12956 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012957 I915_STATE_WARN(!pll->on && pll->active_mask,
12958 "pll in active use but not on in sw tracking\n");
12959 I915_STATE_WARN(pll->on && !pll->active_mask,
12960 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012961 I915_STATE_WARN(pll->on != active,
12962 "pll on state mismatch (expected %i, found %i)\n",
12963 pll->on, active);
12964 }
Daniel Vetter53589012013-06-05 13:34:16 +020012965
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012966 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012967 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012968 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12969 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12970 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012971 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012972
12973 I915_STATE_WARN(pll->active_mask != active_crtcs,
12974 "pll active crtcs mismatch (expected %x, found %x)\n",
12975 pll->active_mask, active_crtcs);
12976 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12977 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12978 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012979
Rob Clarke2c719b2014-12-15 13:56:32 -050012980 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012981 sizeof(dpll_hw_state)),
12982 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012983 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012984}
12985
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012986static void
12987intel_modeset_check_state(struct drm_device *dev,
12988 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012989{
Damien Lespiau08db6652014-11-04 17:06:52 +000012990 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012991 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012992 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012993 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012994 check_shared_dpll_state(dev);
12995}
12996
Ville Syrjälä80715b22014-05-15 20:23:23 +030012997static void update_scanline_offset(struct intel_crtc *crtc)
12998{
12999 struct drm_device *dev = crtc->base.dev;
13000
13001 /*
13002 * The scanline counter increments at the leading edge of hsync.
13003 *
13004 * On most platforms it starts counting from vtotal-1 on the
13005 * first active line. That means the scanline counter value is
13006 * always one less than what we would expect. Ie. just after
13007 * start of vblank, which also occurs at start of hsync (on the
13008 * last active line), the scanline counter will read vblank_start-1.
13009 *
13010 * On gen2 the scanline counter starts counting from 1 instead
13011 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13012 * to keep the value positive), instead of adding one.
13013 *
13014 * On HSW+ the behaviour of the scanline counter depends on the output
13015 * type. For DP ports it behaves like most other platforms, but on HDMI
13016 * there's an extra 1 line difference. So we need to add two instead of
13017 * one to the value.
13018 */
13019 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013020 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013021 int vtotal;
13022
Ville Syrjälä124abe02015-09-08 13:40:45 +030013023 vtotal = adjusted_mode->crtc_vtotal;
13024 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013025 vtotal /= 2;
13026
13027 crtc->scanline_offset = vtotal - 1;
13028 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013029 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013030 crtc->scanline_offset = 2;
13031 } else
13032 crtc->scanline_offset = 1;
13033}
13034
Maarten Lankhorstad421372015-06-15 12:33:42 +020013035static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013036{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013037 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013038 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013039 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013040 struct drm_crtc *crtc;
13041 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013042 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013043
13044 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013045 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013046
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013047 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013049 struct intel_shared_dpll *old_dpll =
13050 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013051
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013052 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013053 continue;
13054
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013055 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013056
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013057 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013058 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013059
Maarten Lankhorstad421372015-06-15 12:33:42 +020013060 if (!shared_dpll)
13061 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13062
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013063 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013064 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013065}
13066
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013067/*
13068 * This implements the workaround described in the "notes" section of the mode
13069 * set sequence documentation. When going from no pipes or single pipe to
13070 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13071 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13072 */
13073static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13074{
13075 struct drm_crtc_state *crtc_state;
13076 struct intel_crtc *intel_crtc;
13077 struct drm_crtc *crtc;
13078 struct intel_crtc_state *first_crtc_state = NULL;
13079 struct intel_crtc_state *other_crtc_state = NULL;
13080 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13081 int i;
13082
13083 /* look at all crtc's that are going to be enabled in during modeset */
13084 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13085 intel_crtc = to_intel_crtc(crtc);
13086
13087 if (!crtc_state->active || !needs_modeset(crtc_state))
13088 continue;
13089
13090 if (first_crtc_state) {
13091 other_crtc_state = to_intel_crtc_state(crtc_state);
13092 break;
13093 } else {
13094 first_crtc_state = to_intel_crtc_state(crtc_state);
13095 first_pipe = intel_crtc->pipe;
13096 }
13097 }
13098
13099 /* No workaround needed? */
13100 if (!first_crtc_state)
13101 return 0;
13102
13103 /* w/a possibly needed, check how many crtc's are already enabled. */
13104 for_each_intel_crtc(state->dev, intel_crtc) {
13105 struct intel_crtc_state *pipe_config;
13106
13107 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13108 if (IS_ERR(pipe_config))
13109 return PTR_ERR(pipe_config);
13110
13111 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13112
13113 if (!pipe_config->base.active ||
13114 needs_modeset(&pipe_config->base))
13115 continue;
13116
13117 /* 2 or more enabled crtcs means no need for w/a */
13118 if (enabled_pipe != INVALID_PIPE)
13119 return 0;
13120
13121 enabled_pipe = intel_crtc->pipe;
13122 }
13123
13124 if (enabled_pipe != INVALID_PIPE)
13125 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13126 else if (other_crtc_state)
13127 other_crtc_state->hsw_workaround_pipe = first_pipe;
13128
13129 return 0;
13130}
13131
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013132static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13133{
13134 struct drm_crtc *crtc;
13135 struct drm_crtc_state *crtc_state;
13136 int ret = 0;
13137
13138 /* add all active pipes to the state */
13139 for_each_crtc(state->dev, crtc) {
13140 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13141 if (IS_ERR(crtc_state))
13142 return PTR_ERR(crtc_state);
13143
13144 if (!crtc_state->active || needs_modeset(crtc_state))
13145 continue;
13146
13147 crtc_state->mode_changed = true;
13148
13149 ret = drm_atomic_add_affected_connectors(state, crtc);
13150 if (ret)
13151 break;
13152
13153 ret = drm_atomic_add_affected_planes(state, crtc);
13154 if (ret)
13155 break;
13156 }
13157
13158 return ret;
13159}
13160
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013161static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013162{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013163 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13164 struct drm_i915_private *dev_priv = state->dev->dev_private;
13165 struct drm_crtc *crtc;
13166 struct drm_crtc_state *crtc_state;
13167 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013168
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013169 if (!check_digital_port_conflicts(state)) {
13170 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13171 return -EINVAL;
13172 }
13173
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013174 intel_state->modeset = true;
13175 intel_state->active_crtcs = dev_priv->active_crtcs;
13176
13177 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13178 if (crtc_state->active)
13179 intel_state->active_crtcs |= 1 << i;
13180 else
13181 intel_state->active_crtcs &= ~(1 << i);
13182 }
13183
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013184 /*
13185 * See if the config requires any additional preparation, e.g.
13186 * to adjust global state with pipes off. We need to do this
13187 * here so we can get the modeset_pipe updated config for the new
13188 * mode set on this crtc. For other crtcs we need to use the
13189 * adjusted_mode bits in the crtc directly.
13190 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013191 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013192 ret = dev_priv->display.modeset_calc_cdclk(state);
13193
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013194 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013195 ret = intel_modeset_all_pipes(state);
13196
13197 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013198 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013199
13200 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13201 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013202 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013203 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013204
Maarten Lankhorstad421372015-06-15 12:33:42 +020013205 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013206
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013207 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013208 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013209
Maarten Lankhorstad421372015-06-15 12:33:42 +020013210 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013211}
13212
Matt Roperaa363132015-09-24 15:53:18 -070013213/*
13214 * Handle calculation of various watermark data at the end of the atomic check
13215 * phase. The code here should be run after the per-crtc and per-plane 'check'
13216 * handlers to ensure that all derived state has been updated.
13217 */
13218static void calc_watermark_data(struct drm_atomic_state *state)
13219{
13220 struct drm_device *dev = state->dev;
13221 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13222 struct drm_crtc *crtc;
13223 struct drm_crtc_state *cstate;
13224 struct drm_plane *plane;
13225 struct drm_plane_state *pstate;
13226
13227 /*
13228 * Calculate watermark configuration details now that derived
13229 * plane/crtc state is all properly updated.
13230 */
13231 drm_for_each_crtc(crtc, dev) {
13232 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13233 crtc->state;
13234
13235 if (cstate->active)
13236 intel_state->wm_config.num_pipes_active++;
13237 }
13238 drm_for_each_legacy_plane(plane, dev) {
13239 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13240 plane->state;
13241
13242 if (!to_intel_plane_state(pstate)->visible)
13243 continue;
13244
13245 intel_state->wm_config.sprites_enabled = true;
13246 if (pstate->crtc_w != pstate->src_w >> 16 ||
13247 pstate->crtc_h != pstate->src_h >> 16)
13248 intel_state->wm_config.sprites_scaled = true;
13249 }
13250}
13251
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013252/**
13253 * intel_atomic_check - validate state object
13254 * @dev: drm device
13255 * @state: state to validate
13256 */
13257static int intel_atomic_check(struct drm_device *dev,
13258 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013259{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013260 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013261 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013262 struct drm_crtc *crtc;
13263 struct drm_crtc_state *crtc_state;
13264 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013265 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013266
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013267 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013268 if (ret)
13269 return ret;
13270
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013271 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013272 struct intel_crtc_state *pipe_config =
13273 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013274
13275 /* Catch I915_MODE_FLAG_INHERITED */
13276 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13277 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013278
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013279 if (!crtc_state->enable) {
13280 if (needs_modeset(crtc_state))
13281 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013282 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013283 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013284
Daniel Vetter26495482015-07-15 14:15:52 +020013285 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013286 continue;
13287
Daniel Vetter26495482015-07-15 14:15:52 +020013288 /* FIXME: For only active_changed we shouldn't need to do any
13289 * state recomputation at all. */
13290
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013291 ret = drm_atomic_add_affected_connectors(state, crtc);
13292 if (ret)
13293 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013294
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013295 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013296 if (ret)
13297 return ret;
13298
Jani Nikula73831232015-11-19 10:26:30 +020013299 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013300 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013301 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013302 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013303 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013304 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013305 }
13306
13307 if (needs_modeset(crtc_state)) {
13308 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013309
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013310 ret = drm_atomic_add_affected_planes(state, crtc);
13311 if (ret)
13312 return ret;
13313 }
13314
Daniel Vetter26495482015-07-15 14:15:52 +020013315 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13316 needs_modeset(crtc_state) ?
13317 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013318 }
13319
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013320 if (any_ms) {
13321 ret = intel_modeset_checks(state);
13322
13323 if (ret)
13324 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013325 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013326 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013327
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013328 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013329 if (ret)
13330 return ret;
13331
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013332 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013333 calc_watermark_data(state);
13334
13335 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013336}
13337
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013338static int intel_atomic_prepare_commit(struct drm_device *dev,
13339 struct drm_atomic_state *state,
13340 bool async)
13341{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013342 struct drm_i915_private *dev_priv = dev->dev_private;
13343 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013344 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013345 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013346 struct drm_crtc *crtc;
13347 int i, ret;
13348
13349 if (async) {
13350 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13351 return -EINVAL;
13352 }
13353
13354 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13355 ret = intel_crtc_wait_for_pending_flips(crtc);
13356 if (ret)
13357 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013358
13359 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13360 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013361 }
13362
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013363 ret = mutex_lock_interruptible(&dev->struct_mutex);
13364 if (ret)
13365 return ret;
13366
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013367 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013368 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13369 u32 reset_counter;
13370
13371 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13372 mutex_unlock(&dev->struct_mutex);
13373
13374 for_each_plane_in_state(state, plane, plane_state, i) {
13375 struct intel_plane_state *intel_plane_state =
13376 to_intel_plane_state(plane_state);
13377
13378 if (!intel_plane_state->wait_req)
13379 continue;
13380
13381 ret = __i915_wait_request(intel_plane_state->wait_req,
13382 reset_counter, true,
13383 NULL, NULL);
13384
13385 /* Swallow -EIO errors to allow updates during hw lockup. */
13386 if (ret == -EIO)
13387 ret = 0;
13388
13389 if (ret)
13390 break;
13391 }
13392
13393 if (!ret)
13394 return 0;
13395
13396 mutex_lock(&dev->struct_mutex);
13397 drm_atomic_helper_cleanup_planes(dev, state);
13398 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013399
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013400 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013401 return ret;
13402}
13403
Maarten Lankhorste8861672016-02-24 11:24:26 +010013404static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13405 struct drm_i915_private *dev_priv,
13406 unsigned crtc_mask)
13407{
13408 unsigned last_vblank_count[I915_MAX_PIPES];
13409 enum pipe pipe;
13410 int ret;
13411
13412 if (!crtc_mask)
13413 return;
13414
13415 for_each_pipe(dev_priv, pipe) {
13416 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13417
13418 if (!((1 << pipe) & crtc_mask))
13419 continue;
13420
13421 ret = drm_crtc_vblank_get(crtc);
13422 if (WARN_ON(ret != 0)) {
13423 crtc_mask &= ~(1 << pipe);
13424 continue;
13425 }
13426
13427 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13428 }
13429
13430 for_each_pipe(dev_priv, pipe) {
13431 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13432 long lret;
13433
13434 if (!((1 << pipe) & crtc_mask))
13435 continue;
13436
13437 lret = wait_event_timeout(dev->vblank[pipe].queue,
13438 last_vblank_count[pipe] !=
13439 drm_crtc_vblank_count(crtc),
13440 msecs_to_jiffies(50));
13441
13442 WARN_ON(!lret);
13443
13444 drm_crtc_vblank_put(crtc);
13445 }
13446}
13447
13448static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13449{
13450 /* fb updated, need to unpin old fb */
13451 if (crtc_state->fb_changed)
13452 return true;
13453
13454 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013455 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013456 return true;
13457
13458 /*
13459 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013460 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013461 * but added for clarity.
13462 */
13463 if (crtc_state->disable_cxsr)
13464 return true;
13465
13466 return false;
13467}
13468
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013469/**
13470 * intel_atomic_commit - commit validated state object
13471 * @dev: DRM device
13472 * @state: the top-level driver state object
13473 * @async: asynchronous commit
13474 *
13475 * This function commits a top-level state object that has been validated
13476 * with drm_atomic_helper_check().
13477 *
13478 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13479 * we can only handle plane-related operations and do not yet support
13480 * asynchronous commit.
13481 *
13482 * RETURNS
13483 * Zero for success or -errno.
13484 */
13485static int intel_atomic_commit(struct drm_device *dev,
13486 struct drm_atomic_state *state,
13487 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013488{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013489 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013490 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013491 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013492 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013493 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013494 int ret = 0, i;
13495 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013496 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013497 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013498
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013499 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013500 if (ret) {
13501 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013502 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013503 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013504
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013505 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013506 dev_priv->wm.config = intel_state->wm_config;
13507 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013508
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013509 if (intel_state->modeset) {
13510 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13511 sizeof(intel_state->min_pixclk));
13512 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013513 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013514
13515 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013516 }
13517
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013518 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13520
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013521 if (needs_modeset(crtc->state) ||
13522 to_intel_crtc_state(crtc->state)->update_pipe) {
13523 hw_check = true;
13524
13525 put_domains[to_intel_crtc(crtc)->pipe] =
13526 modeset_get_crtc_power_domains(crtc,
13527 to_intel_crtc_state(crtc->state));
13528 }
13529
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013530 if (!needs_modeset(crtc->state))
13531 continue;
13532
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013533 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013534
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013535 if (old_crtc_state->active) {
13536 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013537 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013538 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013539 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013540 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013541
13542 /*
13543 * Underruns don't always raise
13544 * interrupts, so check manually.
13545 */
13546 intel_check_cpu_fifo_underruns(dev_priv);
13547 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013548
13549 if (!crtc->state->active)
13550 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013551 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013552 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013553
Daniel Vetterea9d7582012-07-10 10:42:52 +020013554 /* Only after disabling all output pipelines that will be changed can we
13555 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013556 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013557
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013558 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013559 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013560
13561 if (dev_priv->display.modeset_commit_cdclk &&
13562 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13563 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013564 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013565
Daniel Vettera6778b32012-07-02 09:56:42 +020013566 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013567 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13569 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013570 struct intel_crtc_state *pipe_config =
13571 to_intel_crtc_state(crtc->state);
13572 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013573
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013574 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013575 update_scanline_offset(to_intel_crtc(crtc));
13576 dev_priv->display.crtc_enable(crtc);
13577 }
13578
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013579 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013580 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013581
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013582 if (crtc->state->active &&
13583 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013584 intel_fbc_enable(intel_crtc);
13585
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013586 if (crtc->state->active &&
13587 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013588 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013589
Maarten Lankhorste8861672016-02-24 11:24:26 +010013590 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13591 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013592 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013593
Daniel Vettera6778b32012-07-02 09:56:42 +020013594 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013595
Maarten Lankhorste8861672016-02-24 11:24:26 +010013596 if (!state->legacy_cursor_update)
13597 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013598
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013599 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010013600 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorste8861672016-02-24 11:24:26 +010013601
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013602 if (put_domains[i])
13603 modeset_put_power_domains(dev_priv, put_domains[i]);
13604 }
13605
13606 if (intel_state->modeset)
13607 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13608
Matt Ropered4a6a72016-02-23 17:20:13 -080013609 /*
13610 * Now that the vblank has passed, we can go ahead and program the
13611 * optimal watermarks on platforms that need two-step watermark
13612 * programming.
13613 *
13614 * TODO: Move this (and other cleanup) to an async worker eventually.
13615 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013616 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013617 intel_cstate = to_intel_crtc_state(crtc->state);
13618
13619 if (dev_priv->display.optimize_watermarks)
13620 dev_priv->display.optimize_watermarks(intel_cstate);
13621 }
13622
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013623 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013624 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013625 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013626
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013627 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013628 intel_modeset_check_state(dev, state);
13629
13630 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013631
Mika Kuoppala75714942015-12-16 09:26:48 +020013632 /* As one of the primary mmio accessors, KMS has a high likelihood
13633 * of triggering bugs in unclaimed access. After we finish
13634 * modesetting, see if an error has been flagged, and if so
13635 * enable debugging for the next modeset - and hope we catch
13636 * the culprit.
13637 *
13638 * XXX note that we assume display power is on at this point.
13639 * This might hold true now but we need to add pm helper to check
13640 * unclaimed only when the hardware is on, as atomic commits
13641 * can happen also when the device is completely off.
13642 */
13643 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13644
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013645 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013646}
13647
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013648void intel_crtc_restore_mode(struct drm_crtc *crtc)
13649{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013650 struct drm_device *dev = crtc->dev;
13651 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013652 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013653 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013654
13655 state = drm_atomic_state_alloc(dev);
13656 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013657 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013658 crtc->base.id);
13659 return;
13660 }
13661
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013662 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013663
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013664retry:
13665 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13666 ret = PTR_ERR_OR_ZERO(crtc_state);
13667 if (!ret) {
13668 if (!crtc_state->active)
13669 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013670
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013671 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013672 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013673 }
13674
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013675 if (ret == -EDEADLK) {
13676 drm_atomic_state_clear(state);
13677 drm_modeset_backoff(state->acquire_ctx);
13678 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013679 }
13680
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013681 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013682out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013683 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013684}
13685
Daniel Vetter25c5b262012-07-08 22:08:04 +020013686#undef for_each_intel_crtc_masked
13687
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013688static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013689 .gamma_set = intel_color_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013690 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013691 .destroy = intel_crtc_destroy,
13692 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013693 .atomic_duplicate_state = intel_crtc_duplicate_state,
13694 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013695};
13696
Matt Roper6beb8c232014-12-01 15:40:14 -080013697/**
13698 * intel_prepare_plane_fb - Prepare fb for usage on plane
13699 * @plane: drm plane to prepare for
13700 * @fb: framebuffer to prepare for presentation
13701 *
13702 * Prepares a framebuffer for usage on a display plane. Generally this
13703 * involves pinning the underlying object and updating the frontbuffer tracking
13704 * bits. Some older platforms need special physical address handling for
13705 * cursor planes.
13706 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013707 * Must be called with struct_mutex held.
13708 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013709 * Returns 0 on success, negative error code on failure.
13710 */
13711int
13712intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013713 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013714{
13715 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013716 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013717 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013718 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013719 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013720 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013721
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013722 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013723 return 0;
13724
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013725 if (old_obj) {
13726 struct drm_crtc_state *crtc_state =
13727 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13728
13729 /* Big Hammer, we also need to ensure that any pending
13730 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13731 * current scanout is retired before unpinning the old
13732 * framebuffer. Note that we rely on userspace rendering
13733 * into the buffer attached to the pipe they are waiting
13734 * on. If not, userspace generates a GPU hang with IPEHR
13735 * point to the MI_WAIT_FOR_EVENT.
13736 *
13737 * This should only fail upon a hung GPU, in which case we
13738 * can safely continue.
13739 */
13740 if (needs_modeset(crtc_state))
13741 ret = i915_gem_object_wait_rendering(old_obj, true);
13742
13743 /* Swallow -EIO errors to allow updates during hw lockup. */
13744 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013745 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013746 }
13747
Alex Goins3c28ff22015-11-25 18:43:39 -080013748 /* For framebuffer backed by dmabuf, wait for fence */
13749 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013750 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013751
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013752 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13753 false, true,
13754 MAX_SCHEDULE_TIMEOUT);
13755 if (lret == -ERESTARTSYS)
13756 return lret;
13757
13758 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013759 }
13760
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013761 if (!obj) {
13762 ret = 0;
13763 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013764 INTEL_INFO(dev)->cursor_needs_physical) {
13765 int align = IS_I830(dev) ? 16 * 1024 : 256;
13766 ret = i915_gem_object_attach_phys(obj, align);
13767 if (ret)
13768 DRM_DEBUG_KMS("failed to attach phys object\n");
13769 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013770 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013771 }
13772
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013773 if (ret == 0) {
13774 if (obj) {
13775 struct intel_plane_state *plane_state =
13776 to_intel_plane_state(new_state);
13777
13778 i915_gem_request_assign(&plane_state->wait_req,
13779 obj->last_write_req);
13780 }
13781
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013782 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013783 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013784
Matt Roper6beb8c232014-12-01 15:40:14 -080013785 return ret;
13786}
13787
Matt Roper38f3ce32014-12-02 07:45:25 -080013788/**
13789 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13790 * @plane: drm plane to clean up for
13791 * @fb: old framebuffer that was on plane
13792 *
13793 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013794 *
13795 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013796 */
13797void
13798intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013799 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013800{
13801 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013802 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013803 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013804 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13805 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013806
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013807 old_intel_state = to_intel_plane_state(old_state);
13808
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013809 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013810 return;
13811
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013812 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13813 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013814 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013815
13816 /* prepare_fb aborted? */
13817 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13818 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13819 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013820
13821 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013822}
13823
Chandra Konduru6156a452015-04-27 13:48:39 -070013824int
13825skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13826{
13827 int max_scale;
13828 struct drm_device *dev;
13829 struct drm_i915_private *dev_priv;
13830 int crtc_clock, cdclk;
13831
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013832 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013833 return DRM_PLANE_HELPER_NO_SCALING;
13834
13835 dev = intel_crtc->base.dev;
13836 dev_priv = dev->dev_private;
13837 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013838 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013839
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013840 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013841 return DRM_PLANE_HELPER_NO_SCALING;
13842
13843 /*
13844 * skl max scale is lower of:
13845 * close to 3 but not 3, -1 is for that purpose
13846 * or
13847 * cdclk/crtc_clock
13848 */
13849 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13850
13851 return max_scale;
13852}
13853
Matt Roper465c1202014-05-29 08:06:54 -070013854static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013855intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013856 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013857 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013858{
Matt Roper2b875c22014-12-01 15:40:13 -080013859 struct drm_crtc *crtc = state->base.crtc;
13860 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013861 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013862 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13863 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013864
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013865 if (INTEL_INFO(plane->dev)->gen >= 9) {
13866 /* use scaler when colorkey is not required */
13867 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13868 min_scale = 1;
13869 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13870 }
Sonika Jindald8106362015-04-10 14:37:28 +053013871 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013872 }
Sonika Jindald8106362015-04-10 14:37:28 +053013873
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013874 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13875 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013876 min_scale, max_scale,
13877 can_position, true,
13878 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013879}
13880
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013881static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13882 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013883{
13884 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013886 struct intel_crtc_state *old_intel_state =
13887 to_intel_crtc_state(old_crtc_state);
13888 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013889
Matt Roperc34c9ee2014-12-23 10:41:50 -080013890 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013891 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013892
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013893 if (modeset)
13894 return;
13895
13896 if (to_intel_crtc_state(crtc->state)->update_pipe)
13897 intel_update_pipe_config(intel_crtc, old_intel_state);
13898 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013899 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013900}
13901
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013902static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13903 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013904{
Matt Roper32b7eee2014-12-24 07:59:06 -080013905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013906
Maarten Lankhorst62852622015-09-23 16:29:38 +020013907 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013908}
13909
Matt Ropercf4c7c12014-12-04 10:27:42 -080013910/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013911 * intel_plane_destroy - destroy a plane
13912 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013913 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013914 * Common destruction function for all types of planes (primary, cursor,
13915 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013916 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013917void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013918{
13919 struct intel_plane *intel_plane = to_intel_plane(plane);
13920 drm_plane_cleanup(plane);
13921 kfree(intel_plane);
13922}
13923
Matt Roper65a3fea2015-01-21 16:35:42 -080013924const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013925 .update_plane = drm_atomic_helper_update_plane,
13926 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013927 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013928 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013929 .atomic_get_property = intel_plane_atomic_get_property,
13930 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013931 .atomic_duplicate_state = intel_plane_duplicate_state,
13932 .atomic_destroy_state = intel_plane_destroy_state,
13933
Matt Roper465c1202014-05-29 08:06:54 -070013934};
13935
13936static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13937 int pipe)
13938{
13939 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013940 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013941 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013942 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013943
13944 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13945 if (primary == NULL)
13946 return NULL;
13947
Matt Roper8e7d6882015-01-21 16:35:41 -080013948 state = intel_create_plane_state(&primary->base);
13949 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013950 kfree(primary);
13951 return NULL;
13952 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013953 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013954
Matt Roper465c1202014-05-29 08:06:54 -070013955 primary->can_scale = false;
13956 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013957 if (INTEL_INFO(dev)->gen >= 9) {
13958 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013959 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013960 }
Matt Roper465c1202014-05-29 08:06:54 -070013961 primary->pipe = pipe;
13962 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013963 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013964 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013965 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13966 primary->plane = !pipe;
13967
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013968 if (INTEL_INFO(dev)->gen >= 9) {
13969 intel_primary_formats = skl_primary_formats;
13970 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013971
13972 primary->update_plane = skylake_update_primary_plane;
13973 primary->disable_plane = skylake_disable_primary_plane;
13974 } else if (HAS_PCH_SPLIT(dev)) {
13975 intel_primary_formats = i965_primary_formats;
13976 num_formats = ARRAY_SIZE(i965_primary_formats);
13977
13978 primary->update_plane = ironlake_update_primary_plane;
13979 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013980 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013981 intel_primary_formats = i965_primary_formats;
13982 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013983
13984 primary->update_plane = i9xx_update_primary_plane;
13985 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013986 } else {
13987 intel_primary_formats = i8xx_primary_formats;
13988 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013989
13990 primary->update_plane = i9xx_update_primary_plane;
13991 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013992 }
13993
13994 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013995 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013996 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020013997 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053013998
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013999 if (INTEL_INFO(dev)->gen >= 4)
14000 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014001
Matt Roperea2c67b2014-12-23 10:41:52 -080014002 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14003
Matt Roper465c1202014-05-29 08:06:54 -070014004 return &primary->base;
14005}
14006
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014007void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14008{
14009 if (!dev->mode_config.rotation_property) {
14010 unsigned long flags = BIT(DRM_ROTATE_0) |
14011 BIT(DRM_ROTATE_180);
14012
14013 if (INTEL_INFO(dev)->gen >= 9)
14014 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14015
14016 dev->mode_config.rotation_property =
14017 drm_mode_create_rotation_property(dev, flags);
14018 }
14019 if (dev->mode_config.rotation_property)
14020 drm_object_attach_property(&plane->base.base,
14021 dev->mode_config.rotation_property,
14022 plane->base.state->rotation);
14023}
14024
Matt Roper3d7d6512014-06-10 08:28:13 -070014025static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014026intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014027 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014028 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014029{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014030 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014031 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014032 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014033 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014034 unsigned stride;
14035 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014036
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014037 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14038 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014039 DRM_PLANE_HELPER_NO_SCALING,
14040 DRM_PLANE_HELPER_NO_SCALING,
14041 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014042 if (ret)
14043 return ret;
14044
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014045 /* if we want to turn off the cursor ignore width and height */
14046 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014047 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014048
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014049 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014050 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014051 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14052 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014053 return -EINVAL;
14054 }
14055
Matt Roperea2c67b2014-12-23 10:41:52 -080014056 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14057 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014058 DRM_DEBUG_KMS("buffer is too small\n");
14059 return -ENOMEM;
14060 }
14061
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014062 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014063 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014064 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014065 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014066
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014067 /*
14068 * There's something wrong with the cursor on CHV pipe C.
14069 * If it straddles the left edge of the screen then
14070 * moving it away from the edge or disabling it often
14071 * results in a pipe underrun, and often that can lead to
14072 * dead pipe (constant underrun reported, and it scans
14073 * out just a solid color). To recover from that, the
14074 * display power well must be turned off and on again.
14075 * Refuse the put the cursor into that compromised position.
14076 */
14077 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14078 state->visible && state->base.crtc_x < 0) {
14079 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14080 return -EINVAL;
14081 }
14082
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014083 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014084}
14085
Matt Roperf4a2cf22014-12-01 15:40:12 -080014086static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014087intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014088 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014089{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14091
14092 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014093 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014094}
14095
14096static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014097intel_update_cursor_plane(struct drm_plane *plane,
14098 const struct intel_crtc_state *crtc_state,
14099 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014100{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014101 struct drm_crtc *crtc = crtc_state->base.crtc;
14102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014103 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014104 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014105 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014106
Matt Roperf4a2cf22014-12-01 15:40:12 -080014107 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014108 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014109 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014110 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014111 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014112 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014113
Gustavo Padovana912f122014-12-01 15:40:10 -080014114 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014115 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014116}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014117
Matt Roper3d7d6512014-06-10 08:28:13 -070014118static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14119 int pipe)
14120{
14121 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014122 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014123
14124 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14125 if (cursor == NULL)
14126 return NULL;
14127
Matt Roper8e7d6882015-01-21 16:35:41 -080014128 state = intel_create_plane_state(&cursor->base);
14129 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014130 kfree(cursor);
14131 return NULL;
14132 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014133 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014134
Matt Roper3d7d6512014-06-10 08:28:13 -070014135 cursor->can_scale = false;
14136 cursor->max_downscale = 1;
14137 cursor->pipe = pipe;
14138 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014139 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014140 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014141 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014142 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014143
14144 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014145 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014146 intel_cursor_formats,
14147 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014148 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014149
14150 if (INTEL_INFO(dev)->gen >= 4) {
14151 if (!dev->mode_config.rotation_property)
14152 dev->mode_config.rotation_property =
14153 drm_mode_create_rotation_property(dev,
14154 BIT(DRM_ROTATE_0) |
14155 BIT(DRM_ROTATE_180));
14156 if (dev->mode_config.rotation_property)
14157 drm_object_attach_property(&cursor->base.base,
14158 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014159 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014160 }
14161
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014162 if (INTEL_INFO(dev)->gen >=9)
14163 state->scaler_id = -1;
14164
Matt Roperea2c67b2014-12-23 10:41:52 -080014165 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14166
Matt Roper3d7d6512014-06-10 08:28:13 -070014167 return &cursor->base;
14168}
14169
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014170static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14171 struct intel_crtc_state *crtc_state)
14172{
14173 int i;
14174 struct intel_scaler *intel_scaler;
14175 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14176
14177 for (i = 0; i < intel_crtc->num_scalers; i++) {
14178 intel_scaler = &scaler_state->scalers[i];
14179 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014180 intel_scaler->mode = PS_SCALER_MODE_DYN;
14181 }
14182
14183 scaler_state->scaler_id = -1;
14184}
14185
Hannes Ederb358d0a2008-12-18 21:18:47 +010014186static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014187{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014188 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014189 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014190 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014191 struct drm_plane *primary = NULL;
14192 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014193 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014194
Daniel Vetter955382f2013-09-19 14:05:45 +020014195 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014196 if (intel_crtc == NULL)
14197 return;
14198
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014199 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14200 if (!crtc_state)
14201 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014202 intel_crtc->config = crtc_state;
14203 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014204 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014205
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014206 /* initialize shared scalers */
14207 if (INTEL_INFO(dev)->gen >= 9) {
14208 if (pipe == PIPE_C)
14209 intel_crtc->num_scalers = 1;
14210 else
14211 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14212
14213 skl_init_scalers(dev, intel_crtc, crtc_state);
14214 }
14215
Matt Roper465c1202014-05-29 08:06:54 -070014216 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014217 if (!primary)
14218 goto fail;
14219
14220 cursor = intel_cursor_plane_create(dev, pipe);
14221 if (!cursor)
14222 goto fail;
14223
Matt Roper465c1202014-05-29 08:06:54 -070014224 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014225 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014226 if (ret)
14227 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014228
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014229 /*
14230 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014231 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014232 */
Jesse Barnes80824002009-09-10 15:28:06 -070014233 intel_crtc->pipe = pipe;
14234 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014235 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014236 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014237 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014238 }
14239
Chris Wilson4b0e3332014-05-30 16:35:26 +030014240 intel_crtc->cursor_base = ~0;
14241 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014242 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014243
Ville Syrjälä852eb002015-06-24 22:00:07 +030014244 intel_crtc->wm.cxsr_allowed = true;
14245
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014246 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14247 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14248 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14249 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14250
Jesse Barnes79e53942008-11-07 14:24:08 -080014251 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014252
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014253 intel_color_init(&intel_crtc->base);
14254
Daniel Vetter87b6b102014-05-15 15:33:46 +020014255 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014256 return;
14257
14258fail:
14259 if (primary)
14260 drm_plane_cleanup(primary);
14261 if (cursor)
14262 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014263 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014264 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014265}
14266
Jesse Barnes752aa882013-10-31 18:55:49 +020014267enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14268{
14269 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014270 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014271
Rob Clark51fd3712013-11-19 12:10:12 -050014272 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014273
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014274 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014275 return INVALID_PIPE;
14276
14277 return to_intel_crtc(encoder->crtc)->pipe;
14278}
14279
Carl Worth08d7b3d2009-04-29 14:43:54 -070014280int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014281 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014282{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014283 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014284 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014285 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014286
Rob Clark7707e652014-07-17 23:30:04 -040014287 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014288
Rob Clark7707e652014-07-17 23:30:04 -040014289 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014290 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014291 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014292 }
14293
Rob Clark7707e652014-07-17 23:30:04 -040014294 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014295 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014296
Daniel Vetterc05422d2009-08-11 16:05:30 +020014297 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014298}
14299
Daniel Vetter66a92782012-07-12 20:08:18 +020014300static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014301{
Daniel Vetter66a92782012-07-12 20:08:18 +020014302 struct drm_device *dev = encoder->base.dev;
14303 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014304 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014305 int entry = 0;
14306
Damien Lespiaub2784e12014-08-05 11:29:37 +010014307 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014308 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014309 index_mask |= (1 << entry);
14310
Jesse Barnes79e53942008-11-07 14:24:08 -080014311 entry++;
14312 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014313
Jesse Barnes79e53942008-11-07 14:24:08 -080014314 return index_mask;
14315}
14316
Chris Wilson4d302442010-12-14 19:21:29 +000014317static bool has_edp_a(struct drm_device *dev)
14318{
14319 struct drm_i915_private *dev_priv = dev->dev_private;
14320
14321 if (!IS_MOBILE(dev))
14322 return false;
14323
14324 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14325 return false;
14326
Damien Lespiaue3589902014-02-07 19:12:50 +000014327 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014328 return false;
14329
14330 return true;
14331}
14332
Jesse Barnes84b4e042014-06-25 08:24:29 -070014333static bool intel_crt_present(struct drm_device *dev)
14334{
14335 struct drm_i915_private *dev_priv = dev->dev_private;
14336
Damien Lespiau884497e2013-12-03 13:56:23 +000014337 if (INTEL_INFO(dev)->gen >= 9)
14338 return false;
14339
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014340 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014341 return false;
14342
14343 if (IS_CHERRYVIEW(dev))
14344 return false;
14345
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014346 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14347 return false;
14348
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014349 /* DDI E can't be used if DDI A requires 4 lanes */
14350 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14351 return false;
14352
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014353 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014354 return false;
14355
14356 return true;
14357}
14358
Jesse Barnes79e53942008-11-07 14:24:08 -080014359static void intel_setup_outputs(struct drm_device *dev)
14360{
Eric Anholt725e30a2009-01-22 13:01:02 -080014361 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014362 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014363 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014364
Daniel Vetterc9093352013-06-06 22:22:47 +020014365 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014366
Jesse Barnes84b4e042014-06-25 08:24:29 -070014367 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014368 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014369
Vandana Kannanc776eb22014-08-19 12:05:01 +053014370 if (IS_BROXTON(dev)) {
14371 /*
14372 * FIXME: Broxton doesn't support port detection via the
14373 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14374 * detect the ports.
14375 */
14376 intel_ddi_init(dev, PORT_A);
14377 intel_ddi_init(dev, PORT_B);
14378 intel_ddi_init(dev, PORT_C);
14379 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014380 int found;
14381
Jesse Barnesde31fac2015-03-06 15:53:32 -080014382 /*
14383 * Haswell uses DDI functions to detect digital outputs.
14384 * On SKL pre-D0 the strap isn't connected, so we assume
14385 * it's there.
14386 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014387 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014388 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014389 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014390 intel_ddi_init(dev, PORT_A);
14391
14392 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14393 * register */
14394 found = I915_READ(SFUSE_STRAP);
14395
14396 if (found & SFUSE_STRAP_DDIB_DETECTED)
14397 intel_ddi_init(dev, PORT_B);
14398 if (found & SFUSE_STRAP_DDIC_DETECTED)
14399 intel_ddi_init(dev, PORT_C);
14400 if (found & SFUSE_STRAP_DDID_DETECTED)
14401 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014402 /*
14403 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14404 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014405 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014406 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14407 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14408 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14409 intel_ddi_init(dev, PORT_E);
14410
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014411 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014412 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014413 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014414
14415 if (has_edp_a(dev))
14416 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014417
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014418 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014419 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014420 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014421 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014422 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014423 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014424 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014425 }
14426
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014427 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014428 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014429
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014430 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014431 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014432
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014433 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014434 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014435
Daniel Vetter270b3042012-10-27 15:52:05 +020014436 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014437 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014438 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014439 /*
14440 * The DP_DETECTED bit is the latched state of the DDC
14441 * SDA pin at boot. However since eDP doesn't require DDC
14442 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14443 * eDP ports may have been muxed to an alternate function.
14444 * Thus we can't rely on the DP_DETECTED bit alone to detect
14445 * eDP ports. Consult the VBT as well as DP_DETECTED to
14446 * detect eDP ports.
14447 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014448 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014449 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014450 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14451 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014452 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014453 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014454
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014455 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014456 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014457 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14458 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014459 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014460 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014461
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014462 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014463 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014464 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14465 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14466 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14467 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014468 }
14469
Jani Nikula3cfca972013-08-27 15:12:26 +030014470 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014471 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014472 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014473
Paulo Zanonie2debe92013-02-18 19:00:27 -030014474 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014475 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014476 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014477 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014478 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014479 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014480 }
Ma Ling27185ae2009-08-24 13:50:23 +080014481
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014482 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014483 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014484 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014485
14486 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014487
Paulo Zanonie2debe92013-02-18 19:00:27 -030014488 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014489 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014490 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014491 }
Ma Ling27185ae2009-08-24 13:50:23 +080014492
Paulo Zanonie2debe92013-02-18 19:00:27 -030014493 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014494
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014495 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014496 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014497 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014498 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014499 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014500 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014501 }
Ma Ling27185ae2009-08-24 13:50:23 +080014502
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014503 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014504 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014505 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014506 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014507 intel_dvo_init(dev);
14508
Zhenyu Wang103a1962009-11-27 11:44:36 +080014509 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014510 intel_tv_init(dev);
14511
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014512 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014513
Damien Lespiaub2784e12014-08-05 11:29:37 +010014514 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014515 encoder->base.possible_crtcs = encoder->crtc_mask;
14516 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014517 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014518 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014519
Paulo Zanonidde86e22012-12-01 12:04:25 -020014520 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014521
14522 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014523}
14524
14525static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14526{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014527 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014528 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014529
Daniel Vetteref2d6332014-02-10 18:00:38 +010014530 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014531 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014532 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014533 drm_gem_object_unreference(&intel_fb->obj->base);
14534 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014535 kfree(intel_fb);
14536}
14537
14538static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014539 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014540 unsigned int *handle)
14541{
14542 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014543 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014544
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014545 if (obj->userptr.mm) {
14546 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14547 return -EINVAL;
14548 }
14549
Chris Wilson05394f32010-11-08 19:18:58 +000014550 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014551}
14552
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014553static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14554 struct drm_file *file,
14555 unsigned flags, unsigned color,
14556 struct drm_clip_rect *clips,
14557 unsigned num_clips)
14558{
14559 struct drm_device *dev = fb->dev;
14560 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14561 struct drm_i915_gem_object *obj = intel_fb->obj;
14562
14563 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014564 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014565 mutex_unlock(&dev->struct_mutex);
14566
14567 return 0;
14568}
14569
Jesse Barnes79e53942008-11-07 14:24:08 -080014570static const struct drm_framebuffer_funcs intel_fb_funcs = {
14571 .destroy = intel_user_framebuffer_destroy,
14572 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014573 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014574};
14575
Damien Lespiaub3218032015-02-27 11:15:18 +000014576static
14577u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14578 uint32_t pixel_format)
14579{
14580 u32 gen = INTEL_INFO(dev)->gen;
14581
14582 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014583 int cpp = drm_format_plane_cpp(pixel_format, 0);
14584
Damien Lespiaub3218032015-02-27 11:15:18 +000014585 /* "The stride in bytes must not exceed the of the size of 8K
14586 * pixels and 32K bytes."
14587 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014588 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014589 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014590 return 32*1024;
14591 } else if (gen >= 4) {
14592 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14593 return 16*1024;
14594 else
14595 return 32*1024;
14596 } else if (gen >= 3) {
14597 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14598 return 8*1024;
14599 else
14600 return 16*1024;
14601 } else {
14602 /* XXX DSPC is limited to 4k tiled */
14603 return 8*1024;
14604 }
14605}
14606
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014607static int intel_framebuffer_init(struct drm_device *dev,
14608 struct intel_framebuffer *intel_fb,
14609 struct drm_mode_fb_cmd2 *mode_cmd,
14610 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014611{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014612 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014613 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014614 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014615 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014616
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014617 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14618
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014619 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14620 /* Enforce that fb modifier and tiling mode match, but only for
14621 * X-tiled. This is needed for FBC. */
14622 if (!!(obj->tiling_mode == I915_TILING_X) !=
14623 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14624 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14625 return -EINVAL;
14626 }
14627 } else {
14628 if (obj->tiling_mode == I915_TILING_X)
14629 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14630 else if (obj->tiling_mode == I915_TILING_Y) {
14631 DRM_DEBUG("No Y tiling for legacy addfb\n");
14632 return -EINVAL;
14633 }
14634 }
14635
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014636 /* Passed in modifier sanity checking. */
14637 switch (mode_cmd->modifier[0]) {
14638 case I915_FORMAT_MOD_Y_TILED:
14639 case I915_FORMAT_MOD_Yf_TILED:
14640 if (INTEL_INFO(dev)->gen < 9) {
14641 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14642 mode_cmd->modifier[0]);
14643 return -EINVAL;
14644 }
14645 case DRM_FORMAT_MOD_NONE:
14646 case I915_FORMAT_MOD_X_TILED:
14647 break;
14648 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014649 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14650 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014651 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014652 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014653
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014654 stride_alignment = intel_fb_stride_alignment(dev_priv,
14655 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014656 mode_cmd->pixel_format);
14657 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14658 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14659 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014660 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014661 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014662
Damien Lespiaub3218032015-02-27 11:15:18 +000014663 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14664 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014665 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014666 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14667 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014668 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014669 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014670 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014671 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014672
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014673 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014674 mode_cmd->pitches[0] != obj->stride) {
14675 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14676 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014677 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014678 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014679
Ville Syrjälä57779d02012-10-31 17:50:14 +020014680 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014681 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014682 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014683 case DRM_FORMAT_RGB565:
14684 case DRM_FORMAT_XRGB8888:
14685 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014686 break;
14687 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014688 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014689 DRM_DEBUG("unsupported pixel format: %s\n",
14690 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014691 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014692 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014693 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014694 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014695 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14696 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014697 DRM_DEBUG("unsupported pixel format: %s\n",
14698 drm_get_format_name(mode_cmd->pixel_format));
14699 return -EINVAL;
14700 }
14701 break;
14702 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014703 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014704 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014705 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014706 DRM_DEBUG("unsupported pixel format: %s\n",
14707 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014708 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014709 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014710 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014711 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014712 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014713 DRM_DEBUG("unsupported pixel format: %s\n",
14714 drm_get_format_name(mode_cmd->pixel_format));
14715 return -EINVAL;
14716 }
14717 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014718 case DRM_FORMAT_YUYV:
14719 case DRM_FORMAT_UYVY:
14720 case DRM_FORMAT_YVYU:
14721 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014722 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014723 DRM_DEBUG("unsupported pixel format: %s\n",
14724 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014725 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014726 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014727 break;
14728 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014729 DRM_DEBUG("unsupported pixel format: %s\n",
14730 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014731 return -EINVAL;
14732 }
14733
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014734 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14735 if (mode_cmd->offsets[0] != 0)
14736 return -EINVAL;
14737
Damien Lespiauec2c9812015-01-20 12:51:45 +000014738 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014739 mode_cmd->pixel_format,
14740 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014741 /* FIXME drm helper for size checks (especially planar formats)? */
14742 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14743 return -EINVAL;
14744
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014745 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14746 intel_fb->obj = obj;
14747
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014748 intel_fill_fb_info(dev_priv, &intel_fb->base);
14749
Jesse Barnes79e53942008-11-07 14:24:08 -080014750 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14751 if (ret) {
14752 DRM_ERROR("framebuffer init failed %d\n", ret);
14753 return ret;
14754 }
14755
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014756 intel_fb->obj->framebuffer_references++;
14757
Jesse Barnes79e53942008-11-07 14:24:08 -080014758 return 0;
14759}
14760
Jesse Barnes79e53942008-11-07 14:24:08 -080014761static struct drm_framebuffer *
14762intel_user_framebuffer_create(struct drm_device *dev,
14763 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014764 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014765{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014766 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014767 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014768 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014769
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014770 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014771 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014772 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014773 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014774
Daniel Vetter92907cb2015-11-23 09:04:05 +010014775 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014776 if (IS_ERR(fb))
14777 drm_gem_object_unreference_unlocked(&obj->base);
14778
14779 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014780}
14781
Daniel Vetter06957262015-08-10 13:34:08 +020014782#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014783static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014784{
14785}
14786#endif
14787
Jesse Barnes79e53942008-11-07 14:24:08 -080014788static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014789 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014790 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014791 .atomic_check = intel_atomic_check,
14792 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014793 .atomic_state_alloc = intel_atomic_state_alloc,
14794 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014795};
14796
Imre Deak88212942016-03-16 13:38:53 +020014797/**
14798 * intel_init_display_hooks - initialize the display modesetting hooks
14799 * @dev_priv: device private
14800 */
14801void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014802{
Imre Deak88212942016-03-16 13:38:53 +020014803 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014804 dev_priv->display.find_dpll = g4x_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014805 else if (IS_CHERRYVIEW(dev_priv))
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014806 dev_priv->display.find_dpll = chv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014807 else if (IS_VALLEYVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014808 dev_priv->display.find_dpll = vlv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014809 else if (IS_PINEVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014810 dev_priv->display.find_dpll = pnv_find_best_dpll;
14811 else
14812 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14813
Imre Deak88212942016-03-16 13:38:53 +020014814 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014815 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014816 dev_priv->display.get_initial_plane_config =
14817 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014818 dev_priv->display.crtc_compute_clock =
14819 haswell_crtc_compute_clock;
14820 dev_priv->display.crtc_enable = haswell_crtc_enable;
14821 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014822 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014823 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014824 dev_priv->display.get_initial_plane_config =
14825 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014826 dev_priv->display.crtc_compute_clock =
14827 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014828 dev_priv->display.crtc_enable = haswell_crtc_enable;
14829 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014830 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014831 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014832 dev_priv->display.get_initial_plane_config =
14833 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014834 dev_priv->display.crtc_compute_clock =
14835 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014836 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14837 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014838 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014839 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014840 dev_priv->display.get_initial_plane_config =
14841 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014842 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014843 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14844 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014845 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014846 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014847 dev_priv->display.get_initial_plane_config =
14848 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014849 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014850 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14851 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014852 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014853
Jesse Barnese70236a2009-09-21 10:42:27 -070014854 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014855 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014856 dev_priv->display.get_display_clock_speed =
14857 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014858 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014859 dev_priv->display.get_display_clock_speed =
14860 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014861 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014862 dev_priv->display.get_display_clock_speed =
14863 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014864 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014865 dev_priv->display.get_display_clock_speed =
14866 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014867 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014868 dev_priv->display.get_display_clock_speed =
14869 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014870 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014871 dev_priv->display.get_display_clock_speed =
14872 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014873 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14874 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014875 dev_priv->display.get_display_clock_speed =
14876 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014877 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014878 dev_priv->display.get_display_clock_speed =
14879 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014880 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014881 dev_priv->display.get_display_clock_speed =
14882 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014883 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014884 dev_priv->display.get_display_clock_speed =
14885 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014886 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014887 dev_priv->display.get_display_clock_speed =
14888 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014889 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014890 dev_priv->display.get_display_clock_speed =
14891 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014892 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014893 dev_priv->display.get_display_clock_speed =
14894 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014895 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014896 dev_priv->display.get_display_clock_speed =
14897 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014898 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014899 dev_priv->display.get_display_clock_speed =
14900 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014901 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014902 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014903 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014904 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014905 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014906 dev_priv->display.get_display_clock_speed =
14907 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014908 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014909
Imre Deak88212942016-03-16 13:38:53 +020014910 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014911 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014912 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014913 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014914 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014915 /* FIXME: detect B0+ stepping and use auto training */
14916 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014917 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014918 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014919 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014920 dev_priv->display.modeset_commit_cdclk =
14921 broadwell_modeset_commit_cdclk;
14922 dev_priv->display.modeset_calc_cdclk =
14923 broadwell_modeset_calc_cdclk;
14924 }
Imre Deak88212942016-03-16 13:38:53 +020014925 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014926 dev_priv->display.modeset_commit_cdclk =
14927 valleyview_modeset_commit_cdclk;
14928 dev_priv->display.modeset_calc_cdclk =
14929 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014930 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014931 dev_priv->display.modeset_commit_cdclk =
14932 broxton_modeset_commit_cdclk;
14933 dev_priv->display.modeset_calc_cdclk =
14934 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014935 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014936
Imre Deak88212942016-03-16 13:38:53 +020014937 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014938 case 2:
14939 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14940 break;
14941
14942 case 3:
14943 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14944 break;
14945
14946 case 4:
14947 case 5:
14948 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14949 break;
14950
14951 case 6:
14952 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14953 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014954 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014955 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014956 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14957 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014958 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014959 /* Drop through - unsupported since execlist only. */
14960 default:
14961 /* Default just returns -ENODEV to indicate unsupported */
14962 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014963 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014964}
14965
Jesse Barnesb690e962010-07-19 13:53:12 -070014966/*
14967 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14968 * resume, or other times. This quirk makes sure that's the case for
14969 * affected systems.
14970 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014971static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014972{
14973 struct drm_i915_private *dev_priv = dev->dev_private;
14974
14975 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014976 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014977}
14978
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014979static void quirk_pipeb_force(struct drm_device *dev)
14980{
14981 struct drm_i915_private *dev_priv = dev->dev_private;
14982
14983 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14984 DRM_INFO("applying pipe b force quirk\n");
14985}
14986
Keith Packard435793d2011-07-12 14:56:22 -070014987/*
14988 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14989 */
14990static void quirk_ssc_force_disable(struct drm_device *dev)
14991{
14992 struct drm_i915_private *dev_priv = dev->dev_private;
14993 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014994 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014995}
14996
Carsten Emde4dca20e2012-03-15 15:56:26 +010014997/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014998 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14999 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015000 */
15001static void quirk_invert_brightness(struct drm_device *dev)
15002{
15003 struct drm_i915_private *dev_priv = dev->dev_private;
15004 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015005 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015006}
15007
Scot Doyle9c72cc62014-07-03 23:27:50 +000015008/* Some VBT's incorrectly indicate no backlight is present */
15009static void quirk_backlight_present(struct drm_device *dev)
15010{
15011 struct drm_i915_private *dev_priv = dev->dev_private;
15012 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15013 DRM_INFO("applying backlight present quirk\n");
15014}
15015
Jesse Barnesb690e962010-07-19 13:53:12 -070015016struct intel_quirk {
15017 int device;
15018 int subsystem_vendor;
15019 int subsystem_device;
15020 void (*hook)(struct drm_device *dev);
15021};
15022
Egbert Eich5f85f172012-10-14 15:46:38 +020015023/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15024struct intel_dmi_quirk {
15025 void (*hook)(struct drm_device *dev);
15026 const struct dmi_system_id (*dmi_id_list)[];
15027};
15028
15029static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15030{
15031 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15032 return 1;
15033}
15034
15035static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15036 {
15037 .dmi_id_list = &(const struct dmi_system_id[]) {
15038 {
15039 .callback = intel_dmi_reverse_brightness,
15040 .ident = "NCR Corporation",
15041 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15042 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15043 },
15044 },
15045 { } /* terminating entry */
15046 },
15047 .hook = quirk_invert_brightness,
15048 },
15049};
15050
Ben Widawskyc43b5632012-04-16 14:07:40 -070015051static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015052 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15053 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15054
Jesse Barnesb690e962010-07-19 13:53:12 -070015055 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15056 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15057
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015058 /* 830 needs to leave pipe A & dpll A up */
15059 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15060
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015061 /* 830 needs to leave pipe B & dpll B up */
15062 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15063
Keith Packard435793d2011-07-12 14:56:22 -070015064 /* Lenovo U160 cannot use SSC on LVDS */
15065 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015066
15067 /* Sony Vaio Y cannot use SSC on LVDS */
15068 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015069
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015070 /* Acer Aspire 5734Z must invert backlight brightness */
15071 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15072
15073 /* Acer/eMachines G725 */
15074 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15075
15076 /* Acer/eMachines e725 */
15077 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15078
15079 /* Acer/Packard Bell NCL20 */
15080 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15081
15082 /* Acer Aspire 4736Z */
15083 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015084
15085 /* Acer Aspire 5336 */
15086 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015087
15088 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15089 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015090
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015091 /* Acer C720 Chromebook (Core i3 4005U) */
15092 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15093
jens steinb2a96012014-10-28 20:25:53 +010015094 /* Apple Macbook 2,1 (Core 2 T7400) */
15095 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15096
Jani Nikula1b9448b02015-11-05 11:49:59 +020015097 /* Apple Macbook 4,1 */
15098 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15099
Scot Doyled4967d82014-07-03 23:27:52 +000015100 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15101 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015102
15103 /* HP Chromebook 14 (Celeron 2955U) */
15104 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015105
15106 /* Dell Chromebook 11 */
15107 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015108
15109 /* Dell Chromebook 11 (2015 version) */
15110 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015111};
15112
15113static void intel_init_quirks(struct drm_device *dev)
15114{
15115 struct pci_dev *d = dev->pdev;
15116 int i;
15117
15118 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15119 struct intel_quirk *q = &intel_quirks[i];
15120
15121 if (d->device == q->device &&
15122 (d->subsystem_vendor == q->subsystem_vendor ||
15123 q->subsystem_vendor == PCI_ANY_ID) &&
15124 (d->subsystem_device == q->subsystem_device ||
15125 q->subsystem_device == PCI_ANY_ID))
15126 q->hook(dev);
15127 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015128 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15129 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15130 intel_dmi_quirks[i].hook(dev);
15131 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015132}
15133
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015134/* Disable the VGA plane that we never use */
15135static void i915_disable_vga(struct drm_device *dev)
15136{
15137 struct drm_i915_private *dev_priv = dev->dev_private;
15138 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015139 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015140
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015141 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015142 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015143 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015144 sr1 = inb(VGA_SR_DATA);
15145 outb(sr1 | 1<<5, VGA_SR_DATA);
15146 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15147 udelay(300);
15148
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015149 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015150 POSTING_READ(vga_reg);
15151}
15152
Daniel Vetterf8175862012-04-10 15:50:11 +020015153void intel_modeset_init_hw(struct drm_device *dev)
15154{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015155 struct drm_i915_private *dev_priv = dev->dev_private;
15156
Ville Syrjäläb6283052015-06-03 15:45:07 +030015157 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015158
15159 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15160
Daniel Vetterf8175862012-04-10 15:50:11 +020015161 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015162 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015163}
15164
Matt Roperd93c0372015-12-03 11:37:41 -080015165/*
15166 * Calculate what we think the watermarks should be for the state we've read
15167 * out of the hardware and then immediately program those watermarks so that
15168 * we ensure the hardware settings match our internal state.
15169 *
15170 * We can calculate what we think WM's should be by creating a duplicate of the
15171 * current state (which was constructed during hardware readout) and running it
15172 * through the atomic check code to calculate new watermark values in the
15173 * state object.
15174 */
15175static void sanitize_watermarks(struct drm_device *dev)
15176{
15177 struct drm_i915_private *dev_priv = to_i915(dev);
15178 struct drm_atomic_state *state;
15179 struct drm_crtc *crtc;
15180 struct drm_crtc_state *cstate;
15181 struct drm_modeset_acquire_ctx ctx;
15182 int ret;
15183 int i;
15184
15185 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015186 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015187 return;
15188
15189 /*
15190 * We need to hold connection_mutex before calling duplicate_state so
15191 * that the connector loop is protected.
15192 */
15193 drm_modeset_acquire_init(&ctx, 0);
15194retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015195 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015196 if (ret == -EDEADLK) {
15197 drm_modeset_backoff(&ctx);
15198 goto retry;
15199 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015200 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015201 }
15202
15203 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15204 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015205 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015206
Matt Ropered4a6a72016-02-23 17:20:13 -080015207 /*
15208 * Hardware readout is the only time we don't want to calculate
15209 * intermediate watermarks (since we don't trust the current
15210 * watermarks).
15211 */
15212 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15213
Matt Roperd93c0372015-12-03 11:37:41 -080015214 ret = intel_atomic_check(dev, state);
15215 if (ret) {
15216 /*
15217 * If we fail here, it means that the hardware appears to be
15218 * programmed in a way that shouldn't be possible, given our
15219 * understanding of watermark requirements. This might mean a
15220 * mistake in the hardware readout code or a mistake in the
15221 * watermark calculations for a given platform. Raise a WARN
15222 * so that this is noticeable.
15223 *
15224 * If this actually happens, we'll have to just leave the
15225 * BIOS-programmed watermarks untouched and hope for the best.
15226 */
15227 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015228 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015229 }
15230
15231 /* Write calculated watermark values back */
15232 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15233 for_each_crtc_in_state(state, crtc, cstate, i) {
15234 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15235
Matt Ropered4a6a72016-02-23 17:20:13 -080015236 cs->wm.need_postvbl_update = true;
15237 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015238 }
15239
15240 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015241fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015242 drm_modeset_drop_locks(&ctx);
15243 drm_modeset_acquire_fini(&ctx);
15244}
15245
Jesse Barnes79e53942008-11-07 14:24:08 -080015246void intel_modeset_init(struct drm_device *dev)
15247{
Jesse Barnes652c3932009-08-17 13:31:43 -070015248 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015249 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015250 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015251 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015252
15253 drm_mode_config_init(dev);
15254
15255 dev->mode_config.min_width = 0;
15256 dev->mode_config.min_height = 0;
15257
Dave Airlie019d96c2011-09-29 16:20:42 +010015258 dev->mode_config.preferred_depth = 24;
15259 dev->mode_config.prefer_shadow = 1;
15260
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015261 dev->mode_config.allow_fb_modifiers = true;
15262
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015263 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015264
Jesse Barnesb690e962010-07-19 13:53:12 -070015265 intel_init_quirks(dev);
15266
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015267 intel_init_pm(dev);
15268
Ben Widawskye3c74752013-04-05 13:12:39 -070015269 if (INTEL_INFO(dev)->num_pipes == 0)
15270 return;
15271
Lukas Wunner69f92f62015-07-15 13:57:35 +020015272 /*
15273 * There may be no VBT; and if the BIOS enabled SSC we can
15274 * just keep using it to avoid unnecessary flicker. Whereas if the
15275 * BIOS isn't using it, don't assume it will work even if the VBT
15276 * indicates as much.
15277 */
15278 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15279 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15280 DREF_SSC1_ENABLE);
15281
15282 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15283 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15284 bios_lvds_use_ssc ? "en" : "dis",
15285 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15286 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15287 }
15288 }
15289
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015290 if (IS_GEN2(dev)) {
15291 dev->mode_config.max_width = 2048;
15292 dev->mode_config.max_height = 2048;
15293 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015294 dev->mode_config.max_width = 4096;
15295 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015296 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015297 dev->mode_config.max_width = 8192;
15298 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015299 }
Damien Lespiau068be562014-03-28 14:17:49 +000015300
Ville Syrjälädc41c152014-08-13 11:57:05 +030015301 if (IS_845G(dev) || IS_I865G(dev)) {
15302 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15303 dev->mode_config.cursor_height = 1023;
15304 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015305 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15306 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15307 } else {
15308 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15309 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15310 }
15311
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015312 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015313
Zhao Yakui28c97732009-10-09 11:39:41 +080015314 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015315 INTEL_INFO(dev)->num_pipes,
15316 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015317
Damien Lespiau055e3932014-08-18 13:49:10 +010015318 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015319 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015320 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015321 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015322 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015323 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015324 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015325 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015326 }
15327
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015328 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015329 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015330 intel_update_cdclk(dev);
15331
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015332 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015333
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015334 /* Just disable it once at startup */
15335 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015336 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015337
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015338 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015339 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015340 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015341
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015342 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015343 struct intel_initial_plane_config plane_config = {};
15344
Jesse Barnes46f297f2014-03-07 08:57:48 -080015345 if (!crtc->active)
15346 continue;
15347
Jesse Barnes46f297f2014-03-07 08:57:48 -080015348 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015349 * Note that reserving the BIOS fb up front prevents us
15350 * from stuffing other stolen allocations like the ring
15351 * on top. This prevents some ugliness at boot time, and
15352 * can even allow for smooth boot transitions if the BIOS
15353 * fb is large enough for the active pipe configuration.
15354 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015355 dev_priv->display.get_initial_plane_config(crtc,
15356 &plane_config);
15357
15358 /*
15359 * If the fb is shared between multiple heads, we'll
15360 * just get the first one.
15361 */
15362 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015363 }
Matt Roperd93c0372015-12-03 11:37:41 -080015364
15365 /*
15366 * Make sure hardware watermarks really match the state we read out.
15367 * Note that we need to do this after reconstructing the BIOS fb's
15368 * since the watermark calculation done here will use pstate->fb.
15369 */
15370 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015371}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015372
Daniel Vetter7fad7982012-07-04 17:51:47 +020015373static void intel_enable_pipe_a(struct drm_device *dev)
15374{
15375 struct intel_connector *connector;
15376 struct drm_connector *crt = NULL;
15377 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015378 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015379
15380 /* We can't just switch on the pipe A, we need to set things up with a
15381 * proper mode and output configuration. As a gross hack, enable pipe A
15382 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015383 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015384 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15385 crt = &connector->base;
15386 break;
15387 }
15388 }
15389
15390 if (!crt)
15391 return;
15392
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015393 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015394 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015395}
15396
Daniel Vetterfa555832012-10-10 23:14:00 +020015397static bool
15398intel_check_plane_mapping(struct intel_crtc *crtc)
15399{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015400 struct drm_device *dev = crtc->base.dev;
15401 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015402 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015403
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015404 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015405 return true;
15406
Ville Syrjälä649636e2015-09-22 19:50:01 +030015407 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015408
15409 if ((val & DISPLAY_PLANE_ENABLE) &&
15410 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15411 return false;
15412
15413 return true;
15414}
15415
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015416static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15417{
15418 struct drm_device *dev = crtc->base.dev;
15419 struct intel_encoder *encoder;
15420
15421 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15422 return true;
15423
15424 return false;
15425}
15426
Ville Syrjälädd756192016-02-17 21:28:45 +020015427static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15428{
15429 struct drm_device *dev = encoder->base.dev;
15430 struct intel_connector *connector;
15431
15432 for_each_connector_on_encoder(dev, &encoder->base, connector)
15433 return true;
15434
15435 return false;
15436}
15437
Daniel Vetter24929352012-07-02 20:28:59 +020015438static void intel_sanitize_crtc(struct intel_crtc *crtc)
15439{
15440 struct drm_device *dev = crtc->base.dev;
15441 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015442 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015443
Daniel Vetter24929352012-07-02 20:28:59 +020015444 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015445 if (!transcoder_is_dsi(cpu_transcoder)) {
15446 i915_reg_t reg = PIPECONF(cpu_transcoder);
15447
15448 I915_WRITE(reg,
15449 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15450 }
Daniel Vetter24929352012-07-02 20:28:59 +020015451
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015452 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015453 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015454 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015455 struct intel_plane *plane;
15456
Daniel Vetter96256042015-02-13 21:03:42 +010015457 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015458
15459 /* Disable everything but the primary plane */
15460 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15461 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15462 continue;
15463
15464 plane->disable_plane(&plane->base, &crtc->base);
15465 }
Daniel Vetter96256042015-02-13 21:03:42 +010015466 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015467
Daniel Vetter24929352012-07-02 20:28:59 +020015468 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015469 * disable the crtc (and hence change the state) if it is wrong. Note
15470 * that gen4+ has a fixed plane -> pipe mapping. */
15471 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015472 bool plane;
15473
Daniel Vetter24929352012-07-02 20:28:59 +020015474 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15475 crtc->base.base.id);
15476
15477 /* Pipe has the wrong plane attached and the plane is active.
15478 * Temporarily change the plane mapping and disable everything
15479 * ... */
15480 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015481 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015482 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015483 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015484 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015485 }
Daniel Vetter24929352012-07-02 20:28:59 +020015486
Daniel Vetter7fad7982012-07-04 17:51:47 +020015487 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15488 crtc->pipe == PIPE_A && !crtc->active) {
15489 /* BIOS forgot to enable pipe A, this mostly happens after
15490 * resume. Force-enable the pipe to fix this, the update_dpms
15491 * call below we restore the pipe to the right state, but leave
15492 * the required bits on. */
15493 intel_enable_pipe_a(dev);
15494 }
15495
Daniel Vetter24929352012-07-02 20:28:59 +020015496 /* Adjust the state of the output pipe according to whether we
15497 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015498 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015499 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015500
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015501 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015502 /*
15503 * We start out with underrun reporting disabled to avoid races.
15504 * For correct bookkeeping mark this on active crtcs.
15505 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015506 * Also on gmch platforms we dont have any hardware bits to
15507 * disable the underrun reporting. Which means we need to start
15508 * out with underrun reporting disabled also on inactive pipes,
15509 * since otherwise we'll complain about the garbage we read when
15510 * e.g. coming up after runtime pm.
15511 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015512 * No protection against concurrent access is required - at
15513 * worst a fifo underrun happens which also sets this to false.
15514 */
15515 crtc->cpu_fifo_underrun_disabled = true;
15516 crtc->pch_fifo_underrun_disabled = true;
15517 }
Daniel Vetter24929352012-07-02 20:28:59 +020015518}
15519
15520static void intel_sanitize_encoder(struct intel_encoder *encoder)
15521{
15522 struct intel_connector *connector;
15523 struct drm_device *dev = encoder->base.dev;
15524
15525 /* We need to check both for a crtc link (meaning that the
15526 * encoder is active and trying to read from a pipe) and the
15527 * pipe itself being active. */
15528 bool has_active_crtc = encoder->base.crtc &&
15529 to_intel_crtc(encoder->base.crtc)->active;
15530
Ville Syrjälädd756192016-02-17 21:28:45 +020015531 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015532 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15533 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015534 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015535
15536 /* Connector is active, but has no active pipe. This is
15537 * fallout from our resume register restoring. Disable
15538 * the encoder manually again. */
15539 if (encoder->base.crtc) {
15540 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15541 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015542 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015543 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015544 if (encoder->post_disable)
15545 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015546 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015547 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015548
15549 /* Inconsistent output/port/pipe state happens presumably due to
15550 * a bug in one of the get_hw_state functions. Or someplace else
15551 * in our code, like the register restore mess on resume. Clamp
15552 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015553 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015554 if (connector->encoder != encoder)
15555 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015556 connector->base.dpms = DRM_MODE_DPMS_OFF;
15557 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015558 }
15559 }
15560 /* Enabled encoders without active connectors will be fixed in
15561 * the crtc fixup. */
15562}
15563
Imre Deak04098752014-02-18 00:02:16 +020015564void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015565{
15566 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015567 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015568
Imre Deak04098752014-02-18 00:02:16 +020015569 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15570 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15571 i915_disable_vga(dev);
15572 }
15573}
15574
15575void i915_redisable_vga(struct drm_device *dev)
15576{
15577 struct drm_i915_private *dev_priv = dev->dev_private;
15578
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015579 /* This function can be called both from intel_modeset_setup_hw_state or
15580 * at a very early point in our resume sequence, where the power well
15581 * structures are not yet restored. Since this function is at a very
15582 * paranoid "someone might have enabled VGA while we were not looking"
15583 * level, just check if the power well is enabled instead of trying to
15584 * follow the "don't touch the power well if we don't need it" policy
15585 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015586 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015587 return;
15588
Imre Deak04098752014-02-18 00:02:16 +020015589 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015590
15591 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015592}
15593
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015594static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015595{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015596 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015597
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015598 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015599}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015600
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015601/* FIXME read out full plane state for all planes */
15602static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015603{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015604 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015605 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015606 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015607
Matt Roper19b8d382015-09-24 15:53:17 -070015608 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015609 primary_get_hw_state(to_intel_plane(primary));
15610
15611 if (plane_state->visible)
15612 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015613}
15614
Daniel Vetter30e984d2013-06-05 13:34:17 +020015615static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015616{
15617 struct drm_i915_private *dev_priv = dev->dev_private;
15618 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015619 struct intel_crtc *crtc;
15620 struct intel_encoder *encoder;
15621 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015622 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015623
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015624 dev_priv->active_crtcs = 0;
15625
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015626 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015627 struct intel_crtc_state *crtc_state = crtc->config;
15628 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015629
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015630 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15631 memset(crtc_state, 0, sizeof(*crtc_state));
15632 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015633
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015634 crtc_state->base.active = crtc_state->base.enable =
15635 dev_priv->display.get_pipe_config(crtc, crtc_state);
15636
15637 crtc->base.enabled = crtc_state->base.enable;
15638 crtc->active = crtc_state->base.active;
15639
15640 if (crtc_state->base.active) {
15641 dev_priv->active_crtcs |= 1 << crtc->pipe;
15642
15643 if (IS_BROADWELL(dev_priv)) {
15644 pixclk = ilk_pipe_pixel_rate(crtc_state);
15645
15646 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15647 if (crtc_state->ips_enabled)
15648 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15649 } else if (IS_VALLEYVIEW(dev_priv) ||
15650 IS_CHERRYVIEW(dev_priv) ||
15651 IS_BROXTON(dev_priv))
15652 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15653 else
15654 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15655 }
15656
15657 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015658
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015659 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015660
15661 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15662 crtc->base.base.id,
15663 crtc->active ? "enabled" : "disabled");
15664 }
15665
Daniel Vetter53589012013-06-05 13:34:16 +020015666 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15667 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15668
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015669 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15670 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015671 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015672 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015673 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015674 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015675 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015676 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015677
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015678 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015679 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015680 }
15681
Damien Lespiaub2784e12014-08-05 11:29:37 +010015682 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015683 pipe = 0;
15684
15685 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015686 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15687 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015688 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015689 } else {
15690 encoder->base.crtc = NULL;
15691 }
15692
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015693 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015694 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015695 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015696 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015697 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015698 }
15699
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015700 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015701 if (connector->get_hw_state(connector)) {
15702 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015703
15704 encoder = connector->encoder;
15705 connector->base.encoder = &encoder->base;
15706
15707 if (encoder->base.crtc &&
15708 encoder->base.crtc->state->active) {
15709 /*
15710 * This has to be done during hardware readout
15711 * because anything calling .crtc_disable may
15712 * rely on the connector_mask being accurate.
15713 */
15714 encoder->base.crtc->state->connector_mask |=
15715 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015716 encoder->base.crtc->state->encoder_mask |=
15717 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015718 }
15719
Daniel Vetter24929352012-07-02 20:28:59 +020015720 } else {
15721 connector->base.dpms = DRM_MODE_DPMS_OFF;
15722 connector->base.encoder = NULL;
15723 }
15724 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15725 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015726 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015727 connector->base.encoder ? "enabled" : "disabled");
15728 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015729
15730 for_each_intel_crtc(dev, crtc) {
15731 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15732
15733 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15734 if (crtc->base.state->active) {
15735 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15736 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15737 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15738
15739 /*
15740 * The initial mode needs to be set in order to keep
15741 * the atomic core happy. It wants a valid mode if the
15742 * crtc's enabled, so we do the above call.
15743 *
15744 * At this point some state updated by the connectors
15745 * in their ->detect() callback has not run yet, so
15746 * no recalculation can be done yet.
15747 *
15748 * Even if we could do a recalculation and modeset
15749 * right now it would cause a double modeset if
15750 * fbdev or userspace chooses a different initial mode.
15751 *
15752 * If that happens, someone indicated they wanted a
15753 * mode change, which means it's safe to do a full
15754 * recalculation.
15755 */
15756 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015757
15758 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15759 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015760 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015761
15762 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015763 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015764}
15765
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015766/* Scan out the current hw modeset state,
15767 * and sanitizes it to the current state
15768 */
15769static void
15770intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015771{
15772 struct drm_i915_private *dev_priv = dev->dev_private;
15773 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015774 struct intel_crtc *crtc;
15775 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015776 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015777
15778 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015779
15780 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015781 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015782 intel_sanitize_encoder(encoder);
15783 }
15784
Damien Lespiau055e3932014-08-18 13:49:10 +010015785 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015786 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15787 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015788 intel_dump_pipe_config(crtc, crtc->config,
15789 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015790 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015791
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015792 intel_modeset_update_connector_atomic_state(dev);
15793
Daniel Vetter35c95372013-07-17 06:55:04 +020015794 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15795 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15796
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015797 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015798 continue;
15799
15800 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15801
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015802 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015803 pll->on = false;
15804 }
15805
Wayne Boyer666a4532015-12-09 12:29:35 -080015806 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015807 vlv_wm_get_hw_state(dev);
15808 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015809 skl_wm_get_hw_state(dev);
15810 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015811 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015812
15813 for_each_intel_crtc(dev, crtc) {
15814 unsigned long put_domains;
15815
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015816 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015817 if (WARN_ON(put_domains))
15818 modeset_put_power_domains(dev_priv, put_domains);
15819 }
15820 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015821
15822 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015823}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015824
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015825void intel_display_resume(struct drm_device *dev)
15826{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015827 struct drm_i915_private *dev_priv = to_i915(dev);
15828 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15829 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015830 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015831 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015832
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015833 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015834
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015835 /*
15836 * This is a cludge because with real atomic modeset mode_config.mutex
15837 * won't be taken. Unfortunately some probed state like
15838 * audio_codec_enable is still protected by mode_config.mutex, so lock
15839 * it here for now.
15840 */
15841 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015842 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015843
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015844retry:
15845 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015846
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015847 if (ret == 0 && !setup) {
15848 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015849
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015850 intel_modeset_setup_hw_state(dev);
15851 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015852 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015853
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015854 if (ret == 0 && state) {
15855 struct drm_crtc_state *crtc_state;
15856 struct drm_crtc *crtc;
15857 int i;
15858
15859 state->acquire_ctx = &ctx;
15860
15861 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15862 /*
15863 * Force recalculation even if we restore
15864 * current state. With fast modeset this may not result
15865 * in a modeset when the state is compatible.
15866 */
15867 crtc_state->mode_changed = true;
15868 }
15869
15870 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015871 }
15872
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015873 if (ret == -EDEADLK) {
15874 drm_modeset_backoff(&ctx);
15875 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015876 }
15877
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015878 drm_modeset_drop_locks(&ctx);
15879 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015880 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015881
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015882 if (ret) {
15883 DRM_ERROR("Restoring old state failed with %i\n", ret);
15884 drm_atomic_state_free(state);
15885 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015886}
15887
15888void intel_modeset_gem_init(struct drm_device *dev)
15889{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015890 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015891 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015892 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015893
Imre Deakae484342014-03-31 15:10:44 +030015894 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015895
Chris Wilson1833b132012-05-09 11:56:28 +010015896 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015897
15898 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015899
15900 /*
15901 * Make sure any fbs we allocated at startup are properly
15902 * pinned & fenced. When we do the allocation it's too early
15903 * for this.
15904 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015905 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015906 obj = intel_fb_obj(c->primary->fb);
15907 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015908 continue;
15909
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015910 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015911 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15912 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015913 mutex_unlock(&dev->struct_mutex);
15914 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015915 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15916 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015917 drm_framebuffer_unreference(c->primary->fb);
15918 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015919 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015920 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015921 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015922 }
15923 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015924
15925 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015926}
15927
Imre Deak4932e2c2014-02-11 17:12:48 +020015928void intel_connector_unregister(struct intel_connector *intel_connector)
15929{
15930 struct drm_connector *connector = &intel_connector->base;
15931
15932 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015933 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015934}
15935
Jesse Barnes79e53942008-11-07 14:24:08 -080015936void intel_modeset_cleanup(struct drm_device *dev)
15937{
Jesse Barnes652c3932009-08-17 13:31:43 -070015938 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015939 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015940
Imre Deak2eb52522014-11-19 15:30:05 +020015941 intel_disable_gt_powersave(dev);
15942
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015943 intel_backlight_unregister(dev);
15944
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015945 /*
15946 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015947 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015948 * experience fancy races otherwise.
15949 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015950 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015951
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015952 /*
15953 * Due to the hpd irq storm handling the hotplug work can re-arm the
15954 * poll handlers. Hence disable polling after hpd handling is shut down.
15955 */
Keith Packardf87ea762010-10-03 19:36:26 -070015956 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015957
Jesse Barnes723bfd72010-10-07 16:01:13 -070015958 intel_unregister_dsm_handler();
15959
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015960 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015961
Chris Wilson1630fe72011-07-08 12:22:42 +010015962 /* flush any delayed tasks or pending work */
15963 flush_scheduled_work();
15964
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015965 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020015966 for_each_intel_connector(dev, connector)
15967 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030015968
Jesse Barnes79e53942008-11-07 14:24:08 -080015969 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015970
15971 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015972
Imre Deakae484342014-03-31 15:10:44 +030015973 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010015974
15975 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015976}
15977
Dave Airlie28d52042009-09-21 14:33:58 +100015978/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015979 * Return which encoder is currently attached for connector.
15980 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015981struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015982{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015983 return &intel_attached_encoder(connector)->base;
15984}
Jesse Barnes79e53942008-11-07 14:24:08 -080015985
Chris Wilsondf0e9242010-09-09 16:20:55 +010015986void intel_connector_attach_encoder(struct intel_connector *connector,
15987 struct intel_encoder *encoder)
15988{
15989 connector->encoder = encoder;
15990 drm_mode_connector_attach_encoder(&connector->base,
15991 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015992}
Dave Airlie28d52042009-09-21 14:33:58 +100015993
15994/*
15995 * set vga decode state - true == enable VGA decode
15996 */
15997int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15998{
15999 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016000 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016001 u16 gmch_ctrl;
16002
Chris Wilson75fa0412014-02-07 18:37:02 -020016003 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16004 DRM_ERROR("failed to read control word\n");
16005 return -EIO;
16006 }
16007
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016008 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16009 return 0;
16010
Dave Airlie28d52042009-09-21 14:33:58 +100016011 if (state)
16012 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16013 else
16014 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016015
16016 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16017 DRM_ERROR("failed to write control word\n");
16018 return -EIO;
16019 }
16020
Dave Airlie28d52042009-09-21 14:33:58 +100016021 return 0;
16022}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016023
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016024struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016025
16026 u32 power_well_driver;
16027
Chris Wilson63b66e52013-08-08 15:12:06 +020016028 int num_transcoders;
16029
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016030 struct intel_cursor_error_state {
16031 u32 control;
16032 u32 position;
16033 u32 base;
16034 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016035 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016036
16037 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016038 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016039 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016040 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016041 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016042
16043 struct intel_plane_error_state {
16044 u32 control;
16045 u32 stride;
16046 u32 size;
16047 u32 pos;
16048 u32 addr;
16049 u32 surface;
16050 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016051 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016052
16053 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016054 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016055 enum transcoder cpu_transcoder;
16056
16057 u32 conf;
16058
16059 u32 htotal;
16060 u32 hblank;
16061 u32 hsync;
16062 u32 vtotal;
16063 u32 vblank;
16064 u32 vsync;
16065 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016066};
16067
16068struct intel_display_error_state *
16069intel_display_capture_error_state(struct drm_device *dev)
16070{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016071 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016072 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016073 int transcoders[] = {
16074 TRANSCODER_A,
16075 TRANSCODER_B,
16076 TRANSCODER_C,
16077 TRANSCODER_EDP,
16078 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016079 int i;
16080
Chris Wilson63b66e52013-08-08 15:12:06 +020016081 if (INTEL_INFO(dev)->num_pipes == 0)
16082 return NULL;
16083
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016084 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016085 if (error == NULL)
16086 return NULL;
16087
Imre Deak190be112013-11-25 17:15:31 +020016088 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016089 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16090
Damien Lespiau055e3932014-08-18 13:49:10 +010016091 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016092 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016093 __intel_display_power_is_enabled(dev_priv,
16094 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016095 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016096 continue;
16097
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016098 error->cursor[i].control = I915_READ(CURCNTR(i));
16099 error->cursor[i].position = I915_READ(CURPOS(i));
16100 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016101
16102 error->plane[i].control = I915_READ(DSPCNTR(i));
16103 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016104 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016105 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016106 error->plane[i].pos = I915_READ(DSPPOS(i));
16107 }
Paulo Zanonica291362013-03-06 20:03:14 -030016108 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16109 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016110 if (INTEL_INFO(dev)->gen >= 4) {
16111 error->plane[i].surface = I915_READ(DSPSURF(i));
16112 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16113 }
16114
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016115 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016116
Sonika Jindal3abfce72014-07-21 15:23:43 +053016117 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016118 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016119 }
16120
Jani Nikula4d1de972016-03-18 17:05:42 +020016121 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016122 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16123 if (HAS_DDI(dev_priv->dev))
16124 error->num_transcoders++; /* Account for eDP. */
16125
16126 for (i = 0; i < error->num_transcoders; i++) {
16127 enum transcoder cpu_transcoder = transcoders[i];
16128
Imre Deakddf9c532013-11-27 22:02:02 +020016129 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016130 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016131 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016132 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016133 continue;
16134
Chris Wilson63b66e52013-08-08 15:12:06 +020016135 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16136
16137 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16138 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16139 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16140 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16141 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16142 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16143 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016144 }
16145
16146 return error;
16147}
16148
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016149#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16150
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016151void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016152intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016153 struct drm_device *dev,
16154 struct intel_display_error_state *error)
16155{
Damien Lespiau055e3932014-08-18 13:49:10 +010016156 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016157 int i;
16158
Chris Wilson63b66e52013-08-08 15:12:06 +020016159 if (!error)
16160 return;
16161
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016162 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016163 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016164 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016165 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016166 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016167 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016168 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016169 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016170 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016171 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016172
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016173 err_printf(m, "Plane [%d]:\n", i);
16174 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16175 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016176 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016177 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16178 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016179 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016180 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016181 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016182 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016183 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16184 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016185 }
16186
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016187 err_printf(m, "Cursor [%d]:\n", i);
16188 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16189 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16190 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016191 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016192
16193 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016194 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016195 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016196 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016197 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016198 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16199 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16200 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16201 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16202 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16203 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16204 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16205 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016206}