blob: 28ead66ed987537afd237b949b02af7925107137 [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_pipe_gamma(struct drm_crtc *crtc);
106static void haswell_set_pipemisc(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200107static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200108static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200109 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200110static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200111 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200112static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
113static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700114static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
115 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200116static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
117 int num_connectors);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200118static void skylake_pfit_enable(struct intel_crtc *crtc);
119static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
120static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200121static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200122static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100123
Jesse Barnes79e53942008-11-07 14:24:08 -0800124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_range_t;
127
128typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400129 int dot_limit;
130 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800131} intel_p2_t;
132
Ma Lingd4906092009-03-18 20:13:27 +0800133typedef struct intel_limit intel_limit_t;
134struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
136 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
153static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg)
155{
156 u32 val;
157 int divider;
158
159 if (dev_priv->hpll_freq == 0)
160 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
161
162 mutex_lock(&dev_priv->sb_lock);
163 val = vlv_cck_read(dev_priv, reg);
164 mutex_unlock(&dev_priv->sb_lock);
165
166 divider = val & CCK_FREQUENCY_VALUES;
167
168 WARN((val & CCK_FREQUENCY_STATUS) !=
169 (divider << CCK_FREQUENCY_STATUS_SHIFT),
170 "%s change in progress\n", name);
171
172 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
173}
174
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200175static int
176intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200177{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200178 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200179}
180
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200181static int
182intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300183{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200184 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
185 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200186}
187
188static int
189intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
190{
Jani Nikula79e50a42015-08-26 10:58:20 +0300191 uint32_t clkcfg;
192
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200193 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300194 clkcfg = I915_READ(CLKCFG);
195 switch (clkcfg & CLKCFG_FSB_MASK) {
196 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200197 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200199 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 /* these two are just a guess; one of them might be right */
209 case CLKCFG_FSB_1600:
210 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 }
215}
216
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200217static void intel_update_rawclk(struct drm_i915_private *dev_priv)
218{
219 if (HAS_PCH_SPLIT(dev_priv))
220 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
221 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
222 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
223 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
224 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
225 else
226 return; /* no rawclk on other platforms, or no need to know it */
227
228 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
229}
230
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300231static void intel_update_czclk(struct drm_i915_private *dev_priv)
232{
Wayne Boyer666a4532015-12-09 12:29:35 -0800233 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300234 return;
235
236 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
237 CCK_CZ_CLOCK_CONTROL);
238
239 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
240}
241
Chris Wilson021357a2010-09-07 20:54:59 +0100242static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200243intel_fdi_link_freq(struct drm_i915_private *dev_priv,
244 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100245{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200246 if (HAS_DDI(dev_priv))
247 return pipe_config->port_clock; /* SPLL */
248 else if (IS_GEN5(dev_priv))
249 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200250 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100252}
253
Daniel Vetter5d536e22013-07-06 12:52:06 +0200254static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400255 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200256 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200257 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400258 .m = { .min = 96, .max = 140 },
259 .m1 = { .min = 18, .max = 26 },
260 .m2 = { .min = 6, .max = 16 },
261 .p = { .min = 4, .max = 128 },
262 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700263 .p2 = { .dot_limit = 165000,
264 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
Daniel Vetter5d536e22013-07-06 12:52:06 +0200267static const intel_limit_t intel_limits_i8xx_dvo = {
268 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200269 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200270 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200271 .m = { .min = 96, .max = 140 },
272 .m1 = { .min = 18, .max = 26 },
273 .m2 = { .min = 6, .max = 16 },
274 .p = { .min = 4, .max = 128 },
275 .p1 = { .min = 2, .max = 33 },
276 .p2 = { .dot_limit = 165000,
277 .p2_slow = 4, .p2_fast = 4 },
278};
279
Keith Packarde4b36692009-06-05 19:22:17 -0700280static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200282 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200283 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400284 .m = { .min = 96, .max = 140 },
285 .m1 = { .min = 18, .max = 26 },
286 .m2 = { .min = 6, .max = 16 },
287 .p = { .min = 4, .max = 128 },
288 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .p2 = { .dot_limit = 165000,
290 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
Eric Anholt273e27c2011-03-30 13:01:10 -0700292
Keith Packarde4b36692009-06-05 19:22:17 -0700293static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1400000, .max = 2800000 },
296 .n = { .min = 1, .max = 6 },
297 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100298 .m1 = { .min = 8, .max = 18 },
299 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400300 .p = { .min = 5, .max = 80 },
301 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .p2 = { .dot_limit = 200000,
303 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
306static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .dot = { .min = 20000, .max = 400000 },
308 .vco = { .min = 1400000, .max = 2800000 },
309 .n = { .min = 1, .max = 6 },
310 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100311 .m1 = { .min = 8, .max = 18 },
312 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400313 .p = { .min = 7, .max = 98 },
314 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .p2 = { .dot_limit = 112000,
316 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700317};
318
Eric Anholt273e27c2011-03-30 13:01:10 -0700319
Keith Packarde4b36692009-06-05 19:22:17 -0700320static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 .dot = { .min = 25000, .max = 270000 },
322 .vco = { .min = 1750000, .max = 3500000},
323 .n = { .min = 1, .max = 4 },
324 .m = { .min = 104, .max = 138 },
325 .m1 = { .min = 17, .max = 23 },
326 .m2 = { .min = 5, .max = 11 },
327 .p = { .min = 10, .max = 30 },
328 .p1 = { .min = 1, .max = 3},
329 .p2 = { .dot_limit = 270000,
330 .p2_slow = 10,
331 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800332 },
Keith Packarde4b36692009-06-05 19:22:17 -0700333};
334
335static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .dot = { .min = 22000, .max = 400000 },
337 .vco = { .min = 1750000, .max = 3500000},
338 .n = { .min = 1, .max = 4 },
339 .m = { .min = 104, .max = 138 },
340 .m1 = { .min = 16, .max = 23 },
341 .m2 = { .min = 5, .max = 11 },
342 .p = { .min = 5, .max = 80 },
343 .p1 = { .min = 1, .max = 8},
344 .p2 = { .dot_limit = 165000,
345 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700346};
347
348static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700349 .dot = { .min = 20000, .max = 115000 },
350 .vco = { .min = 1750000, .max = 3500000 },
351 .n = { .min = 1, .max = 3 },
352 .m = { .min = 104, .max = 138 },
353 .m1 = { .min = 17, .max = 23 },
354 .m2 = { .min = 5, .max = 11 },
355 .p = { .min = 28, .max = 112 },
356 .p1 = { .min = 2, .max = 8 },
357 .p2 = { .dot_limit = 0,
358 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800359 },
Keith Packarde4b36692009-06-05 19:22:17 -0700360};
361
362static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .dot = { .min = 80000, .max = 224000 },
364 .vco = { .min = 1750000, .max = 3500000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 104, .max = 138 },
367 .m1 = { .min = 17, .max = 23 },
368 .m2 = { .min = 5, .max = 11 },
369 .p = { .min = 14, .max = 42 },
370 .p1 = { .min = 2, .max = 6 },
371 .p2 = { .dot_limit = 0,
372 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800373 },
Keith Packarde4b36692009-06-05 19:22:17 -0700374};
375
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500376static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400377 .dot = { .min = 20000, .max = 400000},
378 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700379 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .n = { .min = 3, .max = 6 },
381 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700382 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400383 .m1 = { .min = 0, .max = 0 },
384 .m2 = { .min = 0, .max = 254 },
385 .p = { .min = 5, .max = 80 },
386 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 .p2 = { .dot_limit = 200000,
388 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700389};
390
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500391static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400392 .dot = { .min = 20000, .max = 400000 },
393 .vco = { .min = 1700000, .max = 3500000 },
394 .n = { .min = 3, .max = 6 },
395 .m = { .min = 2, .max = 256 },
396 .m1 = { .min = 0, .max = 0 },
397 .m2 = { .min = 0, .max = 254 },
398 .p = { .min = 7, .max = 112 },
399 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700400 .p2 = { .dot_limit = 112000,
401 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700402};
403
Eric Anholt273e27c2011-03-30 13:01:10 -0700404/* Ironlake / Sandybridge
405 *
406 * We calculate clock using (register_value + 2) for N/M1/M2, so here
407 * the range value for them is (actual_value - 2).
408 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800409static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700410 .dot = { .min = 25000, .max = 350000 },
411 .vco = { .min = 1760000, .max = 3510000 },
412 .n = { .min = 1, .max = 5 },
413 .m = { .min = 79, .max = 127 },
414 .m1 = { .min = 12, .max = 22 },
415 .m2 = { .min = 5, .max = 9 },
416 .p = { .min = 5, .max = 80 },
417 .p1 = { .min = 1, .max = 8 },
418 .p2 = { .dot_limit = 225000,
419 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700420};
421
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800422static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700423 .dot = { .min = 25000, .max = 350000 },
424 .vco = { .min = 1760000, .max = 3510000 },
425 .n = { .min = 1, .max = 3 },
426 .m = { .min = 79, .max = 118 },
427 .m1 = { .min = 12, .max = 22 },
428 .m2 = { .min = 5, .max = 9 },
429 .p = { .min = 28, .max = 112 },
430 .p1 = { .min = 2, .max = 8 },
431 .p2 = { .dot_limit = 225000,
432 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800433};
434
435static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700436 .dot = { .min = 25000, .max = 350000 },
437 .vco = { .min = 1760000, .max = 3510000 },
438 .n = { .min = 1, .max = 3 },
439 .m = { .min = 79, .max = 127 },
440 .m1 = { .min = 12, .max = 22 },
441 .m2 = { .min = 5, .max = 9 },
442 .p = { .min = 14, .max = 56 },
443 .p1 = { .min = 2, .max = 8 },
444 .p2 = { .dot_limit = 225000,
445 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800446};
447
Eric Anholt273e27c2011-03-30 13:01:10 -0700448/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800449static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700450 .dot = { .min = 25000, .max = 350000 },
451 .vco = { .min = 1760000, .max = 3510000 },
452 .n = { .min = 1, .max = 2 },
453 .m = { .min = 79, .max = 126 },
454 .m1 = { .min = 12, .max = 22 },
455 .m2 = { .min = 5, .max = 9 },
456 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400457 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .p2 = { .dot_limit = 225000,
459 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800460};
461
462static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700463 .dot = { .min = 25000, .max = 350000 },
464 .vco = { .min = 1760000, .max = 3510000 },
465 .n = { .min = 1, .max = 3 },
466 .m = { .min = 79, .max = 126 },
467 .m1 = { .min = 12, .max = 22 },
468 .m2 = { .min = 5, .max = 9 },
469 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400470 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .p2 = { .dot_limit = 225000,
472 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800473};
474
Ville Syrjälädc730512013-09-24 21:26:30 +0300475static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300476 /*
477 * These are the data rate limits (measured in fast clocks)
478 * since those are the strictest limits we have. The fast
479 * clock and actual rate limits are more relaxed, so checking
480 * them would make no difference.
481 */
482 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200483 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700484 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700485 .m1 = { .min = 2, .max = 3 },
486 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300487 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300488 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700489};
490
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300491static const intel_limit_t intel_limits_chv = {
492 /*
493 * These are the data rate limits (measured in fast clocks)
494 * since those are the strictest limits we have. The fast
495 * clock and actual rate limits are more relaxed, so checking
496 * them would make no difference.
497 */
498 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200499 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 .n = { .min = 1, .max = 1 },
501 .m1 = { .min = 2, .max = 2 },
502 .m2 = { .min = 24 << 22, .max = 175 << 22 },
503 .p1 = { .min = 2, .max = 4 },
504 .p2 = { .p2_slow = 1, .p2_fast = 14 },
505};
506
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200507static const intel_limit_t intel_limits_bxt = {
508 /* FIXME: find real dot limits */
509 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530510 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200511 .n = { .min = 1, .max = 1 },
512 .m1 = { .min = 2, .max = 2 },
513 /* FIXME: find real m2 limits */
514 .m2 = { .min = 2 << 22, .max = 255 << 22 },
515 .p1 = { .min = 2, .max = 4 },
516 .p2 = { .p2_slow = 1, .p2_fast = 20 },
517};
518
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200519static bool
520needs_modeset(struct drm_crtc_state *state)
521{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200522 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200523}
524
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300525/**
526 * Returns whether any output on the specified pipe is of the specified type
527 */
Damien Lespiau40935612014-10-29 11:16:59 +0000528bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300529{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300530 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300531 struct intel_encoder *encoder;
532
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300533 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300534 if (encoder->type == type)
535 return true;
536
537 return false;
538}
539
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200540/**
541 * Returns whether any output on the specified pipe will have the specified
542 * type after a staged modeset is complete, i.e., the same as
543 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
544 * encoder->crtc.
545 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
547 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300550 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200552 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200554
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300555 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 if (connector_state->crtc != crtc_state->base.crtc)
557 continue;
558
559 num_connectors++;
560
561 encoder = to_intel_encoder(connector_state->best_encoder);
562 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200563 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200564 }
565
566 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200567
568 return false;
569}
570
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200571static const intel_limit_t *
572intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800573{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200574 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800575 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800576
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200577 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100578 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000579 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800580 limit = &intel_limits_ironlake_dual_lvds_100m;
581 else
582 limit = &intel_limits_ironlake_dual_lvds;
583 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000584 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800585 limit = &intel_limits_ironlake_single_lvds_100m;
586 else
587 limit = &intel_limits_ironlake_single_lvds;
588 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200589 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800590 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800591
592 return limit;
593}
594
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200595static const intel_limit_t *
596intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800597{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200598 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800599 const intel_limit_t *limit;
600
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200601 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100602 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700603 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800604 else
Keith Packarde4b36692009-06-05 19:22:17 -0700605 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200606 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
607 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700608 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200609 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700610 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800611 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700612 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800613
614 return limit;
615}
616
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200617static const intel_limit_t *
618intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800619{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200620 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800621 const intel_limit_t *limit;
622
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200623 if (IS_BROXTON(dev))
624 limit = &intel_limits_bxt;
625 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200626 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800627 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200628 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500629 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200630 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500631 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800632 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500633 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300634 } else if (IS_CHERRYVIEW(dev)) {
635 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700636 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300637 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100638 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200639 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100640 limit = &intel_limits_i9xx_lvds;
641 else
642 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200644 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700645 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200646 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700647 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200648 else
649 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 }
651 return limit;
652}
653
Imre Deakdccbea32015-06-22 23:35:51 +0300654/*
655 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
656 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
657 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
658 * The helpers' return value is the rate of the clock that is fed to the
659 * display engine's pipe which can be the above fast dot clock rate or a
660 * divided-down version of it.
661 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500662/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300663static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800664{
Shaohua Li21778322009-02-23 15:19:16 +0800665 clock->m = clock->m2 + 2;
666 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200667 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300668 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300669 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
670 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300671
672 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800673}
674
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200675static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
676{
677 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
678}
679
Imre Deakdccbea32015-06-22 23:35:51 +0300680static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800681{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200682 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200684 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300685 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300686 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
687 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300688
689 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690}
691
Imre Deakdccbea32015-06-22 23:35:51 +0300692static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300693{
694 clock->m = clock->m1 * clock->m2;
695 clock->p = clock->p1 * clock->p2;
696 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300697 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300698 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
699 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300700
701 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300702}
703
Imre Deakdccbea32015-06-22 23:35:51 +0300704int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300705{
706 clock->m = clock->m1 * clock->m2;
707 clock->p = clock->p1 * clock->p2;
708 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300709 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300710 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
711 clock->n << 22);
712 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300713
714 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300715}
716
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800717#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800718/**
719 * Returns whether the given set of divisors are valid for a given refclk with
720 * the given connectors.
721 */
722
Chris Wilson1b894b52010-12-14 20:04:54 +0000723static bool intel_PLL_is_valid(struct drm_device *dev,
724 const intel_limit_t *limit,
725 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800726{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300727 if (clock->n < limit->n.min || limit->n.max < clock->n)
728 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800729 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400730 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800731 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400732 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800733 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400734 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300735
Wayne Boyer666a4532015-12-09 12:29:35 -0800736 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
737 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300738 if (clock->m1 <= clock->m2)
739 INTELPllInvalid("m1 <= m2\n");
740
Wayne Boyer666a4532015-12-09 12:29:35 -0800741 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300742 if (clock->p < limit->p.min || limit->p.max < clock->p)
743 INTELPllInvalid("p out of range\n");
744 if (clock->m < limit->m.min || limit->m.max < clock->m)
745 INTELPllInvalid("m out of range\n");
746 }
747
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400749 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800750 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
751 * connector, etc., rather than just a single range.
752 */
753 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400754 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800755
756 return true;
757}
758
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300759static int
760i9xx_select_p2_div(const intel_limit_t *limit,
761 const struct intel_crtc_state *crtc_state,
762 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800763{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300764 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800765
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800767 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100768 * For LVDS just rely on its current settings for dual-channel.
769 * We haven't figured out how to reliably set up different
770 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800771 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100772 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300773 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800774 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300775 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800776 } else {
777 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800779 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300780 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800781 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782}
783
784static bool
785i9xx_find_best_dpll(const intel_limit_t *limit,
786 struct intel_crtc_state *crtc_state,
787 int target, int refclk, intel_clock_t *match_clock,
788 intel_clock_t *best_clock)
789{
790 struct drm_device *dev = crtc_state->base.crtc->dev;
791 intel_clock_t clock;
792 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800793
Akshay Joshi0206e352011-08-16 15:34:10 -0400794 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800795
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300796 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
797
Zhao Yakui42158662009-11-20 11:24:18 +0800798 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
799 clock.m1++) {
800 for (clock.m2 = limit->m2.min;
801 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200802 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800803 break;
804 for (clock.n = limit->n.min;
805 clock.n <= limit->n.max; clock.n++) {
806 for (clock.p1 = limit->p1.min;
807 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800808 int this_err;
809
Imre Deakdccbea32015-06-22 23:35:51 +0300810 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000811 if (!intel_PLL_is_valid(dev, limit,
812 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800813 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800814 if (match_clock &&
815 clock.p != match_clock->p)
816 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800817
818 this_err = abs(clock.dot - target);
819 if (this_err < err) {
820 *best_clock = clock;
821 err = this_err;
822 }
823 }
824 }
825 }
826 }
827
828 return (err != target);
829}
830
Ma Lingd4906092009-03-18 20:13:27 +0800831static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200832pnv_find_best_dpll(const intel_limit_t *limit,
833 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200834 int target, int refclk, intel_clock_t *match_clock,
835 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200836{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300837 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200838 intel_clock_t clock;
839 int err = target;
840
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200841 memset(best_clock, 0, sizeof(*best_clock));
842
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300843 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
844
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200845 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
846 clock.m1++) {
847 for (clock.m2 = limit->m2.min;
848 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200849 for (clock.n = limit->n.min;
850 clock.n <= limit->n.max; clock.n++) {
851 for (clock.p1 = limit->p1.min;
852 clock.p1 <= limit->p1.max; clock.p1++) {
853 int this_err;
854
Imre Deakdccbea32015-06-22 23:35:51 +0300855 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800856 if (!intel_PLL_is_valid(dev, limit,
857 &clock))
858 continue;
859 if (match_clock &&
860 clock.p != match_clock->p)
861 continue;
862
863 this_err = abs(clock.dot - target);
864 if (this_err < err) {
865 *best_clock = clock;
866 err = this_err;
867 }
868 }
869 }
870 }
871 }
872
873 return (err != target);
874}
875
Ma Lingd4906092009-03-18 20:13:27 +0800876static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200877g4x_find_best_dpll(const intel_limit_t *limit,
878 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200879 int target, int refclk, intel_clock_t *match_clock,
880 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800881{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300882 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800883 intel_clock_t clock;
884 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300885 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400886 /* approximately equals target * 0.00585 */
887 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800888
889 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300890
891 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
892
Ma Lingd4906092009-03-18 20:13:27 +0800893 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200894 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800895 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200896 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800897 for (clock.m1 = limit->m1.max;
898 clock.m1 >= limit->m1.min; clock.m1--) {
899 for (clock.m2 = limit->m2.max;
900 clock.m2 >= limit->m2.min; clock.m2--) {
901 for (clock.p1 = limit->p1.max;
902 clock.p1 >= limit->p1.min; clock.p1--) {
903 int this_err;
904
Imre Deakdccbea32015-06-22 23:35:51 +0300905 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000906 if (!intel_PLL_is_valid(dev, limit,
907 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800908 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000909
910 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800911 if (this_err < err_most) {
912 *best_clock = clock;
913 err_most = this_err;
914 max_n = clock.n;
915 found = true;
916 }
917 }
918 }
919 }
920 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800921 return found;
922}
Ma Lingd4906092009-03-18 20:13:27 +0800923
Imre Deakd5dd62b2015-03-17 11:40:03 +0200924/*
925 * Check if the calculated PLL configuration is more optimal compared to the
926 * best configuration and error found so far. Return the calculated error.
927 */
928static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
929 const intel_clock_t *calculated_clock,
930 const intel_clock_t *best_clock,
931 unsigned int best_error_ppm,
932 unsigned int *error_ppm)
933{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200934 /*
935 * For CHV ignore the error and consider only the P value.
936 * Prefer a bigger P value based on HW requirements.
937 */
938 if (IS_CHERRYVIEW(dev)) {
939 *error_ppm = 0;
940
941 return calculated_clock->p > best_clock->p;
942 }
943
Imre Deak24be4e42015-03-17 11:40:04 +0200944 if (WARN_ON_ONCE(!target_freq))
945 return false;
946
Imre Deakd5dd62b2015-03-17 11:40:03 +0200947 *error_ppm = div_u64(1000000ULL *
948 abs(target_freq - calculated_clock->dot),
949 target_freq);
950 /*
951 * Prefer a better P value over a better (smaller) error if the error
952 * is small. Ensure this preference for future configurations too by
953 * setting the error to 0.
954 */
955 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
956 *error_ppm = 0;
957
958 return true;
959 }
960
961 return *error_ppm + 10 < best_error_ppm;
962}
963
Zhenyu Wang2c072452009-06-05 15:38:42 +0800964static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200965vlv_find_best_dpll(const intel_limit_t *limit,
966 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200967 int target, int refclk, intel_clock_t *match_clock,
968 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700969{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200970 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300971 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300972 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300973 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300974 /* min update 19.2 MHz */
975 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300976 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700977
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300978 target *= 5; /* fast clock */
979
980 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700981
982 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300983 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300984 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300985 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300986 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300987 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700988 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300989 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200990 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300991
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300992 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
993 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300994
Imre Deakdccbea32015-06-22 23:35:51 +0300995 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300996
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300997 if (!intel_PLL_is_valid(dev, limit,
998 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300999 continue;
1000
Imre Deakd5dd62b2015-03-17 11:40:03 +02001001 if (!vlv_PLL_is_optimal(dev, target,
1002 &clock,
1003 best_clock,
1004 bestppm, &ppm))
1005 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +03001006
Imre Deakd5dd62b2015-03-17 11:40:03 +02001007 *best_clock = clock;
1008 bestppm = ppm;
1009 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001010 }
1011 }
1012 }
1013 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001014
Ville Syrjälä49e497e2013-09-24 21:26:31 +03001015 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001016}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001017
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001018static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001019chv_find_best_dpll(const intel_limit_t *limit,
1020 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001021 int target, int refclk, intel_clock_t *match_clock,
1022 intel_clock_t *best_clock)
1023{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001024 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001025 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001026 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001027 intel_clock_t clock;
1028 uint64_t m2;
1029 int found = false;
1030
1031 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001032 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001033
1034 /*
1035 * Based on hardware doc, the n always set to 1, and m1 always
1036 * set to 2. If requires to support 200Mhz refclk, we need to
1037 * revisit this because n may not 1 anymore.
1038 */
1039 clock.n = 1, clock.m1 = 2;
1040 target *= 5; /* fast clock */
1041
1042 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1043 for (clock.p2 = limit->p2.p2_fast;
1044 clock.p2 >= limit->p2.p2_slow;
1045 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001046 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001047
1048 clock.p = clock.p1 * clock.p2;
1049
1050 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1051 clock.n) << 22, refclk * clock.m1);
1052
1053 if (m2 > INT_MAX/clock.m1)
1054 continue;
1055
1056 clock.m2 = m2;
1057
Imre Deakdccbea32015-06-22 23:35:51 +03001058 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001059
1060 if (!intel_PLL_is_valid(dev, limit, &clock))
1061 continue;
1062
Imre Deak9ca3ba02015-03-17 11:40:05 +02001063 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1064 best_error_ppm, &error_ppm))
1065 continue;
1066
1067 *best_clock = clock;
1068 best_error_ppm = error_ppm;
1069 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001070 }
1071 }
1072
1073 return found;
1074}
1075
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001076bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1077 intel_clock_t *best_clock)
1078{
1079 int refclk = i9xx_get_refclk(crtc_state, 0);
1080
1081 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1082 target_clock, refclk, NULL, best_clock);
1083}
1084
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001085bool intel_crtc_active(struct drm_crtc *crtc)
1086{
1087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1088
1089 /* Be paranoid as we can arrive here with only partial
1090 * state retrieved from the hardware during setup.
1091 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001092 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001093 * as Haswell has gained clock readout/fastboot support.
1094 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001095 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001096 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001097 *
1098 * FIXME: The intel_crtc->active here should be switched to
1099 * crtc->state->active once we have proper CRTC states wired up
1100 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001101 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001102 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001103 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001104}
1105
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001106enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1107 enum pipe pipe)
1108{
1109 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1111
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001112 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001113}
1114
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001115static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1116{
1117 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001118 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001119 u32 line1, line2;
1120 u32 line_mask;
1121
1122 if (IS_GEN2(dev))
1123 line_mask = DSL_LINEMASK_GEN2;
1124 else
1125 line_mask = DSL_LINEMASK_GEN3;
1126
1127 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001128 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001129 line2 = I915_READ(reg) & line_mask;
1130
1131 return line1 == line2;
1132}
1133
Keith Packardab7ad7f2010-10-03 00:33:06 -07001134/*
1135 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001136 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001137 *
1138 * After disabling a pipe, we can't wait for vblank in the usual way,
1139 * spinning on the vblank interrupt status bit, since we won't actually
1140 * see an interrupt when the pipe is disabled.
1141 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001142 * On Gen4 and above:
1143 * wait for the pipe register state bit to turn off
1144 *
1145 * Otherwise:
1146 * wait for the display line value to settle (it usually
1147 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001148 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001149 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001150static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001151{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001152 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001153 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001154 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001155 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001156
Keith Packardab7ad7f2010-10-03 00:33:06 -07001157 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001158 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001159
Keith Packardab7ad7f2010-10-03 00:33:06 -07001160 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001161 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1162 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001163 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001164 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001165 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001166 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001167 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001168 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001169}
1170
Jesse Barnesb24e7172011-01-04 15:09:30 -08001171/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001172void assert_pll(struct drm_i915_private *dev_priv,
1173 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001174{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001175 u32 val;
1176 bool cur_state;
1177
Ville Syrjälä649636e2015-09-22 19:50:01 +03001178 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001179 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001180 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001181 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001182 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001183}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001184
Jani Nikula23538ef2013-08-27 15:12:22 +03001185/* XXX: the dsi pll is shared between MIPI DSI ports */
1186static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1187{
1188 u32 val;
1189 bool cur_state;
1190
Ville Syrjäläa5805162015-05-26 20:42:30 +03001191 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001192 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001193 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001194
1195 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001197 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001198 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001199}
1200#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1201#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1202
Jesse Barnes040484a2011-01-03 12:14:26 -08001203static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, bool state)
1205{
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001207 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1208 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001209
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001210 if (HAS_DDI(dev_priv->dev)) {
1211 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001212 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001213 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001214 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001215 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001216 cur_state = !!(val & FDI_TX_ENABLE);
1217 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001219 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001220 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001221}
1222#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1223#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1224
1225static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1226 enum pipe pipe, bool state)
1227{
Jesse Barnes040484a2011-01-03 12:14:26 -08001228 u32 val;
1229 bool cur_state;
1230
Ville Syrjälä649636e2015-09-22 19:50:01 +03001231 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001232 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001233 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001234 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001235 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001236}
1237#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1238#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1239
1240static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1241 enum pipe pipe)
1242{
Jesse Barnes040484a2011-01-03 12:14:26 -08001243 u32 val;
1244
1245 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001246 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001247 return;
1248
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001249 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001250 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001251 return;
1252
Ville Syrjälä649636e2015-09-22 19:50:01 +03001253 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001254 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 -08001255}
1256
Daniel Vetter55607e82013-06-16 21:42:39 +02001257void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1258 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001259{
Jesse Barnes040484a2011-01-03 12:14:26 -08001260 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001261 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001262
Ville Syrjälä649636e2015-09-22 19:50:01 +03001263 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001264 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001265 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001266 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001267 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001268}
1269
Daniel Vetterb680c372014-09-19 18:27:27 +02001270void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1271 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001273 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001274 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001275 u32 val;
1276 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001277 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001278
Jani Nikulabedd4db2014-08-22 15:04:13 +03001279 if (WARN_ON(HAS_DDI(dev)))
1280 return;
1281
1282 if (HAS_PCH_SPLIT(dev)) {
1283 u32 port_sel;
1284
Jesse Barnesea0760c2011-01-04 15:09:32 -08001285 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001286 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1287
1288 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1289 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
1291 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001292 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001293 /* presumably write lock depends on pipe, not port select */
1294 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1295 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 } else {
1297 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001298 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1299 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001300 }
1301
1302 val = I915_READ(pp_reg);
1303 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001304 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001305 locked = false;
1306
Rob Clarke2c719b2014-12-15 13:56:32 -05001307 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001308 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001309 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001310}
1311
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001312static void assert_cursor(struct drm_i915_private *dev_priv,
1313 enum pipe pipe, bool state)
1314{
1315 struct drm_device *dev = dev_priv->dev;
1316 bool cur_state;
1317
Paulo Zanonid9d82082014-02-27 16:30:56 -03001318 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001319 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001320 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001321 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001322
Rob Clarke2c719b2014-12-15 13:56:32 -05001323 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001324 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001325 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001326}
1327#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1328#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1329
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001330void assert_pipe(struct drm_i915_private *dev_priv,
1331 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001333 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001334 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1335 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001336 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001337
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001338 /* if we need the pipe quirk it must be always on */
1339 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1340 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001341 state = true;
1342
Imre Deak4feed0e2016-02-12 18:55:14 +02001343 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1344 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001346 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001347
1348 intel_display_power_put(dev_priv, power_domain);
1349 } else {
1350 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001351 }
1352
Rob Clarke2c719b2014-12-15 13:56:32 -05001353 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001354 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001355 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356}
1357
Chris Wilson931872f2012-01-16 23:01:13 +00001358static void assert_plane(struct drm_i915_private *dev_priv,
1359 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001361 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001362 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001363
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001365 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001367 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001368 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369}
1370
Chris Wilson931872f2012-01-16 23:01:13 +00001371#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1372#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1373
Jesse Barnesb24e7172011-01-04 15:09:30 -08001374static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1375 enum pipe pipe)
1376{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001377 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001378 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001379
Ville Syrjälä653e1022013-06-04 13:49:05 +03001380 /* Primary planes are fixed to pipes on gen4+ */
1381 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001382 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001383 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001384 "plane %c assertion failure, should be disabled but not\n",
1385 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001386 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001387 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001388
Jesse Barnesb24e7172011-01-04 15:09:30 -08001389 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001390 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001391 u32 val = I915_READ(DSPCNTR(i));
1392 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001393 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001395 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1396 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001397 }
1398}
1399
Jesse Barnes19332d72013-03-28 09:55:38 -07001400static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1401 enum pipe pipe)
1402{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001403 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001404 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001405
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001407 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001408 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001409 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001410 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1411 sprite, pipe_name(pipe));
1412 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001413 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001414 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001415 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001416 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001417 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001418 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 }
1420 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001421 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001422 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001426 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001427 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1429 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001430 }
1431}
1432
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001433static void assert_vblank_disabled(struct drm_crtc *crtc)
1434{
Rob Clarke2c719b2014-12-15 13:56:32 -05001435 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001436 drm_crtc_vblank_put(crtc);
1437}
1438
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001439void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1440 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001441{
Jesse Barnes92f25842011-01-04 15:09:34 -08001442 u32 val;
1443 bool enabled;
1444
Ville Syrjälä649636e2015-09-22 19:50:01 +03001445 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001446 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001448 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1449 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001450}
1451
Keith Packard4e634382011-08-06 10:39:45 -07001452static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1453 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001454{
1455 if ((val & DP_PORT_EN) == 0)
1456 return false;
1457
1458 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001459 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001460 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1461 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001462 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1463 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1464 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001465 } else {
1466 if ((val & DP_PIPE_MASK) != (pipe << 30))
1467 return false;
1468 }
1469 return true;
1470}
1471
Keith Packard1519b992011-08-06 10:35:34 -07001472static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001475 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001476 return false;
1477
1478 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001479 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001480 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001481 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1482 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1483 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001484 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001485 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001486 return false;
1487 }
1488 return true;
1489}
1490
1491static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1492 enum pipe pipe, u32 val)
1493{
1494 if ((val & LVDS_PORT_EN) == 0)
1495 return false;
1496
1497 if (HAS_PCH_CPT(dev_priv->dev)) {
1498 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1499 return false;
1500 } else {
1501 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1502 return false;
1503 }
1504 return true;
1505}
1506
1507static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1508 enum pipe pipe, u32 val)
1509{
1510 if ((val & ADPA_DAC_ENABLE) == 0)
1511 return false;
1512 if (HAS_PCH_CPT(dev_priv->dev)) {
1513 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1514 return false;
1515 } else {
1516 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1517 return false;
1518 }
1519 return true;
1520}
1521
Jesse Barnes291906f2011-02-02 12:28:03 -08001522static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001523 enum pipe pipe, i915_reg_t reg,
1524 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001525{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001526 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001527 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001528 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001529 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001530
Rob Clarke2c719b2014-12-15 13:56:32 -05001531 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001532 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001533 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001534}
1535
1536static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001537 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001538{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001539 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001540 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001541 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001542 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001543
Rob Clarke2c719b2014-12-15 13:56:32 -05001544 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001545 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001546 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001547}
1548
1549static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1550 enum pipe pipe)
1551{
Jesse Barnes291906f2011-02-02 12:28:03 -08001552 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001553
Keith Packardf0575e92011-07-25 22:12:43 -07001554 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1555 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001557
Ville Syrjälä649636e2015-09-22 19:50:01 +03001558 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001559 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001560 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001561 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001562
Ville Syrjälä649636e2015-09-22 19:50:01 +03001563 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001564 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001565 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001566 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001567
Paulo Zanonie2debe92013-02-18 19:00:27 -03001568 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1569 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1570 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001571}
1572
Ville Syrjäläd288f652014-10-28 13:20:22 +02001573static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001574 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001575{
Daniel Vetter426115c2013-07-11 22:13:42 +02001576 struct drm_device *dev = crtc->base.dev;
1577 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001578 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001579 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001580
Daniel Vetter426115c2013-07-11 22:13:42 +02001581 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001582
Daniel Vetter87442f72013-06-06 00:52:17 +02001583 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001584 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001585 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001586
Daniel Vetter426115c2013-07-11 22:13:42 +02001587 I915_WRITE(reg, dpll);
1588 POSTING_READ(reg);
1589 udelay(150);
1590
1591 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1592 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1593
Ville Syrjäläd288f652014-10-28 13:20:22 +02001594 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001595 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001596
1597 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001598 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001601 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001604 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
Ville Syrjäläd288f652014-10-28 13:20:22 +02001609static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001610 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001611{
1612 struct drm_device *dev = crtc->base.dev;
1613 struct drm_i915_private *dev_priv = dev->dev_private;
1614 int pipe = crtc->pipe;
1615 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001616 u32 tmp;
1617
1618 assert_pipe_disabled(dev_priv, crtc->pipe);
1619
Ville Syrjäläa5805162015-05-26 20:42:30 +03001620 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001621
1622 /* Enable back the 10bit clock to display controller */
1623 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1624 tmp |= DPIO_DCLKP_EN;
1625 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1626
Ville Syrjälä54433e92015-05-26 20:42:31 +03001627 mutex_unlock(&dev_priv->sb_lock);
1628
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001629 /*
1630 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1631 */
1632 udelay(1);
1633
1634 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001635 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001636
1637 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001638 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001639 DRM_ERROR("PLL %d failed to lock\n", pipe);
1640
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001641 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001642 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001643 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001644}
1645
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001646static int intel_num_dvo_pipes(struct drm_device *dev)
1647{
1648 struct intel_crtc *crtc;
1649 int count = 0;
1650
1651 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001652 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001653 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001654
1655 return count;
1656}
1657
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001659{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001660 struct drm_device *dev = crtc->base.dev;
1661 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001662 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001663 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001664
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001665 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001666
1667 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001668 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001669
1670 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001671 if (IS_MOBILE(dev) && !IS_I830(dev))
1672 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001673
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001674 /* Enable DVO 2x clock on both PLLs if necessary */
1675 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1676 /*
1677 * It appears to be important that we don't enable this
1678 * for the current pipe before otherwise configuring the
1679 * PLL. No idea how this should be handled if multiple
1680 * DVO outputs are enabled simultaneosly.
1681 */
1682 dpll |= DPLL_DVO_2X_MODE;
1683 I915_WRITE(DPLL(!crtc->pipe),
1684 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1685 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001687 /*
1688 * Apparently we need to have VGA mode enabled prior to changing
1689 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1690 * dividers, even though the register value does change.
1691 */
1692 I915_WRITE(reg, 0);
1693
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001694 I915_WRITE(reg, dpll);
1695
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001696 /* Wait for the clocks to stabilize. */
1697 POSTING_READ(reg);
1698 udelay(150);
1699
1700 if (INTEL_INFO(dev)->gen >= 4) {
1701 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001702 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703 } else {
1704 /* The pixel multiplier can only be updated once the
1705 * DPLL is enabled and the clocks are stable.
1706 *
1707 * So write it again.
1708 */
1709 I915_WRITE(reg, dpll);
1710 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711
1712 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001713 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714 POSTING_READ(reg);
1715 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001716 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717 POSTING_READ(reg);
1718 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001719 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001720 POSTING_READ(reg);
1721 udelay(150); /* wait for warmup */
1722}
1723
1724/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001725 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001726 * @dev_priv: i915 private structure
1727 * @pipe: pipe PLL to disable
1728 *
1729 * Disable the PLL for @pipe, making sure the pipe is off first.
1730 *
1731 * Note! This is for pre-ILK only.
1732 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001733static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001735 struct drm_device *dev = crtc->base.dev;
1736 struct drm_i915_private *dev_priv = dev->dev_private;
1737 enum pipe pipe = crtc->pipe;
1738
1739 /* Disable DVO 2x clock on both PLLs if necessary */
1740 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001741 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001742 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001743 I915_WRITE(DPLL(PIPE_B),
1744 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1745 I915_WRITE(DPLL(PIPE_A),
1746 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1747 }
1748
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001749 /* Don't disable pipe or pipe PLLs if needed */
1750 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1751 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 return;
1753
1754 /* Make sure the pipe isn't still relying on us */
1755 assert_pipe_disabled(dev_priv, pipe);
1756
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001757 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001758 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001759}
1760
Jesse Barnesf6071162013-10-01 10:41:38 -07001761static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1762{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001763 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001764
1765 /* Make sure the pipe isn't still relying on us */
1766 assert_pipe_disabled(dev_priv, pipe);
1767
Imre Deake5cbfbf2014-01-09 17:08:16 +02001768 /*
1769 * Leave integrated clock source and reference clock enabled for pipe B.
1770 * The latter is needed for VGA hotplug / manual detection.
1771 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001772 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001773 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001774 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001775 I915_WRITE(DPLL(pipe), val);
1776 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001777
1778}
1779
1780static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1781{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001782 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001783 u32 val;
1784
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001787
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001788 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001789 val = DPLL_SSC_REF_CLK_CHV |
1790 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001791 if (pipe != PIPE_A)
1792 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1793 I915_WRITE(DPLL(pipe), val);
1794 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001795
Ville Syrjäläa5805162015-05-26 20:42:30 +03001796 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001797
1798 /* Disable 10bit clock to display controller */
1799 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1800 val &= ~DPIO_DCLKP_EN;
1801 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1802
Ville Syrjäläa5805162015-05-26 20:42:30 +03001803 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001804}
1805
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001806void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001807 struct intel_digital_port *dport,
1808 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001809{
1810 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001811 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001812
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001813 switch (dport->port) {
1814 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001816 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001817 break;
1818 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001819 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001820 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001821 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001822 break;
1823 case PORT_D:
1824 port_mask = DPLL_PORTD_READY_MASK;
1825 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001826 break;
1827 default:
1828 BUG();
1829 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001830
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001831 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1832 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1833 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001834}
1835
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001836static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1837 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001838{
Daniel Vetter23670b322012-11-01 09:15:30 +01001839 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001840 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001842 i915_reg_t reg;
1843 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001844
1845 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001846 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001847
1848 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001849 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001850
1851 /* FDI must be feeding us bits for PCH ports */
1852 assert_fdi_tx_enabled(dev_priv, pipe);
1853 assert_fdi_rx_enabled(dev_priv, pipe);
1854
Daniel Vetter23670b322012-11-01 09:15:30 +01001855 if (HAS_PCH_CPT(dev)) {
1856 /* Workaround: Set the timing override bit before enabling the
1857 * pch transcoder. */
1858 reg = TRANS_CHICKEN2(pipe);
1859 val = I915_READ(reg);
1860 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1861 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001862 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001863
Daniel Vetterab9412b2013-05-03 11:49:46 +02001864 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001865 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001866 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001867
1868 if (HAS_PCH_IBX(dev_priv->dev)) {
1869 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001870 * Make the BPC in transcoder be consistent with
1871 * that in pipeconf reg. For HDMI we must use 8bpc
1872 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001873 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001874 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001875 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1876 val |= PIPECONF_8BPC;
1877 else
1878 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001879 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001880
1881 val &= ~TRANS_INTERLACE_MASK;
1882 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001883 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001884 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001885 val |= TRANS_LEGACY_INTERLACED_ILK;
1886 else
1887 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001888 else
1889 val |= TRANS_PROGRESSIVE;
1890
Jesse Barnes040484a2011-01-03 12:14:26 -08001891 I915_WRITE(reg, val | TRANS_ENABLE);
1892 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001893 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001894}
1895
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001896static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001897 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001898{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001899 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001900
1901 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001902 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001904 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001905 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001906 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001907
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001908 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001909 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001910 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001911 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001912
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001913 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001914 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001916 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1917 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001918 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001919 else
1920 val |= TRANS_PROGRESSIVE;
1921
Daniel Vetterab9412b2013-05-03 11:49:46 +02001922 I915_WRITE(LPT_TRANSCONF, val);
1923 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001924 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001925}
1926
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001927static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1928 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001929{
Daniel Vetter23670b322012-11-01 09:15:30 +01001930 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001931 i915_reg_t reg;
1932 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001933
1934 /* FDI relies on the transcoder */
1935 assert_fdi_tx_disabled(dev_priv, pipe);
1936 assert_fdi_rx_disabled(dev_priv, pipe);
1937
Jesse Barnes291906f2011-02-02 12:28:03 -08001938 /* Ports must be off as well */
1939 assert_pch_ports_disabled(dev_priv, pipe);
1940
Daniel Vetterab9412b2013-05-03 11:49:46 +02001941 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001942 val = I915_READ(reg);
1943 val &= ~TRANS_ENABLE;
1944 I915_WRITE(reg, val);
1945 /* wait for PCH transcoder off, transcoder state */
1946 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001947 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001948
Ville Syrjäläc4656132015-10-29 21:25:56 +02001949 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001950 /* Workaround: Clear the timing override chicken bit again. */
1951 reg = TRANS_CHICKEN2(pipe);
1952 val = I915_READ(reg);
1953 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1954 I915_WRITE(reg, val);
1955 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001956}
1957
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001958static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001959{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001960 u32 val;
1961
Daniel Vetterab9412b2013-05-03 11:49:46 +02001962 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001963 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001964 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001965 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001966 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001967 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001968
1969 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001970 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001972 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001973}
1974
1975/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001976 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001977 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001979 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001980 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001982static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001983{
Paulo Zanoni03722642014-01-17 13:51:09 -02001984 struct drm_device *dev = crtc->base.dev;
1985 struct drm_i915_private *dev_priv = dev->dev_private;
1986 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001987 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001988 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001989 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001990 u32 val;
1991
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001992 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1993
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001994 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001995 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001996 assert_sprites_disabled(dev_priv, pipe);
1997
Paulo Zanoni681e5812012-12-06 11:12:38 -02001998 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001999 pch_transcoder = TRANSCODER_A;
2000 else
2001 pch_transcoder = pipe;
2002
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003 /*
2004 * A pipe without a PLL won't actually be able to drive bits from
2005 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2006 * need the check.
2007 */
Imre Deak50360402015-01-16 00:55:16 -08002008 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002009 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002010 assert_dsi_pll_enabled(dev_priv);
2011 else
2012 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002013 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002014 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002015 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002016 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002017 assert_fdi_tx_pll_enabled(dev_priv,
2018 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002019 }
2020 /* FIXME: assert CPU port conditions for SNB+ */
2021 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002023 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002025 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002026 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2027 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002028 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002029 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002030
2031 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002032 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002033
2034 /*
2035 * Until the pipe starts DSL will read as 0, which would cause
2036 * an apparent vblank timestamp jump, which messes up also the
2037 * frame count when it's derived from the timestamps. So let's
2038 * wait for the pipe to start properly before we call
2039 * drm_crtc_vblank_on()
2040 */
2041 if (dev->max_vblank_count == 0 &&
2042 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2043 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002044}
2045
2046/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002047 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002048 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002050 * Disable the pipe of @crtc, making sure that various hardware
2051 * specific requirements are met, if applicable, e.g. plane
2052 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002053 *
2054 * Will wait until the pipe has shut down before returning.
2055 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002056static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002057{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002058 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002059 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002060 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002061 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002062 u32 val;
2063
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002064 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2065
Jesse Barnesb24e7172011-01-04 15:09:30 -08002066 /*
2067 * Make sure planes won't keep trying to pump pixels to us,
2068 * or we might hang the display.
2069 */
2070 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002071 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002072 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002074 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002075 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002076 if ((val & PIPECONF_ENABLE) == 0)
2077 return;
2078
Ville Syrjälä67adc642014-08-15 01:21:57 +03002079 /*
2080 * Double wide has implications for planes
2081 * so best keep it disabled when not needed.
2082 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002083 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002084 val &= ~PIPECONF_DOUBLE_WIDE;
2085
2086 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002087 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2088 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002089 val &= ~PIPECONF_ENABLE;
2090
2091 I915_WRITE(reg, val);
2092 if ((val & PIPECONF_ENABLE) == 0)
2093 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002094}
2095
Chris Wilson693db182013-03-05 14:52:39 +00002096static bool need_vtd_wa(struct drm_device *dev)
2097{
2098#ifdef CONFIG_INTEL_IOMMU
2099 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2100 return true;
2101#endif
2102 return false;
2103}
2104
Ville Syrjälä832be822016-01-12 21:08:33 +02002105static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2106{
2107 return IS_GEN2(dev_priv) ? 2048 : 4096;
2108}
2109
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002110static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2111 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002112{
2113 switch (fb_modifier) {
2114 case DRM_FORMAT_MOD_NONE:
2115 return cpp;
2116 case I915_FORMAT_MOD_X_TILED:
2117 if (IS_GEN2(dev_priv))
2118 return 128;
2119 else
2120 return 512;
2121 case I915_FORMAT_MOD_Y_TILED:
2122 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2123 return 128;
2124 else
2125 return 512;
2126 case I915_FORMAT_MOD_Yf_TILED:
2127 switch (cpp) {
2128 case 1:
2129 return 64;
2130 case 2:
2131 case 4:
2132 return 128;
2133 case 8:
2134 case 16:
2135 return 256;
2136 default:
2137 MISSING_CASE(cpp);
2138 return cpp;
2139 }
2140 break;
2141 default:
2142 MISSING_CASE(fb_modifier);
2143 return cpp;
2144 }
2145}
2146
Ville Syrjälä832be822016-01-12 21:08:33 +02002147unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2148 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002149{
Ville Syrjälä832be822016-01-12 21:08:33 +02002150 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2151 return 1;
2152 else
2153 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002154 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002155}
2156
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002157/* Return the tile dimensions in pixel units */
2158static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2159 unsigned int *tile_width,
2160 unsigned int *tile_height,
2161 uint64_t fb_modifier,
2162 unsigned int cpp)
2163{
2164 unsigned int tile_width_bytes =
2165 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2166
2167 *tile_width = tile_width_bytes / cpp;
2168 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2169}
2170
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002171unsigned int
2172intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002173 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002174{
Ville Syrjälä832be822016-01-12 21:08:33 +02002175 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2176 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2177
2178 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002179}
2180
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002181unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2182{
2183 unsigned int size = 0;
2184 int i;
2185
2186 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2187 size += rot_info->plane[i].width * rot_info->plane[i].height;
2188
2189 return size;
2190}
2191
Daniel Vetter75c82a52015-10-14 16:51:04 +02002192static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002193intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2194 const struct drm_framebuffer *fb,
2195 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002196{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002197 if (intel_rotation_90_or_270(rotation)) {
2198 *view = i915_ggtt_view_rotated;
2199 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2200 } else {
2201 *view = i915_ggtt_view_normal;
2202 }
2203}
2204
2205static void
2206intel_fill_fb_info(struct drm_i915_private *dev_priv,
2207 struct drm_framebuffer *fb)
2208{
2209 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002210 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002211
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002212 tile_size = intel_tile_size(dev_priv);
2213
2214 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002215 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2216 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002217
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002218 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2219 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002220
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002221 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002222 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002223 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2224 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002225
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002226 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002227 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2228 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002229 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002230}
2231
Ville Syrjälä603525d2016-01-12 21:08:37 +02002232static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002233{
2234 if (INTEL_INFO(dev_priv)->gen >= 9)
2235 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002236 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002237 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002238 return 128 * 1024;
2239 else if (INTEL_INFO(dev_priv)->gen >= 4)
2240 return 4 * 1024;
2241 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002242 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002243}
2244
Ville Syrjälä603525d2016-01-12 21:08:37 +02002245static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2246 uint64_t fb_modifier)
2247{
2248 switch (fb_modifier) {
2249 case DRM_FORMAT_MOD_NONE:
2250 return intel_linear_alignment(dev_priv);
2251 case I915_FORMAT_MOD_X_TILED:
2252 if (INTEL_INFO(dev_priv)->gen >= 9)
2253 return 256 * 1024;
2254 return 0;
2255 case I915_FORMAT_MOD_Y_TILED:
2256 case I915_FORMAT_MOD_Yf_TILED:
2257 return 1 * 1024 * 1024;
2258 default:
2259 MISSING_CASE(fb_modifier);
2260 return 0;
2261 }
2262}
2263
Chris Wilson127bd2a2010-07-23 23:32:05 +01002264int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002265intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2266 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002267{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002268 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002269 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002270 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002271 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002272 u32 alignment;
2273 int ret;
2274
Matt Roperebcdd392014-07-09 16:22:11 -07002275 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2276
Ville Syrjälä603525d2016-01-12 21:08:37 +02002277 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002278
Ville Syrjälä3465c582016-02-15 22:54:43 +02002279 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002280
Chris Wilson693db182013-03-05 14:52:39 +00002281 /* Note that the w/a also requires 64 PTE of padding following the
2282 * bo. We currently fill all unused PTE with the shadow page and so
2283 * we should always have valid PTE following the scanout preventing
2284 * the VT-d warning.
2285 */
2286 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2287 alignment = 256 * 1024;
2288
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002289 /*
2290 * Global gtt pte registers are special registers which actually forward
2291 * writes to a chunk of system memory. Which means that there is no risk
2292 * that the register values disappear as soon as we call
2293 * intel_runtime_pm_put(), so it is correct to wrap only the
2294 * pin/unpin/fence and not more.
2295 */
2296 intel_runtime_pm_get(dev_priv);
2297
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002298 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2299 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002300 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002301 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002302
2303 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2304 * fence, whereas 965+ only requires a fence if using
2305 * framebuffer compression. For simplicity, we always install
2306 * a fence as the cost is not that onerous.
2307 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002308 if (view.type == I915_GGTT_VIEW_NORMAL) {
2309 ret = i915_gem_object_get_fence(obj);
2310 if (ret == -EDEADLK) {
2311 /*
2312 * -EDEADLK means there are no free fences
2313 * no pending flips.
2314 *
2315 * This is propagated to atomic, but it uses
2316 * -EDEADLK to force a locking recovery, so
2317 * change the returned error to -EBUSY.
2318 */
2319 ret = -EBUSY;
2320 goto err_unpin;
2321 } else if (ret)
2322 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002323
Vivek Kasireddy98072162015-10-29 18:54:38 -07002324 i915_gem_object_pin_fence(obj);
2325 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002326
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002327 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002328 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002329
2330err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002331 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002332err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002333 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002334 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002335}
2336
Ville Syrjälä3465c582016-02-15 22:54:43 +02002337static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002338{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002340 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002341
Matt Roperebcdd392014-07-09 16:22:11 -07002342 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2343
Ville Syrjälä3465c582016-02-15 22:54:43 +02002344 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002345
Vivek Kasireddy98072162015-10-29 18:54:38 -07002346 if (view.type == I915_GGTT_VIEW_NORMAL)
2347 i915_gem_object_unpin_fence(obj);
2348
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002349 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002350}
2351
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002352/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002353 * Adjust the tile offset by moving the difference into
2354 * the x/y offsets.
2355 *
2356 * Input tile dimensions and pitch must already be
2357 * rotated to match x and y, and in pixel units.
2358 */
2359static u32 intel_adjust_tile_offset(int *x, int *y,
2360 unsigned int tile_width,
2361 unsigned int tile_height,
2362 unsigned int tile_size,
2363 unsigned int pitch_tiles,
2364 u32 old_offset,
2365 u32 new_offset)
2366{
2367 unsigned int tiles;
2368
2369 WARN_ON(old_offset & (tile_size - 1));
2370 WARN_ON(new_offset & (tile_size - 1));
2371 WARN_ON(new_offset > old_offset);
2372
2373 tiles = (old_offset - new_offset) / tile_size;
2374
2375 *y += tiles / pitch_tiles * tile_height;
2376 *x += tiles % pitch_tiles * tile_width;
2377
2378 return new_offset;
2379}
2380
2381/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002382 * Computes the linear offset to the base tile and adjusts
2383 * x, y. bytes per pixel is assumed to be a power-of-two.
2384 *
2385 * In the 90/270 rotated case, x and y are assumed
2386 * to be already rotated to match the rotated GTT view, and
2387 * pitch is the tile_height aligned framebuffer height.
2388 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002389u32 intel_compute_tile_offset(int *x, int *y,
2390 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002391 unsigned int pitch,
2392 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002393{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002394 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2395 uint64_t fb_modifier = fb->modifier[plane];
2396 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002397 u32 offset, offset_aligned, alignment;
2398
2399 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2400 if (alignment)
2401 alignment--;
2402
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002403 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002404 unsigned int tile_size, tile_width, tile_height;
2405 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002406
Ville Syrjäläd8433102016-01-12 21:08:35 +02002407 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002408 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2409 fb_modifier, cpp);
2410
2411 if (intel_rotation_90_or_270(rotation)) {
2412 pitch_tiles = pitch / tile_height;
2413 swap(tile_width, tile_height);
2414 } else {
2415 pitch_tiles = pitch / (tile_width * cpp);
2416 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002417
Ville Syrjäläd8433102016-01-12 21:08:35 +02002418 tile_rows = *y / tile_height;
2419 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002420
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002421 tiles = *x / tile_width;
2422 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002423
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002424 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2425 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002426
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002427 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2428 tile_size, pitch_tiles,
2429 offset, offset_aligned);
2430 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002431 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002432 offset_aligned = offset & ~alignment;
2433
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002434 *y = (offset & alignment) / pitch;
2435 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002436 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002437
2438 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002439}
2440
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002441static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002442{
2443 switch (format) {
2444 case DISPPLANE_8BPP:
2445 return DRM_FORMAT_C8;
2446 case DISPPLANE_BGRX555:
2447 return DRM_FORMAT_XRGB1555;
2448 case DISPPLANE_BGRX565:
2449 return DRM_FORMAT_RGB565;
2450 default:
2451 case DISPPLANE_BGRX888:
2452 return DRM_FORMAT_XRGB8888;
2453 case DISPPLANE_RGBX888:
2454 return DRM_FORMAT_XBGR8888;
2455 case DISPPLANE_BGRX101010:
2456 return DRM_FORMAT_XRGB2101010;
2457 case DISPPLANE_RGBX101010:
2458 return DRM_FORMAT_XBGR2101010;
2459 }
2460}
2461
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002462static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2463{
2464 switch (format) {
2465 case PLANE_CTL_FORMAT_RGB_565:
2466 return DRM_FORMAT_RGB565;
2467 default:
2468 case PLANE_CTL_FORMAT_XRGB_8888:
2469 if (rgb_order) {
2470 if (alpha)
2471 return DRM_FORMAT_ABGR8888;
2472 else
2473 return DRM_FORMAT_XBGR8888;
2474 } else {
2475 if (alpha)
2476 return DRM_FORMAT_ARGB8888;
2477 else
2478 return DRM_FORMAT_XRGB8888;
2479 }
2480 case PLANE_CTL_FORMAT_XRGB_2101010:
2481 if (rgb_order)
2482 return DRM_FORMAT_XBGR2101010;
2483 else
2484 return DRM_FORMAT_XRGB2101010;
2485 }
2486}
2487
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002488static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002489intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2490 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002491{
2492 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002493 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002494 struct drm_i915_gem_object *obj = NULL;
2495 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002496 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002497 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2498 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2499 PAGE_SIZE);
2500
2501 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502
Chris Wilsonff2652e2014-03-10 08:07:02 +00002503 if (plane_config->size == 0)
2504 return false;
2505
Paulo Zanoni3badb492015-09-23 12:52:23 -03002506 /* If the FB is too big, just don't use it since fbdev is not very
2507 * important and we should probably use that space with FBC or other
2508 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002509 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002510 return false;
2511
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002512 mutex_lock(&dev->struct_mutex);
2513
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002514 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2515 base_aligned,
2516 base_aligned,
2517 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002518 if (!obj) {
2519 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002520 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002521 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522
Damien Lespiau49af4492015-01-20 12:51:44 +00002523 obj->tiling_mode = plane_config->tiling;
2524 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002525 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002526
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002527 mode_cmd.pixel_format = fb->pixel_format;
2528 mode_cmd.width = fb->width;
2529 mode_cmd.height = fb->height;
2530 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002531 mode_cmd.modifier[0] = fb->modifier[0];
2532 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002533
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002534 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002535 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536 DRM_DEBUG_KMS("intel fb init failed\n");
2537 goto out_unref_obj;
2538 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002539
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002541
Daniel Vetterf6936e22015-03-26 12:17:05 +01002542 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002543 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002544
2545out_unref_obj:
2546 drm_gem_object_unreference(&obj->base);
2547 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 return false;
2549}
2550
Matt Roperafd65eb2015-02-03 13:10:04 -08002551/* Update plane->state->fb to match plane->fb after driver-internal updates */
2552static void
2553update_state_fb(struct drm_plane *plane)
2554{
2555 if (plane->fb == plane->state->fb)
2556 return;
2557
2558 if (plane->state->fb)
2559 drm_framebuffer_unreference(plane->state->fb);
2560 plane->state->fb = plane->fb;
2561 if (plane->state->fb)
2562 drm_framebuffer_reference(plane->state->fb);
2563}
2564
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002565static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002566intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2567 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568{
2569 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002570 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571 struct drm_crtc *c;
2572 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002573 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002574 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002575 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002576 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2577 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002578 struct intel_plane_state *intel_state =
2579 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002580 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581
Damien Lespiau2d140302015-02-05 17:22:18 +00002582 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002583 return;
2584
Daniel Vetterf6936e22015-03-26 12:17:05 +01002585 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002586 fb = &plane_config->fb->base;
2587 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002588 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589
Damien Lespiau2d140302015-02-05 17:22:18 +00002590 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002591
2592 /*
2593 * Failed to alloc the obj, check to see if we should share
2594 * an fb with another CRTC instead
2595 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002596 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002597 i = to_intel_crtc(c);
2598
2599 if (c == &intel_crtc->base)
2600 continue;
2601
Matt Roper2ff8fde2014-07-08 07:50:07 -07002602 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603 continue;
2604
Daniel Vetter88595ac2015-03-26 12:42:24 +01002605 fb = c->primary->fb;
2606 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002607 continue;
2608
Daniel Vetter88595ac2015-03-26 12:42:24 +01002609 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002610 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002611 drm_framebuffer_reference(fb);
2612 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002613 }
2614 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002615
Matt Roper200757f2015-12-03 11:37:36 -08002616 /*
2617 * We've failed to reconstruct the BIOS FB. Current display state
2618 * indicates that the primary plane is visible, but has a NULL FB,
2619 * which will lead to problems later if we don't fix it up. The
2620 * simplest solution is to just disable the primary plane now and
2621 * pretend the BIOS never had it enabled.
2622 */
2623 to_intel_plane_state(plane_state)->visible = false;
2624 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002625 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002626 intel_plane->disable_plane(primary, &intel_crtc->base);
2627
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628 return;
2629
2630valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002631 plane_state->src_x = 0;
2632 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002633 plane_state->src_w = fb->width << 16;
2634 plane_state->src_h = fb->height << 16;
2635
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002636 plane_state->crtc_x = 0;
2637 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002638 plane_state->crtc_w = fb->width;
2639 plane_state->crtc_h = fb->height;
2640
Matt Roper0a8d8a82015-12-03 11:37:38 -08002641 intel_state->src.x1 = plane_state->src_x;
2642 intel_state->src.y1 = plane_state->src_y;
2643 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2644 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2645 intel_state->dst.x1 = plane_state->crtc_x;
2646 intel_state->dst.y1 = plane_state->crtc_y;
2647 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2648 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2649
Daniel Vetter88595ac2015-03-26 12:42:24 +01002650 obj = intel_fb_obj(fb);
2651 if (obj->tiling_mode != I915_TILING_NONE)
2652 dev_priv->preserve_bios_swizzle = true;
2653
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002654 drm_framebuffer_reference(fb);
2655 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002656 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002657 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002658 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002659}
2660
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002661static void i9xx_update_primary_plane(struct drm_plane *primary,
2662 const struct intel_crtc_state *crtc_state,
2663 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002664{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002665 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002666 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2668 struct drm_framebuffer *fb = plane_state->base.fb;
2669 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002670 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002671 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002672 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002673 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002674 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002675 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002676 int x = plane_state->src.x1 >> 16;
2677 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002678
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002679 dspcntr = DISPPLANE_GAMMA_ENABLE;
2680
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002681 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002682
2683 if (INTEL_INFO(dev)->gen < 4) {
2684 if (intel_crtc->pipe == PIPE_B)
2685 dspcntr |= DISPPLANE_SEL_PIPE_B;
2686
2687 /* pipesrc and dspsize control the size that is scaled from,
2688 * which should always be the user's requested size.
2689 */
2690 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002691 ((crtc_state->pipe_src_h - 1) << 16) |
2692 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002693 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002694 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2695 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002696 ((crtc_state->pipe_src_h - 1) << 16) |
2697 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002698 I915_WRITE(PRIMPOS(plane), 0);
2699 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002700 }
2701
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 switch (fb->pixel_format) {
2703 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002704 dspcntr |= DISPPLANE_8BPP;
2705 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002708 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 case DRM_FORMAT_RGB565:
2710 dspcntr |= DISPPLANE_BGRX565;
2711 break;
2712 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002713 dspcntr |= DISPPLANE_BGRX888;
2714 break;
2715 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716 dspcntr |= DISPPLANE_RGBX888;
2717 break;
2718 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002719 dspcntr |= DISPPLANE_BGRX101010;
2720 break;
2721 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002722 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002723 break;
2724 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002725 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002726 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002727
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002728 if (INTEL_INFO(dev)->gen >= 4 &&
2729 obj->tiling_mode != I915_TILING_NONE)
2730 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002731
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002732 if (IS_G4X(dev))
2733 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2734
Ville Syrjäläac484962016-01-20 21:05:26 +02002735 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002736
Daniel Vetterc2c75132012-07-05 12:17:30 +02002737 if (INTEL_INFO(dev)->gen >= 4) {
2738 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002739 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002740 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002741 linear_offset -= intel_crtc->dspaddr_offset;
2742 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002743 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002744 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002745
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002746 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302747 dspcntr |= DISPPLANE_ROTATE_180;
2748
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002749 x += (crtc_state->pipe_src_w - 1);
2750 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302751
2752 /* Finding the last pixel of the last line of the display
2753 data and adding to linear_offset*/
2754 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002755 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002756 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302757 }
2758
Paulo Zanoni2db33662015-09-14 15:20:03 -03002759 intel_crtc->adjusted_x = x;
2760 intel_crtc->adjusted_y = y;
2761
Sonika Jindal48404c12014-08-22 14:06:04 +05302762 I915_WRITE(reg, dspcntr);
2763
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002764 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002765 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002766 I915_WRITE(DSPSURF(plane),
2767 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002769 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002770 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002771 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773}
2774
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002775static void i9xx_disable_primary_plane(struct drm_plane *primary,
2776 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777{
2778 struct drm_device *dev = crtc->dev;
2779 struct drm_i915_private *dev_priv = dev->dev_private;
2780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002781 int plane = intel_crtc->plane;
2782
2783 I915_WRITE(DSPCNTR(plane), 0);
2784 if (INTEL_INFO(dev_priv)->gen >= 4)
2785 I915_WRITE(DSPSURF(plane), 0);
2786 else
2787 I915_WRITE(DSPADDR(plane), 0);
2788 POSTING_READ(DSPCNTR(plane));
2789}
2790
2791static void ironlake_update_primary_plane(struct drm_plane *primary,
2792 const struct intel_crtc_state *crtc_state,
2793 const struct intel_plane_state *plane_state)
2794{
2795 struct drm_device *dev = primary->dev;
2796 struct drm_i915_private *dev_priv = dev->dev_private;
2797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2798 struct drm_framebuffer *fb = plane_state->base.fb;
2799 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002801 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002802 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002803 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002804 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002806 int x = plane_state->src.x1 >> 16;
2807 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002808
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002809 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002810 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002811
2812 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2813 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2814
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 switch (fb->pixel_format) {
2816 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 dspcntr |= DISPPLANE_8BPP;
2818 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 case DRM_FORMAT_RGB565:
2820 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002823 dspcntr |= DISPPLANE_BGRX888;
2824 break;
2825 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 dspcntr |= DISPPLANE_RGBX888;
2827 break;
2828 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002829 dspcntr |= DISPPLANE_BGRX101010;
2830 break;
2831 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002832 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002833 break;
2834 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002835 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843
Ville Syrjäläac484962016-01-20 21:05:26 +02002844 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002845 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002846 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002847 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002848 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002849 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302850 dspcntr |= DISPPLANE_ROTATE_180;
2851
2852 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002853 x += (crtc_state->pipe_src_w - 1);
2854 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302855
2856 /* Finding the last pixel of the last line of the display
2857 data and adding to linear_offset*/
2858 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002859 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002860 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302861 }
2862 }
2863
Paulo Zanoni2db33662015-09-14 15:20:03 -03002864 intel_crtc->adjusted_x = x;
2865 intel_crtc->adjusted_y = y;
2866
Sonika Jindal48404c12014-08-22 14:06:04 +05302867 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002868
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002869 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002870 I915_WRITE(DSPSURF(plane),
2871 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002872 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002873 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2874 } else {
2875 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2876 I915_WRITE(DSPLINOFF(plane), linear_offset);
2877 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002879}
2880
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002881u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2882 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002883{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002884 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2885 return 64;
2886 } else {
2887 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002888
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002889 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002890 }
2891}
2892
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002893u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2894 struct drm_i915_gem_object *obj,
2895 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002896{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002897 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002898 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002899 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002900
Ville Syrjäläe7941292016-01-19 18:23:17 +02002901 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002902 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002903
Daniel Vetterce7f1722015-10-14 16:51:06 +02002904 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002905 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002906 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002907 return -1;
2908
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002909 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002910
2911 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002912 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002913 PAGE_SIZE;
2914 }
2915
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002916 WARN_ON(upper_32_bits(offset));
2917
2918 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002919}
2920
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002921static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2922{
2923 struct drm_device *dev = intel_crtc->base.dev;
2924 struct drm_i915_private *dev_priv = dev->dev_private;
2925
2926 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2927 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2928 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002929}
2930
Chandra Kondurua1b22782015-04-07 15:28:45 -07002931/*
2932 * This function detaches (aka. unbinds) unused scalers in hardware
2933 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002934static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002935{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002936 struct intel_crtc_scaler_state *scaler_state;
2937 int i;
2938
Chandra Kondurua1b22782015-04-07 15:28:45 -07002939 scaler_state = &intel_crtc->config->scaler_state;
2940
2941 /* loop through and disable scalers that aren't in use */
2942 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002943 if (!scaler_state->scalers[i].in_use)
2944 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002945 }
2946}
2947
Chandra Konduru6156a452015-04-27 13:48:39 -07002948u32 skl_plane_ctl_format(uint32_t pixel_format)
2949{
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002951 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 /*
2960 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2961 * to be already pre-multiplied. We need to add a knob (or a different
2962 * DRM_FORMAT) for user-space to configure that.
2963 */
2964 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002981 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002983 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002985
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987}
2988
2989u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2990{
Chandra Konduru6156a452015-04-27 13:48:39 -07002991 switch (fb_modifier) {
2992 case DRM_FORMAT_MOD_NONE:
2993 break;
2994 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002997 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002999 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 default:
3001 MISSING_CASE(fb_modifier);
3002 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003003
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003004 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005}
3006
3007u32 skl_plane_ctl_rotation(unsigned int rotation)
3008{
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 switch (rotation) {
3010 case BIT(DRM_ROTATE_0):
3011 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303012 /*
3013 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3014 * while i915 HW rotation is clockwise, thats why this swapping.
3015 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003016 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303017 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003019 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303021 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 default:
3023 MISSING_CASE(rotation);
3024 }
3025
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003026 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027}
3028
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003029static void skylake_update_primary_plane(struct drm_plane *plane,
3030 const struct intel_crtc_state *crtc_state,
3031 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003032{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003033 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3036 struct drm_framebuffer *fb = plane_state->base.fb;
3037 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003038 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303039 u32 plane_ctl, stride_div, stride;
3040 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003041 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003043 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003044 int scaler_id = plane_state->scaler_id;
3045 int src_x = plane_state->src.x1 >> 16;
3046 int src_y = plane_state->src.y1 >> 16;
3047 int src_w = drm_rect_width(&plane_state->src) >> 16;
3048 int src_h = drm_rect_height(&plane_state->src) >> 16;
3049 int dst_x = plane_state->dst.x1;
3050 int dst_y = plane_state->dst.y1;
3051 int dst_w = drm_rect_width(&plane_state->dst);
3052 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053
3054 plane_ctl = PLANE_CTL_ENABLE |
3055 PLANE_CTL_PIPE_GAMMA_ENABLE |
3056 PLANE_CTL_PIPE_CSC_ENABLE;
3057
Chandra Konduru6156a452015-04-27 13:48:39 -07003058 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3059 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003060 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003061 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003062
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003063 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003064 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003065 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003067 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003068
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003070 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3071
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303072 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003073 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303074 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003075 x_offset = stride * tile_height - src_y - src_h;
3076 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303078 } else {
3079 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003080 x_offset = src_x;
3081 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003082 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 }
3084 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003085
Paulo Zanoni2db33662015-09-14 15:20:03 -03003086 intel_crtc->adjusted_x = x_offset;
3087 intel_crtc->adjusted_y = y_offset;
3088
Damien Lespiau70d21f02013-07-03 21:06:04 +01003089 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3091 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3092 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003093
3094 if (scaler_id >= 0) {
3095 uint32_t ps_ctrl = 0;
3096
3097 WARN_ON(!dst_w || !dst_h);
3098 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3099 crtc_state->scaler_state.scalers[scaler_id].mode;
3100 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3101 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3102 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3103 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3104 I915_WRITE(PLANE_POS(pipe, 0), 0);
3105 } else {
3106 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3107 }
3108
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003109 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003110
3111 POSTING_READ(PLANE_SURF(pipe, 0));
3112}
3113
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003114static void skylake_disable_primary_plane(struct drm_plane *primary,
3115 struct drm_crtc *crtc)
3116{
3117 struct drm_device *dev = crtc->dev;
3118 struct drm_i915_private *dev_priv = dev->dev_private;
3119 int pipe = to_intel_crtc(crtc)->pipe;
3120
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003121 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3122 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3123 POSTING_READ(PLANE_SURF(pipe, 0));
3124}
3125
Jesse Barnes17638cd2011-06-24 12:19:23 -07003126/* Assume fb object is pinned & idle & fenced and just update base pointers */
3127static int
3128intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3129 int x, int y, enum mode_set_atomic state)
3130{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003131 /* Support for kgdboc is disabled, this needs a major rework. */
3132 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003133
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003134 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003135}
3136
Ville Syrjälä75147472014-11-24 18:28:11 +02003137static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003138{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003139 struct drm_crtc *crtc;
3140
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003141 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3143 enum plane plane = intel_crtc->plane;
3144
3145 intel_prepare_page_flip(dev, plane);
3146 intel_finish_page_flip_plane(dev, plane);
3147 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003148}
3149
3150static void intel_update_primary_planes(struct drm_device *dev)
3151{
Ville Syrjälä75147472014-11-24 18:28:11 +02003152 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003153
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003154 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003155 struct intel_plane *plane = to_intel_plane(crtc->primary);
3156 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003157
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003158 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003159 plane_state = to_intel_plane_state(plane->base.state);
3160
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003161 if (plane_state->visible)
3162 plane->update_plane(&plane->base,
3163 to_intel_crtc_state(crtc->state),
3164 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003165
3166 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003167 }
3168}
3169
Ville Syrjälä75147472014-11-24 18:28:11 +02003170void intel_prepare_reset(struct drm_device *dev)
3171{
3172 /* no reset support for gen2 */
3173 if (IS_GEN2(dev))
3174 return;
3175
3176 /* reset doesn't touch the display */
3177 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3178 return;
3179
3180 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003181 /*
3182 * Disabling the crtcs gracefully seems nicer. Also the
3183 * g33 docs say we should at least disable all the planes.
3184 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003185 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003186}
3187
3188void intel_finish_reset(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = to_i915(dev);
3191
3192 /*
3193 * Flips in the rings will be nuked by the reset,
3194 * so complete all pending flips so that user space
3195 * will get its events and not get stuck.
3196 */
3197 intel_complete_page_flips(dev);
3198
3199 /* no reset support for gen2 */
3200 if (IS_GEN2(dev))
3201 return;
3202
3203 /* reset doesn't touch the display */
3204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3205 /*
3206 * Flips in the rings have been nuked by the reset,
3207 * so update the base address of all primary
3208 * planes to the the last fb to make sure we're
3209 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003210 *
3211 * FIXME: Atomic will make this obsolete since we won't schedule
3212 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003213 */
3214 intel_update_primary_planes(dev);
3215 return;
3216 }
3217
3218 /*
3219 * The display has been reset as well,
3220 * so need a full re-initialization.
3221 */
3222 intel_runtime_pm_disable_interrupts(dev_priv);
3223 intel_runtime_pm_enable_interrupts(dev_priv);
3224
3225 intel_modeset_init_hw(dev);
3226
3227 spin_lock_irq(&dev_priv->irq_lock);
3228 if (dev_priv->display.hpd_irq_setup)
3229 dev_priv->display.hpd_irq_setup(dev);
3230 spin_unlock_irq(&dev_priv->irq_lock);
3231
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003232 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003233
3234 intel_hpd_init(dev_priv);
3235
3236 drm_modeset_unlock_all(dev);
3237}
3238
Chris Wilson7d5e3792014-03-04 13:15:08 +00003239static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3240{
3241 struct drm_device *dev = crtc->dev;
3242 struct drm_i915_private *dev_priv = dev->dev_private;
3243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003244 bool pending;
3245
3246 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3247 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3248 return false;
3249
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003250 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003251 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003252 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003253
3254 return pending;
3255}
3256
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003257static void intel_update_pipe_config(struct intel_crtc *crtc,
3258 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003259{
3260 struct drm_device *dev = crtc->base.dev;
3261 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003262 struct intel_crtc_state *pipe_config =
3263 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003264
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003265 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3266 crtc->base.mode = crtc->base.state->mode;
3267
3268 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3269 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3270 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003271
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003272 if (HAS_DDI(dev))
3273 intel_set_pipe_csc(&crtc->base);
3274
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003275 /*
3276 * Update pipe size and adjust fitter if needed: the reason for this is
3277 * that in compute_mode_changes we check the native mode (not the pfit
3278 * mode) to see if we can flip rather than do a full mode set. In the
3279 * fastboot case, we'll flip, but if we don't update the pipesrc and
3280 * pfit state, we'll end up with a big fb scanned out into the wrong
3281 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003282 */
3283
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003284 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003285 ((pipe_config->pipe_src_w - 1) << 16) |
3286 (pipe_config->pipe_src_h - 1));
3287
3288 /* on skylake this is done by detaching scalers */
3289 if (INTEL_INFO(dev)->gen >= 9) {
3290 skl_detach_scalers(crtc);
3291
3292 if (pipe_config->pch_pfit.enabled)
3293 skylake_pfit_enable(crtc);
3294 } else if (HAS_PCH_SPLIT(dev)) {
3295 if (pipe_config->pch_pfit.enabled)
3296 ironlake_pfit_enable(crtc);
3297 else if (old_crtc_state->pch_pfit.enabled)
3298 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003299 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003300}
3301
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003302static void intel_fdi_normal_train(struct drm_crtc *crtc)
3303{
3304 struct drm_device *dev = crtc->dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3307 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003308 i915_reg_t reg;
3309 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003310
3311 /* enable normal train */
3312 reg = FDI_TX_CTL(pipe);
3313 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003314 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003315 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3316 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003317 } else {
3318 temp &= ~FDI_LINK_TRAIN_NONE;
3319 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003320 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003321 I915_WRITE(reg, temp);
3322
3323 reg = FDI_RX_CTL(pipe);
3324 temp = I915_READ(reg);
3325 if (HAS_PCH_CPT(dev)) {
3326 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3327 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3328 } else {
3329 temp &= ~FDI_LINK_TRAIN_NONE;
3330 temp |= FDI_LINK_TRAIN_NONE;
3331 }
3332 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3333
3334 /* wait one idle pattern time */
3335 POSTING_READ(reg);
3336 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003337
3338 /* IVB wants error correction enabled */
3339 if (IS_IVYBRIDGE(dev))
3340 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3341 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003342}
3343
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003344/* The FDI link training functions for ILK/Ibexpeak. */
3345static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3350 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003351 i915_reg_t reg;
3352 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003354 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003355 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003356
Adam Jacksone1a44742010-06-25 15:32:14 -04003357 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3358 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 reg = FDI_RX_IMR(pipe);
3360 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 temp &= ~FDI_RX_SYMBOL_LOCK;
3362 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 I915_WRITE(reg, temp);
3364 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003365 udelay(150);
3366
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 reg = FDI_TX_CTL(pipe);
3369 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003370 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003371 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003372 temp &= ~FDI_LINK_TRAIN_NONE;
3373 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003375
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 reg = FDI_RX_CTL(pipe);
3377 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3381
3382 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 udelay(150);
3384
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003385 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003386 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3387 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3388 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003389
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003391 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3394
3395 if ((temp & FDI_RX_BIT_LOCK)) {
3396 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398 break;
3399 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
3404 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 temp &= ~FDI_LINK_TRAIN_NONE;
3408 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 reg = FDI_RX_CTL(pipe);
3412 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 temp &= ~FDI_LINK_TRAIN_NONE;
3414 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 I915_WRITE(reg, temp);
3416
3417 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 udelay(150);
3419
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003422 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3424
3425 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 DRM_DEBUG_KMS("FDI train 2 done.\n");
3428 break;
3429 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433
3434 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003435
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436}
3437
Akshay Joshi0206e352011-08-16 15:34:10 -04003438static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3440 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3441 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3442 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3443};
3444
3445/* The FDI link training functions for SNB/Cougarpoint. */
3446static void gen6_fdi_link_train(struct drm_crtc *crtc)
3447{
3448 struct drm_device *dev = crtc->dev;
3449 struct drm_i915_private *dev_priv = dev->dev_private;
3450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3451 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003452 i915_reg_t reg;
3453 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454
Adam Jacksone1a44742010-06-25 15:32:14 -04003455 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3456 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 reg = FDI_RX_IMR(pipe);
3458 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003459 temp &= ~FDI_RX_SYMBOL_LOCK;
3460 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 I915_WRITE(reg, temp);
3462
3463 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003464 udelay(150);
3465
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003469 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003470 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 temp &= ~FDI_LINK_TRAIN_NONE;
3472 temp |= FDI_LINK_TRAIN_PATTERN_1;
3473 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3474 /* SNB-B */
3475 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477
Daniel Vetterd74cf322012-10-26 10:58:13 +02003478 I915_WRITE(FDI_RX_MISC(pipe),
3479 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3480
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 reg = FDI_RX_CTL(pipe);
3482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483 if (HAS_PCH_CPT(dev)) {
3484 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3486 } else {
3487 temp &= ~FDI_LINK_TRAIN_NONE;
3488 temp |= FDI_LINK_TRAIN_PATTERN_1;
3489 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3491
3492 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 udelay(150);
3494
Akshay Joshi0206e352011-08-16 15:34:10 -04003495 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 reg = FDI_TX_CTL(pipe);
3497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3499 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 I915_WRITE(reg, temp);
3501
3502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 udelay(500);
3504
Sean Paulfa37d392012-03-02 12:53:39 -05003505 for (retry = 0; retry < 5; retry++) {
3506 reg = FDI_RX_IIR(pipe);
3507 temp = I915_READ(reg);
3508 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3509 if (temp & FDI_RX_BIT_LOCK) {
3510 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3511 DRM_DEBUG_KMS("FDI train 1 done.\n");
3512 break;
3513 }
3514 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 }
Sean Paulfa37d392012-03-02 12:53:39 -05003516 if (retry < 5)
3517 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 }
3519 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521
3522 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 reg = FDI_TX_CTL(pipe);
3524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 temp &= ~FDI_LINK_TRAIN_NONE;
3526 temp |= FDI_LINK_TRAIN_PATTERN_2;
3527 if (IS_GEN6(dev)) {
3528 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3529 /* SNB-B */
3530 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3531 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536 if (HAS_PCH_CPT(dev)) {
3537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3538 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3539 } else {
3540 temp &= ~FDI_LINK_TRAIN_NONE;
3541 temp |= FDI_LINK_TRAIN_PATTERN_2;
3542 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003543 I915_WRITE(reg, temp);
3544
3545 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546 udelay(150);
3547
Akshay Joshi0206e352011-08-16 15:34:10 -04003548 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 reg = FDI_TX_CTL(pipe);
3550 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3552 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003553 I915_WRITE(reg, temp);
3554
3555 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 udelay(500);
3557
Sean Paulfa37d392012-03-02 12:53:39 -05003558 for (retry = 0; retry < 5; retry++) {
3559 reg = FDI_RX_IIR(pipe);
3560 temp = I915_READ(reg);
3561 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3562 if (temp & FDI_RX_SYMBOL_LOCK) {
3563 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3564 DRM_DEBUG_KMS("FDI train 2 done.\n");
3565 break;
3566 }
3567 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568 }
Sean Paulfa37d392012-03-02 12:53:39 -05003569 if (retry < 5)
3570 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 }
3572 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003573 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574
3575 DRM_DEBUG_KMS("FDI train done.\n");
3576}
3577
Jesse Barnes357555c2011-04-28 15:09:55 -07003578/* Manual link training for Ivy Bridge A0 parts */
3579static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3580{
3581 struct drm_device *dev = crtc->dev;
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3584 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003585 i915_reg_t reg;
3586 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003587
3588 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3589 for train result */
3590 reg = FDI_RX_IMR(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_RX_SYMBOL_LOCK;
3593 temp &= ~FDI_RX_BIT_LOCK;
3594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
3597 udelay(150);
3598
Daniel Vetter01a415f2012-10-27 15:58:40 +02003599 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3600 I915_READ(FDI_RX_IIR(pipe)));
3601
Jesse Barnes139ccd32013-08-19 11:04:55 -07003602 /* Try each vswing and preemphasis setting twice before moving on */
3603 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3604 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003605 reg = FDI_TX_CTL(pipe);
3606 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003607 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3608 temp &= ~FDI_TX_ENABLE;
3609 I915_WRITE(reg, temp);
3610
3611 reg = FDI_RX_CTL(pipe);
3612 temp = I915_READ(reg);
3613 temp &= ~FDI_LINK_TRAIN_AUTO;
3614 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3615 temp &= ~FDI_RX_ENABLE;
3616 I915_WRITE(reg, temp);
3617
3618 /* enable CPU FDI TX and PCH FDI RX */
3619 reg = FDI_TX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003622 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003623 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003624 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 temp |= snb_b_fdi_train_param[j/2];
3626 temp |= FDI_COMPOSITE_SYNC;
3627 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3628
3629 I915_WRITE(FDI_RX_MISC(pipe),
3630 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3631
3632 reg = FDI_RX_CTL(pipe);
3633 temp = I915_READ(reg);
3634 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3635 temp |= FDI_COMPOSITE_SYNC;
3636 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3637
3638 POSTING_READ(reg);
3639 udelay(1); /* should be 0.5us */
3640
3641 for (i = 0; i < 4; i++) {
3642 reg = FDI_RX_IIR(pipe);
3643 temp = I915_READ(reg);
3644 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3645
3646 if (temp & FDI_RX_BIT_LOCK ||
3647 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3648 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3649 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3650 i);
3651 break;
3652 }
3653 udelay(1); /* should be 0.5us */
3654 }
3655 if (i == 4) {
3656 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3657 continue;
3658 }
3659
3660 /* Train 2 */
3661 reg = FDI_TX_CTL(pipe);
3662 temp = I915_READ(reg);
3663 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3664 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3665 I915_WRITE(reg, temp);
3666
3667 reg = FDI_RX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3670 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003671 I915_WRITE(reg, temp);
3672
3673 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003674 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003675
Jesse Barnes139ccd32013-08-19 11:04:55 -07003676 for (i = 0; i < 4; i++) {
3677 reg = FDI_RX_IIR(pipe);
3678 temp = I915_READ(reg);
3679 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003680
Jesse Barnes139ccd32013-08-19 11:04:55 -07003681 if (temp & FDI_RX_SYMBOL_LOCK ||
3682 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3683 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3684 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3685 i);
3686 goto train_done;
3687 }
3688 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003689 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003690 if (i == 4)
3691 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003692 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003693
Jesse Barnes139ccd32013-08-19 11:04:55 -07003694train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003695 DRM_DEBUG_KMS("FDI train done.\n");
3696}
3697
Daniel Vetter88cefb62012-08-12 19:27:14 +02003698static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003699{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003700 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003701 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003703 i915_reg_t reg;
3704 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003705
Jesse Barnes0e23b992010-09-10 11:10:00 -07003706 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003707 reg = FDI_RX_CTL(pipe);
3708 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003709 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003710 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003711 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003712 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3713
3714 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003715 udelay(200);
3716
3717 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003718 temp = I915_READ(reg);
3719 I915_WRITE(reg, temp | FDI_PCDCLK);
3720
3721 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003722 udelay(200);
3723
Paulo Zanoni20749732012-11-23 15:30:38 -02003724 /* Enable CPU FDI TX PLL, always on for Ironlake */
3725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3728 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003729
Paulo Zanoni20749732012-11-23 15:30:38 -02003730 POSTING_READ(reg);
3731 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003732 }
3733}
3734
Daniel Vetter88cefb62012-08-12 19:27:14 +02003735static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3736{
3737 struct drm_device *dev = intel_crtc->base.dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003740 i915_reg_t reg;
3741 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003742
3743 /* Switch from PCDclk to Rawclk */
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3747
3748 /* Disable CPU FDI TX PLL */
3749 reg = FDI_TX_CTL(pipe);
3750 temp = I915_READ(reg);
3751 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
3754 udelay(100);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3759
3760 /* Wait for the clocks to turn off. */
3761 POSTING_READ(reg);
3762 udelay(100);
3763}
3764
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003765static void ironlake_fdi_disable(struct drm_crtc *crtc)
3766{
3767 struct drm_device *dev = crtc->dev;
3768 struct drm_i915_private *dev_priv = dev->dev_private;
3769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3770 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003771 i915_reg_t reg;
3772 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003773
3774 /* disable CPU FDI tx and PCH FDI rx */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3778 POSTING_READ(reg);
3779
3780 reg = FDI_RX_CTL(pipe);
3781 temp = I915_READ(reg);
3782 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003783 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003784 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3785
3786 POSTING_READ(reg);
3787 udelay(100);
3788
3789 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003790 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003791 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003792
3793 /* still set train pattern 1 */
3794 reg = FDI_TX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 temp &= ~FDI_LINK_TRAIN_NONE;
3797 temp |= FDI_LINK_TRAIN_PATTERN_1;
3798 I915_WRITE(reg, temp);
3799
3800 reg = FDI_RX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 if (HAS_PCH_CPT(dev)) {
3803 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3804 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3805 } else {
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 }
3809 /* BPC in FDI rx is consistent with that in PIPECONF */
3810 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003811 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003812 I915_WRITE(reg, temp);
3813
3814 POSTING_READ(reg);
3815 udelay(100);
3816}
3817
Chris Wilson5dce5b932014-01-20 10:17:36 +00003818bool intel_has_pending_fb_unpin(struct drm_device *dev)
3819{
3820 struct intel_crtc *crtc;
3821
3822 /* Note that we don't need to be called with mode_config.lock here
3823 * as our list of CRTC objects is static for the lifetime of the
3824 * device and so cannot disappear as we iterate. Similarly, we can
3825 * happily treat the predicates as racy, atomic checks as userspace
3826 * cannot claim and pin a new fb without at least acquring the
3827 * struct_mutex and so serialising with us.
3828 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003829 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003830 if (atomic_read(&crtc->unpin_work_count) == 0)
3831 continue;
3832
3833 if (crtc->unpin_work)
3834 intel_wait_for_vblank(dev, crtc->pipe);
3835
3836 return true;
3837 }
3838
3839 return false;
3840}
3841
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003842static void page_flip_completed(struct intel_crtc *intel_crtc)
3843{
3844 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3845 struct intel_unpin_work *work = intel_crtc->unpin_work;
3846
3847 /* ensure that the unpin work is consistent wrt ->pending. */
3848 smp_rmb();
3849 intel_crtc->unpin_work = NULL;
3850
3851 if (work->event)
3852 drm_send_vblank_event(intel_crtc->base.dev,
3853 intel_crtc->pipe,
3854 work->event);
3855
3856 drm_crtc_vblank_put(&intel_crtc->base);
3857
3858 wake_up_all(&dev_priv->pending_flip_queue);
3859 queue_work(dev_priv->wq, &work->work);
3860
3861 trace_i915_flip_complete(intel_crtc->plane,
3862 work->pending_flip_obj);
3863}
3864
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003865static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003866{
Chris Wilson0f911282012-04-17 10:05:38 +01003867 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003868 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003869 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003870
Daniel Vetter2c10d572012-12-20 21:24:07 +01003871 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003872
3873 ret = wait_event_interruptible_timeout(
3874 dev_priv->pending_flip_queue,
3875 !intel_crtc_has_pending_flip(crtc),
3876 60*HZ);
3877
3878 if (ret < 0)
3879 return ret;
3880
3881 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003883
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003884 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003885 if (intel_crtc->unpin_work) {
3886 WARN_ONCE(1, "Removing stuck page flip\n");
3887 page_flip_completed(intel_crtc);
3888 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003889 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003890 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003891
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003892 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003893}
3894
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003895static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3896{
3897 u32 temp;
3898
3899 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3900
3901 mutex_lock(&dev_priv->sb_lock);
3902
3903 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3904 temp |= SBI_SSCCTL_DISABLE;
3905 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3906
3907 mutex_unlock(&dev_priv->sb_lock);
3908}
3909
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910/* Program iCLKIP clock to the desired frequency */
3911static void lpt_program_iclkip(struct drm_crtc *crtc)
3912{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003913 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003914 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3916 u32 temp;
3917
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003918 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003920 /* The iCLK virtual clock root frequency is in MHz,
3921 * but the adjusted_mode->crtc_clock in in KHz. To get the
3922 * divisors, it is necessary to divide one by another, so we
3923 * convert the virtual clock precision to KHz here for higher
3924 * precision.
3925 */
3926 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927 u32 iclk_virtual_root_freq = 172800 * 1000;
3928 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003929 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003931 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3932 clock << auxdiv);
3933 divsel = (desired_divisor / iclk_pi_range) - 2;
3934 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003936 /*
3937 * Near 20MHz is a corner case which is
3938 * out of range for the 7-bit divisor
3939 */
3940 if (divsel <= 0x7f)
3941 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942 }
3943
3944 /* This should not happen with any sane values */
3945 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3946 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3947 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3948 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3949
3950 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 +03003951 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952 auxdiv,
3953 divsel,
3954 phasedir,
3955 phaseinc);
3956
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003957 mutex_lock(&dev_priv->sb_lock);
3958
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003960 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003961 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3962 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3963 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3964 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3965 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3966 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003967 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968
3969 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003970 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3972 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003973 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003974
3975 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003976 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003978 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003980 mutex_unlock(&dev_priv->sb_lock);
3981
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003982 /* Wait for initialization time */
3983 udelay(24);
3984
3985 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3986}
3987
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003988int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3989{
3990 u32 divsel, phaseinc, auxdiv;
3991 u32 iclk_virtual_root_freq = 172800 * 1000;
3992 u32 iclk_pi_range = 64;
3993 u32 desired_divisor;
3994 u32 temp;
3995
3996 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3997 return 0;
3998
3999 mutex_lock(&dev_priv->sb_lock);
4000
4001 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4002 if (temp & SBI_SSCCTL_DISABLE) {
4003 mutex_unlock(&dev_priv->sb_lock);
4004 return 0;
4005 }
4006
4007 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4008 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4009 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4010 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4011 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4012
4013 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4014 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4015 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4016
4017 mutex_unlock(&dev_priv->sb_lock);
4018
4019 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4020
4021 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4022 desired_divisor << auxdiv);
4023}
4024
Daniel Vetter275f01b22013-05-03 11:49:47 +02004025static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4026 enum pipe pch_transcoder)
4027{
4028 struct drm_device *dev = crtc->base.dev;
4029 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004030 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004031
4032 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4033 I915_READ(HTOTAL(cpu_transcoder)));
4034 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4035 I915_READ(HBLANK(cpu_transcoder)));
4036 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4037 I915_READ(HSYNC(cpu_transcoder)));
4038
4039 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4040 I915_READ(VTOTAL(cpu_transcoder)));
4041 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4042 I915_READ(VBLANK(cpu_transcoder)));
4043 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4044 I915_READ(VSYNC(cpu_transcoder)));
4045 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4046 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4047}
4048
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004049static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004050{
4051 struct drm_i915_private *dev_priv = dev->dev_private;
4052 uint32_t temp;
4053
4054 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004055 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004056 return;
4057
4058 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4059 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4060
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004061 temp &= ~FDI_BC_BIFURCATION_SELECT;
4062 if (enable)
4063 temp |= FDI_BC_BIFURCATION_SELECT;
4064
4065 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004066 I915_WRITE(SOUTH_CHICKEN1, temp);
4067 POSTING_READ(SOUTH_CHICKEN1);
4068}
4069
4070static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4071{
4072 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073
4074 switch (intel_crtc->pipe) {
4075 case PIPE_A:
4076 break;
4077 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004078 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004079 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004080 else
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 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004085 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004086
4087 break;
4088 default:
4089 BUG();
4090 }
4091}
4092
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004093/* Return which DP Port should be selected for Transcoder DP control */
4094static enum port
4095intel_trans_dp_port_sel(struct drm_crtc *crtc)
4096{
4097 struct drm_device *dev = crtc->dev;
4098 struct intel_encoder *encoder;
4099
4100 for_each_encoder_on_crtc(dev, crtc, encoder) {
4101 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4102 encoder->type == INTEL_OUTPUT_EDP)
4103 return enc_to_dig_port(&encoder->base)->port;
4104 }
4105
4106 return -1;
4107}
4108
Jesse Barnesf67a5592011-01-05 10:31:48 -08004109/*
4110 * Enable PCH resources required for PCH ports:
4111 * - PCH PLLs
4112 * - FDI training & RX/TX
4113 * - update transcoder timings
4114 * - DP transcoding bits
4115 * - transcoder
4116 */
4117static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004118{
4119 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004120 struct drm_i915_private *dev_priv = dev->dev_private;
4121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4122 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004123 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004124
Daniel Vetterab9412b2013-05-03 11:49:46 +02004125 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004126
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004127 if (IS_IVYBRIDGE(dev))
4128 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4129
Daniel Vettercd986ab2012-10-26 10:58:12 +02004130 /* Write the TU size bits before fdi link training, so that error
4131 * detection works. */
4132 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4133 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4134
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004135 /*
4136 * Sometimes spurious CPU pipe underruns happen during FDI
4137 * training, at least with VGA+HDMI cloning. Suppress them.
4138 */
4139 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4140
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004142 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004143
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004144 /* We need to program the right clock selection before writing the pixel
4145 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004146 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004147 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004148
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004150 temp |= TRANS_DPLL_ENABLE(pipe);
4151 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004152 if (intel_crtc->config->shared_dpll ==
4153 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004154 temp |= sel;
4155 else
4156 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004160 /* XXX: pch pll's can be enabled any time before we enable the PCH
4161 * transcoder, and we actually should do this to not upset any PCH
4162 * transcoder that already use the clock when we share it.
4163 *
4164 * Note that enable_shared_dpll tries to do the right thing, but
4165 * get_shared_dpll unconditionally resets the pll - we need that to have
4166 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004167 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004168
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004169 /* set transcoder timing, panel must allow it */
4170 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004171 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004173 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004174
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004175 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4176
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004178 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004179 const struct drm_display_mode *adjusted_mode =
4180 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004181 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004182 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004183 temp = I915_READ(reg);
4184 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004185 TRANS_DP_SYNC_MASK |
4186 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004187 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004188 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004190 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004191 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004192 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004194
4195 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004196 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004197 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004199 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004202 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004204 break;
4205 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004206 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004207 }
4208
Chris Wilson5eddb702010-09-11 13:48:45 +01004209 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004210 }
4211
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004212 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004213}
4214
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004215static void lpt_pch_enable(struct drm_crtc *crtc)
4216{
4217 struct drm_device *dev = crtc->dev;
4218 struct drm_i915_private *dev_priv = dev->dev_private;
4219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004220 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004221
Daniel Vetterab9412b2013-05-03 11:49:46 +02004222 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004223
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004224 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004225
Paulo Zanoni0540e482012-10-31 18:12:40 -02004226 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004227 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004228
Paulo Zanoni937bb612012-10-31 18:12:47 -02004229 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004230}
4231
Daniel Vettera1520312013-05-03 11:49:50 +02004232static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004233{
4234 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004235 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004236 u32 temp;
4237
4238 temp = I915_READ(dslreg);
4239 udelay(500);
4240 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004241 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004242 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004243 }
4244}
4245
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004246static int
4247skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4248 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4249 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 struct intel_crtc_scaler_state *scaler_state =
4252 &crtc_state->scaler_state;
4253 struct intel_crtc *intel_crtc =
4254 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004256
4257 need_scaling = intel_rotation_90_or_270(rotation) ?
4258 (src_h != dst_w || src_w != dst_h):
4259 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004260
4261 /*
4262 * if plane is being disabled or scaler is no more required or force detach
4263 * - free scaler binded to this plane/crtc
4264 * - in order to do this, update crtc->scaler_usage
4265 *
4266 * Here scaler state in crtc_state is set free so that
4267 * scaler can be assigned to other user. Actual register
4268 * update to free the scaler is done in plane/panel-fit programming.
4269 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4270 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004272 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004274 scaler_state->scalers[*scaler_id].in_use = 0;
4275
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4277 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4278 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004279 scaler_state->scaler_users);
4280 *scaler_id = -1;
4281 }
4282 return 0;
4283 }
4284
4285 /* range checks */
4286 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4287 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4288
4289 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4290 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004292 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004294 return -EINVAL;
4295 }
4296
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004297 /* mark this plane as a scaler user in crtc_state */
4298 scaler_state->scaler_users |= (1 << scaler_user);
4299 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4300 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4301 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4302 scaler_state->scaler_users);
4303
4304 return 0;
4305}
4306
4307/**
4308 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4309 *
4310 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004311 *
4312 * Return
4313 * 0 - scaler_usage updated successfully
4314 * error - requested scaling cannot be supported or other error condition
4315 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004316int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004317{
4318 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004319 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320
4321 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4322 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4323
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004324 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004325 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004326 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004327 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004328}
4329
4330/**
4331 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4332 *
4333 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004334 * @plane_state: atomic plane state to update
4335 *
4336 * Return
4337 * 0 - scaler_usage updated successfully
4338 * error - requested scaling cannot be supported or other error condition
4339 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004340static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4341 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004342{
4343
4344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004345 struct intel_plane *intel_plane =
4346 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004347 struct drm_framebuffer *fb = plane_state->base.fb;
4348 int ret;
4349
4350 bool force_detach = !fb || !plane_state->visible;
4351
4352 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4353 intel_plane->base.base.id, intel_crtc->pipe,
4354 drm_plane_index(&intel_plane->base));
4355
4356 ret = skl_update_scaler(crtc_state, force_detach,
4357 drm_plane_index(&intel_plane->base),
4358 &plane_state->scaler_id,
4359 plane_state->base.rotation,
4360 drm_rect_width(&plane_state->src) >> 16,
4361 drm_rect_height(&plane_state->src) >> 16,
4362 drm_rect_width(&plane_state->dst),
4363 drm_rect_height(&plane_state->dst));
4364
4365 if (ret || plane_state->scaler_id < 0)
4366 return ret;
4367
Chandra Kondurua1b22782015-04-07 15:28:45 -07004368 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004369 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004370 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004371 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004372 return -EINVAL;
4373 }
4374
4375 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004376 switch (fb->pixel_format) {
4377 case DRM_FORMAT_RGB565:
4378 case DRM_FORMAT_XBGR8888:
4379 case DRM_FORMAT_XRGB8888:
4380 case DRM_FORMAT_ABGR8888:
4381 case DRM_FORMAT_ARGB8888:
4382 case DRM_FORMAT_XRGB2101010:
4383 case DRM_FORMAT_XBGR2101010:
4384 case DRM_FORMAT_YUYV:
4385 case DRM_FORMAT_YVYU:
4386 case DRM_FORMAT_UYVY:
4387 case DRM_FORMAT_VYUY:
4388 break;
4389 default:
4390 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4391 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4392 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004393 }
4394
Chandra Kondurua1b22782015-04-07 15:28:45 -07004395 return 0;
4396}
4397
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004398static void skylake_scaler_disable(struct intel_crtc *crtc)
4399{
4400 int i;
4401
4402 for (i = 0; i < crtc->num_scalers; i++)
4403 skl_detach_scaler(crtc, i);
4404}
4405
4406static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004407{
4408 struct drm_device *dev = crtc->base.dev;
4409 struct drm_i915_private *dev_priv = dev->dev_private;
4410 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004411 struct intel_crtc_scaler_state *scaler_state =
4412 &crtc->config->scaler_state;
4413
4414 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4415
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004416 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004417 int id;
4418
4419 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4420 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4421 return;
4422 }
4423
4424 id = scaler_state->scaler_id;
4425 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4426 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4427 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4428 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4429
4430 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004431 }
4432}
4433
Jesse Barnesb074cec2013-04-25 12:55:02 -07004434static void ironlake_pfit_enable(struct intel_crtc *crtc)
4435{
4436 struct drm_device *dev = crtc->base.dev;
4437 struct drm_i915_private *dev_priv = dev->dev_private;
4438 int pipe = crtc->pipe;
4439
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004440 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004441 /* Force use of hard-coded filter coefficients
4442 * as some pre-programmed values are broken,
4443 * e.g. x201.
4444 */
4445 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4446 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4447 PF_PIPE_SEL_IVB(pipe));
4448 else
4449 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004450 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4451 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004452 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004453}
4454
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004455void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004456{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004457 struct drm_device *dev = crtc->base.dev;
4458 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004459
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004460 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004461 return;
4462
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004463 /* We can only enable IPS after we enable a plane and wait for a vblank */
4464 intel_wait_for_vblank(dev, crtc->pipe);
4465
Paulo Zanonid77e4532013-09-24 13:52:55 -03004466 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004467 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004468 mutex_lock(&dev_priv->rps.hw_lock);
4469 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4470 mutex_unlock(&dev_priv->rps.hw_lock);
4471 /* Quoting Art Runyan: "its not safe to expect any particular
4472 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004473 * mailbox." Moreover, the mailbox may return a bogus state,
4474 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004475 */
4476 } else {
4477 I915_WRITE(IPS_CTL, IPS_ENABLE);
4478 /* The bit only becomes 1 in the next vblank, so this wait here
4479 * is essentially intel_wait_for_vblank. If we don't have this
4480 * and don't wait for vblanks until the end of crtc_enable, then
4481 * the HW state readout code will complain that the expected
4482 * IPS_CTL value is not the one we read. */
4483 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4484 DRM_ERROR("Timed out waiting for IPS enable\n");
4485 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004486}
4487
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004488void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004489{
4490 struct drm_device *dev = crtc->base.dev;
4491 struct drm_i915_private *dev_priv = dev->dev_private;
4492
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004493 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004494 return;
4495
4496 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004497 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004498 mutex_lock(&dev_priv->rps.hw_lock);
4499 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4500 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004501 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4502 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4503 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004504 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004505 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004506 POSTING_READ(IPS_CTL);
4507 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004508
4509 /* We need to wait for a vblank before we can disable the plane. */
4510 intel_wait_for_vblank(dev, crtc->pipe);
4511}
4512
4513/** Loads the palette/gamma unit for the CRTC with the prepared values */
4514static void intel_crtc_load_lut(struct drm_crtc *crtc)
4515{
4516 struct drm_device *dev = crtc->dev;
4517 struct drm_i915_private *dev_priv = dev->dev_private;
4518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4519 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004520 int i;
4521 bool reenable_ips = false;
4522
4523 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004524 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004525 return;
4526
Imre Deak50360402015-01-16 00:55:16 -08004527 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Jani Nikulaa65347b2015-11-27 12:21:46 +02004528 if (intel_crtc->config->has_dsi_encoder)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004529 assert_dsi_pll_enabled(dev_priv);
4530 else
4531 assert_pll_enabled(dev_priv, pipe);
4532 }
4533
Paulo Zanonid77e4532013-09-24 13:52:55 -03004534 /* Workaround : Do not read or write the pipe palette/gamma data while
4535 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4536 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004537 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004538 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4539 GAMMA_MODE_MODE_SPLIT)) {
4540 hsw_disable_ips(intel_crtc);
4541 reenable_ips = true;
4542 }
4543
4544 for (i = 0; i < 256; i++) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004545 i915_reg_t palreg;
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004546
4547 if (HAS_GMCH_DISPLAY(dev))
4548 palreg = PALETTE(pipe, i);
4549 else
4550 palreg = LGC_PALETTE(pipe, i);
4551
4552 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004553 (intel_crtc->lut_r[i] << 16) |
4554 (intel_crtc->lut_g[i] << 8) |
4555 intel_crtc->lut_b[i]);
4556 }
4557
4558 if (reenable_ips)
4559 hsw_enable_ips(intel_crtc);
4560}
4561
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004562static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004563{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004564 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004565 struct drm_device *dev = intel_crtc->base.dev;
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567
4568 mutex_lock(&dev->struct_mutex);
4569 dev_priv->mm.interruptible = false;
4570 (void) intel_overlay_switch_off(intel_crtc->overlay);
4571 dev_priv->mm.interruptible = true;
4572 mutex_unlock(&dev->struct_mutex);
4573 }
4574
4575 /* Let userspace switch the overlay on again. In most cases userspace
4576 * has to recompute where to put it anyway.
4577 */
4578}
4579
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004580/**
4581 * intel_post_enable_primary - Perform operations after enabling primary plane
4582 * @crtc: the CRTC whose primary plane was just enabled
4583 *
4584 * Performs potentially sleeping operations that must be done after the primary
4585 * plane is enabled, such as updating FBC and IPS. Note that this may be
4586 * called due to an explicit primary plane update, or due to an implicit
4587 * re-enable that is caused when a sprite plane is updated to no longer
4588 * completely hide the primary plane.
4589 */
4590static void
4591intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004592{
4593 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004594 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004597
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004598 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004599 * FIXME IPS should be fine as long as one plane is
4600 * enabled, but in practice it seems to have problems
4601 * when going from primary only to sprite only and vice
4602 * versa.
4603 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004604 hsw_enable_ips(intel_crtc);
4605
Daniel Vetterf99d7062014-06-19 16:01:59 +02004606 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004607 * Gen2 reports pipe underruns whenever all planes are disabled.
4608 * So don't enable underrun reporting before at least some planes
4609 * are enabled.
4610 * FIXME: Need to fix the logic to work when we turn off all planes
4611 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004612 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004613 if (IS_GEN2(dev))
4614 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4615
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004616 /* Underruns don't always raise interrupts, so check manually. */
4617 intel_check_cpu_fifo_underruns(dev_priv);
4618 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004619}
4620
Ville Syrjälä2622a082016-03-09 19:07:26 +02004621/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004622static void
4623intel_pre_disable_primary(struct drm_crtc *crtc)
4624{
4625 struct drm_device *dev = crtc->dev;
4626 struct drm_i915_private *dev_priv = dev->dev_private;
4627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4628 int pipe = intel_crtc->pipe;
4629
4630 /*
4631 * Gen2 reports pipe underruns whenever all planes are disabled.
4632 * So diasble underrun reporting before all the planes get disabled.
4633 * FIXME: Need to fix the logic to work when we turn off all planes
4634 * but leave the pipe running.
4635 */
4636 if (IS_GEN2(dev))
4637 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4638
4639 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004640 * FIXME IPS should be fine as long as one plane is
4641 * enabled, but in practice it seems to have problems
4642 * when going from primary only to sprite only and vice
4643 * versa.
4644 */
4645 hsw_disable_ips(intel_crtc);
4646}
4647
4648/* FIXME get rid of this and use pre_plane_update */
4649static void
4650intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4651{
4652 struct drm_device *dev = crtc->dev;
4653 struct drm_i915_private *dev_priv = dev->dev_private;
4654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4655 int pipe = intel_crtc->pipe;
4656
4657 intel_pre_disable_primary(crtc);
4658
4659 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004660 * Vblank time updates from the shadow to live plane control register
4661 * are blocked if the memory self-refresh mode is active at that
4662 * moment. So to make sure the plane gets truly disabled, disable
4663 * first the self-refresh mode. The self-refresh enable bit in turn
4664 * will be checked/applied by the HW only at the next frame start
4665 * event which is after the vblank start event, so we need to have a
4666 * wait-for-vblank between disabling the plane and the pipe.
4667 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004668 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004669 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004670 dev_priv->wm.vlv.cxsr = false;
4671 intel_wait_for_vblank(dev, pipe);
4672 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004673}
4674
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004675static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004676{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004677 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4678 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004679 struct intel_crtc_state *pipe_config =
4680 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004681 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004682 struct drm_plane *primary = crtc->base.primary;
4683 struct drm_plane_state *old_pri_state =
4684 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004685
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004686 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004687
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004688 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004689
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004690 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004691 intel_update_watermarks(&crtc->base);
4692
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004693 if (old_pri_state) {
4694 struct intel_plane_state *primary_state =
4695 to_intel_plane_state(primary->state);
4696 struct intel_plane_state *old_primary_state =
4697 to_intel_plane_state(old_pri_state);
4698
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004699 intel_fbc_post_update(crtc);
4700
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004701 if (primary_state->visible &&
4702 (needs_modeset(&pipe_config->base) ||
4703 !old_primary_state->visible))
4704 intel_post_enable_primary(&crtc->base);
4705 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004706}
4707
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004708static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004709{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004710 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004711 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004712 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004713 struct intel_crtc_state *pipe_config =
4714 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004715 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4716 struct drm_plane *primary = crtc->base.primary;
4717 struct drm_plane_state *old_pri_state =
4718 drm_atomic_get_existing_plane_state(old_state, primary);
4719 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004720
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004721 if (old_pri_state) {
4722 struct intel_plane_state *primary_state =
4723 to_intel_plane_state(primary->state);
4724 struct intel_plane_state *old_primary_state =
4725 to_intel_plane_state(old_pri_state);
4726
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004727 intel_fbc_pre_update(crtc);
4728
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004729 if (old_primary_state->visible &&
4730 (modeset || !primary_state->visible))
4731 intel_pre_disable_primary(&crtc->base);
4732 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004733
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004734 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004735 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004736
Ville Syrjälä2622a082016-03-09 19:07:26 +02004737 /*
4738 * Vblank time updates from the shadow to live plane control register
4739 * are blocked if the memory self-refresh mode is active at that
4740 * moment. So to make sure the plane gets truly disabled, disable
4741 * first the self-refresh mode. The self-refresh enable bit in turn
4742 * will be checked/applied by the HW only at the next frame start
4743 * event which is after the vblank start event, so we need to have a
4744 * wait-for-vblank between disabling the plane and the pipe.
4745 */
4746 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004747 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004748 dev_priv->wm.vlv.cxsr = false;
4749 intel_wait_for_vblank(dev, crtc->pipe);
4750 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004751 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004752
Matt Ropered4a6a72016-02-23 17:20:13 -08004753 /*
4754 * IVB workaround: must disable low power watermarks for at least
4755 * one frame before enabling scaling. LP watermarks can be re-enabled
4756 * when scaling is disabled.
4757 *
4758 * WaCxSRDisabledForSpriteScaling:ivb
4759 */
4760 if (pipe_config->disable_lp_wm) {
4761 ilk_disable_lp_wm(dev);
4762 intel_wait_for_vblank(dev, crtc->pipe);
4763 }
4764
4765 /*
4766 * If we're doing a modeset, we're done. No need to do any pre-vblank
4767 * watermark programming here.
4768 */
4769 if (needs_modeset(&pipe_config->base))
4770 return;
4771
4772 /*
4773 * For platforms that support atomic watermarks, program the
4774 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4775 * will be the intermediate values that are safe for both pre- and
4776 * post- vblank; when vblank happens, the 'active' values will be set
4777 * to the final 'target' values and we'll do this again to get the
4778 * optimal watermarks. For gen9+ platforms, the values we program here
4779 * will be the final target values which will get automatically latched
4780 * at vblank time; no further programming will be necessary.
4781 *
4782 * If a platform hasn't been transitioned to atomic watermarks yet,
4783 * we'll continue to update watermarks the old way, if flags tell
4784 * us to.
4785 */
4786 if (dev_priv->display.initial_watermarks != NULL)
4787 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004788 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004789 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004790}
4791
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004792static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004793{
4794 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004796 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004797 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004798
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004799 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004800
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004801 drm_for_each_plane_mask(p, dev, plane_mask)
4802 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004803
Daniel Vetterf99d7062014-06-19 16:01:59 +02004804 /*
4805 * FIXME: Once we grow proper nuclear flip support out of this we need
4806 * to compute the mask of flip planes precisely. For the time being
4807 * consider this a flip to a NULL plane.
4808 */
4809 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004810}
4811
Jesse Barnesf67a5592011-01-05 10:31:48 -08004812static void ironlake_crtc_enable(struct drm_crtc *crtc)
4813{
4814 struct drm_device *dev = crtc->dev;
4815 struct drm_i915_private *dev_priv = dev->dev_private;
4816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004817 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004818 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004819
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004820 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004821 return;
4822
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004823 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004824 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4825
4826 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004827 intel_prepare_shared_dpll(intel_crtc);
4828
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004829 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304830 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004831
4832 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004833 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004834
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004835 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004836 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004837 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004838 }
4839
4840 ironlake_set_pipeconf(crtc);
4841
Jesse Barnesf67a5592011-01-05 10:31:48 -08004842 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004843
Daniel Vettera72e4c92014-09-30 10:56:47 +02004844 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004845
Daniel Vetterf6736a12013-06-05 13:34:30 +02004846 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004847 if (encoder->pre_enable)
4848 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004850 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004851 /* Note: FDI PLL enabling _must_ be done before we enable the
4852 * cpu pipes, hence this is separate from all the other fdi/pch
4853 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004854 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004855 } else {
4856 assert_fdi_tx_disabled(dev_priv, pipe);
4857 assert_fdi_rx_disabled(dev_priv, pipe);
4858 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004859
Jesse Barnesb074cec2013-04-25 12:55:02 -07004860 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004861
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004862 /*
4863 * On ILK+ LUT must be loaded before the pipe is running but with
4864 * clocks enabled
4865 */
4866 intel_crtc_load_lut(crtc);
4867
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004868 if (dev_priv->display.initial_watermarks != NULL)
4869 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004870 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004871
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004872 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004873 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004874
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004875 assert_vblank_disabled(crtc);
4876 drm_crtc_vblank_on(crtc);
4877
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004878 for_each_encoder_on_crtc(dev, crtc, encoder)
4879 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004880
4881 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004882 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004883
4884 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4885 if (intel_crtc->config->has_pch_encoder)
4886 intel_wait_for_vblank(dev, pipe);
4887 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004888}
4889
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004890/* IPS only exists on ULT machines and is tied to pipe A. */
4891static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4892{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004893 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004894}
4895
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004896static void haswell_crtc_enable(struct drm_crtc *crtc)
4897{
4898 struct drm_device *dev = crtc->dev;
4899 struct drm_i915_private *dev_priv = dev->dev_private;
4900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4901 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004902 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004903 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004904 struct intel_crtc_state *pipe_config =
4905 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004906
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004907 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004908 return;
4909
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004910 if (intel_crtc->config->has_pch_encoder)
4911 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4912 false);
4913
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004914 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004915 intel_enable_shared_dpll(intel_crtc);
4916
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004917 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304918 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004919
Jani Nikula4d1de972016-03-18 17:05:42 +02004920 if (!intel_crtc->config->has_dsi_encoder)
4921 intel_set_pipe_timings(intel_crtc);
4922
Jani Nikulabc58be62016-03-18 17:05:39 +02004923 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004924
Jani Nikula4d1de972016-03-18 17:05:42 +02004925 if (cpu_transcoder != TRANSCODER_EDP &&
4926 !transcoder_is_dsi(cpu_transcoder)) {
4927 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004928 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004929 }
4930
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004931 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004932 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004933 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004934 }
4935
Jani Nikula4d1de972016-03-18 17:05:42 +02004936 if (!intel_crtc->config->has_dsi_encoder)
4937 haswell_set_pipeconf(crtc);
4938
Jani Nikula391bf042016-03-18 17:05:40 +02004939 haswell_set_pipe_gamma(crtc);
4940 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004941
4942 intel_set_pipe_csc(crtc);
4943
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004945
Daniel Vetter6b698512015-11-28 11:05:39 +01004946 if (intel_crtc->config->has_pch_encoder)
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4948 else
4949 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4950
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304951 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004952 if (encoder->pre_enable)
4953 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304954 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004955
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004956 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004957 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004958
Jani Nikulaa65347b2015-11-27 12:21:46 +02004959 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304960 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004962 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004963 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004964 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004965 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004966
4967 /*
4968 * On ILK+ LUT must be loaded before the pipe is running but with
4969 * clocks enabled
4970 */
4971 intel_crtc_load_lut(crtc);
4972
Paulo Zanoni1f544382012-10-24 11:32:00 -02004973 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004974 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304975 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004976
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004977 if (dev_priv->display.initial_watermarks != NULL)
4978 dev_priv->display.initial_watermarks(pipe_config);
4979 else
4980 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004981
4982 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4983 if (!intel_crtc->config->has_dsi_encoder)
4984 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004985
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004986 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004987 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004988
Jani Nikulaa65347b2015-11-27 12:21:46 +02004989 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004990 intel_ddi_set_vc_payload_alloc(crtc, true);
4991
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004992 assert_vblank_disabled(crtc);
4993 drm_crtc_vblank_on(crtc);
4994
Jani Nikula8807e552013-08-30 19:40:32 +03004995 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004997 intel_opregion_notify_encoder(encoder, true);
4998 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004999
Daniel Vetter6b698512015-11-28 11:05:39 +01005000 if (intel_crtc->config->has_pch_encoder) {
5001 intel_wait_for_vblank(dev, pipe);
5002 intel_wait_for_vblank(dev, pipe);
5003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005004 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5005 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005006 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005007
Paulo Zanonie4916942013-09-20 16:21:19 -03005008 /* If we change the relative order between pipe/planes enabling, we need
5009 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005010 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5011 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5012 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5013 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5014 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005015}
5016
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005017static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005018{
5019 struct drm_device *dev = crtc->base.dev;
5020 struct drm_i915_private *dev_priv = dev->dev_private;
5021 int pipe = crtc->pipe;
5022
5023 /* To avoid upsetting the power well on haswell only disable the pfit if
5024 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005025 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005026 I915_WRITE(PF_CTL(pipe), 0);
5027 I915_WRITE(PF_WIN_POS(pipe), 0);
5028 I915_WRITE(PF_WIN_SZ(pipe), 0);
5029 }
5030}
5031
Jesse Barnes6be4a602010-09-10 10:26:01 -07005032static void ironlake_crtc_disable(struct drm_crtc *crtc)
5033{
5034 struct drm_device *dev = crtc->dev;
5035 struct drm_i915_private *dev_priv = dev->dev_private;
5036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005037 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005038 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005039
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005040 if (intel_crtc->config->has_pch_encoder)
5041 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5042
Daniel Vetterea9d7582012-07-10 10:42:52 +02005043 for_each_encoder_on_crtc(dev, crtc, encoder)
5044 encoder->disable(encoder);
5045
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005046 drm_crtc_vblank_off(crtc);
5047 assert_vblank_disabled(crtc);
5048
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005049 /*
5050 * Sometimes spurious CPU pipe underruns happen when the
5051 * pipe is already disabled, but FDI RX/TX is still enabled.
5052 * Happens at least with VGA+HDMI cloning. Suppress them.
5053 */
5054 if (intel_crtc->config->has_pch_encoder)
5055 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5056
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005057 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005058
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005059 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005060
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005061 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005062 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005063 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5064 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005065
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005066 for_each_encoder_on_crtc(dev, crtc, encoder)
5067 if (encoder->post_disable)
5068 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005069
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005070 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005071 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005072
Daniel Vetterd925c592013-06-05 13:34:04 +02005073 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005074 i915_reg_t reg;
5075 u32 temp;
5076
Daniel Vetterd925c592013-06-05 13:34:04 +02005077 /* disable TRANS_DP_CTL */
5078 reg = TRANS_DP_CTL(pipe);
5079 temp = I915_READ(reg);
5080 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5081 TRANS_DP_PORT_SEL_MASK);
5082 temp |= TRANS_DP_PORT_SEL_NONE;
5083 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005084
Daniel Vetterd925c592013-06-05 13:34:04 +02005085 /* disable DPLL_SEL */
5086 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005087 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005088 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005089 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005090
Daniel Vetterd925c592013-06-05 13:34:04 +02005091 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005092 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005093
5094 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005095}
5096
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097static void haswell_crtc_disable(struct drm_crtc *crtc)
5098{
5099 struct drm_device *dev = crtc->dev;
5100 struct drm_i915_private *dev_priv = dev->dev_private;
5101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5102 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005103 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005104
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005105 if (intel_crtc->config->has_pch_encoder)
5106 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5107 false);
5108
Jani Nikula8807e552013-08-30 19:40:32 +03005109 for_each_encoder_on_crtc(dev, crtc, encoder) {
5110 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005111 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005112 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005113
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005114 drm_crtc_vblank_off(crtc);
5115 assert_vblank_disabled(crtc);
5116
Jani Nikula4d1de972016-03-18 17:05:42 +02005117 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5118 if (!intel_crtc->config->has_dsi_encoder)
5119 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005120
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005121 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005122 intel_ddi_set_vc_payload_alloc(crtc, false);
5123
Jani Nikulaa65347b2015-11-27 12:21:46 +02005124 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305125 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005126
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005127 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005128 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005129 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005130 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005131
Jani Nikulaa65347b2015-11-27 12:21:46 +02005132 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305133 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005134
Imre Deak97b040a2014-06-25 22:01:50 +03005135 for_each_encoder_on_crtc(dev, crtc, encoder)
5136 if (encoder->post_disable)
5137 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005138
Ville Syrjälä92966a32015-12-08 16:05:48 +02005139 if (intel_crtc->config->has_pch_encoder) {
5140 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005141 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005142 intel_ddi_fdi_disable(crtc);
5143
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005144 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5145 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005146 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005147}
5148
Jesse Barnes2dd24552013-04-25 12:55:01 -07005149static void i9xx_pfit_enable(struct intel_crtc *crtc)
5150{
5151 struct drm_device *dev = crtc->base.dev;
5152 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005153 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005154
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005155 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005156 return;
5157
Daniel Vetterc0b03412013-05-28 12:05:54 +02005158 /*
5159 * The panel fitter should only be adjusted whilst the pipe is disabled,
5160 * according to register description and PRM.
5161 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005162 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5163 assert_pipe_disabled(dev_priv, crtc->pipe);
5164
Jesse Barnesb074cec2013-04-25 12:55:02 -07005165 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5166 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005167
5168 /* Border color in case we don't scale up to the full screen. Black by
5169 * default, change to something else for debugging. */
5170 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005171}
5172
Dave Airlied05410f2014-06-05 13:22:59 +10005173static enum intel_display_power_domain port_to_power_domain(enum port port)
5174{
5175 switch (port) {
5176 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005177 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005178 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005179 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005180 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005181 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005182 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005183 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005184 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005185 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005186 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005187 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005188 return POWER_DOMAIN_PORT_OTHER;
5189 }
5190}
5191
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005192static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5193{
5194 switch (port) {
5195 case PORT_A:
5196 return POWER_DOMAIN_AUX_A;
5197 case PORT_B:
5198 return POWER_DOMAIN_AUX_B;
5199 case PORT_C:
5200 return POWER_DOMAIN_AUX_C;
5201 case PORT_D:
5202 return POWER_DOMAIN_AUX_D;
5203 case PORT_E:
5204 /* FIXME: Check VBT for actual wiring of PORT E */
5205 return POWER_DOMAIN_AUX_D;
5206 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005207 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005208 return POWER_DOMAIN_AUX_A;
5209 }
5210}
5211
Imre Deak319be8a2014-03-04 19:22:57 +02005212enum intel_display_power_domain
5213intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005214{
Imre Deak319be8a2014-03-04 19:22:57 +02005215 struct drm_device *dev = intel_encoder->base.dev;
5216 struct intel_digital_port *intel_dig_port;
5217
5218 switch (intel_encoder->type) {
5219 case INTEL_OUTPUT_UNKNOWN:
5220 /* Only DDI platforms should ever use this output type */
5221 WARN_ON_ONCE(!HAS_DDI(dev));
5222 case INTEL_OUTPUT_DISPLAYPORT:
5223 case INTEL_OUTPUT_HDMI:
5224 case INTEL_OUTPUT_EDP:
5225 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005226 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005227 case INTEL_OUTPUT_DP_MST:
5228 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5229 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005230 case INTEL_OUTPUT_ANALOG:
5231 return POWER_DOMAIN_PORT_CRT;
5232 case INTEL_OUTPUT_DSI:
5233 return POWER_DOMAIN_PORT_DSI;
5234 default:
5235 return POWER_DOMAIN_PORT_OTHER;
5236 }
5237}
5238
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005239enum intel_display_power_domain
5240intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5241{
5242 struct drm_device *dev = intel_encoder->base.dev;
5243 struct intel_digital_port *intel_dig_port;
5244
5245 switch (intel_encoder->type) {
5246 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005247 case INTEL_OUTPUT_HDMI:
5248 /*
5249 * Only DDI platforms should ever use these output types.
5250 * We can get here after the HDMI detect code has already set
5251 * the type of the shared encoder. Since we can't be sure
5252 * what's the status of the given connectors, play safe and
5253 * run the DP detection too.
5254 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005255 WARN_ON_ONCE(!HAS_DDI(dev));
5256 case INTEL_OUTPUT_DISPLAYPORT:
5257 case INTEL_OUTPUT_EDP:
5258 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5259 return port_to_aux_power_domain(intel_dig_port->port);
5260 case INTEL_OUTPUT_DP_MST:
5261 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5262 return port_to_aux_power_domain(intel_dig_port->port);
5263 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005264 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005265 return POWER_DOMAIN_AUX_A;
5266 }
5267}
5268
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005269static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5270 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005271{
5272 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005273 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5275 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005276 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005277 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005278
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005279 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005280 return 0;
5281
Imre Deak77d22dc2014-03-05 16:20:52 +02005282 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5283 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005284 if (crtc_state->pch_pfit.enabled ||
5285 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005286 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5287
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005288 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5289 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5290
Imre Deak319be8a2014-03-04 19:22:57 +02005291 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005292 }
Imre Deak319be8a2014-03-04 19:22:57 +02005293
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005294 if (crtc_state->shared_dpll)
5295 mask |= BIT(POWER_DOMAIN_PLLS);
5296
Imre Deak77d22dc2014-03-05 16:20:52 +02005297 return mask;
5298}
5299
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005300static unsigned long
5301modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5302 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005303{
5304 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5306 enum intel_display_power_domain domain;
5307 unsigned long domains, new_domains, old_domains;
5308
5309 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005310 intel_crtc->enabled_power_domains = new_domains =
5311 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005312
5313 domains = new_domains & ~old_domains;
5314
5315 for_each_power_domain(domain, domains)
5316 intel_display_power_get(dev_priv, domain);
5317
5318 return old_domains & ~new_domains;
5319}
5320
5321static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5322 unsigned long domains)
5323{
5324 enum intel_display_power_domain domain;
5325
5326 for_each_power_domain(domain, domains)
5327 intel_display_power_put(dev_priv, domain);
5328}
5329
Mika Kaholaadafdc62015-08-18 14:36:59 +03005330static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5331{
5332 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5333
5334 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5335 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5336 return max_cdclk_freq;
5337 else if (IS_CHERRYVIEW(dev_priv))
5338 return max_cdclk_freq*95/100;
5339 else if (INTEL_INFO(dev_priv)->gen < 4)
5340 return 2*max_cdclk_freq*90/100;
5341 else
5342 return max_cdclk_freq*90/100;
5343}
5344
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005345static void intel_update_max_cdclk(struct drm_device *dev)
5346{
5347 struct drm_i915_private *dev_priv = dev->dev_private;
5348
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005349 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005350 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5351
5352 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5353 dev_priv->max_cdclk_freq = 675000;
5354 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5355 dev_priv->max_cdclk_freq = 540000;
5356 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5357 dev_priv->max_cdclk_freq = 450000;
5358 else
5359 dev_priv->max_cdclk_freq = 337500;
5360 } else if (IS_BROADWELL(dev)) {
5361 /*
5362 * FIXME with extra cooling we can allow
5363 * 540 MHz for ULX and 675 Mhz for ULT.
5364 * How can we know if extra cooling is
5365 * available? PCI ID, VTB, something else?
5366 */
5367 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5368 dev_priv->max_cdclk_freq = 450000;
5369 else if (IS_BDW_ULX(dev))
5370 dev_priv->max_cdclk_freq = 450000;
5371 else if (IS_BDW_ULT(dev))
5372 dev_priv->max_cdclk_freq = 540000;
5373 else
5374 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005375 } else if (IS_CHERRYVIEW(dev)) {
5376 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005377 } else if (IS_VALLEYVIEW(dev)) {
5378 dev_priv->max_cdclk_freq = 400000;
5379 } else {
5380 /* otherwise assume cdclk is fixed */
5381 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5382 }
5383
Mika Kaholaadafdc62015-08-18 14:36:59 +03005384 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5385
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005386 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5387 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005388
5389 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5390 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005391}
5392
5393static void intel_update_cdclk(struct drm_device *dev)
5394{
5395 struct drm_i915_private *dev_priv = dev->dev_private;
5396
5397 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5398 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5399 dev_priv->cdclk_freq);
5400
5401 /*
5402 * Program the gmbus_freq based on the cdclk frequency.
5403 * BSpec erroneously claims we should aim for 4MHz, but
5404 * in fact 1MHz is the correct frequency.
5405 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005406 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005407 /*
5408 * Program the gmbus_freq based on the cdclk frequency.
5409 * BSpec erroneously claims we should aim for 4MHz, but
5410 * in fact 1MHz is the correct frequency.
5411 */
5412 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5413 }
5414
5415 if (dev_priv->max_cdclk_freq == 0)
5416 intel_update_max_cdclk(dev);
5417}
5418
Damien Lespiau70d0c572015-06-04 18:21:29 +01005419static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305420{
5421 struct drm_i915_private *dev_priv = dev->dev_private;
5422 uint32_t divider;
5423 uint32_t ratio;
5424 uint32_t current_freq;
5425 int ret;
5426
5427 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5428 switch (frequency) {
5429 case 144000:
5430 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5431 ratio = BXT_DE_PLL_RATIO(60);
5432 break;
5433 case 288000:
5434 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5435 ratio = BXT_DE_PLL_RATIO(60);
5436 break;
5437 case 384000:
5438 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5439 ratio = BXT_DE_PLL_RATIO(60);
5440 break;
5441 case 576000:
5442 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5443 ratio = BXT_DE_PLL_RATIO(60);
5444 break;
5445 case 624000:
5446 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5447 ratio = BXT_DE_PLL_RATIO(65);
5448 break;
5449 case 19200:
5450 /*
5451 * Bypass frequency with DE PLL disabled. Init ratio, divider
5452 * to suppress GCC warning.
5453 */
5454 ratio = 0;
5455 divider = 0;
5456 break;
5457 default:
5458 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5459
5460 return;
5461 }
5462
5463 mutex_lock(&dev_priv->rps.hw_lock);
5464 /* Inform power controller of upcoming frequency change */
5465 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5466 0x80000000);
5467 mutex_unlock(&dev_priv->rps.hw_lock);
5468
5469 if (ret) {
5470 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5471 ret, frequency);
5472 return;
5473 }
5474
5475 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5476 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5477 current_freq = current_freq * 500 + 1000;
5478
5479 /*
5480 * DE PLL has to be disabled when
5481 * - setting to 19.2MHz (bypass, PLL isn't used)
5482 * - before setting to 624MHz (PLL needs toggling)
5483 * - before setting to any frequency from 624MHz (PLL needs toggling)
5484 */
5485 if (frequency == 19200 || frequency == 624000 ||
5486 current_freq == 624000) {
5487 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5488 /* Timeout 200us */
5489 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5490 1))
5491 DRM_ERROR("timout waiting for DE PLL unlock\n");
5492 }
5493
5494 if (frequency != 19200) {
5495 uint32_t val;
5496
5497 val = I915_READ(BXT_DE_PLL_CTL);
5498 val &= ~BXT_DE_PLL_RATIO_MASK;
5499 val |= ratio;
5500 I915_WRITE(BXT_DE_PLL_CTL, val);
5501
5502 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5503 /* Timeout 200us */
5504 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5505 DRM_ERROR("timeout waiting for DE PLL lock\n");
5506
5507 val = I915_READ(CDCLK_CTL);
5508 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5509 val |= divider;
5510 /*
5511 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5512 * enable otherwise.
5513 */
5514 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5515 if (frequency >= 500000)
5516 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5517
5518 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5519 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5520 val |= (frequency - 1000) / 500;
5521 I915_WRITE(CDCLK_CTL, val);
5522 }
5523
5524 mutex_lock(&dev_priv->rps.hw_lock);
5525 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5526 DIV_ROUND_UP(frequency, 25000));
5527 mutex_unlock(&dev_priv->rps.hw_lock);
5528
5529 if (ret) {
5530 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5531 ret, frequency);
5532 return;
5533 }
5534
Damien Lespiaua47871b2015-06-04 18:21:34 +01005535 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305536}
5537
5538void broxton_init_cdclk(struct drm_device *dev)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541 uint32_t val;
5542
5543 /*
5544 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5545 * or else the reset will hang because there is no PCH to respond.
5546 * Move the handshake programming to initialization sequence.
5547 * Previously was left up to BIOS.
5548 */
5549 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5550 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5551 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5552
5553 /* Enable PG1 for cdclk */
5554 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5555
5556 /* check if cd clock is enabled */
5557 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5558 DRM_DEBUG_KMS("Display already initialized\n");
5559 return;
5560 }
5561
5562 /*
5563 * FIXME:
5564 * - The initial CDCLK needs to be read from VBT.
5565 * Need to make this change after VBT has changes for BXT.
5566 * - check if setting the max (or any) cdclk freq is really necessary
5567 * here, it belongs to modeset time
5568 */
5569 broxton_set_cdclk(dev, 624000);
5570
5571 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005572 POSTING_READ(DBUF_CTL);
5573
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305574 udelay(10);
5575
5576 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5577 DRM_ERROR("DBuf power enable timeout!\n");
5578}
5579
5580void broxton_uninit_cdclk(struct drm_device *dev)
5581{
5582 struct drm_i915_private *dev_priv = dev->dev_private;
5583
5584 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005585 POSTING_READ(DBUF_CTL);
5586
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305587 udelay(10);
5588
5589 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5590 DRM_ERROR("DBuf power disable timeout!\n");
5591
5592 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5593 broxton_set_cdclk(dev, 19200);
5594
5595 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5596}
5597
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005598static const struct skl_cdclk_entry {
5599 unsigned int freq;
5600 unsigned int vco;
5601} skl_cdclk_frequencies[] = {
5602 { .freq = 308570, .vco = 8640 },
5603 { .freq = 337500, .vco = 8100 },
5604 { .freq = 432000, .vco = 8640 },
5605 { .freq = 450000, .vco = 8100 },
5606 { .freq = 540000, .vco = 8100 },
5607 { .freq = 617140, .vco = 8640 },
5608 { .freq = 675000, .vco = 8100 },
5609};
5610
5611static unsigned int skl_cdclk_decimal(unsigned int freq)
5612{
5613 return (freq - 1000) / 500;
5614}
5615
5616static unsigned int skl_cdclk_get_vco(unsigned int freq)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5621 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5622
5623 if (e->freq == freq)
5624 return e->vco;
5625 }
5626
5627 return 8100;
5628}
5629
5630static void
5631skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5632{
5633 unsigned int min_freq;
5634 u32 val;
5635
5636 /* select the minimum CDCLK before enabling DPLL 0 */
5637 val = I915_READ(CDCLK_CTL);
5638 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5639 val |= CDCLK_FREQ_337_308;
5640
5641 if (required_vco == 8640)
5642 min_freq = 308570;
5643 else
5644 min_freq = 337500;
5645
5646 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5647
5648 I915_WRITE(CDCLK_CTL, val);
5649 POSTING_READ(CDCLK_CTL);
5650
5651 /*
5652 * We always enable DPLL0 with the lowest link rate possible, but still
5653 * taking into account the VCO required to operate the eDP panel at the
5654 * desired frequency. The usual DP link rates operate with a VCO of
5655 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5656 * The modeset code is responsible for the selection of the exact link
5657 * rate later on, with the constraint of choosing a frequency that
5658 * works with required_vco.
5659 */
5660 val = I915_READ(DPLL_CTRL1);
5661
5662 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5663 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5664 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5665 if (required_vco == 8640)
5666 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5667 SKL_DPLL0);
5668 else
5669 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5670 SKL_DPLL0);
5671
5672 I915_WRITE(DPLL_CTRL1, val);
5673 POSTING_READ(DPLL_CTRL1);
5674
5675 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5676
5677 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5678 DRM_ERROR("DPLL0 not locked\n");
5679}
5680
5681static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5682{
5683 int ret;
5684 u32 val;
5685
5686 /* inform PCU we want to change CDCLK */
5687 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5688 mutex_lock(&dev_priv->rps.hw_lock);
5689 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5690 mutex_unlock(&dev_priv->rps.hw_lock);
5691
5692 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5693}
5694
5695static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5696{
5697 unsigned int i;
5698
5699 for (i = 0; i < 15; i++) {
5700 if (skl_cdclk_pcu_ready(dev_priv))
5701 return true;
5702 udelay(10);
5703 }
5704
5705 return false;
5706}
5707
5708static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5709{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005710 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005711 u32 freq_select, pcu_ack;
5712
5713 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5714
5715 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5716 DRM_ERROR("failed to inform PCU about cdclk change\n");
5717 return;
5718 }
5719
5720 /* set CDCLK_CTL */
5721 switch(freq) {
5722 case 450000:
5723 case 432000:
5724 freq_select = CDCLK_FREQ_450_432;
5725 pcu_ack = 1;
5726 break;
5727 case 540000:
5728 freq_select = CDCLK_FREQ_540;
5729 pcu_ack = 2;
5730 break;
5731 case 308570:
5732 case 337500:
5733 default:
5734 freq_select = CDCLK_FREQ_337_308;
5735 pcu_ack = 0;
5736 break;
5737 case 617140:
5738 case 675000:
5739 freq_select = CDCLK_FREQ_675_617;
5740 pcu_ack = 3;
5741 break;
5742 }
5743
5744 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5745 POSTING_READ(CDCLK_CTL);
5746
5747 /* inform PCU of the change */
5748 mutex_lock(&dev_priv->rps.hw_lock);
5749 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5750 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005751
5752 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005753}
5754
5755void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5756{
5757 /* disable DBUF power */
5758 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5759 POSTING_READ(DBUF_CTL);
5760
5761 udelay(10);
5762
5763 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5764 DRM_ERROR("DBuf power disable timeout\n");
5765
Imre Deakab96c1ee2015-11-04 19:24:18 +02005766 /* disable DPLL0 */
5767 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5768 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5769 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005770}
5771
5772void skl_init_cdclk(struct drm_i915_private *dev_priv)
5773{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005774 unsigned int required_vco;
5775
Gary Wang39d9b852015-08-28 16:40:34 +08005776 /* DPLL0 not enabled (happens on early BIOS versions) */
5777 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5778 /* enable DPLL0 */
5779 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5780 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005781 }
5782
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005783 /* set CDCLK to the frequency the BIOS chose */
5784 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5785
5786 /* enable DBUF power */
5787 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5788 POSTING_READ(DBUF_CTL);
5789
5790 udelay(10);
5791
5792 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5793 DRM_ERROR("DBuf power enable timeout\n");
5794}
5795
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305796int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5797{
5798 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5799 uint32_t cdctl = I915_READ(CDCLK_CTL);
5800 int freq = dev_priv->skl_boot_cdclk;
5801
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305802 /*
5803 * check if the pre-os intialized the display
5804 * There is SWF18 scratchpad register defined which is set by the
5805 * pre-os which can be used by the OS drivers to check the status
5806 */
5807 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5808 goto sanitize;
5809
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305810 /* Is PLL enabled and locked ? */
5811 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5812 goto sanitize;
5813
5814 /* DPLL okay; verify the cdclock
5815 *
5816 * Noticed in some instances that the freq selection is correct but
5817 * decimal part is programmed wrong from BIOS where pre-os does not
5818 * enable display. Verify the same as well.
5819 */
5820 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5821 /* All well; nothing to sanitize */
5822 return false;
5823sanitize:
5824 /*
5825 * As of now initialize with max cdclk till
5826 * we get dynamic cdclk support
5827 * */
5828 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5829 skl_init_cdclk(dev_priv);
5830
5831 /* we did have to sanitize */
5832 return true;
5833}
5834
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835/* Adjust CDclk dividers to allow high res or save power if possible */
5836static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5837{
5838 struct drm_i915_private *dev_priv = dev->dev_private;
5839 u32 val, cmd;
5840
Vandana Kannan164dfd22014-11-24 13:37:41 +05305841 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5842 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005843
Ville Syrjälädfcab172014-06-13 13:37:47 +03005844 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005846 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 cmd = 1;
5848 else
5849 cmd = 0;
5850
5851 mutex_lock(&dev_priv->rps.hw_lock);
5852 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5853 val &= ~DSPFREQGUAR_MASK;
5854 val |= (cmd << DSPFREQGUAR_SHIFT);
5855 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5856 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5857 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5858 50)) {
5859 DRM_ERROR("timed out waiting for CDclk change\n");
5860 }
5861 mutex_unlock(&dev_priv->rps.hw_lock);
5862
Ville Syrjälä54433e92015-05-26 20:42:31 +03005863 mutex_lock(&dev_priv->sb_lock);
5864
Ville Syrjälädfcab172014-06-13 13:37:47 +03005865 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005866 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005867
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005868 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869
Jesse Barnes30a970c2013-11-04 13:48:12 -08005870 /* adjust cdclk divider */
5871 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005872 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005873 val |= divider;
5874 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005875
5876 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005877 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005878 50))
5879 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 }
5881
Jesse Barnes30a970c2013-11-04 13:48:12 -08005882 /* adjust self-refresh exit latency value */
5883 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5884 val &= ~0x7f;
5885
5886 /*
5887 * For high bandwidth configs, we set a higher latency in the bunit
5888 * so that the core display fetch happens in time to avoid underruns.
5889 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005890 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005891 val |= 4500 / 250; /* 4.5 usec */
5892 else
5893 val |= 3000 / 250; /* 3.0 usec */
5894 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005895
Ville Syrjäläa5805162015-05-26 20:42:30 +03005896 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897
Ville Syrjäläb6283052015-06-03 15:45:07 +03005898 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005899}
5900
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005901static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5902{
5903 struct drm_i915_private *dev_priv = dev->dev_private;
5904 u32 val, cmd;
5905
Vandana Kannan164dfd22014-11-24 13:37:41 +05305906 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5907 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005908
5909 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005910 case 333333:
5911 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005912 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005913 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005914 break;
5915 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005916 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005917 return;
5918 }
5919
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005920 /*
5921 * Specs are full of misinformation, but testing on actual
5922 * hardware has shown that we just need to write the desired
5923 * CCK divider into the Punit register.
5924 */
5925 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5926
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005927 mutex_lock(&dev_priv->rps.hw_lock);
5928 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5929 val &= ~DSPFREQGUAR_MASK_CHV;
5930 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5931 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5932 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5933 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5934 50)) {
5935 DRM_ERROR("timed out waiting for CDclk change\n");
5936 }
5937 mutex_unlock(&dev_priv->rps.hw_lock);
5938
Ville Syrjäläb6283052015-06-03 15:45:07 +03005939 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005940}
5941
Jesse Barnes30a970c2013-11-04 13:48:12 -08005942static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5943 int max_pixclk)
5944{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005945 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005946 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005947
Jesse Barnes30a970c2013-11-04 13:48:12 -08005948 /*
5949 * Really only a few cases to deal with, as only 4 CDclks are supported:
5950 * 200MHz
5951 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005952 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005953 * 400MHz (VLV only)
5954 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5955 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005956 *
5957 * We seem to get an unstable or solid color picture at 200MHz.
5958 * Not sure what's wrong. For now use 200MHz only when all pipes
5959 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005960 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005961 if (!IS_CHERRYVIEW(dev_priv) &&
5962 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005963 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005964 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005965 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005966 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005967 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005968 else
5969 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005970}
5971
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305972static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5973 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005974{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305975 /*
5976 * FIXME:
5977 * - remove the guardband, it's not needed on BXT
5978 * - set 19.2MHz bypass frequency if there are no active pipes
5979 */
5980 if (max_pixclk > 576000*9/10)
5981 return 624000;
5982 else if (max_pixclk > 384000*9/10)
5983 return 576000;
5984 else if (max_pixclk > 288000*9/10)
5985 return 384000;
5986 else if (max_pixclk > 144000*9/10)
5987 return 288000;
5988 else
5989 return 144000;
5990}
5991
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005992/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005993static int intel_mode_max_pixclk(struct drm_device *dev,
5994 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005995{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005996 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5997 struct drm_i915_private *dev_priv = dev->dev_private;
5998 struct drm_crtc *crtc;
5999 struct drm_crtc_state *crtc_state;
6000 unsigned max_pixclk = 0, i;
6001 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006002
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006003 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6004 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006005
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006006 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6007 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006008
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006009 if (crtc_state->enable)
6010 pixclk = crtc_state->adjusted_mode.crtc_clock;
6011
6012 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006013 }
6014
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006015 for_each_pipe(dev_priv, pipe)
6016 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6017
Jesse Barnes30a970c2013-11-04 13:48:12 -08006018 return max_pixclk;
6019}
6020
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006021static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006022{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006023 struct drm_device *dev = state->dev;
6024 struct drm_i915_private *dev_priv = dev->dev_private;
6025 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006026 struct intel_atomic_state *intel_state =
6027 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006029 if (max_pixclk < 0)
6030 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006031
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006032 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006033 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306034
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006035 if (!intel_state->active_crtcs)
6036 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6037
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006038 return 0;
6039}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006040
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006041static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6042{
6043 struct drm_device *dev = state->dev;
6044 struct drm_i915_private *dev_priv = dev->dev_private;
6045 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006046 struct intel_atomic_state *intel_state =
6047 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006048
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006049 if (max_pixclk < 0)
6050 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006051
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006052 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006053 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006054
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006055 if (!intel_state->active_crtcs)
6056 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6057
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006058 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006059}
6060
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006061static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6062{
6063 unsigned int credits, default_credits;
6064
6065 if (IS_CHERRYVIEW(dev_priv))
6066 default_credits = PFI_CREDIT(12);
6067 else
6068 default_credits = PFI_CREDIT(8);
6069
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006070 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006071 /* CHV suggested value is 31 or 63 */
6072 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006073 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006074 else
6075 credits = PFI_CREDIT(15);
6076 } else {
6077 credits = default_credits;
6078 }
6079
6080 /*
6081 * WA - write default credits before re-programming
6082 * FIXME: should we also set the resend bit here?
6083 */
6084 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6085 default_credits);
6086
6087 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6088 credits | PFI_CREDIT_RESEND);
6089
6090 /*
6091 * FIXME is this guaranteed to clear
6092 * immediately or should we poll for it?
6093 */
6094 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6095}
6096
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006097static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006098{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006099 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006100 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006101 struct intel_atomic_state *old_intel_state =
6102 to_intel_atomic_state(old_state);
6103 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006104
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006105 /*
6106 * FIXME: We can end up here with all power domains off, yet
6107 * with a CDCLK frequency other than the minimum. To account
6108 * for this take the PIPE-A power domain, which covers the HW
6109 * blocks needed for the following programming. This can be
6110 * removed once it's guaranteed that we get here either with
6111 * the minimum CDCLK set, or the required power domains
6112 * enabled.
6113 */
6114 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006115
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006116 if (IS_CHERRYVIEW(dev))
6117 cherryview_set_cdclk(dev, req_cdclk);
6118 else
6119 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006120
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006121 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006122
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006123 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006124}
6125
Jesse Barnes89b667f2013-04-18 14:51:36 -07006126static void valleyview_crtc_enable(struct drm_crtc *crtc)
6127{
6128 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006129 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6131 struct intel_encoder *encoder;
6132 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006133
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006134 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006135 return;
6136
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006137 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306138 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006139
6140 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006141 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006142
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006143 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6144 struct drm_i915_private *dev_priv = dev->dev_private;
6145
6146 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6147 I915_WRITE(CHV_CANVAS(pipe), 0);
6148 }
6149
Daniel Vetter5b18e572014-04-24 23:55:06 +02006150 i9xx_set_pipeconf(intel_crtc);
6151
Jesse Barnes89b667f2013-04-18 14:51:36 -07006152 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006153
Daniel Vettera72e4c92014-09-30 10:56:47 +02006154 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006155
Jesse Barnes89b667f2013-04-18 14:51:36 -07006156 for_each_encoder_on_crtc(dev, crtc, encoder)
6157 if (encoder->pre_pll_enable)
6158 encoder->pre_pll_enable(encoder);
6159
Jani Nikulaa65347b2015-11-27 12:21:46 +02006160 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006161 if (IS_CHERRYVIEW(dev)) {
6162 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006163 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006164 } else {
6165 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006166 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006167 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006168 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006169
6170 for_each_encoder_on_crtc(dev, crtc, encoder)
6171 if (encoder->pre_enable)
6172 encoder->pre_enable(encoder);
6173
Jesse Barnes2dd24552013-04-25 12:55:01 -07006174 i9xx_pfit_enable(intel_crtc);
6175
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006176 intel_crtc_load_lut(crtc);
6177
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006178 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006179 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006180
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006181 assert_vblank_disabled(crtc);
6182 drm_crtc_vblank_on(crtc);
6183
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006184 for_each_encoder_on_crtc(dev, crtc, encoder)
6185 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006186}
6187
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006188static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6189{
6190 struct drm_device *dev = crtc->base.dev;
6191 struct drm_i915_private *dev_priv = dev->dev_private;
6192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006193 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6194 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006195}
6196
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006197static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006198{
6199 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006200 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006202 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006203 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006204
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006205 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006206 return;
6207
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006208 i9xx_set_pll_dividers(intel_crtc);
6209
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006210 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306211 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006212
6213 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006214 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006215
Daniel Vetter5b18e572014-04-24 23:55:06 +02006216 i9xx_set_pipeconf(intel_crtc);
6217
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006218 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006219
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006220 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006221 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006222
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006223 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006224 if (encoder->pre_enable)
6225 encoder->pre_enable(encoder);
6226
Daniel Vetterf6736a12013-06-05 13:34:30 +02006227 i9xx_enable_pll(intel_crtc);
6228
Jesse Barnes2dd24552013-04-25 12:55:01 -07006229 i9xx_pfit_enable(intel_crtc);
6230
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006231 intel_crtc_load_lut(crtc);
6232
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006233 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006234 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006235
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006236 assert_vblank_disabled(crtc);
6237 drm_crtc_vblank_on(crtc);
6238
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006239 for_each_encoder_on_crtc(dev, crtc, encoder)
6240 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006241}
6242
Daniel Vetter87476d62013-04-11 16:29:06 +02006243static void i9xx_pfit_disable(struct intel_crtc *crtc)
6244{
6245 struct drm_device *dev = crtc->base.dev;
6246 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006248 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006249 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006250
6251 assert_pipe_disabled(dev_priv, crtc->pipe);
6252
Daniel Vetter328d8e82013-05-08 10:36:31 +02006253 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6254 I915_READ(PFIT_CONTROL));
6255 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006256}
6257
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006258static void i9xx_crtc_disable(struct drm_crtc *crtc)
6259{
6260 struct drm_device *dev = crtc->dev;
6261 struct drm_i915_private *dev_priv = dev->dev_private;
6262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006263 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006264 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006265
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006266 /*
6267 * On gen2 planes are double buffered but the pipe isn't, so we must
6268 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006269 * We also need to wait on all gmch platforms because of the
6270 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006271 */
Imre Deak564ed192014-06-13 14:54:21 +03006272 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006273
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006274 for_each_encoder_on_crtc(dev, crtc, encoder)
6275 encoder->disable(encoder);
6276
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006277 drm_crtc_vblank_off(crtc);
6278 assert_vblank_disabled(crtc);
6279
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006280 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006281
Daniel Vetter87476d62013-04-11 16:29:06 +02006282 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006283
Jesse Barnes89b667f2013-04-18 14:51:36 -07006284 for_each_encoder_on_crtc(dev, crtc, encoder)
6285 if (encoder->post_disable)
6286 encoder->post_disable(encoder);
6287
Jani Nikulaa65347b2015-11-27 12:21:46 +02006288 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006289 if (IS_CHERRYVIEW(dev))
6290 chv_disable_pll(dev_priv, pipe);
6291 else if (IS_VALLEYVIEW(dev))
6292 vlv_disable_pll(dev_priv, pipe);
6293 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006294 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006295 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006296
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006297 for_each_encoder_on_crtc(dev, crtc, encoder)
6298 if (encoder->post_pll_disable)
6299 encoder->post_pll_disable(encoder);
6300
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006301 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006302 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006303}
6304
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006305static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006306{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006307 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006309 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006310 enum intel_display_power_domain domain;
6311 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006312
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006313 if (!intel_crtc->active)
6314 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006315
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006316 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006317 WARN_ON(intel_crtc->unpin_work);
6318
Ville Syrjälä2622a082016-03-09 19:07:26 +02006319 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006320
6321 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6322 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006323 }
6324
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006325 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006326
6327 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6328 crtc->base.id);
6329
6330 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6331 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006332 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006333 crtc->enabled = false;
6334 crtc->state->connector_mask = 0;
6335 crtc->state->encoder_mask = 0;
6336
6337 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6338 encoder->base.crtc = NULL;
6339
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006340 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006341 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006342 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006343
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006344 domains = intel_crtc->enabled_power_domains;
6345 for_each_power_domain(domain, domains)
6346 intel_display_power_put(dev_priv, domain);
6347 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006348
6349 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6350 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006351}
6352
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006353/*
6354 * turn all crtc's off, but do not adjust state
6355 * This has to be paired with a call to intel_modeset_setup_hw_state.
6356 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006357int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006358{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006359 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006360 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006361 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006362
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006363 state = drm_atomic_helper_suspend(dev);
6364 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006365 if (ret)
6366 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006367 else
6368 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006369 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006370}
6371
Chris Wilsonea5b2132010-08-04 13:50:23 +01006372void intel_encoder_destroy(struct drm_encoder *encoder)
6373{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006374 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006375
Chris Wilsonea5b2132010-08-04 13:50:23 +01006376 drm_encoder_cleanup(encoder);
6377 kfree(intel_encoder);
6378}
6379
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006380/* Cross check the actual hw state with our own modeset state tracking (and it's
6381 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006382static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006383{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006384 struct drm_crtc *crtc = connector->base.state->crtc;
6385
6386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6387 connector->base.base.id,
6388 connector->base.name);
6389
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006390 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006391 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006392 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006394 I915_STATE_WARN(!crtc,
6395 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006396
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006397 if (!crtc)
6398 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006399
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006400 I915_STATE_WARN(!crtc->state->active,
6401 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006402
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006403 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006404 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006405
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006406 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006407 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006408
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006409 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006410 "attached encoder crtc differs from connector crtc\n");
6411 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006412 I915_STATE_WARN(crtc && crtc->state->active,
6413 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006414 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6415 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006416 }
6417}
6418
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006419int intel_connector_init(struct intel_connector *connector)
6420{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006421 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006422
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006423 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006424 return -ENOMEM;
6425
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006426 return 0;
6427}
6428
6429struct intel_connector *intel_connector_alloc(void)
6430{
6431 struct intel_connector *connector;
6432
6433 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6434 if (!connector)
6435 return NULL;
6436
6437 if (intel_connector_init(connector) < 0) {
6438 kfree(connector);
6439 return NULL;
6440 }
6441
6442 return connector;
6443}
6444
Daniel Vetterf0947c32012-07-02 13:10:34 +02006445/* Simple connector->get_hw_state implementation for encoders that support only
6446 * one connector and no cloning and hence the encoder state determines the state
6447 * of the connector. */
6448bool intel_connector_get_hw_state(struct intel_connector *connector)
6449{
Daniel Vetter24929352012-07-02 20:28:59 +02006450 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006451 struct intel_encoder *encoder = connector->encoder;
6452
6453 return encoder->get_hw_state(encoder, &pipe);
6454}
6455
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006456static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006457{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6459 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006460
6461 return 0;
6462}
6463
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006464static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006465 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006466{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467 struct drm_atomic_state *state = pipe_config->base.state;
6468 struct intel_crtc *other_crtc;
6469 struct intel_crtc_state *other_crtc_state;
6470
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006471 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6472 pipe_name(pipe), pipe_config->fdi_lanes);
6473 if (pipe_config->fdi_lanes > 4) {
6474 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6475 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006477 }
6478
Paulo Zanonibafb6552013-11-02 21:07:44 -07006479 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 if (pipe_config->fdi_lanes > 2) {
6481 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6482 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006484 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486 }
6487 }
6488
6489 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491
6492 /* Ivybridge 3 pipe is really complicated */
6493 switch (pipe) {
6494 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006496 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 if (pipe_config->fdi_lanes <= 2)
6498 return 0;
6499
6500 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6501 other_crtc_state =
6502 intel_atomic_get_crtc_state(state, other_crtc);
6503 if (IS_ERR(other_crtc_state))
6504 return PTR_ERR(other_crtc_state);
6505
6506 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006507 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6508 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006509 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006510 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006511 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006512 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006513 if (pipe_config->fdi_lanes > 2) {
6514 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6515 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006516 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006517 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006518
6519 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6520 other_crtc_state =
6521 intel_atomic_get_crtc_state(state, other_crtc);
6522 if (IS_ERR(other_crtc_state))
6523 return PTR_ERR(other_crtc_state);
6524
6525 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006526 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006527 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006528 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006529 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006530 default:
6531 BUG();
6532 }
6533}
6534
Daniel Vettere29c22c2013-02-21 00:00:16 +01006535#define RETRY 1
6536static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006537 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006538{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006539 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006540 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006541 int lane, link_bw, fdi_dotclock, ret;
6542 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006543
Daniel Vettere29c22c2013-02-21 00:00:16 +01006544retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006545 /* FDI is a binary signal running at ~2.7GHz, encoding
6546 * each output octet as 10 bits. The actual frequency
6547 * is stored as a divider into a 100MHz clock, and the
6548 * mode pixel clock is stored in units of 1KHz.
6549 * Hence the bw of each lane in terms of the mode signal
6550 * is:
6551 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006552 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006553
Damien Lespiau241bfc32013-09-25 16:45:37 +01006554 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006555
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006556 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006557 pipe_config->pipe_bpp);
6558
6559 pipe_config->fdi_lanes = lane;
6560
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006561 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006562 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006563
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006564 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006565 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006566 pipe_config->pipe_bpp -= 2*3;
6567 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6568 pipe_config->pipe_bpp);
6569 needs_recompute = true;
6570 pipe_config->bw_constrained = true;
6571
6572 goto retry;
6573 }
6574
6575 if (needs_recompute)
6576 return RETRY;
6577
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006578 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006579}
6580
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006581static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6582 struct intel_crtc_state *pipe_config)
6583{
6584 if (pipe_config->pipe_bpp > 24)
6585 return false;
6586
6587 /* HSW can handle pixel rate up to cdclk? */
6588 if (IS_HASWELL(dev_priv->dev))
6589 return true;
6590
6591 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006592 * We compare against max which means we must take
6593 * the increased cdclk requirement into account when
6594 * calculating the new cdclk.
6595 *
6596 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006597 */
6598 return ilk_pipe_pixel_rate(pipe_config) <=
6599 dev_priv->max_cdclk_freq * 95 / 100;
6600}
6601
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006602static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006603 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006604{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006605 struct drm_device *dev = crtc->base.dev;
6606 struct drm_i915_private *dev_priv = dev->dev_private;
6607
Jani Nikulad330a952014-01-21 11:24:25 +02006608 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006609 hsw_crtc_supports_ips(crtc) &&
6610 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006611}
6612
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006613static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6614{
6615 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6616
6617 /* GDG double wide on either pipe, otherwise pipe A only */
6618 return INTEL_INFO(dev_priv)->gen < 4 &&
6619 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6620}
6621
Daniel Vettera43f6e02013-06-07 23:10:32 +02006622static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006623 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006624{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006625 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006626 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006627 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006628
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006629 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006630 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006631 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006632
6633 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006634 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006635 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006636 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006637 if (intel_crtc_supports_double_wide(crtc) &&
6638 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006639 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006640 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006641 }
6642
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006643 if (adjusted_mode->crtc_clock > clock_limit) {
6644 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6645 adjusted_mode->crtc_clock, clock_limit,
6646 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006647 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006648 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006649 }
Chris Wilson89749352010-09-12 18:25:19 +01006650
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006651 /*
6652 * Pipe horizontal size must be even in:
6653 * - DVO ganged mode
6654 * - LVDS dual channel mode
6655 * - Double wide pipe
6656 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006657 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006658 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6659 pipe_config->pipe_src_w &= ~1;
6660
Damien Lespiau8693a822013-05-03 18:48:11 +01006661 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6662 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006663 */
6664 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006665 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006666 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006667
Damien Lespiauf5adf942013-06-24 18:29:34 +01006668 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006669 hsw_compute_ips_config(crtc, pipe_config);
6670
Daniel Vetter877d48d2013-04-19 11:24:43 +02006671 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006672 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006673
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006674 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006675}
6676
Ville Syrjälä1652d192015-03-31 14:12:01 +03006677static int skylake_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = to_i915(dev);
6680 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6681 uint32_t cdctl = I915_READ(CDCLK_CTL);
6682 uint32_t linkrate;
6683
Damien Lespiau414355a2015-06-04 18:21:31 +01006684 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006685 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006686
6687 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6688 return 540000;
6689
6690 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006691 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006692
Damien Lespiau71cd8422015-04-30 16:39:17 +01006693 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6694 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006695 /* vco 8640 */
6696 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6697 case CDCLK_FREQ_450_432:
6698 return 432000;
6699 case CDCLK_FREQ_337_308:
6700 return 308570;
6701 case CDCLK_FREQ_675_617:
6702 return 617140;
6703 default:
6704 WARN(1, "Unknown cd freq selection\n");
6705 }
6706 } else {
6707 /* vco 8100 */
6708 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6709 case CDCLK_FREQ_450_432:
6710 return 450000;
6711 case CDCLK_FREQ_337_308:
6712 return 337500;
6713 case CDCLK_FREQ_675_617:
6714 return 675000;
6715 default:
6716 WARN(1, "Unknown cd freq selection\n");
6717 }
6718 }
6719
6720 /* error case, do as if DPLL0 isn't enabled */
6721 return 24000;
6722}
6723
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006724static int broxton_get_display_clock_speed(struct drm_device *dev)
6725{
6726 struct drm_i915_private *dev_priv = to_i915(dev);
6727 uint32_t cdctl = I915_READ(CDCLK_CTL);
6728 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6729 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6730 int cdclk;
6731
6732 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6733 return 19200;
6734
6735 cdclk = 19200 * pll_ratio / 2;
6736
6737 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6738 case BXT_CDCLK_CD2X_DIV_SEL_1:
6739 return cdclk; /* 576MHz or 624MHz */
6740 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6741 return cdclk * 2 / 3; /* 384MHz */
6742 case BXT_CDCLK_CD2X_DIV_SEL_2:
6743 return cdclk / 2; /* 288MHz */
6744 case BXT_CDCLK_CD2X_DIV_SEL_4:
6745 return cdclk / 4; /* 144MHz */
6746 }
6747
6748 /* error case, do as if DE PLL isn't enabled */
6749 return 19200;
6750}
6751
Ville Syrjälä1652d192015-03-31 14:12:01 +03006752static int broadwell_get_display_clock_speed(struct drm_device *dev)
6753{
6754 struct drm_i915_private *dev_priv = dev->dev_private;
6755 uint32_t lcpll = I915_READ(LCPLL_CTL);
6756 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6757
6758 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6759 return 800000;
6760 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6761 return 450000;
6762 else if (freq == LCPLL_CLK_FREQ_450)
6763 return 450000;
6764 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6765 return 540000;
6766 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6767 return 337500;
6768 else
6769 return 675000;
6770}
6771
6772static int haswell_get_display_clock_speed(struct drm_device *dev)
6773{
6774 struct drm_i915_private *dev_priv = dev->dev_private;
6775 uint32_t lcpll = I915_READ(LCPLL_CTL);
6776 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6777
6778 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6779 return 800000;
6780 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6781 return 450000;
6782 else if (freq == LCPLL_CLK_FREQ_450)
6783 return 450000;
6784 else if (IS_HSW_ULT(dev))
6785 return 337500;
6786 else
6787 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006788}
6789
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006790static int valleyview_get_display_clock_speed(struct drm_device *dev)
6791{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006792 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6793 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006794}
6795
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006796static int ilk_get_display_clock_speed(struct drm_device *dev)
6797{
6798 return 450000;
6799}
6800
Jesse Barnese70236a2009-09-21 10:42:27 -07006801static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006802{
Jesse Barnese70236a2009-09-21 10:42:27 -07006803 return 400000;
6804}
Jesse Barnes79e53942008-11-07 14:24:08 -08006805
Jesse Barnese70236a2009-09-21 10:42:27 -07006806static int i915_get_display_clock_speed(struct drm_device *dev)
6807{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006809}
Jesse Barnes79e53942008-11-07 14:24:08 -08006810
Jesse Barnese70236a2009-09-21 10:42:27 -07006811static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6812{
6813 return 200000;
6814}
Jesse Barnes79e53942008-11-07 14:24:08 -08006815
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006816static int pnv_get_display_clock_speed(struct drm_device *dev)
6817{
6818 u16 gcfgc = 0;
6819
6820 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6821
6822 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6823 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006825 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006826 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006827 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006828 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006829 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6830 return 200000;
6831 default:
6832 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6833 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006834 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006835 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006836 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006837 }
6838}
6839
Jesse Barnese70236a2009-09-21 10:42:27 -07006840static int i915gm_get_display_clock_speed(struct drm_device *dev)
6841{
6842 u16 gcfgc = 0;
6843
6844 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6845
6846 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006847 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006848 else {
6849 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6850 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006851 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006852 default:
6853 case GC_DISPLAY_CLOCK_190_200_MHZ:
6854 return 190000;
6855 }
6856 }
6857}
Jesse Barnes79e53942008-11-07 14:24:08 -08006858
Jesse Barnese70236a2009-09-21 10:42:27 -07006859static int i865_get_display_clock_speed(struct drm_device *dev)
6860{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006861 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006862}
6863
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006864static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006865{
6866 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006867
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006868 /*
6869 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6870 * encoding is different :(
6871 * FIXME is this the right way to detect 852GM/852GMV?
6872 */
6873 if (dev->pdev->revision == 0x1)
6874 return 133333;
6875
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006876 pci_bus_read_config_word(dev->pdev->bus,
6877 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6878
Jesse Barnese70236a2009-09-21 10:42:27 -07006879 /* Assume that the hardware is in the high speed state. This
6880 * should be the default.
6881 */
6882 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6883 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006884 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006885 case GC_CLOCK_100_200:
6886 return 200000;
6887 case GC_CLOCK_166_250:
6888 return 250000;
6889 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006890 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006891 case GC_CLOCK_133_266:
6892 case GC_CLOCK_133_266_2:
6893 case GC_CLOCK_166_266:
6894 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006895 }
6896
6897 /* Shouldn't happen */
6898 return 0;
6899}
6900
6901static int i830_get_display_clock_speed(struct drm_device *dev)
6902{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006903 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006904}
6905
Ville Syrjälä34edce22015-05-22 11:22:33 +03006906static unsigned int intel_hpll_vco(struct drm_device *dev)
6907{
6908 struct drm_i915_private *dev_priv = dev->dev_private;
6909 static const unsigned int blb_vco[8] = {
6910 [0] = 3200000,
6911 [1] = 4000000,
6912 [2] = 5333333,
6913 [3] = 4800000,
6914 [4] = 6400000,
6915 };
6916 static const unsigned int pnv_vco[8] = {
6917 [0] = 3200000,
6918 [1] = 4000000,
6919 [2] = 5333333,
6920 [3] = 4800000,
6921 [4] = 2666667,
6922 };
6923 static const unsigned int cl_vco[8] = {
6924 [0] = 3200000,
6925 [1] = 4000000,
6926 [2] = 5333333,
6927 [3] = 6400000,
6928 [4] = 3333333,
6929 [5] = 3566667,
6930 [6] = 4266667,
6931 };
6932 static const unsigned int elk_vco[8] = {
6933 [0] = 3200000,
6934 [1] = 4000000,
6935 [2] = 5333333,
6936 [3] = 4800000,
6937 };
6938 static const unsigned int ctg_vco[8] = {
6939 [0] = 3200000,
6940 [1] = 4000000,
6941 [2] = 5333333,
6942 [3] = 6400000,
6943 [4] = 2666667,
6944 [5] = 4266667,
6945 };
6946 const unsigned int *vco_table;
6947 unsigned int vco;
6948 uint8_t tmp = 0;
6949
6950 /* FIXME other chipsets? */
6951 if (IS_GM45(dev))
6952 vco_table = ctg_vco;
6953 else if (IS_G4X(dev))
6954 vco_table = elk_vco;
6955 else if (IS_CRESTLINE(dev))
6956 vco_table = cl_vco;
6957 else if (IS_PINEVIEW(dev))
6958 vco_table = pnv_vco;
6959 else if (IS_G33(dev))
6960 vco_table = blb_vco;
6961 else
6962 return 0;
6963
6964 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6965
6966 vco = vco_table[tmp & 0x7];
6967 if (vco == 0)
6968 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6969 else
6970 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6971
6972 return vco;
6973}
6974
6975static int gm45_get_display_clock_speed(struct drm_device *dev)
6976{
6977 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6978 uint16_t tmp = 0;
6979
6980 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6981
6982 cdclk_sel = (tmp >> 12) & 0x1;
6983
6984 switch (vco) {
6985 case 2666667:
6986 case 4000000:
6987 case 5333333:
6988 return cdclk_sel ? 333333 : 222222;
6989 case 3200000:
6990 return cdclk_sel ? 320000 : 228571;
6991 default:
6992 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6993 return 222222;
6994 }
6995}
6996
6997static int i965gm_get_display_clock_speed(struct drm_device *dev)
6998{
6999 static const uint8_t div_3200[] = { 16, 10, 8 };
7000 static const uint8_t div_4000[] = { 20, 12, 10 };
7001 static const uint8_t div_5333[] = { 24, 16, 14 };
7002 const uint8_t *div_table;
7003 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7004 uint16_t tmp = 0;
7005
7006 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7007
7008 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7009
7010 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7011 goto fail;
7012
7013 switch (vco) {
7014 case 3200000:
7015 div_table = div_3200;
7016 break;
7017 case 4000000:
7018 div_table = div_4000;
7019 break;
7020 case 5333333:
7021 div_table = div_5333;
7022 break;
7023 default:
7024 goto fail;
7025 }
7026
7027 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7028
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007029fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007030 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7031 return 200000;
7032}
7033
7034static int g33_get_display_clock_speed(struct drm_device *dev)
7035{
7036 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7037 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7038 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7039 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7040 const uint8_t *div_table;
7041 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7042 uint16_t tmp = 0;
7043
7044 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7045
7046 cdclk_sel = (tmp >> 4) & 0x7;
7047
7048 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7049 goto fail;
7050
7051 switch (vco) {
7052 case 3200000:
7053 div_table = div_3200;
7054 break;
7055 case 4000000:
7056 div_table = div_4000;
7057 break;
7058 case 4800000:
7059 div_table = div_4800;
7060 break;
7061 case 5333333:
7062 div_table = div_5333;
7063 break;
7064 default:
7065 goto fail;
7066 }
7067
7068 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7069
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007070fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007071 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7072 return 190476;
7073}
7074
Zhenyu Wang2c072452009-06-05 15:38:42 +08007075static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007076intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007077{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007078 while (*num > DATA_LINK_M_N_MASK ||
7079 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007080 *num >>= 1;
7081 *den >>= 1;
7082 }
7083}
7084
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007085static void compute_m_n(unsigned int m, unsigned int n,
7086 uint32_t *ret_m, uint32_t *ret_n)
7087{
7088 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7089 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7090 intel_reduce_m_n_ratio(ret_m, ret_n);
7091}
7092
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007093void
7094intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7095 int pixel_clock, int link_clock,
7096 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007097{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007098 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007099
7100 compute_m_n(bits_per_pixel * pixel_clock,
7101 link_clock * nlanes * 8,
7102 &m_n->gmch_m, &m_n->gmch_n);
7103
7104 compute_m_n(pixel_clock, link_clock,
7105 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007106}
7107
Chris Wilsona7615032011-01-12 17:04:08 +00007108static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7109{
Jani Nikulad330a952014-01-21 11:24:25 +02007110 if (i915.panel_use_ssc >= 0)
7111 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007112 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007113 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007114}
7115
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007116static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7117 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007118{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007119 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007120 struct drm_i915_private *dev_priv = dev->dev_private;
7121 int refclk;
7122
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007123 WARN_ON(!crtc_state->base.state);
7124
Wayne Boyer666a4532015-12-09 12:29:35 -08007125 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007126 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007127 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007128 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007129 refclk = dev_priv->vbt.lvds_ssc_freq;
7130 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007131 } else if (!IS_GEN2(dev)) {
7132 refclk = 96000;
7133 } else {
7134 refclk = 48000;
7135 }
7136
7137 return refclk;
7138}
7139
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007140static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007141{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007142 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007143}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007144
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007145static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7146{
7147 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007148}
7149
Daniel Vetterf47709a2013-03-28 10:42:02 +01007150static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007151 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007152 intel_clock_t *reduced_clock)
7153{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007154 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007155 u32 fp, fp2 = 0;
7156
7157 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007158 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007159 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007160 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007161 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007162 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007163 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007164 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007165 }
7166
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007167 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007168
Daniel Vetterf47709a2013-03-28 10:42:02 +01007169 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007170 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007171 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007173 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007174 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007175 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007176 }
7177}
7178
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007179static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7180 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007181{
7182 u32 reg_val;
7183
7184 /*
7185 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7186 * and set it to a reasonable value instead.
7187 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007188 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007189 reg_val &= 0xffffff00;
7190 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007191 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007192
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007193 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007194 reg_val &= 0x8cffffff;
7195 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007196 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007197
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007198 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007199 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007200 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007201
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007202 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007203 reg_val &= 0x00ffffff;
7204 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007205 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007206}
7207
Daniel Vetterb5518422013-05-03 11:49:48 +02007208static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7209 struct intel_link_m_n *m_n)
7210{
7211 struct drm_device *dev = crtc->base.dev;
7212 struct drm_i915_private *dev_priv = dev->dev_private;
7213 int pipe = crtc->pipe;
7214
Daniel Vettere3b95f12013-05-03 11:49:49 +02007215 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7216 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7217 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7218 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007219}
7220
7221static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007222 struct intel_link_m_n *m_n,
7223 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007224{
7225 struct drm_device *dev = crtc->base.dev;
7226 struct drm_i915_private *dev_priv = dev->dev_private;
7227 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007228 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007229
7230 if (INTEL_INFO(dev)->gen >= 5) {
7231 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7232 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7233 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7234 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007235 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7236 * for gen < 8) and if DRRS is supported (to make sure the
7237 * registers are not unnecessarily accessed).
7238 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307239 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007240 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007241 I915_WRITE(PIPE_DATA_M2(transcoder),
7242 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7243 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7244 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7245 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7246 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007247 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007248 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7249 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7250 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7251 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007252 }
7253}
7254
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307255void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007256{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307257 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7258
7259 if (m_n == M1_N1) {
7260 dp_m_n = &crtc->config->dp_m_n;
7261 dp_m2_n2 = &crtc->config->dp_m2_n2;
7262 } else if (m_n == M2_N2) {
7263
7264 /*
7265 * M2_N2 registers are not supported. Hence m2_n2 divider value
7266 * needs to be programmed into M1_N1.
7267 */
7268 dp_m_n = &crtc->config->dp_m2_n2;
7269 } else {
7270 DRM_ERROR("Unsupported divider value\n");
7271 return;
7272 }
7273
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007274 if (crtc->config->has_pch_encoder)
7275 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007276 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307277 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007278}
7279
Daniel Vetter251ac862015-06-18 10:30:24 +02007280static void vlv_compute_dpll(struct intel_crtc *crtc,
7281 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007282{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007283 u32 dpll, dpll_md;
7284
7285 /*
7286 * Enable DPIO clock input. We should never disable the reference
7287 * clock for pipe B, since VGA hotplug / manual detection depends
7288 * on it.
7289 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007290 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7291 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007292 /* We should never disable this, set it here for state tracking */
7293 if (crtc->pipe == PIPE_B)
7294 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7295 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007296 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007297
Ville Syrjäläd288f652014-10-28 13:20:22 +02007298 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007299 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007300 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007301}
7302
Ville Syrjäläd288f652014-10-28 13:20:22 +02007303static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007304 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007305{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007306 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007307 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007308 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007309 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007310 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007311 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007312
Ville Syrjäläa5805162015-05-26 20:42:30 +03007313 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007314
Ville Syrjäläd288f652014-10-28 13:20:22 +02007315 bestn = pipe_config->dpll.n;
7316 bestm1 = pipe_config->dpll.m1;
7317 bestm2 = pipe_config->dpll.m2;
7318 bestp1 = pipe_config->dpll.p1;
7319 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007320
Jesse Barnes89b667f2013-04-18 14:51:36 -07007321 /* See eDP HDMI DPIO driver vbios notes doc */
7322
7323 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007324 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007325 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326
7327 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007329
7330 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007331 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007333 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007334
7335 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337
7338 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007339 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7340 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7341 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007342 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007343
7344 /*
7345 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7346 * but we don't support that).
7347 * Note: don't use the DAC post divider as it seems unstable.
7348 */
7349 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007350 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007351
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007352 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007354
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007356 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007357 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7358 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007359 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007360 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007361 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007363 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007364
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007365 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007366 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007367 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007368 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007369 0x0df40000);
7370 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007372 0x0df70000);
7373 } else { /* HDMI or VGA */
7374 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007375 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 0x0df70000);
7378 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007380 0x0df40000);
7381 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007382
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007383 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007384 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007385 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7386 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007387 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007390 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007391 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007392}
7393
Daniel Vetter251ac862015-06-18 10:30:24 +02007394static void chv_compute_dpll(struct intel_crtc *crtc,
7395 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007396{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007397 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7398 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007399 DPLL_VCO_ENABLE;
7400 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007401 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007402
Ville Syrjäläd288f652014-10-28 13:20:22 +02007403 pipe_config->dpll_hw_state.dpll_md =
7404 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007405}
7406
Ville Syrjäläd288f652014-10-28 13:20:22 +02007407static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007408 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007409{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007410 struct drm_device *dev = crtc->base.dev;
7411 struct drm_i915_private *dev_priv = dev->dev_private;
7412 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007413 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007414 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307415 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007416 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307417 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307418 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007419
Ville Syrjäläd288f652014-10-28 13:20:22 +02007420 bestn = pipe_config->dpll.n;
7421 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7422 bestm1 = pipe_config->dpll.m1;
7423 bestm2 = pipe_config->dpll.m2 >> 22;
7424 bestp1 = pipe_config->dpll.p1;
7425 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307426 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307427 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307428 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007429
7430 /*
7431 * Enable Refclk and SSC
7432 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007433 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007434 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007435
Ville Syrjäläa5805162015-05-26 20:42:30 +03007436 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007437
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007438 /* p1 and p2 divider */
7439 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7440 5 << DPIO_CHV_S1_DIV_SHIFT |
7441 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7442 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7443 1 << DPIO_CHV_K_DIV_SHIFT);
7444
7445 /* Feedback post-divider - m2 */
7446 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7447
7448 /* Feedback refclk divider - n and m1 */
7449 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7450 DPIO_CHV_M1_DIV_BY_2 |
7451 1 << DPIO_CHV_N_DIV_SHIFT);
7452
7453 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007455
7456 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307457 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7458 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7459 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7460 if (bestm2_frac)
7461 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7462 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007463
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307464 /* Program digital lock detect threshold */
7465 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7466 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7467 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7468 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7469 if (!bestm2_frac)
7470 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7471 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7472
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007473 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307474 if (vco == 5400000) {
7475 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7476 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7477 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7478 tribuf_calcntr = 0x9;
7479 } else if (vco <= 6200000) {
7480 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7481 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7482 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7483 tribuf_calcntr = 0x9;
7484 } else if (vco <= 6480000) {
7485 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7486 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7487 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7488 tribuf_calcntr = 0x8;
7489 } else {
7490 /* Not supported. Apply the same limits as in the max case */
7491 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7492 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7493 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7494 tribuf_calcntr = 0;
7495 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007496 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7497
Ville Syrjälä968040b2015-03-11 22:52:08 +02007498 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307499 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7500 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7501 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7502
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007503 /* AFC Recal */
7504 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7505 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7506 DPIO_AFC_RECAL);
7507
Ville Syrjäläa5805162015-05-26 20:42:30 +03007508 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007509}
7510
Ville Syrjäläd288f652014-10-28 13:20:22 +02007511/**
7512 * vlv_force_pll_on - forcibly enable just the PLL
7513 * @dev_priv: i915 private structure
7514 * @pipe: pipe PLL to enable
7515 * @dpll: PLL configuration
7516 *
7517 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7518 * in cases where we need the PLL enabled even when @pipe is not going to
7519 * be enabled.
7520 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007521int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7522 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007523{
7524 struct intel_crtc *crtc =
7525 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007526 struct intel_crtc_state *pipe_config;
7527
7528 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7529 if (!pipe_config)
7530 return -ENOMEM;
7531
7532 pipe_config->base.crtc = &crtc->base;
7533 pipe_config->pixel_multiplier = 1;
7534 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007535
7536 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007537 chv_compute_dpll(crtc, pipe_config);
7538 chv_prepare_pll(crtc, pipe_config);
7539 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007540 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007541 vlv_compute_dpll(crtc, pipe_config);
7542 vlv_prepare_pll(crtc, pipe_config);
7543 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007544 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007545
7546 kfree(pipe_config);
7547
7548 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007549}
7550
7551/**
7552 * vlv_force_pll_off - forcibly disable just the PLL
7553 * @dev_priv: i915 private structure
7554 * @pipe: pipe PLL to disable
7555 *
7556 * Disable the PLL for @pipe. To be used in cases where we need
7557 * the PLL enabled even when @pipe is not going to be enabled.
7558 */
7559void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7560{
7561 if (IS_CHERRYVIEW(dev))
7562 chv_disable_pll(to_i915(dev), pipe);
7563 else
7564 vlv_disable_pll(to_i915(dev), pipe);
7565}
7566
Daniel Vetter251ac862015-06-18 10:30:24 +02007567static void i9xx_compute_dpll(struct intel_crtc *crtc,
7568 struct intel_crtc_state *crtc_state,
7569 intel_clock_t *reduced_clock,
7570 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007571{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007572 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007574 u32 dpll;
7575 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007576 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007577
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007578 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307579
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007580 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7581 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007582
7583 dpll = DPLL_VGA_MODE_DIS;
7584
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007585 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586 dpll |= DPLLB_MODE_LVDS;
7587 else
7588 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007589
Daniel Vetteref1b4602013-06-01 17:17:04 +02007590 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007591 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007592 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007593 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007594
7595 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007596 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007597
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007598 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007599 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600
7601 /* compute bitmask from p1 value */
7602 if (IS_PINEVIEW(dev))
7603 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7604 else {
7605 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7606 if (IS_G4X(dev) && reduced_clock)
7607 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7608 }
7609 switch (clock->p2) {
7610 case 5:
7611 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7612 break;
7613 case 7:
7614 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7615 break;
7616 case 10:
7617 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7618 break;
7619 case 14:
7620 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7621 break;
7622 }
7623 if (INTEL_INFO(dev)->gen >= 4)
7624 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7625
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007626 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007627 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007628 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007629 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7630 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7631 else
7632 dpll |= PLL_REF_INPUT_DREFCLK;
7633
7634 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007635 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007636
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007637 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007638 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007639 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007640 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007641 }
7642}
7643
Daniel Vetter251ac862015-06-18 10:30:24 +02007644static void i8xx_compute_dpll(struct intel_crtc *crtc,
7645 struct intel_crtc_state *crtc_state,
7646 intel_clock_t *reduced_clock,
7647 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007648{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007649 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007650 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007651 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007652 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007653
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007654 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307655
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007656 dpll = DPLL_VGA_MODE_DIS;
7657
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007658 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007659 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7660 } else {
7661 if (clock->p1 == 2)
7662 dpll |= PLL_P1_DIVIDE_BY_TWO;
7663 else
7664 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7665 if (clock->p2 == 4)
7666 dpll |= PLL_P2_DIVIDE_BY_4;
7667 }
7668
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007669 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007670 dpll |= DPLL_DVO_2X_MODE;
7671
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007672 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7674 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7675 else
7676 dpll |= PLL_REF_INPUT_DREFCLK;
7677
7678 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007679 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007680}
7681
Daniel Vetter8a654f32013-06-01 17:16:22 +02007682static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007683{
7684 struct drm_device *dev = intel_crtc->base.dev;
7685 struct drm_i915_private *dev_priv = dev->dev_private;
7686 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007687 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007688 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007689 uint32_t crtc_vtotal, crtc_vblank_end;
7690 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007691
7692 /* We need to be careful not to changed the adjusted mode, for otherwise
7693 * the hw state checker will get angry at the mismatch. */
7694 crtc_vtotal = adjusted_mode->crtc_vtotal;
7695 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007696
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007697 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007698 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007699 crtc_vtotal -= 1;
7700 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007701
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007702 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007703 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7704 else
7705 vsyncshift = adjusted_mode->crtc_hsync_start -
7706 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007707 if (vsyncshift < 0)
7708 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007709 }
7710
7711 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007712 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007713
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007714 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007715 (adjusted_mode->crtc_hdisplay - 1) |
7716 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007717 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007718 (adjusted_mode->crtc_hblank_start - 1) |
7719 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007720 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007721 (adjusted_mode->crtc_hsync_start - 1) |
7722 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7723
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007724 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007725 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007726 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007727 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007728 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007729 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007730 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007731 (adjusted_mode->crtc_vsync_start - 1) |
7732 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7733
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007734 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7735 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7736 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7737 * bits. */
7738 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7739 (pipe == PIPE_B || pipe == PIPE_C))
7740 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7741
Jani Nikulabc58be62016-03-18 17:05:39 +02007742}
7743
7744static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7745{
7746 struct drm_device *dev = intel_crtc->base.dev;
7747 struct drm_i915_private *dev_priv = dev->dev_private;
7748 enum pipe pipe = intel_crtc->pipe;
7749
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007750 /* pipesrc controls the size that is scaled from, which should
7751 * always be the user's requested size.
7752 */
7753 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007754 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7755 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007756}
7757
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007758static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007759 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007760{
7761 struct drm_device *dev = crtc->base.dev;
7762 struct drm_i915_private *dev_priv = dev->dev_private;
7763 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7764 uint32_t tmp;
7765
7766 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007767 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7768 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007769 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007770 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7771 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007772 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007773 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007775
7776 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007777 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7778 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007779 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007780 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7781 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007782 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007783 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007785
7786 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007787 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7788 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7789 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007790 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007791}
7792
7793static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7794 struct intel_crtc_state *pipe_config)
7795{
7796 struct drm_device *dev = crtc->base.dev;
7797 struct drm_i915_private *dev_priv = dev->dev_private;
7798 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007799
7800 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007801 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7802 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7803
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007804 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7805 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007806}
7807
Daniel Vetterf6a83282014-02-11 15:28:57 -08007808void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007809 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007810{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007811 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7812 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7813 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7814 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007815
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007816 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7817 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7818 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7819 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007820
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007821 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007822 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007823
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007824 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7825 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007826
7827 mode->hsync = drm_mode_hsync(mode);
7828 mode->vrefresh = drm_mode_vrefresh(mode);
7829 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007830}
7831
Daniel Vetter84b046f2013-02-19 18:48:54 +01007832static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7833{
7834 struct drm_device *dev = intel_crtc->base.dev;
7835 struct drm_i915_private *dev_priv = dev->dev_private;
7836 uint32_t pipeconf;
7837
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007838 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007839
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007840 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7841 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7842 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007844 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007845 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007846
Daniel Vetterff9ce462013-04-24 14:57:17 +02007847 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007848 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007849 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007850 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007851 pipeconf |= PIPECONF_DITHER_EN |
7852 PIPECONF_DITHER_TYPE_SP;
7853
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007854 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007855 case 18:
7856 pipeconf |= PIPECONF_6BPC;
7857 break;
7858 case 24:
7859 pipeconf |= PIPECONF_8BPC;
7860 break;
7861 case 30:
7862 pipeconf |= PIPECONF_10BPC;
7863 break;
7864 default:
7865 /* Case prevented by intel_choose_pipe_bpp_dither. */
7866 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007867 }
7868 }
7869
7870 if (HAS_PIPE_CXSR(dev)) {
7871 if (intel_crtc->lowfreq_avail) {
7872 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7873 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7874 } else {
7875 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007876 }
7877 }
7878
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007879 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007880 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007881 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007882 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7883 else
7884 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7885 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007886 pipeconf |= PIPECONF_PROGRESSIVE;
7887
Wayne Boyer666a4532015-12-09 12:29:35 -08007888 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7889 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007890 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007891
Daniel Vetter84b046f2013-02-19 18:48:54 +01007892 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7893 POSTING_READ(PIPECONF(intel_crtc->pipe));
7894}
7895
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007896static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7897 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007898{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007899 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007900 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007901 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007902 intel_clock_t clock;
7903 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007904 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007905 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007906 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007907 struct drm_connector_state *connector_state;
7908 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007909
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007910 memset(&crtc_state->dpll_hw_state, 0,
7911 sizeof(crtc_state->dpll_hw_state));
7912
Jani Nikulaa65347b2015-11-27 12:21:46 +02007913 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007914 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007915
Jani Nikulaa65347b2015-11-27 12:21:46 +02007916 for_each_connector_in_state(state, connector, connector_state, i) {
7917 if (connector_state->crtc == &crtc->base)
7918 num_connectors++;
7919 }
7920
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007921 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007922 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007923
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007924 /*
7925 * Returns a set of divisors for the desired target clock with
7926 * the given refclk, or FALSE. The returned values represent
7927 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7928 * 2) / p1 / p2.
7929 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007930 limit = intel_limit(crtc_state, refclk);
7931 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007932 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007933 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007934 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007935 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7936 return -EINVAL;
7937 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007938
Jani Nikulaf2335332013-09-13 11:03:09 +03007939 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007940 crtc_state->dpll.n = clock.n;
7941 crtc_state->dpll.m1 = clock.m1;
7942 crtc_state->dpll.m2 = clock.m2;
7943 crtc_state->dpll.p1 = clock.p1;
7944 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007945 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007946
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007947 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007948 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007949 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007950 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007951 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007952 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007953 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007954 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007955 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007956 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007957 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007958
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007959 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007960}
7961
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007962static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007963 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007964{
7965 struct drm_device *dev = crtc->base.dev;
7966 struct drm_i915_private *dev_priv = dev->dev_private;
7967 uint32_t tmp;
7968
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007969 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7970 return;
7971
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007972 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007973 if (!(tmp & PFIT_ENABLE))
7974 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007975
Daniel Vetter06922822013-07-11 13:35:40 +02007976 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007977 if (INTEL_INFO(dev)->gen < 4) {
7978 if (crtc->pipe != PIPE_B)
7979 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007980 } else {
7981 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7982 return;
7983 }
7984
Daniel Vetter06922822013-07-11 13:35:40 +02007985 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7987 if (INTEL_INFO(dev)->gen < 5)
7988 pipe_config->gmch_pfit.lvds_border_bits =
7989 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7990}
7991
Jesse Barnesacbec812013-09-20 11:29:32 -07007992static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007993 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007994{
7995 struct drm_device *dev = crtc->base.dev;
7996 struct drm_i915_private *dev_priv = dev->dev_private;
7997 int pipe = pipe_config->cpu_transcoder;
7998 intel_clock_t clock;
7999 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008000 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008001
Shobhit Kumarf573de52014-07-30 20:32:37 +05308002 /* In case of MIPI DPLL will not even be used */
8003 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8004 return;
8005
Ville Syrjäläa5805162015-05-26 20:42:30 +03008006 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008007 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008008 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008009
8010 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8011 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8012 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8013 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8014 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8015
Imre Deakdccbea32015-06-22 23:35:51 +03008016 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008017}
8018
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008019static void
8020i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8021 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008022{
8023 struct drm_device *dev = crtc->base.dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 u32 val, base, offset;
8026 int pipe = crtc->pipe, plane = crtc->plane;
8027 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008028 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008029 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008030 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008031
Damien Lespiau42a7b082015-02-05 19:35:13 +00008032 val = I915_READ(DSPCNTR(plane));
8033 if (!(val & DISPLAY_PLANE_ENABLE))
8034 return;
8035
Damien Lespiaud9806c92015-01-21 14:07:19 +00008036 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008037 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008038 DRM_DEBUG_KMS("failed to alloc fb\n");
8039 return;
8040 }
8041
Damien Lespiau1b842c82015-01-21 13:50:54 +00008042 fb = &intel_fb->base;
8043
Daniel Vetter18c52472015-02-10 17:16:09 +00008044 if (INTEL_INFO(dev)->gen >= 4) {
8045 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008046 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008047 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8048 }
8049 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050
8051 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008052 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008053 fb->pixel_format = fourcc;
8054 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008055
8056 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008057 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008058 offset = I915_READ(DSPTILEOFF(plane));
8059 else
8060 offset = I915_READ(DSPLINOFF(plane));
8061 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8062 } else {
8063 base = I915_READ(DSPADDR(plane));
8064 }
8065 plane_config->base = base;
8066
8067 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008068 fb->width = ((val >> 16) & 0xfff) + 1;
8069 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070
8071 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008072 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008073
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008074 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008075 fb->pixel_format,
8076 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008077
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008078 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008079
Damien Lespiau2844a922015-01-20 12:51:48 +00008080 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8081 pipe_name(pipe), plane, fb->width, fb->height,
8082 fb->bits_per_pixel, base, fb->pitches[0],
8083 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008084
Damien Lespiau2d140302015-02-05 17:22:18 +00008085 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008086}
8087
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008088static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008089 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008090{
8091 struct drm_device *dev = crtc->base.dev;
8092 struct drm_i915_private *dev_priv = dev->dev_private;
8093 int pipe = pipe_config->cpu_transcoder;
8094 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8095 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008096 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008097 int refclk = 100000;
8098
Ville Syrjäläa5805162015-05-26 20:42:30 +03008099 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8101 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8102 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8103 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008104 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008105 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008106
8107 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008108 clock.m2 = (pll_dw0 & 0xff) << 22;
8109 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8110 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008111 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8112 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8113 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8114
Imre Deakdccbea32015-06-22 23:35:51 +03008115 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116}
8117
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008118static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008119 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008120{
8121 struct drm_device *dev = crtc->base.dev;
8122 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008123 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008124 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008125 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008126
Imre Deak17290502016-02-12 18:55:11 +02008127 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8128 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008129 return false;
8130
Daniel Vettere143a212013-07-04 12:01:15 +02008131 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008132 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008133
Imre Deak17290502016-02-12 18:55:11 +02008134 ret = false;
8135
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008136 tmp = I915_READ(PIPECONF(crtc->pipe));
8137 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008138 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008139
Wayne Boyer666a4532015-12-09 12:29:35 -08008140 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008141 switch (tmp & PIPECONF_BPC_MASK) {
8142 case PIPECONF_6BPC:
8143 pipe_config->pipe_bpp = 18;
8144 break;
8145 case PIPECONF_8BPC:
8146 pipe_config->pipe_bpp = 24;
8147 break;
8148 case PIPECONF_10BPC:
8149 pipe_config->pipe_bpp = 30;
8150 break;
8151 default:
8152 break;
8153 }
8154 }
8155
Wayne Boyer666a4532015-12-09 12:29:35 -08008156 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8157 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008158 pipe_config->limited_color_range = true;
8159
Ville Syrjälä282740f2013-09-04 18:30:03 +03008160 if (INTEL_INFO(dev)->gen < 4)
8161 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8162
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008163 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008164 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008165
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008166 i9xx_get_pfit_config(crtc, pipe_config);
8167
Daniel Vetter6c49f242013-06-06 12:45:25 +02008168 if (INTEL_INFO(dev)->gen >= 4) {
8169 tmp = I915_READ(DPLL_MD(crtc->pipe));
8170 pipe_config->pixel_multiplier =
8171 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8172 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008173 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008174 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8175 tmp = I915_READ(DPLL(crtc->pipe));
8176 pipe_config->pixel_multiplier =
8177 ((tmp & SDVO_MULTIPLIER_MASK)
8178 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8179 } else {
8180 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8181 * port and will be fixed up in the encoder->get_config
8182 * function. */
8183 pipe_config->pixel_multiplier = 1;
8184 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008185 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008186 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008187 /*
8188 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8189 * on 830. Filter it out here so that we don't
8190 * report errors due to that.
8191 */
8192 if (IS_I830(dev))
8193 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8194
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008195 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8196 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008197 } else {
8198 /* Mask out read-only status bits. */
8199 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8200 DPLL_PORTC_READY_MASK |
8201 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008202 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008203
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008204 if (IS_CHERRYVIEW(dev))
8205 chv_crtc_clock_get(crtc, pipe_config);
8206 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008207 vlv_crtc_clock_get(crtc, pipe_config);
8208 else
8209 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008210
Ville Syrjälä0f646142015-08-26 19:39:18 +03008211 /*
8212 * Normally the dotclock is filled in by the encoder .get_config()
8213 * but in case the pipe is enabled w/o any ports we need a sane
8214 * default.
8215 */
8216 pipe_config->base.adjusted_mode.crtc_clock =
8217 pipe_config->port_clock / pipe_config->pixel_multiplier;
8218
Imre Deak17290502016-02-12 18:55:11 +02008219 ret = true;
8220
8221out:
8222 intel_display_power_put(dev_priv, power_domain);
8223
8224 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008225}
8226
Paulo Zanonidde86e22012-12-01 12:04:25 -02008227static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008228{
8229 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008230 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008231 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008232 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008233 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008234 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008235 bool has_ck505 = false;
8236 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008237
8238 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008239 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008240 switch (encoder->type) {
8241 case INTEL_OUTPUT_LVDS:
8242 has_panel = true;
8243 has_lvds = true;
8244 break;
8245 case INTEL_OUTPUT_EDP:
8246 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008247 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008248 has_cpu_edp = true;
8249 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008250 default:
8251 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008252 }
8253 }
8254
Keith Packard99eb6a02011-09-26 14:29:12 -07008255 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008256 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008257 can_ssc = has_ck505;
8258 } else {
8259 has_ck505 = false;
8260 can_ssc = true;
8261 }
8262
Imre Deak2de69052013-05-08 13:14:04 +03008263 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8264 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008265
8266 /* Ironlake: try to setup display ref clock before DPLL
8267 * enabling. This is only under driver's control after
8268 * PCH B stepping, previous chipset stepping should be
8269 * ignoring this setting.
8270 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008272
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008273 /* As we must carefully and slowly disable/enable each source in turn,
8274 * compute the final state we want first and check if we need to
8275 * make any changes at all.
8276 */
8277 final = val;
8278 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008279 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008280 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008281 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008282 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8283
8284 final &= ~DREF_SSC_SOURCE_MASK;
8285 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8286 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008287
Keith Packard199e5d72011-09-22 12:01:57 -07008288 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008289 final |= DREF_SSC_SOURCE_ENABLE;
8290
8291 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8292 final |= DREF_SSC1_ENABLE;
8293
8294 if (has_cpu_edp) {
8295 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8296 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8297 else
8298 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8299 } else
8300 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8301 } else {
8302 final |= DREF_SSC_SOURCE_DISABLE;
8303 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8304 }
8305
8306 if (final == val)
8307 return;
8308
8309 /* Always enable nonspread source */
8310 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8311
8312 if (has_ck505)
8313 val |= DREF_NONSPREAD_CK505_ENABLE;
8314 else
8315 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8316
8317 if (has_panel) {
8318 val &= ~DREF_SSC_SOURCE_MASK;
8319 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008320
Keith Packard199e5d72011-09-22 12:01:57 -07008321 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008322 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008323 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008324 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008325 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008326 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008327
8328 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008329 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008330 POSTING_READ(PCH_DREF_CONTROL);
8331 udelay(200);
8332
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008333 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008334
8335 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008336 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008337 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008338 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008339 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008340 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008341 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008342 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008344
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008345 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008346 POSTING_READ(PCH_DREF_CONTROL);
8347 udelay(200);
8348 } else {
8349 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8350
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008351 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008352
8353 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008354 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008355
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359
8360 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008361 val &= ~DREF_SSC_SOURCE_MASK;
8362 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008363
8364 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008366
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008367 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008368 POSTING_READ(PCH_DREF_CONTROL);
8369 udelay(200);
8370 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008371
8372 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008373}
8374
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008375static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008376{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008377 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008378
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008379 tmp = I915_READ(SOUTH_CHICKEN2);
8380 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8381 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008382
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008383 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8384 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8385 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008386
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008387 tmp = I915_READ(SOUTH_CHICKEN2);
8388 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8389 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008391 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8392 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8393 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008394}
8395
8396/* WaMPhyProgramming:hsw */
8397static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8398{
8399 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008400
8401 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8402 tmp &= ~(0xFF << 24);
8403 tmp |= (0x12 << 24);
8404 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8405
Paulo Zanonidde86e22012-12-01 12:04:25 -02008406 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8407 tmp |= (1 << 11);
8408 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8409
8410 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8411 tmp |= (1 << 11);
8412 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8413
Paulo Zanonidde86e22012-12-01 12:04:25 -02008414 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8415 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8416 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8417
8418 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8419 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8420 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8421
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008422 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8423 tmp &= ~(7 << 13);
8424 tmp |= (5 << 13);
8425 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008426
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008427 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8428 tmp &= ~(7 << 13);
8429 tmp |= (5 << 13);
8430 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008431
8432 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8433 tmp &= ~0xFF;
8434 tmp |= 0x1C;
8435 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8436
8437 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8438 tmp &= ~0xFF;
8439 tmp |= 0x1C;
8440 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8441
8442 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8443 tmp &= ~(0xFF << 16);
8444 tmp |= (0x1C << 16);
8445 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8446
8447 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8448 tmp &= ~(0xFF << 16);
8449 tmp |= (0x1C << 16);
8450 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8451
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008452 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8453 tmp |= (1 << 27);
8454 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008455
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008456 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8457 tmp |= (1 << 27);
8458 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008459
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008460 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8461 tmp &= ~(0xF << 28);
8462 tmp |= (4 << 28);
8463 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008464
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008465 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8466 tmp &= ~(0xF << 28);
8467 tmp |= (4 << 28);
8468 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008469}
8470
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008471/* Implements 3 different sequences from BSpec chapter "Display iCLK
8472 * Programming" based on the parameters passed:
8473 * - Sequence to enable CLKOUT_DP
8474 * - Sequence to enable CLKOUT_DP without spread
8475 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8476 */
8477static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8478 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008479{
8480 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008481 uint32_t reg, tmp;
8482
8483 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8484 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008485 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008486 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008487
Ville Syrjäläa5805162015-05-26 20:42:30 +03008488 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008489
8490 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8491 tmp &= ~SBI_SSCCTL_DISABLE;
8492 tmp |= SBI_SSCCTL_PATHALT;
8493 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8494
8495 udelay(24);
8496
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008497 if (with_spread) {
8498 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8499 tmp &= ~SBI_SSCCTL_PATHALT;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008501
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008502 if (with_fdi) {
8503 lpt_reset_fdi_mphy(dev_priv);
8504 lpt_program_fdi_mphy(dev_priv);
8505 }
8506 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008507
Ville Syrjäläc2699522015-08-27 23:55:59 +03008508 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008509 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8510 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8511 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008512
Ville Syrjäläa5805162015-05-26 20:42:30 +03008513 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514}
8515
Paulo Zanoni47701c32013-07-23 11:19:25 -03008516/* Sequence to disable CLKOUT_DP */
8517static void lpt_disable_clkout_dp(struct drm_device *dev)
8518{
8519 struct drm_i915_private *dev_priv = dev->dev_private;
8520 uint32_t reg, tmp;
8521
Ville Syrjäläa5805162015-05-26 20:42:30 +03008522 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008523
Ville Syrjäläc2699522015-08-27 23:55:59 +03008524 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008525 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8526 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8527 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8528
8529 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8530 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8531 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8532 tmp |= SBI_SSCCTL_PATHALT;
8533 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8534 udelay(32);
8535 }
8536 tmp |= SBI_SSCCTL_DISABLE;
8537 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8538 }
8539
Ville Syrjäläa5805162015-05-26 20:42:30 +03008540 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008541}
8542
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008543#define BEND_IDX(steps) ((50 + (steps)) / 5)
8544
8545static const uint16_t sscdivintphase[] = {
8546 [BEND_IDX( 50)] = 0x3B23,
8547 [BEND_IDX( 45)] = 0x3B23,
8548 [BEND_IDX( 40)] = 0x3C23,
8549 [BEND_IDX( 35)] = 0x3C23,
8550 [BEND_IDX( 30)] = 0x3D23,
8551 [BEND_IDX( 25)] = 0x3D23,
8552 [BEND_IDX( 20)] = 0x3E23,
8553 [BEND_IDX( 15)] = 0x3E23,
8554 [BEND_IDX( 10)] = 0x3F23,
8555 [BEND_IDX( 5)] = 0x3F23,
8556 [BEND_IDX( 0)] = 0x0025,
8557 [BEND_IDX( -5)] = 0x0025,
8558 [BEND_IDX(-10)] = 0x0125,
8559 [BEND_IDX(-15)] = 0x0125,
8560 [BEND_IDX(-20)] = 0x0225,
8561 [BEND_IDX(-25)] = 0x0225,
8562 [BEND_IDX(-30)] = 0x0325,
8563 [BEND_IDX(-35)] = 0x0325,
8564 [BEND_IDX(-40)] = 0x0425,
8565 [BEND_IDX(-45)] = 0x0425,
8566 [BEND_IDX(-50)] = 0x0525,
8567};
8568
8569/*
8570 * Bend CLKOUT_DP
8571 * steps -50 to 50 inclusive, in steps of 5
8572 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8573 * change in clock period = -(steps / 10) * 5.787 ps
8574 */
8575static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8576{
8577 uint32_t tmp;
8578 int idx = BEND_IDX(steps);
8579
8580 if (WARN_ON(steps % 5 != 0))
8581 return;
8582
8583 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8584 return;
8585
8586 mutex_lock(&dev_priv->sb_lock);
8587
8588 if (steps % 10 != 0)
8589 tmp = 0xAAAAAAAB;
8590 else
8591 tmp = 0x00000000;
8592 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8593
8594 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8595 tmp &= 0xffff0000;
8596 tmp |= sscdivintphase[idx];
8597 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8598
8599 mutex_unlock(&dev_priv->sb_lock);
8600}
8601
8602#undef BEND_IDX
8603
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008604static void lpt_init_pch_refclk(struct drm_device *dev)
8605{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008606 struct intel_encoder *encoder;
8607 bool has_vga = false;
8608
Damien Lespiaub2784e12014-08-05 11:29:37 +01008609 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008610 switch (encoder->type) {
8611 case INTEL_OUTPUT_ANALOG:
8612 has_vga = true;
8613 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008614 default:
8615 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008616 }
8617 }
8618
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008619 if (has_vga) {
8620 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008621 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008622 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008623 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008624 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008625}
8626
Paulo Zanonidde86e22012-12-01 12:04:25 -02008627/*
8628 * Initialize reference clocks when the driver loads
8629 */
8630void intel_init_pch_refclk(struct drm_device *dev)
8631{
8632 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8633 ironlake_init_pch_refclk(dev);
8634 else if (HAS_PCH_LPT(dev))
8635 lpt_init_pch_refclk(dev);
8636}
8637
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008638static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008639{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008640 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008641 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008642 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008643 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008644 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008645 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008646 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008647 bool is_lvds = false;
8648
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008649 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008650 if (connector_state->crtc != crtc_state->base.crtc)
8651 continue;
8652
8653 encoder = to_intel_encoder(connector_state->best_encoder);
8654
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008655 switch (encoder->type) {
8656 case INTEL_OUTPUT_LVDS:
8657 is_lvds = true;
8658 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008659 default:
8660 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008661 }
8662 num_connectors++;
8663 }
8664
8665 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008666 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008667 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008668 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008669 }
8670
8671 return 120000;
8672}
8673
Daniel Vetter6ff93602013-04-19 11:24:36 +02008674static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008675{
8676 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8678 int pipe = intel_crtc->pipe;
8679 uint32_t val;
8680
Daniel Vetter78114072013-06-13 00:54:57 +02008681 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008683 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008684 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008685 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008686 break;
8687 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008688 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008689 break;
8690 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008691 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008692 break;
8693 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008694 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008695 break;
8696 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008697 /* Case prevented by intel_choose_pipe_bpp_dither. */
8698 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008699 }
8700
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008701 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008702 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8703
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008705 val |= PIPECONF_INTERLACED_ILK;
8706 else
8707 val |= PIPECONF_PROGRESSIVE;
8708
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008709 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008710 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008711
Paulo Zanonic8203562012-09-12 10:06:29 -03008712 I915_WRITE(PIPECONF(pipe), val);
8713 POSTING_READ(PIPECONF(pipe));
8714}
8715
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008716/*
8717 * Set up the pipe CSC unit.
8718 *
8719 * Currently only full range RGB to limited range RGB conversion
8720 * is supported, but eventually this should handle various
8721 * RGB<->YCbCr scenarios as well.
8722 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008723static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008724{
8725 struct drm_device *dev = crtc->dev;
8726 struct drm_i915_private *dev_priv = dev->dev_private;
8727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8728 int pipe = intel_crtc->pipe;
8729 uint16_t coeff = 0x7800; /* 1.0 */
8730
8731 /*
8732 * TODO: Check what kind of values actually come out of the pipe
8733 * with these coeff/postoff values and adjust to get the best
8734 * accuracy. Perhaps we even need to take the bpc value into
8735 * consideration.
8736 */
8737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008738 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008739 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8740
8741 /*
8742 * GY/GU and RY/RU should be the other way around according
8743 * to BSpec, but reality doesn't agree. Just set them up in
8744 * a way that results in the correct picture.
8745 */
8746 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8747 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8748
8749 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8750 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8751
8752 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8753 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8754
8755 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8756 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8757 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8758
8759 if (INTEL_INFO(dev)->gen > 6) {
8760 uint16_t postoff = 0;
8761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008762 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008763 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008764
8765 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8766 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8767 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8768
8769 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8770 } else {
8771 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008773 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008774 mode |= CSC_BLACK_SCREEN_OFFSET;
8775
8776 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8777 }
8778}
8779
Daniel Vetter6ff93602013-04-19 11:24:36 +02008780static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008781{
Jani Nikula391bf042016-03-18 17:05:40 +02008782 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008784 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008785 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008786
Jani Nikula391bf042016-03-18 17:05:40 +02008787 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008788 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8789
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008790 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008791 val |= PIPECONF_INTERLACED_ILK;
8792 else
8793 val |= PIPECONF_PROGRESSIVE;
8794
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008795 I915_WRITE(PIPECONF(cpu_transcoder), val);
8796 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008797}
8798
8799static void haswell_set_pipe_gamma(struct drm_crtc *crtc)
8800{
8801 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008803
8804 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8805 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Jani Nikula391bf042016-03-18 17:05:40 +02008806}
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008807
Jani Nikula391bf042016-03-18 17:05:40 +02008808static void haswell_set_pipemisc(struct drm_crtc *crtc)
8809{
8810 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8812
8813 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8814 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008816 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008817 case 18:
8818 val |= PIPEMISC_DITHER_6_BPC;
8819 break;
8820 case 24:
8821 val |= PIPEMISC_DITHER_8_BPC;
8822 break;
8823 case 30:
8824 val |= PIPEMISC_DITHER_10_BPC;
8825 break;
8826 case 36:
8827 val |= PIPEMISC_DITHER_12_BPC;
8828 break;
8829 default:
8830 /* Case prevented by pipe_config_set_bpp. */
8831 BUG();
8832 }
8833
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008834 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008835 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8836
Jani Nikula391bf042016-03-18 17:05:40 +02008837 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008838 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008839}
8840
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008841static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008842 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008843 intel_clock_t *clock,
8844 bool *has_reduced_clock,
8845 intel_clock_t *reduced_clock)
8846{
8847 struct drm_device *dev = crtc->dev;
8848 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008849 int refclk;
8850 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008851 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008852
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008853 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008854
8855 /*
8856 * Returns a set of divisors for the desired target clock with the given
8857 * refclk, or FALSE. The returned values represent the clock equation:
8858 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8859 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008860 limit = intel_limit(crtc_state, refclk);
8861 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008862 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008863 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008864 if (!ret)
8865 return false;
8866
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008867 return true;
8868}
8869
Paulo Zanonid4b19312012-11-29 11:29:32 -02008870int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8871{
8872 /*
8873 * Account for spread spectrum to avoid
8874 * oversubscribing the link. Max center spread
8875 * is 2.5%; use 5% for safety's sake.
8876 */
8877 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008878 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008879}
8880
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008881static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008882{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008883 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008884}
8885
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008886static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008888 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008889 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008890{
8891 struct drm_crtc *crtc = &intel_crtc->base;
8892 struct drm_device *dev = crtc->dev;
8893 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008894 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008895 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008896 struct drm_connector_state *connector_state;
8897 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008898 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008899 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008900 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008901
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008902 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008903 if (connector_state->crtc != crtc_state->base.crtc)
8904 continue;
8905
8906 encoder = to_intel_encoder(connector_state->best_encoder);
8907
8908 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008909 case INTEL_OUTPUT_LVDS:
8910 is_lvds = true;
8911 break;
8912 case INTEL_OUTPUT_SDVO:
8913 case INTEL_OUTPUT_HDMI:
8914 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008915 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008916 default:
8917 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008918 }
8919
8920 num_connectors++;
8921 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008922
Chris Wilsonc1858122010-12-03 21:35:48 +00008923 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008924 factor = 21;
8925 if (is_lvds) {
8926 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008927 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008928 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008929 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008930 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008931 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008932
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008933 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008934 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008935
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008936 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8937 *fp2 |= FP_CB_TUNE;
8938
Chris Wilson5eddb702010-09-11 13:48:45 +01008939 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008940
Eric Anholta07d6782011-03-30 13:01:08 -07008941 if (is_lvds)
8942 dpll |= DPLLB_MODE_LVDS;
8943 else
8944 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008945
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008946 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008947 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008948
8949 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008950 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008951 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008952 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008953
Eric Anholta07d6782011-03-30 13:01:08 -07008954 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008955 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008956 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008957 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008958
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008959 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008960 case 5:
8961 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8962 break;
8963 case 7:
8964 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8965 break;
8966 case 10:
8967 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8968 break;
8969 case 14:
8970 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8971 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008972 }
8973
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008974 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008975 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008976 else
8977 dpll |= PLL_REF_INPUT_DREFCLK;
8978
Daniel Vetter959e16d2013-06-05 13:34:21 +02008979 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008980}
8981
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008982static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8983 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008984{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008985 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008986 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008987 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008988 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008989 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008990 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008991
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008992 memset(&crtc_state->dpll_hw_state, 0,
8993 sizeof(crtc_state->dpll_hw_state));
8994
Ville Syrjälä7905df22015-11-25 16:35:30 +02008995 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008996
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008997 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8998 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8999
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009000 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009001 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009002 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009003 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9004 return -EINVAL;
9005 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01009006 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009007 if (!crtc_state->clock_set) {
9008 crtc_state->dpll.n = clock.n;
9009 crtc_state->dpll.m1 = clock.m1;
9010 crtc_state->dpll.m2 = clock.m2;
9011 crtc_state->dpll.p1 = clock.p1;
9012 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009013 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009014
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009015 /* 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 +02009016 if (crtc_state->has_pch_encoder) {
9017 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009018 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009019 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009020
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009021 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009022 &fp, &reduced_clock,
9023 has_reduced_clock ? &fp2 : NULL);
9024
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009025 crtc_state->dpll_hw_state.dpll = dpll;
9026 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009027 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009028 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009029 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009030 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009031
Ander Conselvan de Oliveiradaedf202016-03-08 17:46:23 +02009032 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009033 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009034 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009035 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009036 return -EINVAL;
9037 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009038 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009039
Rodrigo Viviab585de2015-03-24 12:40:09 -07009040 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009041 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009042 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009043 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009044
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009045 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009046}
9047
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009048static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9049 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009050{
9051 struct drm_device *dev = crtc->base.dev;
9052 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009053 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009054
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009055 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9056 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9057 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9058 & ~TU_SIZE_MASK;
9059 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9060 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9061 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9062}
9063
9064static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9065 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009066 struct intel_link_m_n *m_n,
9067 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009068{
9069 struct drm_device *dev = crtc->base.dev;
9070 struct drm_i915_private *dev_priv = dev->dev_private;
9071 enum pipe pipe = crtc->pipe;
9072
9073 if (INTEL_INFO(dev)->gen >= 5) {
9074 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9075 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9076 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9077 & ~TU_SIZE_MASK;
9078 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9079 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9080 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009081 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9082 * gen < 8) and if DRRS is supported (to make sure the
9083 * registers are not unnecessarily read).
9084 */
9085 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009086 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009087 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9088 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9089 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9090 & ~TU_SIZE_MASK;
9091 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9092 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9093 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9094 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009095 } else {
9096 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9097 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9098 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9099 & ~TU_SIZE_MASK;
9100 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9101 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9102 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9103 }
9104}
9105
9106void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009107 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009108{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009109 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009110 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9111 else
9112 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009113 &pipe_config->dp_m_n,
9114 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009115}
9116
Daniel Vetter72419202013-04-04 13:28:53 +02009117static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009118 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009119{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009120 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009121 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009122}
9123
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009124static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009125 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009126{
9127 struct drm_device *dev = crtc->base.dev;
9128 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009129 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9130 uint32_t ps_ctrl = 0;
9131 int id = -1;
9132 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009133
Chandra Kondurua1b22782015-04-07 15:28:45 -07009134 /* find scaler attached to this pipe */
9135 for (i = 0; i < crtc->num_scalers; i++) {
9136 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9137 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9138 id = i;
9139 pipe_config->pch_pfit.enabled = true;
9140 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9141 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9142 break;
9143 }
9144 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009145
Chandra Kondurua1b22782015-04-07 15:28:45 -07009146 scaler_state->scaler_id = id;
9147 if (id >= 0) {
9148 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9149 } else {
9150 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009151 }
9152}
9153
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009154static void
9155skylake_get_initial_plane_config(struct intel_crtc *crtc,
9156 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009157{
9158 struct drm_device *dev = crtc->base.dev;
9159 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009160 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009161 int pipe = crtc->pipe;
9162 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009163 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009164 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009165 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009166
Damien Lespiaud9806c92015-01-21 14:07:19 +00009167 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009168 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009169 DRM_DEBUG_KMS("failed to alloc fb\n");
9170 return;
9171 }
9172
Damien Lespiau1b842c82015-01-21 13:50:54 +00009173 fb = &intel_fb->base;
9174
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009175 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009176 if (!(val & PLANE_CTL_ENABLE))
9177 goto error;
9178
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009179 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9180 fourcc = skl_format_to_fourcc(pixel_format,
9181 val & PLANE_CTL_ORDER_RGBX,
9182 val & PLANE_CTL_ALPHA_MASK);
9183 fb->pixel_format = fourcc;
9184 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9185
Damien Lespiau40f46282015-02-27 11:15:21 +00009186 tiling = val & PLANE_CTL_TILED_MASK;
9187 switch (tiling) {
9188 case PLANE_CTL_TILED_LINEAR:
9189 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9190 break;
9191 case PLANE_CTL_TILED_X:
9192 plane_config->tiling = I915_TILING_X;
9193 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9194 break;
9195 case PLANE_CTL_TILED_Y:
9196 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9197 break;
9198 case PLANE_CTL_TILED_YF:
9199 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9200 break;
9201 default:
9202 MISSING_CASE(tiling);
9203 goto error;
9204 }
9205
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009206 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9207 plane_config->base = base;
9208
9209 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9210
9211 val = I915_READ(PLANE_SIZE(pipe, 0));
9212 fb->height = ((val >> 16) & 0xfff) + 1;
9213 fb->width = ((val >> 0) & 0x1fff) + 1;
9214
9215 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009216 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009217 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009218 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9219
9220 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009221 fb->pixel_format,
9222 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009223
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009224 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009225
9226 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9227 pipe_name(pipe), fb->width, fb->height,
9228 fb->bits_per_pixel, base, fb->pitches[0],
9229 plane_config->size);
9230
Damien Lespiau2d140302015-02-05 17:22:18 +00009231 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009232 return;
9233
9234error:
9235 kfree(fb);
9236}
9237
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009238static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009239 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009240{
9241 struct drm_device *dev = crtc->base.dev;
9242 struct drm_i915_private *dev_priv = dev->dev_private;
9243 uint32_t tmp;
9244
9245 tmp = I915_READ(PF_CTL(crtc->pipe));
9246
9247 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009248 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009249 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9250 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009251
9252 /* We currently do not free assignements of panel fitters on
9253 * ivb/hsw (since we don't use the higher upscaling modes which
9254 * differentiates them) so just WARN about this case for now. */
9255 if (IS_GEN7(dev)) {
9256 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9257 PF_PIPE_SEL_IVB(crtc->pipe));
9258 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009259 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009260}
9261
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009262static void
9263ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9264 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009265{
9266 struct drm_device *dev = crtc->base.dev;
9267 struct drm_i915_private *dev_priv = dev->dev_private;
9268 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009269 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009270 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009271 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009272 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009273 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009274
Damien Lespiau42a7b082015-02-05 19:35:13 +00009275 val = I915_READ(DSPCNTR(pipe));
9276 if (!(val & DISPLAY_PLANE_ENABLE))
9277 return;
9278
Damien Lespiaud9806c92015-01-21 14:07:19 +00009279 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009280 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009281 DRM_DEBUG_KMS("failed to alloc fb\n");
9282 return;
9283 }
9284
Damien Lespiau1b842c82015-01-21 13:50:54 +00009285 fb = &intel_fb->base;
9286
Daniel Vetter18c52472015-02-10 17:16:09 +00009287 if (INTEL_INFO(dev)->gen >= 4) {
9288 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009289 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009290 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9291 }
9292 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009293
9294 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009295 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009296 fb->pixel_format = fourcc;
9297 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009298
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009299 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009300 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009301 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009302 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009303 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009304 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009305 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009306 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009307 }
9308 plane_config->base = base;
9309
9310 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009311 fb->width = ((val >> 16) & 0xfff) + 1;
9312 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009313
9314 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009315 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009316
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009317 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009318 fb->pixel_format,
9319 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009320
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009321 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009322
Damien Lespiau2844a922015-01-20 12:51:48 +00009323 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9324 pipe_name(pipe), fb->width, fb->height,
9325 fb->bits_per_pixel, base, fb->pitches[0],
9326 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009327
Damien Lespiau2d140302015-02-05 17:22:18 +00009328 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009329}
9330
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009331static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009332 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009333{
9334 struct drm_device *dev = crtc->base.dev;
9335 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009336 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009337 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009338 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009339
Imre Deak17290502016-02-12 18:55:11 +02009340 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9341 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009342 return false;
9343
Daniel Vettere143a212013-07-04 12:01:15 +02009344 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009345 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009346
Imre Deak17290502016-02-12 18:55:11 +02009347 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009348 tmp = I915_READ(PIPECONF(crtc->pipe));
9349 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009350 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009351
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009352 switch (tmp & PIPECONF_BPC_MASK) {
9353 case PIPECONF_6BPC:
9354 pipe_config->pipe_bpp = 18;
9355 break;
9356 case PIPECONF_8BPC:
9357 pipe_config->pipe_bpp = 24;
9358 break;
9359 case PIPECONF_10BPC:
9360 pipe_config->pipe_bpp = 30;
9361 break;
9362 case PIPECONF_12BPC:
9363 pipe_config->pipe_bpp = 36;
9364 break;
9365 default:
9366 break;
9367 }
9368
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009369 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9370 pipe_config->limited_color_range = true;
9371
Daniel Vetterab9412b2013-05-03 11:49:46 +02009372 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009373 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009374 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009375
Daniel Vetter88adfff2013-03-28 10:42:01 +01009376 pipe_config->has_pch_encoder = true;
9377
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009378 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9379 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9380 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009381
9382 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009383
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009384 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009385 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009386 } else {
9387 tmp = I915_READ(PCH_DPLL_SEL);
9388 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009389 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009390 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009391 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009392 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009393
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009394 pipe_config->shared_dpll =
9395 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9396 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009397
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009398 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9399 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009400
9401 tmp = pipe_config->dpll_hw_state.dpll;
9402 pipe_config->pixel_multiplier =
9403 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9404 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009405
9406 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009407 } else {
9408 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009409 }
9410
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009411 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009412 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009413
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009414 ironlake_get_pfit_config(crtc, pipe_config);
9415
Imre Deak17290502016-02-12 18:55:11 +02009416 ret = true;
9417
9418out:
9419 intel_display_power_put(dev_priv, power_domain);
9420
9421 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009422}
9423
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9425{
9426 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009427 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009428
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009429 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009430 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009431 pipe_name(crtc->pipe));
9432
Rob Clarke2c719b2014-12-15 13:56:32 -05009433 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9434 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009435 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9436 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009437 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9438 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009440 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009441 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009442 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009443 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009444 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009445 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009446 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009447 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009448
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009449 /*
9450 * In theory we can still leave IRQs enabled, as long as only the HPD
9451 * interrupts remain enabled. We used to check for that, but since it's
9452 * gen-specific and since we only disable LCPLL after we fully disable
9453 * the interrupts, the check below should be enough.
9454 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009455 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009456}
9457
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009458static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9459{
9460 struct drm_device *dev = dev_priv->dev;
9461
9462 if (IS_HASWELL(dev))
9463 return I915_READ(D_COMP_HSW);
9464 else
9465 return I915_READ(D_COMP_BDW);
9466}
9467
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009468static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9469{
9470 struct drm_device *dev = dev_priv->dev;
9471
9472 if (IS_HASWELL(dev)) {
9473 mutex_lock(&dev_priv->rps.hw_lock);
9474 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9475 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009476 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009477 mutex_unlock(&dev_priv->rps.hw_lock);
9478 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009479 I915_WRITE(D_COMP_BDW, val);
9480 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009481 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009482}
9483
9484/*
9485 * This function implements pieces of two sequences from BSpec:
9486 * - Sequence for display software to disable LCPLL
9487 * - Sequence for display software to allow package C8+
9488 * The steps implemented here are just the steps that actually touch the LCPLL
9489 * register. Callers should take care of disabling all the display engine
9490 * functions, doing the mode unset, fixing interrupts, etc.
9491 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009492static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9493 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009494{
9495 uint32_t val;
9496
9497 assert_can_disable_lcpll(dev_priv);
9498
9499 val = I915_READ(LCPLL_CTL);
9500
9501 if (switch_to_fclk) {
9502 val |= LCPLL_CD_SOURCE_FCLK;
9503 I915_WRITE(LCPLL_CTL, val);
9504
9505 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9506 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9507 DRM_ERROR("Switching to FCLK failed\n");
9508
9509 val = I915_READ(LCPLL_CTL);
9510 }
9511
9512 val |= LCPLL_PLL_DISABLE;
9513 I915_WRITE(LCPLL_CTL, val);
9514 POSTING_READ(LCPLL_CTL);
9515
9516 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9517 DRM_ERROR("LCPLL still locked\n");
9518
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009519 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009520 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009521 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009522 ndelay(100);
9523
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009524 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9525 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009526 DRM_ERROR("D_COMP RCOMP still in progress\n");
9527
9528 if (allow_power_down) {
9529 val = I915_READ(LCPLL_CTL);
9530 val |= LCPLL_POWER_DOWN_ALLOW;
9531 I915_WRITE(LCPLL_CTL, val);
9532 POSTING_READ(LCPLL_CTL);
9533 }
9534}
9535
9536/*
9537 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9538 * source.
9539 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009540static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009541{
9542 uint32_t val;
9543
9544 val = I915_READ(LCPLL_CTL);
9545
9546 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9547 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9548 return;
9549
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009550 /*
9551 * Make sure we're not on PC8 state before disabling PC8, otherwise
9552 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009553 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009554 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009555
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009556 if (val & LCPLL_POWER_DOWN_ALLOW) {
9557 val &= ~LCPLL_POWER_DOWN_ALLOW;
9558 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009559 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009560 }
9561
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009562 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009563 val |= D_COMP_COMP_FORCE;
9564 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009565 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009566
9567 val = I915_READ(LCPLL_CTL);
9568 val &= ~LCPLL_PLL_DISABLE;
9569 I915_WRITE(LCPLL_CTL, val);
9570
9571 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9572 DRM_ERROR("LCPLL not locked yet\n");
9573
9574 if (val & LCPLL_CD_SOURCE_FCLK) {
9575 val = I915_READ(LCPLL_CTL);
9576 val &= ~LCPLL_CD_SOURCE_FCLK;
9577 I915_WRITE(LCPLL_CTL, val);
9578
9579 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9580 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9581 DRM_ERROR("Switching back to LCPLL failed\n");
9582 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009583
Mika Kuoppala59bad942015-01-16 11:34:40 +02009584 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009585 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009586}
9587
Paulo Zanoni765dab672014-03-07 20:08:18 -03009588/*
9589 * Package states C8 and deeper are really deep PC states that can only be
9590 * reached when all the devices on the system allow it, so even if the graphics
9591 * device allows PC8+, it doesn't mean the system will actually get to these
9592 * states. Our driver only allows PC8+ when going into runtime PM.
9593 *
9594 * The requirements for PC8+ are that all the outputs are disabled, the power
9595 * well is disabled and most interrupts are disabled, and these are also
9596 * requirements for runtime PM. When these conditions are met, we manually do
9597 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9598 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9599 * hang the machine.
9600 *
9601 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9602 * the state of some registers, so when we come back from PC8+ we need to
9603 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9604 * need to take care of the registers kept by RC6. Notice that this happens even
9605 * if we don't put the device in PCI D3 state (which is what currently happens
9606 * because of the runtime PM support).
9607 *
9608 * For more, read "Display Sequences for Package C8" on the hardware
9609 * documentation.
9610 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009611void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009612{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009613 struct drm_device *dev = dev_priv->dev;
9614 uint32_t val;
9615
Paulo Zanonic67a4702013-08-19 13:18:09 -03009616 DRM_DEBUG_KMS("Enabling package C8+\n");
9617
Ville Syrjäläc2699522015-08-27 23:55:59 +03009618 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009619 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9620 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9621 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9622 }
9623
9624 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009625 hsw_disable_lcpll(dev_priv, true, true);
9626}
9627
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009628void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009629{
9630 struct drm_device *dev = dev_priv->dev;
9631 uint32_t val;
9632
Paulo Zanonic67a4702013-08-19 13:18:09 -03009633 DRM_DEBUG_KMS("Disabling package C8+\n");
9634
9635 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009636 lpt_init_pch_refclk(dev);
9637
Ville Syrjäläc2699522015-08-27 23:55:59 +03009638 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009639 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9640 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9641 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9642 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009643}
9644
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009645static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309646{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009647 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009648 struct intel_atomic_state *old_intel_state =
9649 to_intel_atomic_state(old_state);
9650 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309651
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009652 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309653}
9654
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009655/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009656static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009657{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009658 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9659 struct drm_i915_private *dev_priv = state->dev->dev_private;
9660 struct drm_crtc *crtc;
9661 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009662 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009663 unsigned max_pixel_rate = 0, i;
9664 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009665
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009666 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9667 sizeof(intel_state->min_pixclk));
9668
9669 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009670 int pixel_rate;
9671
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009672 crtc_state = to_intel_crtc_state(cstate);
9673 if (!crtc_state->base.enable) {
9674 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009675 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009676 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009677
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009678 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009679
9680 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009681 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9683
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009684 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009685 }
9686
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009687 for_each_pipe(dev_priv, pipe)
9688 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9689
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009690 return max_pixel_rate;
9691}
9692
9693static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9694{
9695 struct drm_i915_private *dev_priv = dev->dev_private;
9696 uint32_t val, data;
9697 int ret;
9698
9699 if (WARN((I915_READ(LCPLL_CTL) &
9700 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9701 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9702 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9703 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9704 "trying to change cdclk frequency with cdclk not enabled\n"))
9705 return;
9706
9707 mutex_lock(&dev_priv->rps.hw_lock);
9708 ret = sandybridge_pcode_write(dev_priv,
9709 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9710 mutex_unlock(&dev_priv->rps.hw_lock);
9711 if (ret) {
9712 DRM_ERROR("failed to inform pcode about cdclk change\n");
9713 return;
9714 }
9715
9716 val = I915_READ(LCPLL_CTL);
9717 val |= LCPLL_CD_SOURCE_FCLK;
9718 I915_WRITE(LCPLL_CTL, val);
9719
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009720 if (wait_for_us(I915_READ(LCPLL_CTL) &
9721 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009722 DRM_ERROR("Switching to FCLK failed\n");
9723
9724 val = I915_READ(LCPLL_CTL);
9725 val &= ~LCPLL_CLK_FREQ_MASK;
9726
9727 switch (cdclk) {
9728 case 450000:
9729 val |= LCPLL_CLK_FREQ_450;
9730 data = 0;
9731 break;
9732 case 540000:
9733 val |= LCPLL_CLK_FREQ_54O_BDW;
9734 data = 1;
9735 break;
9736 case 337500:
9737 val |= LCPLL_CLK_FREQ_337_5_BDW;
9738 data = 2;
9739 break;
9740 case 675000:
9741 val |= LCPLL_CLK_FREQ_675_BDW;
9742 data = 3;
9743 break;
9744 default:
9745 WARN(1, "invalid cdclk frequency\n");
9746 return;
9747 }
9748
9749 I915_WRITE(LCPLL_CTL, val);
9750
9751 val = I915_READ(LCPLL_CTL);
9752 val &= ~LCPLL_CD_SOURCE_FCLK;
9753 I915_WRITE(LCPLL_CTL, val);
9754
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009755 if (wait_for_us((I915_READ(LCPLL_CTL) &
9756 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009757 DRM_ERROR("Switching back to LCPLL failed\n");
9758
9759 mutex_lock(&dev_priv->rps.hw_lock);
9760 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9761 mutex_unlock(&dev_priv->rps.hw_lock);
9762
9763 intel_update_cdclk(dev);
9764
9765 WARN(cdclk != dev_priv->cdclk_freq,
9766 "cdclk requested %d kHz but got %d kHz\n",
9767 cdclk, dev_priv->cdclk_freq);
9768}
9769
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009770static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009771{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009772 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009773 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009774 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009775 int cdclk;
9776
9777 /*
9778 * FIXME should also account for plane ratio
9779 * once 64bpp pixel formats are supported.
9780 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009781 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009782 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009783 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009784 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009785 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009786 cdclk = 450000;
9787 else
9788 cdclk = 337500;
9789
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009790 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009791 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9792 cdclk, dev_priv->max_cdclk_freq);
9793 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009794 }
9795
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009796 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9797 if (!intel_state->active_crtcs)
9798 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009799
9800 return 0;
9801}
9802
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009803static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009804{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009805 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009806 struct intel_atomic_state *old_intel_state =
9807 to_intel_atomic_state(old_state);
9808 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009809
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009810 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009811}
9812
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009813static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9814 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009815{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009816 struct intel_encoder *intel_encoder =
9817 intel_ddi_get_crtc_new_encoder(crtc_state);
9818
9819 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9820 if (!intel_ddi_pll_select(crtc, crtc_state))
9821 return -EINVAL;
9822 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009823
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009824 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009825
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009826 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009827}
9828
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309829static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9830 enum port port,
9831 struct intel_crtc_state *pipe_config)
9832{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009833 enum intel_dpll_id id;
9834
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309835 switch (port) {
9836 case PORT_A:
9837 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009838 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309839 break;
9840 case PORT_B:
9841 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009842 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309843 break;
9844 case PORT_C:
9845 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009846 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309847 break;
9848 default:
9849 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009850 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309851 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009852
9853 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309854}
9855
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009856static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9857 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009858 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009859{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009860 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009861 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009862
9863 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9864 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9865
9866 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009867 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009868 id = DPLL_ID_SKL_DPLL0;
9869 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009870 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009871 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009872 break;
9873 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009874 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009875 break;
9876 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009877 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009878 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009879 default:
9880 MISSING_CASE(pipe_config->ddi_pll_sel);
9881 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009882 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009883
9884 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009885}
9886
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009887static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9888 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009889 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009890{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009891 enum intel_dpll_id id;
9892
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009893 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9894
9895 switch (pipe_config->ddi_pll_sel) {
9896 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009897 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009898 break;
9899 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009900 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009901 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009902 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009903 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009904 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009905 case PORT_CLK_SEL_LCPLL_810:
9906 id = DPLL_ID_LCPLL_810;
9907 break;
9908 case PORT_CLK_SEL_LCPLL_1350:
9909 id = DPLL_ID_LCPLL_1350;
9910 break;
9911 case PORT_CLK_SEL_LCPLL_2700:
9912 id = DPLL_ID_LCPLL_2700;
9913 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009914 default:
9915 MISSING_CASE(pipe_config->ddi_pll_sel);
9916 /* fall through */
9917 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009918 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009919 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009920
9921 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009922}
9923
Jani Nikulacf304292016-03-18 17:05:41 +02009924static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9925 struct intel_crtc_state *pipe_config,
9926 unsigned long *power_domain_mask)
9927{
9928 struct drm_device *dev = crtc->base.dev;
9929 struct drm_i915_private *dev_priv = dev->dev_private;
9930 enum intel_display_power_domain power_domain;
9931 u32 tmp;
9932
9933 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9934
9935 /*
9936 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9937 * consistency and less surprising code; it's in always on power).
9938 */
9939 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9940 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9941 enum pipe trans_edp_pipe;
9942 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9943 default:
9944 WARN(1, "unknown pipe linked to edp transcoder\n");
9945 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9946 case TRANS_DDI_EDP_INPUT_A_ON:
9947 trans_edp_pipe = PIPE_A;
9948 break;
9949 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9950 trans_edp_pipe = PIPE_B;
9951 break;
9952 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9953 trans_edp_pipe = PIPE_C;
9954 break;
9955 }
9956
9957 if (trans_edp_pipe == crtc->pipe)
9958 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9959 }
9960
9961 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9962 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9963 return false;
9964 *power_domain_mask |= BIT(power_domain);
9965
9966 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9967
9968 return tmp & PIPECONF_ENABLE;
9969}
9970
Jani Nikula4d1de972016-03-18 17:05:42 +02009971static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9972 struct intel_crtc_state *pipe_config,
9973 unsigned long *power_domain_mask)
9974{
9975 struct drm_device *dev = crtc->base.dev;
9976 struct drm_i915_private *dev_priv = dev->dev_private;
9977 enum intel_display_power_domain power_domain;
9978 enum port port;
9979 enum transcoder cpu_transcoder;
9980 u32 tmp;
9981
9982 pipe_config->has_dsi_encoder = false;
9983
9984 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9985 if (port == PORT_A)
9986 cpu_transcoder = TRANSCODER_DSI_A;
9987 else
9988 cpu_transcoder = TRANSCODER_DSI_C;
9989
9990 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9991 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9992 continue;
9993 *power_domain_mask |= BIT(power_domain);
9994
9995 /* XXX: this works for video mode only */
9996 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9997 if (!(tmp & DPI_ENABLE))
9998 continue;
9999
10000 tmp = I915_READ(MIPI_CTRL(port));
10001 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10002 continue;
10003
10004 pipe_config->cpu_transcoder = cpu_transcoder;
10005 pipe_config->has_dsi_encoder = true;
10006 break;
10007 }
10008
10009 return pipe_config->has_dsi_encoder;
10010}
10011
Daniel Vetter26804af2014-06-25 22:01:55 +030010012static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010013 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010014{
10015 struct drm_device *dev = crtc->base.dev;
10016 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010017 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010018 enum port port;
10019 uint32_t tmp;
10020
10021 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10022
10023 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10024
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010025 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010026 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010027 else if (IS_BROXTON(dev))
10028 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010029 else
10030 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010031
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010032 pll = pipe_config->shared_dpll;
10033 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010034 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10035 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010036 }
10037
Daniel Vetter26804af2014-06-25 22:01:55 +030010038 /*
10039 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10040 * DDI E. So just check whether this pipe is wired to DDI E and whether
10041 * the PCH transcoder is on.
10042 */
Damien Lespiauca370452013-12-03 13:56:24 +000010043 if (INTEL_INFO(dev)->gen < 9 &&
10044 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010045 pipe_config->has_pch_encoder = true;
10046
10047 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10048 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10049 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10050
10051 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10052 }
10053}
10054
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010055static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010056 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010057{
10058 struct drm_device *dev = crtc->base.dev;
10059 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010060 enum intel_display_power_domain power_domain;
10061 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010062 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010063
Imre Deak17290502016-02-12 18:55:11 +020010064 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10065 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010066 return false;
Imre Deak17290502016-02-12 18:55:11 +020010067 power_domain_mask = BIT(power_domain);
10068
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010069 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010070
Jani Nikulacf304292016-03-18 17:05:41 +020010071 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010072
Jani Nikula4d1de972016-03-18 17:05:42 +020010073 if (IS_BROXTON(dev_priv)) {
10074 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10075 &power_domain_mask);
10076 WARN_ON(active && pipe_config->has_dsi_encoder);
10077 if (pipe_config->has_dsi_encoder)
10078 active = true;
10079 }
10080
Jani Nikulacf304292016-03-18 17:05:41 +020010081 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010082 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010083
Jani Nikula4d1de972016-03-18 17:05:42 +020010084 if (!pipe_config->has_dsi_encoder) {
10085 haswell_get_ddi_port_state(crtc, pipe_config);
10086 intel_get_pipe_timings(crtc, pipe_config);
10087 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010088
Jani Nikulabc58be62016-03-18 17:05:39 +020010089 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010090
Chandra Kondurua1b22782015-04-07 15:28:45 -070010091 if (INTEL_INFO(dev)->gen >= 9) {
10092 skl_init_scalers(dev, crtc, pipe_config);
10093 }
10094
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010095 if (INTEL_INFO(dev)->gen >= 9) {
10096 pipe_config->scaler_state.scaler_id = -1;
10097 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10098 }
10099
Imre Deak17290502016-02-12 18:55:11 +020010100 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10101 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10102 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010103 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010104 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010105 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010106 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010107 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010108
Jesse Barnese59150d2014-01-07 13:30:45 -080010109 if (IS_HASWELL(dev))
10110 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10111 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010112
Jani Nikula4d1de972016-03-18 17:05:42 +020010113 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10114 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010115 pipe_config->pixel_multiplier =
10116 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10117 } else {
10118 pipe_config->pixel_multiplier = 1;
10119 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010120
Imre Deak17290502016-02-12 18:55:11 +020010121out:
10122 for_each_power_domain(power_domain, power_domain_mask)
10123 intel_display_power_put(dev_priv, power_domain);
10124
Jani Nikulacf304292016-03-18 17:05:41 +020010125 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010126}
10127
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010128static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10129 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010130{
10131 struct drm_device *dev = crtc->dev;
10132 struct drm_i915_private *dev_priv = dev->dev_private;
10133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010134 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010135
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010136 if (plane_state && plane_state->visible) {
10137 unsigned int width = plane_state->base.crtc_w;
10138 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010139 unsigned int stride = roundup_pow_of_two(width) * 4;
10140
10141 switch (stride) {
10142 default:
10143 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10144 width, stride);
10145 stride = 256;
10146 /* fallthrough */
10147 case 256:
10148 case 512:
10149 case 1024:
10150 case 2048:
10151 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010152 }
10153
Ville Syrjälädc41c152014-08-13 11:57:05 +030010154 cntl |= CURSOR_ENABLE |
10155 CURSOR_GAMMA_ENABLE |
10156 CURSOR_FORMAT_ARGB |
10157 CURSOR_STRIDE(stride);
10158
10159 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010160 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010161
Ville Syrjälädc41c152014-08-13 11:57:05 +030010162 if (intel_crtc->cursor_cntl != 0 &&
10163 (intel_crtc->cursor_base != base ||
10164 intel_crtc->cursor_size != size ||
10165 intel_crtc->cursor_cntl != cntl)) {
10166 /* On these chipsets we can only modify the base/size/stride
10167 * whilst the cursor is disabled.
10168 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010169 I915_WRITE(CURCNTR(PIPE_A), 0);
10170 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010171 intel_crtc->cursor_cntl = 0;
10172 }
10173
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010174 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010175 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010176 intel_crtc->cursor_base = base;
10177 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010178
10179 if (intel_crtc->cursor_size != size) {
10180 I915_WRITE(CURSIZE, size);
10181 intel_crtc->cursor_size = size;
10182 }
10183
Chris Wilson4b0e3332014-05-30 16:35:26 +030010184 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010185 I915_WRITE(CURCNTR(PIPE_A), cntl);
10186 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010187 intel_crtc->cursor_cntl = cntl;
10188 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010189}
10190
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010191static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10192 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010193{
10194 struct drm_device *dev = crtc->dev;
10195 struct drm_i915_private *dev_priv = dev->dev_private;
10196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10197 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010198 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010199
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010200 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010201 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010202 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010203 case 64:
10204 cntl |= CURSOR_MODE_64_ARGB_AX;
10205 break;
10206 case 128:
10207 cntl |= CURSOR_MODE_128_ARGB_AX;
10208 break;
10209 case 256:
10210 cntl |= CURSOR_MODE_256_ARGB_AX;
10211 break;
10212 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010213 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010214 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010215 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010216 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010217
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010218 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010219 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010220
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010221 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10222 cntl |= CURSOR_ROTATE_180;
10223 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010224
Chris Wilson4b0e3332014-05-30 16:35:26 +030010225 if (intel_crtc->cursor_cntl != cntl) {
10226 I915_WRITE(CURCNTR(pipe), cntl);
10227 POSTING_READ(CURCNTR(pipe));
10228 intel_crtc->cursor_cntl = cntl;
10229 }
10230
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010231 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010232 I915_WRITE(CURBASE(pipe), base);
10233 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010234
10235 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010236}
10237
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010238/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010239static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010240 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010241{
10242 struct drm_device *dev = crtc->dev;
10243 struct drm_i915_private *dev_priv = dev->dev_private;
10244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10245 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010246 u32 base = intel_crtc->cursor_addr;
10247 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010248
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010249 if (plane_state) {
10250 int x = plane_state->base.crtc_x;
10251 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010252
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010253 if (x < 0) {
10254 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10255 x = -x;
10256 }
10257 pos |= x << CURSOR_X_SHIFT;
10258
10259 if (y < 0) {
10260 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10261 y = -y;
10262 }
10263 pos |= y << CURSOR_Y_SHIFT;
10264
10265 /* ILK+ do this automagically */
10266 if (HAS_GMCH_DISPLAY(dev) &&
10267 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10268 base += (plane_state->base.crtc_h *
10269 plane_state->base.crtc_w - 1) * 4;
10270 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010271 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010272
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010273 I915_WRITE(CURPOS(pipe), pos);
10274
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010275 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010276 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010277 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010278 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010279}
10280
Ville Syrjälädc41c152014-08-13 11:57:05 +030010281static bool cursor_size_ok(struct drm_device *dev,
10282 uint32_t width, uint32_t height)
10283{
10284 if (width == 0 || height == 0)
10285 return false;
10286
10287 /*
10288 * 845g/865g are special in that they are only limited by
10289 * the width of their cursors, the height is arbitrary up to
10290 * the precision of the register. Everything else requires
10291 * square cursors, limited to a few power-of-two sizes.
10292 */
10293 if (IS_845G(dev) || IS_I865G(dev)) {
10294 if ((width & 63) != 0)
10295 return false;
10296
10297 if (width > (IS_845G(dev) ? 64 : 512))
10298 return false;
10299
10300 if (height > 1023)
10301 return false;
10302 } else {
10303 switch (width | height) {
10304 case 256:
10305 case 128:
10306 if (IS_GEN2(dev))
10307 return false;
10308 case 64:
10309 break;
10310 default:
10311 return false;
10312 }
10313 }
10314
10315 return true;
10316}
10317
Jesse Barnes79e53942008-11-07 14:24:08 -080010318static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010319 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010320{
James Simmons72034252010-08-03 01:33:19 +010010321 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010323
James Simmons72034252010-08-03 01:33:19 +010010324 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010325 intel_crtc->lut_r[i] = red[i] >> 8;
10326 intel_crtc->lut_g[i] = green[i] >> 8;
10327 intel_crtc->lut_b[i] = blue[i] >> 8;
10328 }
10329
10330 intel_crtc_load_lut(crtc);
10331}
10332
Jesse Barnes79e53942008-11-07 14:24:08 -080010333/* VESA 640x480x72Hz mode to set on the pipe */
10334static struct drm_display_mode load_detect_mode = {
10335 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10336 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10337};
10338
Daniel Vettera8bb6812014-02-10 18:00:39 +010010339struct drm_framebuffer *
10340__intel_framebuffer_create(struct drm_device *dev,
10341 struct drm_mode_fb_cmd2 *mode_cmd,
10342 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010343{
10344 struct intel_framebuffer *intel_fb;
10345 int ret;
10346
10347 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010348 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010349 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010350
10351 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010352 if (ret)
10353 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010354
10355 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010356
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010357err:
10358 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010359 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010360}
10361
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010362static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010363intel_framebuffer_create(struct drm_device *dev,
10364 struct drm_mode_fb_cmd2 *mode_cmd,
10365 struct drm_i915_gem_object *obj)
10366{
10367 struct drm_framebuffer *fb;
10368 int ret;
10369
10370 ret = i915_mutex_lock_interruptible(dev);
10371 if (ret)
10372 return ERR_PTR(ret);
10373 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10374 mutex_unlock(&dev->struct_mutex);
10375
10376 return fb;
10377}
10378
Chris Wilsond2dff872011-04-19 08:36:26 +010010379static u32
10380intel_framebuffer_pitch_for_width(int width, int bpp)
10381{
10382 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10383 return ALIGN(pitch, 64);
10384}
10385
10386static u32
10387intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10388{
10389 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010390 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010391}
10392
10393static struct drm_framebuffer *
10394intel_framebuffer_create_for_mode(struct drm_device *dev,
10395 struct drm_display_mode *mode,
10396 int depth, int bpp)
10397{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010398 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010399 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010400 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010401
10402 obj = i915_gem_alloc_object(dev,
10403 intel_framebuffer_size_for_mode(mode, bpp));
10404 if (obj == NULL)
10405 return ERR_PTR(-ENOMEM);
10406
10407 mode_cmd.width = mode->hdisplay;
10408 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010409 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10410 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010411 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010412
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010413 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10414 if (IS_ERR(fb))
10415 drm_gem_object_unreference_unlocked(&obj->base);
10416
10417 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010418}
10419
10420static struct drm_framebuffer *
10421mode_fits_in_fbdev(struct drm_device *dev,
10422 struct drm_display_mode *mode)
10423{
Daniel Vetter06957262015-08-10 13:34:08 +020010424#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010425 struct drm_i915_private *dev_priv = dev->dev_private;
10426 struct drm_i915_gem_object *obj;
10427 struct drm_framebuffer *fb;
10428
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010429 if (!dev_priv->fbdev)
10430 return NULL;
10431
10432 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010433 return NULL;
10434
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010435 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010436 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010437
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010438 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010439 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10440 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010441 return NULL;
10442
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010443 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010444 return NULL;
10445
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010446 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010447 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010448#else
10449 return NULL;
10450#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010451}
10452
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010453static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10454 struct drm_crtc *crtc,
10455 struct drm_display_mode *mode,
10456 struct drm_framebuffer *fb,
10457 int x, int y)
10458{
10459 struct drm_plane_state *plane_state;
10460 int hdisplay, vdisplay;
10461 int ret;
10462
10463 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10464 if (IS_ERR(plane_state))
10465 return PTR_ERR(plane_state);
10466
10467 if (mode)
10468 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10469 else
10470 hdisplay = vdisplay = 0;
10471
10472 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10473 if (ret)
10474 return ret;
10475 drm_atomic_set_fb_for_plane(plane_state, fb);
10476 plane_state->crtc_x = 0;
10477 plane_state->crtc_y = 0;
10478 plane_state->crtc_w = hdisplay;
10479 plane_state->crtc_h = vdisplay;
10480 plane_state->src_x = x << 16;
10481 plane_state->src_y = y << 16;
10482 plane_state->src_w = hdisplay << 16;
10483 plane_state->src_h = vdisplay << 16;
10484
10485 return 0;
10486}
10487
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010488bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010489 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010490 struct intel_load_detect_pipe *old,
10491 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010492{
10493 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010494 struct intel_encoder *intel_encoder =
10495 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010496 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010497 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010498 struct drm_crtc *crtc = NULL;
10499 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010500 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010501 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010502 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010503 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010504 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010505 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010506
Chris Wilsond2dff872011-04-19 08:36:26 +010010507 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010508 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010509 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010510
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010511 old->restore_state = NULL;
10512
Rob Clark51fd3712013-11-19 12:10:12 -050010513retry:
10514 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10515 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010516 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010517
Jesse Barnes79e53942008-11-07 14:24:08 -080010518 /*
10519 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010520 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010521 * - if the connector already has an assigned crtc, use it (but make
10522 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010523 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010524 * - try to find the first unused crtc that can drive this connector,
10525 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010526 */
10527
10528 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010529 if (connector->state->crtc) {
10530 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010531
Rob Clark51fd3712013-11-19 12:10:12 -050010532 ret = drm_modeset_lock(&crtc->mutex, ctx);
10533 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010534 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010535
10536 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010537 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010538 }
10539
10540 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010541 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010542 i++;
10543 if (!(encoder->possible_crtcs & (1 << i)))
10544 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010545
10546 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10547 if (ret)
10548 goto fail;
10549
10550 if (possible_crtc->state->enable) {
10551 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010552 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010553 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010554
10555 crtc = possible_crtc;
10556 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010557 }
10558
10559 /*
10560 * If we didn't find an unused CRTC, don't use any.
10561 */
10562 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010563 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010564 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010565 }
10566
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010567found:
10568 intel_crtc = to_intel_crtc(crtc);
10569
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010570 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10571 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010572 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010573
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010574 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010575 restore_state = drm_atomic_state_alloc(dev);
10576 if (!state || !restore_state) {
10577 ret = -ENOMEM;
10578 goto fail;
10579 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010580
10581 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010582 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010583
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010584 connector_state = drm_atomic_get_connector_state(state, connector);
10585 if (IS_ERR(connector_state)) {
10586 ret = PTR_ERR(connector_state);
10587 goto fail;
10588 }
10589
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010590 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10591 if (ret)
10592 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010593
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010594 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10595 if (IS_ERR(crtc_state)) {
10596 ret = PTR_ERR(crtc_state);
10597 goto fail;
10598 }
10599
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010600 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010601
Chris Wilson64927112011-04-20 07:25:26 +010010602 if (!mode)
10603 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010604
Chris Wilsond2dff872011-04-19 08:36:26 +010010605 /* We need a framebuffer large enough to accommodate all accesses
10606 * that the plane may generate whilst we perform load detection.
10607 * We can not rely on the fbcon either being present (we get called
10608 * during its initialisation to detect all boot displays, or it may
10609 * not even exist) or that it is large enough to satisfy the
10610 * requested mode.
10611 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010612 fb = mode_fits_in_fbdev(dev, mode);
10613 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010614 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010615 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010616 } else
10617 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010618 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010619 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010620 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010621 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010622
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010623 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10624 if (ret)
10625 goto fail;
10626
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010627 drm_framebuffer_unreference(fb);
10628
10629 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10630 if (ret)
10631 goto fail;
10632
10633 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10634 if (!ret)
10635 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10636 if (!ret)
10637 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10638 if (ret) {
10639 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10640 goto fail;
10641 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010642
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010643 ret = drm_atomic_commit(state);
10644 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010645 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010646 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010647 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010648
10649 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010650
Jesse Barnes79e53942008-11-07 14:24:08 -080010651 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010652 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010653 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010654
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010655fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010656 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010657 drm_atomic_state_free(restore_state);
10658 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010659
Rob Clark51fd3712013-11-19 12:10:12 -050010660 if (ret == -EDEADLK) {
10661 drm_modeset_backoff(ctx);
10662 goto retry;
10663 }
10664
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010665 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010666}
10667
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010668void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010669 struct intel_load_detect_pipe *old,
10670 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010671{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010672 struct intel_encoder *intel_encoder =
10673 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010674 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010675 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010676 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010677
Chris Wilsond2dff872011-04-19 08:36:26 +010010678 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010679 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010680 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010681
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010682 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010683 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010684
10685 ret = drm_atomic_commit(state);
10686 if (ret) {
10687 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10688 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010689 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010690}
10691
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010692static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010693 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010694{
10695 struct drm_i915_private *dev_priv = dev->dev_private;
10696 u32 dpll = pipe_config->dpll_hw_state.dpll;
10697
10698 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010699 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010700 else if (HAS_PCH_SPLIT(dev))
10701 return 120000;
10702 else if (!IS_GEN2(dev))
10703 return 96000;
10704 else
10705 return 48000;
10706}
10707
Jesse Barnes79e53942008-11-07 14:24:08 -080010708/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010709static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010710 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010711{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010712 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010713 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010714 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010715 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010716 u32 fp;
10717 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010718 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010719 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010720
10721 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010722 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010723 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010724 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010725
10726 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010727 if (IS_PINEVIEW(dev)) {
10728 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10729 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010730 } else {
10731 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10732 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10733 }
10734
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010735 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010736 if (IS_PINEVIEW(dev))
10737 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10738 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010739 else
10740 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010741 DPLL_FPA01_P1_POST_DIV_SHIFT);
10742
10743 switch (dpll & DPLL_MODE_MASK) {
10744 case DPLLB_MODE_DAC_SERIAL:
10745 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10746 5 : 10;
10747 break;
10748 case DPLLB_MODE_LVDS:
10749 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10750 7 : 14;
10751 break;
10752 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010753 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010754 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010755 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010756 }
10757
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010758 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010759 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010760 else
Imre Deakdccbea32015-06-22 23:35:51 +030010761 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010763 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010764 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010765
10766 if (is_lvds) {
10767 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10768 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010769
10770 if (lvds & LVDS_CLKB_POWER_UP)
10771 clock.p2 = 7;
10772 else
10773 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010774 } else {
10775 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10776 clock.p1 = 2;
10777 else {
10778 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10779 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10780 }
10781 if (dpll & PLL_P2_DIVIDE_BY_4)
10782 clock.p2 = 4;
10783 else
10784 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010785 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010786
Imre Deakdccbea32015-06-22 23:35:51 +030010787 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010788 }
10789
Ville Syrjälä18442d02013-09-13 16:00:08 +030010790 /*
10791 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010792 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010793 * encoder's get_config() function.
10794 */
Imre Deakdccbea32015-06-22 23:35:51 +030010795 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010796}
10797
Ville Syrjälä6878da02013-09-13 15:59:11 +030010798int intel_dotclock_calculate(int link_freq,
10799 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010800{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010801 /*
10802 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010803 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010804 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010805 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010806 *
10807 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010808 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010809 */
10810
Ville Syrjälä6878da02013-09-13 15:59:11 +030010811 if (!m_n->link_n)
10812 return 0;
10813
10814 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10815}
10816
Ville Syrjälä18442d02013-09-13 16:00:08 +030010817static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010818 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010819{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010820 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010821
10822 /* read out port_clock from the DPLL */
10823 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010824
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010825 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010826 * In case there is an active pipe without active ports,
10827 * we may need some idea for the dotclock anyway.
10828 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010829 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010830 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010831 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010832 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010833}
10834
10835/** Returns the currently programmed mode of the given pipe. */
10836struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10837 struct drm_crtc *crtc)
10838{
Jesse Barnes548f2452011-02-17 10:40:53 -080010839 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010841 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010842 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010843 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010844 int htot = I915_READ(HTOTAL(cpu_transcoder));
10845 int hsync = I915_READ(HSYNC(cpu_transcoder));
10846 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10847 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010848 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010849
10850 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10851 if (!mode)
10852 return NULL;
10853
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010854 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10855 if (!pipe_config) {
10856 kfree(mode);
10857 return NULL;
10858 }
10859
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010860 /*
10861 * Construct a pipe_config sufficient for getting the clock info
10862 * back out of crtc_clock_get.
10863 *
10864 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10865 * to use a real value here instead.
10866 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010867 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10868 pipe_config->pixel_multiplier = 1;
10869 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10870 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10871 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10872 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010873
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010874 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010875 mode->hdisplay = (htot & 0xffff) + 1;
10876 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10877 mode->hsync_start = (hsync & 0xffff) + 1;
10878 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10879 mode->vdisplay = (vtot & 0xffff) + 1;
10880 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10881 mode->vsync_start = (vsync & 0xffff) + 1;
10882 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10883
10884 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010885
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010886 kfree(pipe_config);
10887
Jesse Barnes79e53942008-11-07 14:24:08 -080010888 return mode;
10889}
10890
Chris Wilsonf047e392012-07-21 12:31:41 +010010891void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010892{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010893 struct drm_i915_private *dev_priv = dev->dev_private;
10894
Chris Wilsonf62a0072014-02-21 17:55:39 +000010895 if (dev_priv->mm.busy)
10896 return;
10897
Paulo Zanoni43694d62014-03-07 20:08:08 -030010898 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010899 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010900 if (INTEL_INFO(dev)->gen >= 6)
10901 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010902 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010903}
10904
10905void intel_mark_idle(struct drm_device *dev)
10906{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010907 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010908
Chris Wilsonf62a0072014-02-21 17:55:39 +000010909 if (!dev_priv->mm.busy)
10910 return;
10911
10912 dev_priv->mm.busy = false;
10913
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010914 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010915 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010916
Paulo Zanoni43694d62014-03-07 20:08:08 -030010917 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010918}
10919
Jesse Barnes79e53942008-11-07 14:24:08 -080010920static void intel_crtc_destroy(struct drm_crtc *crtc)
10921{
10922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010923 struct drm_device *dev = crtc->dev;
10924 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010925
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010926 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010927 work = intel_crtc->unpin_work;
10928 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010929 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010930
10931 if (work) {
10932 cancel_work_sync(&work->work);
10933 kfree(work);
10934 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010935
10936 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010937
Jesse Barnes79e53942008-11-07 14:24:08 -080010938 kfree(intel_crtc);
10939}
10940
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010941static void intel_unpin_work_fn(struct work_struct *__work)
10942{
10943 struct intel_unpin_work *work =
10944 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010945 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10946 struct drm_device *dev = crtc->base.dev;
10947 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010948
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010949 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010950 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010951 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010952
John Harrisonf06cc1b2014-11-24 18:49:37 +000010953 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010954 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010955 mutex_unlock(&dev->struct_mutex);
10956
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010957 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010958 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010959 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010960
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010961 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10962 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010963
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010964 kfree(work);
10965}
10966
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010967static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010968 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010969{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10971 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010972 unsigned long flags;
10973
10974 /* Ignore early vblank irqs */
10975 if (intel_crtc == NULL)
10976 return;
10977
Daniel Vetterf3260382014-09-15 14:55:23 +020010978 /*
10979 * This is called both by irq handlers and the reset code (to complete
10980 * lost pageflips) so needs the full irqsave spinlocks.
10981 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010982 spin_lock_irqsave(&dev->event_lock, flags);
10983 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010984
10985 /* Ensure we don't miss a work->pending update ... */
10986 smp_rmb();
10987
10988 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010989 spin_unlock_irqrestore(&dev->event_lock, flags);
10990 return;
10991 }
10992
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010993 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010994
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010995 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010996}
10997
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010998void intel_finish_page_flip(struct drm_device *dev, int pipe)
10999{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011000 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011001 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11002
Mario Kleiner49b14a52010-12-09 07:00:07 +010011003 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011004}
11005
11006void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
11007{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011008 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011009 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
11010
Mario Kleiner49b14a52010-12-09 07:00:07 +010011011 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011012}
11013
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011014/* Is 'a' after or equal to 'b'? */
11015static bool g4x_flip_count_after_eq(u32 a, u32 b)
11016{
11017 return !((a - b) & 0x80000000);
11018}
11019
11020static bool page_flip_finished(struct intel_crtc *crtc)
11021{
11022 struct drm_device *dev = crtc->base.dev;
11023 struct drm_i915_private *dev_priv = dev->dev_private;
11024
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011025 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11026 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11027 return true;
11028
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011029 /*
11030 * The relevant registers doen't exist on pre-ctg.
11031 * As the flip done interrupt doesn't trigger for mmio
11032 * flips on gmch platforms, a flip count check isn't
11033 * really needed there. But since ctg has the registers,
11034 * include it in the check anyway.
11035 */
11036 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11037 return true;
11038
11039 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010011040 * BDW signals flip done immediately if the plane
11041 * is disabled, even if the plane enable is already
11042 * armed to occur at the next vblank :(
11043 */
11044
11045 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011046 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11047 * used the same base address. In that case the mmio flip might
11048 * have completed, but the CS hasn't even executed the flip yet.
11049 *
11050 * A flip count check isn't enough as the CS might have updated
11051 * the base address just after start of vblank, but before we
11052 * managed to process the interrupt. This means we'd complete the
11053 * CS flip too soon.
11054 *
11055 * Combining both checks should get us a good enough result. It may
11056 * still happen that the CS flip has been executed, but has not
11057 * yet actually completed. But in case the base address is the same
11058 * anyway, we don't really care.
11059 */
11060 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11061 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011062 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011063 crtc->unpin_work->flip_count);
11064}
11065
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011066void intel_prepare_page_flip(struct drm_device *dev, int plane)
11067{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011068 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011069 struct intel_crtc *intel_crtc =
11070 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11071 unsigned long flags;
11072
Daniel Vetterf3260382014-09-15 14:55:23 +020011073
11074 /*
11075 * This is called both by irq handlers and the reset code (to complete
11076 * lost pageflips) so needs the full irqsave spinlocks.
11077 *
11078 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011079 * generate a page-flip completion irq, i.e. every modeset
11080 * is also accompanied by a spurious intel_prepare_page_flip().
11081 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011082 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011083 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011084 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011085 spin_unlock_irqrestore(&dev->event_lock, flags);
11086}
11087
Chris Wilson60426392015-10-10 10:44:32 +010011088static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011089{
11090 /* Ensure that the work item is consistent when activating it ... */
11091 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011092 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011093 /* and that it is marked active as soon as the irq could fire. */
11094 smp_wmb();
11095}
11096
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011097static int intel_gen2_queue_flip(struct drm_device *dev,
11098 struct drm_crtc *crtc,
11099 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011100 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011101 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011102 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011103{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011104 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011106 u32 flip_mask;
11107 int ret;
11108
John Harrison5fb9de12015-05-29 17:44:07 +010011109 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011110 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011111 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011112
11113 /* Can't queue multiple flips, so wait for the previous
11114 * one to finish before executing the next.
11115 */
11116 if (intel_crtc->plane)
11117 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11118 else
11119 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011120 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11121 intel_ring_emit(engine, MI_NOOP);
11122 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011123 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011124 intel_ring_emit(engine, fb->pitches[0]);
11125 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11126 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011127
Chris Wilson60426392015-10-10 10:44:32 +010011128 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011129 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011130}
11131
11132static int intel_gen3_queue_flip(struct drm_device *dev,
11133 struct drm_crtc *crtc,
11134 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011135 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011136 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011137 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011138{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011139 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011141 u32 flip_mask;
11142 int ret;
11143
John Harrison5fb9de12015-05-29 17:44:07 +010011144 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011146 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011147
11148 if (intel_crtc->plane)
11149 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11150 else
11151 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011152 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11153 intel_ring_emit(engine, MI_NOOP);
11154 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011155 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011156 intel_ring_emit(engine, fb->pitches[0]);
11157 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11158 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011159
Chris Wilson60426392015-10-10 10:44:32 +010011160 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011161 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011162}
11163
11164static int intel_gen4_queue_flip(struct drm_device *dev,
11165 struct drm_crtc *crtc,
11166 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011167 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011168 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011169 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011170{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011171 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011172 struct drm_i915_private *dev_priv = dev->dev_private;
11173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11174 uint32_t pf, pipesrc;
11175 int ret;
11176
John Harrison5fb9de12015-05-29 17:44:07 +010011177 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011178 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011179 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011180
11181 /* i965+ uses the linear or tiled offsets from the
11182 * Display Registers (which do not change across a page-flip)
11183 * so we need only reprogram the base address.
11184 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011185 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011186 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011187 intel_ring_emit(engine, fb->pitches[0]);
11188 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011189 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011190
11191 /* XXX Enabling the panel-fitter across page-flip is so far
11192 * untested on non-native modes, so ignore it for now.
11193 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11194 */
11195 pf = 0;
11196 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011197 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011198
Chris Wilson60426392015-10-10 10:44:32 +010011199 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011200 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011201}
11202
11203static int intel_gen6_queue_flip(struct drm_device *dev,
11204 struct drm_crtc *crtc,
11205 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011206 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011207 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011208 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011209{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011210 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011211 struct drm_i915_private *dev_priv = dev->dev_private;
11212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11213 uint32_t pf, pipesrc;
11214 int ret;
11215
John Harrison5fb9de12015-05-29 17:44:07 +010011216 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011217 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011218 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011219
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011220 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011221 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011222 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11223 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011224
Chris Wilson99d9acd2012-04-17 20:37:00 +010011225 /* Contrary to the suggestions in the documentation,
11226 * "Enable Panel Fitter" does not seem to be required when page
11227 * flipping with a non-native mode, and worse causes a normal
11228 * modeset to fail.
11229 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11230 */
11231 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011232 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011233 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011234
Chris Wilson60426392015-10-10 10:44:32 +010011235 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011236 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011237}
11238
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011239static int intel_gen7_queue_flip(struct drm_device *dev,
11240 struct drm_crtc *crtc,
11241 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011242 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011243 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011244 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011245{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011246 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011248 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011249 int len, ret;
11250
Robin Schroereba905b2014-05-18 02:24:50 +020011251 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011252 case PLANE_A:
11253 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11254 break;
11255 case PLANE_B:
11256 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11257 break;
11258 case PLANE_C:
11259 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11260 break;
11261 default:
11262 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011263 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011264 }
11265
Chris Wilsonffe74d72013-08-26 20:58:12 +010011266 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011267 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011268 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011269 /*
11270 * On Gen 8, SRM is now taking an extra dword to accommodate
11271 * 48bits addresses, and we need a NOOP for the batch size to
11272 * stay even.
11273 */
11274 if (IS_GEN8(dev))
11275 len += 2;
11276 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011277
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011278 /*
11279 * BSpec MI_DISPLAY_FLIP for IVB:
11280 * "The full packet must be contained within the same cache line."
11281 *
11282 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11283 * cacheline, if we ever start emitting more commands before
11284 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11285 * then do the cacheline alignment, and finally emit the
11286 * MI_DISPLAY_FLIP.
11287 */
John Harrisonbba09b12015-05-29 17:44:06 +010011288 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011289 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011290 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011291
John Harrison5fb9de12015-05-29 17:44:07 +010011292 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011293 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011294 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011295
Chris Wilsonffe74d72013-08-26 20:58:12 +010011296 /* Unmask the flip-done completion message. Note that the bspec says that
11297 * we should do this for both the BCS and RCS, and that we must not unmask
11298 * more than one flip event at any time (or ensure that one flip message
11299 * can be sent by waiting for flip-done prior to queueing new flips).
11300 * Experimentation says that BCS works despite DERRMR masking all
11301 * flip-done completion events and that unmasking all planes at once
11302 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11303 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11304 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011305 if (engine->id == RCS) {
11306 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11307 intel_ring_emit_reg(engine, DERRMR);
11308 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11309 DERRMR_PIPEB_PRI_FLIP_DONE |
11310 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011311 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011312 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011313 MI_SRM_LRM_GLOBAL_GTT);
11314 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011315 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011316 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011317 intel_ring_emit_reg(engine, DERRMR);
11318 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011319 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011320 intel_ring_emit(engine, 0);
11321 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011322 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011323 }
11324
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011325 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11326 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11327 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11328 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011329
Chris Wilson60426392015-10-10 10:44:32 +010011330 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011331 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011332}
11333
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011334static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011335 struct drm_i915_gem_object *obj)
11336{
11337 /*
11338 * This is not being used for older platforms, because
11339 * non-availability of flip done interrupt forces us to use
11340 * CS flips. Older platforms derive flip done using some clever
11341 * tricks involving the flip_pending status bits and vblank irqs.
11342 * So using MMIO flips there would disrupt this mechanism.
11343 */
11344
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011345 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011346 return true;
11347
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011348 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011349 return false;
11350
11351 if (i915.use_mmio_flip < 0)
11352 return false;
11353 else if (i915.use_mmio_flip > 0)
11354 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011355 else if (i915.enable_execlists)
11356 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011357 else if (obj->base.dma_buf &&
11358 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11359 false))
11360 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011361 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011362 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011363}
11364
Chris Wilson60426392015-10-10 10:44:32 +010011365static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011366 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011367 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011368{
11369 struct drm_device *dev = intel_crtc->base.dev;
11370 struct drm_i915_private *dev_priv = dev->dev_private;
11371 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011372 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011373 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011374
11375 ctl = I915_READ(PLANE_CTL(pipe, 0));
11376 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011377 switch (fb->modifier[0]) {
11378 case DRM_FORMAT_MOD_NONE:
11379 break;
11380 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011381 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011382 break;
11383 case I915_FORMAT_MOD_Y_TILED:
11384 ctl |= PLANE_CTL_TILED_Y;
11385 break;
11386 case I915_FORMAT_MOD_Yf_TILED:
11387 ctl |= PLANE_CTL_TILED_YF;
11388 break;
11389 default:
11390 MISSING_CASE(fb->modifier[0]);
11391 }
Damien Lespiauff944562014-11-20 14:58:16 +000011392
11393 /*
11394 * The stride is either expressed as a multiple of 64 bytes chunks for
11395 * linear buffers or in number of tiles for tiled buffers.
11396 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011397 if (intel_rotation_90_or_270(rotation)) {
11398 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011399 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011400 stride = DIV_ROUND_UP(fb->height, tile_height);
11401 } else {
11402 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011403 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11404 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011405 }
Damien Lespiauff944562014-11-20 14:58:16 +000011406
11407 /*
11408 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11409 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11410 */
11411 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11412 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11413
Chris Wilson60426392015-10-10 10:44:32 +010011414 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011415 POSTING_READ(PLANE_SURF(pipe, 0));
11416}
11417
Chris Wilson60426392015-10-10 10:44:32 +010011418static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11419 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011420{
11421 struct drm_device *dev = intel_crtc->base.dev;
11422 struct drm_i915_private *dev_priv = dev->dev_private;
11423 struct intel_framebuffer *intel_fb =
11424 to_intel_framebuffer(intel_crtc->base.primary->fb);
11425 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011426 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011427 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011428
Sourab Gupta84c33a62014-06-02 16:47:17 +053011429 dspcntr = I915_READ(reg);
11430
Damien Lespiauc5d97472014-10-25 00:11:11 +010011431 if (obj->tiling_mode != I915_TILING_NONE)
11432 dspcntr |= DISPPLANE_TILED;
11433 else
11434 dspcntr &= ~DISPPLANE_TILED;
11435
Sourab Gupta84c33a62014-06-02 16:47:17 +053011436 I915_WRITE(reg, dspcntr);
11437
Chris Wilson60426392015-10-10 10:44:32 +010011438 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011439 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011440}
11441
11442/*
11443 * XXX: This is the temporary way to update the plane registers until we get
11444 * around to using the usual plane update functions for MMIO flips
11445 */
Chris Wilson60426392015-10-10 10:44:32 +010011446static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011447{
Chris Wilson60426392015-10-10 10:44:32 +010011448 struct intel_crtc *crtc = mmio_flip->crtc;
11449 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011450
Chris Wilson60426392015-10-10 10:44:32 +010011451 spin_lock_irq(&crtc->base.dev->event_lock);
11452 work = crtc->unpin_work;
11453 spin_unlock_irq(&crtc->base.dev->event_lock);
11454 if (work == NULL)
11455 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011456
Chris Wilson60426392015-10-10 10:44:32 +010011457 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011458
Chris Wilson60426392015-10-10 10:44:32 +010011459 intel_pipe_update_start(crtc);
11460
11461 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011462 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011463 else
11464 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011465 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011466
Chris Wilson60426392015-10-10 10:44:32 +010011467 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011468}
11469
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011470static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011471{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011472 struct intel_mmio_flip *mmio_flip =
11473 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011474 struct intel_framebuffer *intel_fb =
11475 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11476 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011477
Chris Wilson60426392015-10-10 10:44:32 +010011478 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011479 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011480 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011481 false, NULL,
11482 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011483 i915_gem_request_unreference__unlocked(mmio_flip->req);
11484 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011485
Alex Goinsfd8e0582015-11-25 18:43:38 -080011486 /* For framebuffer backed by dmabuf, wait for fence */
11487 if (obj->base.dma_buf)
11488 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11489 false, false,
11490 MAX_SCHEDULE_TIMEOUT) < 0);
11491
Chris Wilson60426392015-10-10 10:44:32 +010011492 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011493 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011494}
11495
11496static int intel_queue_mmio_flip(struct drm_device *dev,
11497 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011498 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011499{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011500 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011501
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011502 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11503 if (mmio_flip == NULL)
11504 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011505
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011506 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011507 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011508 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011509 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011510
11511 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11512 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011513
Sourab Gupta84c33a62014-06-02 16:47:17 +053011514 return 0;
11515}
11516
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011517static int intel_default_queue_flip(struct drm_device *dev,
11518 struct drm_crtc *crtc,
11519 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011520 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011521 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011522 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011523{
11524 return -ENODEV;
11525}
11526
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011527static bool __intel_pageflip_stall_check(struct drm_device *dev,
11528 struct drm_crtc *crtc)
11529{
11530 struct drm_i915_private *dev_priv = dev->dev_private;
11531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11532 struct intel_unpin_work *work = intel_crtc->unpin_work;
11533 u32 addr;
11534
11535 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11536 return true;
11537
Chris Wilson908565c2015-08-12 13:08:22 +010011538 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11539 return false;
11540
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011541 if (!work->enable_stall_check)
11542 return false;
11543
11544 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011545 if (work->flip_queued_req &&
11546 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011547 return false;
11548
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011549 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011550 }
11551
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011552 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011553 return false;
11554
11555 /* Potential stall - if we see that the flip has happened,
11556 * assume a missed interrupt. */
11557 if (INTEL_INFO(dev)->gen >= 4)
11558 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11559 else
11560 addr = I915_READ(DSPADDR(intel_crtc->plane));
11561
11562 /* There is a potential issue here with a false positive after a flip
11563 * to the same address. We could address this by checking for a
11564 * non-incrementing frame counter.
11565 */
11566 return addr == work->gtt_offset;
11567}
11568
11569void intel_check_page_flip(struct drm_device *dev, int pipe)
11570{
11571 struct drm_i915_private *dev_priv = dev->dev_private;
11572 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011574 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011575
Dave Gordon6c51d462015-03-06 15:34:26 +000011576 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011577
11578 if (crtc == NULL)
11579 return;
11580
Daniel Vetterf3260382014-09-15 14:55:23 +020011581 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011582 work = intel_crtc->unpin_work;
11583 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011584 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011585 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011586 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011587 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011588 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011589 if (work != NULL &&
11590 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11591 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011592 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011593}
11594
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011595static int intel_crtc_page_flip(struct drm_crtc *crtc,
11596 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011597 struct drm_pending_vblank_event *event,
11598 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011599{
11600 struct drm_device *dev = crtc->dev;
11601 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011602 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011603 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011605 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011606 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011607 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011608 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011609 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011610 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011611 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011612
Matt Roper2ff8fde2014-07-08 07:50:07 -070011613 /*
11614 * drm_mode_page_flip_ioctl() should already catch this, but double
11615 * check to be safe. In the future we may enable pageflipping from
11616 * a disabled primary plane.
11617 */
11618 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11619 return -EBUSY;
11620
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011621 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011622 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011623 return -EINVAL;
11624
11625 /*
11626 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11627 * Note that pitch changes could also affect these register.
11628 */
11629 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011630 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11631 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011632 return -EINVAL;
11633
Chris Wilsonf900db42014-02-20 09:26:13 +000011634 if (i915_terminally_wedged(&dev_priv->gpu_error))
11635 goto out_hang;
11636
Daniel Vetterb14c5672013-09-19 12:18:32 +020011637 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011638 if (work == NULL)
11639 return -ENOMEM;
11640
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011641 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011642 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011643 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011644 INIT_WORK(&work->work, intel_unpin_work_fn);
11645
Daniel Vetter87b6b102014-05-15 15:33:46 +020011646 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011647 if (ret)
11648 goto free_work;
11649
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011650 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011651 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011652 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011653 /* Before declaring the flip queue wedged, check if
11654 * the hardware completed the operation behind our backs.
11655 */
11656 if (__intel_pageflip_stall_check(dev, crtc)) {
11657 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11658 page_flip_completed(intel_crtc);
11659 } else {
11660 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011661 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011662
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011663 drm_crtc_vblank_put(crtc);
11664 kfree(work);
11665 return -EBUSY;
11666 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011667 }
11668 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011669 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011670
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011671 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11672 flush_workqueue(dev_priv->wq);
11673
Jesse Barnes75dfca82010-02-10 15:09:44 -080011674 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011675 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011676 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011677
Matt Roperf4510a22014-04-01 15:22:40 -070011678 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011679 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011680 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011681
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011682 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011683
Chris Wilson89ed88b2015-02-16 14:31:49 +000011684 ret = i915_mutex_lock_interruptible(dev);
11685 if (ret)
11686 goto cleanup;
11687
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011688 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011689 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011690
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011691 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011692 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011693
Wayne Boyer666a4532015-12-09 12:29:35 -080011694 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011695 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011696 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011697 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011698 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011699 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011700 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011701 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011702 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011703 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011704 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011705 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011706 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011707 }
11708
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011709 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011710
11711 /* When using CS flips, we want to emit semaphores between rings.
11712 * However, when using mmio flips we will create a task to do the
11713 * synchronisation, so all we want here is to pin the framebuffer
11714 * into the display plane and skip any waits.
11715 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011716 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011717 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011718 if (ret)
11719 goto cleanup_pending;
11720 }
11721
Ville Syrjälä3465c582016-02-15 22:54:43 +020011722 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011723 if (ret)
11724 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011725
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011726 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11727 obj, 0);
11728 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011729
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011730 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011731 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011732 if (ret)
11733 goto cleanup_unpin;
11734
John Harrisonf06cc1b2014-11-24 18:49:37 +000011735 i915_gem_request_assign(&work->flip_queued_req,
11736 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011737 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011738 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011739 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011740 if (IS_ERR(request)) {
11741 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011742 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011743 }
John Harrison6258fbe2015-05-29 17:43:48 +010011744 }
11745
11746 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011747 page_flip_flags);
11748 if (ret)
11749 goto cleanup_unpin;
11750
John Harrison6258fbe2015-05-29 17:43:48 +010011751 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011752 }
11753
John Harrison91af1272015-06-18 13:14:56 +010011754 if (request)
John Harrison75289872015-05-29 17:43:49 +010011755 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011756
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011757 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011758 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011759
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011760 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011761 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011762 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011763
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011764 intel_frontbuffer_flip_prepare(dev,
11765 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011766
Jesse Barnese5510fa2010-07-01 16:48:37 -070011767 trace_i915_flip_request(intel_crtc->plane, obj);
11768
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011769 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011770
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011771cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011772 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011773cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011774 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011775 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011776 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011777 mutex_unlock(&dev->struct_mutex);
11778cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011779 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011780 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011781
Chris Wilson89ed88b2015-02-16 14:31:49 +000011782 drm_gem_object_unreference_unlocked(&obj->base);
11783 drm_framebuffer_unreference(work->old_fb);
11784
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011785 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011786 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011787 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011788
Daniel Vetter87b6b102014-05-15 15:33:46 +020011789 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011790free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011791 kfree(work);
11792
Chris Wilsonf900db42014-02-20 09:26:13 +000011793 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011794 struct drm_atomic_state *state;
11795 struct drm_plane_state *plane_state;
11796
Chris Wilsonf900db42014-02-20 09:26:13 +000011797out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011798 state = drm_atomic_state_alloc(dev);
11799 if (!state)
11800 return -ENOMEM;
11801 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11802
11803retry:
11804 plane_state = drm_atomic_get_plane_state(state, primary);
11805 ret = PTR_ERR_OR_ZERO(plane_state);
11806 if (!ret) {
11807 drm_atomic_set_fb_for_plane(plane_state, fb);
11808
11809 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11810 if (!ret)
11811 ret = drm_atomic_commit(state);
11812 }
11813
11814 if (ret == -EDEADLK) {
11815 drm_modeset_backoff(state->acquire_ctx);
11816 drm_atomic_state_clear(state);
11817 goto retry;
11818 }
11819
11820 if (ret)
11821 drm_atomic_state_free(state);
11822
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011823 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011824 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011825 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011826 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011827 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011828 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011829 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011830}
11831
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011832
11833/**
11834 * intel_wm_need_update - Check whether watermarks need updating
11835 * @plane: drm plane
11836 * @state: new plane state
11837 *
11838 * Check current plane state versus the new one to determine whether
11839 * watermarks need to be recalculated.
11840 *
11841 * Returns true or false.
11842 */
11843static bool intel_wm_need_update(struct drm_plane *plane,
11844 struct drm_plane_state *state)
11845{
Matt Roperd21fbe82015-09-24 15:53:12 -070011846 struct intel_plane_state *new = to_intel_plane_state(state);
11847 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11848
11849 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011850 if (new->visible != cur->visible)
11851 return true;
11852
11853 if (!cur->base.fb || !new->base.fb)
11854 return false;
11855
11856 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11857 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011858 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11859 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11860 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11861 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011862 return true;
11863
11864 return false;
11865}
11866
Matt Roperd21fbe82015-09-24 15:53:12 -070011867static bool needs_scaling(struct intel_plane_state *state)
11868{
11869 int src_w = drm_rect_width(&state->src) >> 16;
11870 int src_h = drm_rect_height(&state->src) >> 16;
11871 int dst_w = drm_rect_width(&state->dst);
11872 int dst_h = drm_rect_height(&state->dst);
11873
11874 return (src_w != dst_w || src_h != dst_h);
11875}
11876
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011877int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11878 struct drm_plane_state *plane_state)
11879{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011880 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011881 struct drm_crtc *crtc = crtc_state->crtc;
11882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11883 struct drm_plane *plane = plane_state->plane;
11884 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011885 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011886 struct intel_plane_state *old_plane_state =
11887 to_intel_plane_state(plane->state);
11888 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011889 bool mode_changed = needs_modeset(crtc_state);
11890 bool was_crtc_enabled = crtc->state->active;
11891 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011892 bool turn_off, turn_on, visible, was_visible;
11893 struct drm_framebuffer *fb = plane_state->fb;
11894
11895 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11896 plane->type != DRM_PLANE_TYPE_CURSOR) {
11897 ret = skl_update_scaler_plane(
11898 to_intel_crtc_state(crtc_state),
11899 to_intel_plane_state(plane_state));
11900 if (ret)
11901 return ret;
11902 }
11903
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011904 was_visible = old_plane_state->visible;
11905 visible = to_intel_plane_state(plane_state)->visible;
11906
11907 if (!was_crtc_enabled && WARN_ON(was_visible))
11908 was_visible = false;
11909
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011910 /*
11911 * Visibility is calculated as if the crtc was on, but
11912 * after scaler setup everything depends on it being off
11913 * when the crtc isn't active.
11914 */
11915 if (!is_crtc_enabled)
11916 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011917
11918 if (!was_visible && !visible)
11919 return 0;
11920
Maarten Lankhorste8861672016-02-24 11:24:26 +010011921 if (fb != old_plane_state->base.fb)
11922 pipe_config->fb_changed = true;
11923
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011924 turn_off = was_visible && (!visible || mode_changed);
11925 turn_on = visible && (!was_visible || mode_changed);
11926
11927 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11928 plane->base.id, fb ? fb->base.id : -1);
11929
11930 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11931 plane->base.id, was_visible, visible,
11932 turn_off, turn_on, mode_changed);
11933
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011934 if (turn_on) {
11935 pipe_config->update_wm_pre = true;
11936
11937 /* must disable cxsr around plane enable/disable */
11938 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11939 pipe_config->disable_cxsr = true;
11940 } else if (turn_off) {
11941 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011942
Ville Syrjälä852eb002015-06-24 22:00:07 +030011943 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011944 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011945 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011946 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011947 /* FIXME bollocks */
11948 pipe_config->update_wm_pre = true;
11949 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011950 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011951
Matt Ropered4a6a72016-02-23 17:20:13 -080011952 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011953 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11954 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011955 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11956
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011957 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011958 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011959
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011960 /*
11961 * WaCxSRDisabledForSpriteScaling:ivb
11962 *
11963 * cstate->update_wm was already set above, so this flag will
11964 * take effect when we commit and program watermarks.
11965 */
11966 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11967 needs_scaling(to_intel_plane_state(plane_state)) &&
11968 !needs_scaling(old_plane_state))
11969 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011970
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011971 return 0;
11972}
11973
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011974static bool encoders_cloneable(const struct intel_encoder *a,
11975 const struct intel_encoder *b)
11976{
11977 /* masks could be asymmetric, so check both ways */
11978 return a == b || (a->cloneable & (1 << b->type) &&
11979 b->cloneable & (1 << a->type));
11980}
11981
11982static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11983 struct intel_crtc *crtc,
11984 struct intel_encoder *encoder)
11985{
11986 struct intel_encoder *source_encoder;
11987 struct drm_connector *connector;
11988 struct drm_connector_state *connector_state;
11989 int i;
11990
11991 for_each_connector_in_state(state, connector, connector_state, i) {
11992 if (connector_state->crtc != &crtc->base)
11993 continue;
11994
11995 source_encoder =
11996 to_intel_encoder(connector_state->best_encoder);
11997 if (!encoders_cloneable(encoder, source_encoder))
11998 return false;
11999 }
12000
12001 return true;
12002}
12003
12004static bool check_encoder_cloning(struct drm_atomic_state *state,
12005 struct intel_crtc *crtc)
12006{
12007 struct intel_encoder *encoder;
12008 struct drm_connector *connector;
12009 struct drm_connector_state *connector_state;
12010 int i;
12011
12012 for_each_connector_in_state(state, connector, connector_state, i) {
12013 if (connector_state->crtc != &crtc->base)
12014 continue;
12015
12016 encoder = to_intel_encoder(connector_state->best_encoder);
12017 if (!check_single_encoder_cloning(state, crtc, encoder))
12018 return false;
12019 }
12020
12021 return true;
12022}
12023
12024static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12025 struct drm_crtc_state *crtc_state)
12026{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012027 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012028 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012030 struct intel_crtc_state *pipe_config =
12031 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012032 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012033 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012034 bool mode_changed = needs_modeset(crtc_state);
12035
12036 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12037 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12038 return -EINVAL;
12039 }
12040
Ville Syrjälä852eb002015-06-24 22:00:07 +030012041 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012042 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012043
Maarten Lankhorstad421372015-06-15 12:33:42 +020012044 if (mode_changed && crtc_state->enable &&
12045 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012046 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012047 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12048 pipe_config);
12049 if (ret)
12050 return ret;
12051 }
12052
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012053 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012054 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012055 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012056 if (ret) {
12057 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012058 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012059 }
12060 }
12061
12062 if (dev_priv->display.compute_intermediate_wm &&
12063 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12064 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12065 return 0;
12066
12067 /*
12068 * Calculate 'intermediate' watermarks that satisfy both the
12069 * old state and the new state. We can program these
12070 * immediately.
12071 */
12072 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12073 intel_crtc,
12074 pipe_config);
12075 if (ret) {
12076 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12077 return ret;
12078 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012079 }
12080
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012081 if (INTEL_INFO(dev)->gen >= 9) {
12082 if (mode_changed)
12083 ret = skl_update_scaler_crtc(pipe_config);
12084
12085 if (!ret)
12086 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12087 pipe_config);
12088 }
12089
12090 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012091}
12092
Jani Nikula65b38e02015-04-13 11:26:56 +030012093static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012094 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12095 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080012096 .atomic_begin = intel_begin_crtc_commit,
12097 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012098 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012099};
12100
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012101static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12102{
12103 struct intel_connector *connector;
12104
12105 for_each_intel_connector(dev, connector) {
12106 if (connector->base.encoder) {
12107 connector->base.state->best_encoder =
12108 connector->base.encoder;
12109 connector->base.state->crtc =
12110 connector->base.encoder->crtc;
12111 } else {
12112 connector->base.state->best_encoder = NULL;
12113 connector->base.state->crtc = NULL;
12114 }
12115 }
12116}
12117
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012118static void
Robin Schroereba905b2014-05-18 02:24:50 +020012119connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012120 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012121{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012122 int bpp = pipe_config->pipe_bpp;
12123
12124 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12125 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012126 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012127
12128 /* Don't use an invalid EDID bpc value */
12129 if (connector->base.display_info.bpc &&
12130 connector->base.display_info.bpc * 3 < bpp) {
12131 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12132 bpp, connector->base.display_info.bpc*3);
12133 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12134 }
12135
Jani Nikula013dd9e2016-01-13 16:35:20 +020012136 /* Clamp bpp to default limit on screens without EDID 1.4 */
12137 if (connector->base.display_info.bpc == 0) {
12138 int type = connector->base.connector_type;
12139 int clamp_bpp = 24;
12140
12141 /* Fall back to 18 bpp when DP sink capability is unknown. */
12142 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12143 type == DRM_MODE_CONNECTOR_eDP)
12144 clamp_bpp = 18;
12145
12146 if (bpp > clamp_bpp) {
12147 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12148 bpp, clamp_bpp);
12149 pipe_config->pipe_bpp = clamp_bpp;
12150 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012151 }
12152}
12153
12154static int
12155compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012156 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012157{
12158 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012159 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012160 struct drm_connector *connector;
12161 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012162 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012163
Wayne Boyer666a4532015-12-09 12:29:35 -080012164 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012165 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012166 else if (INTEL_INFO(dev)->gen >= 5)
12167 bpp = 12*3;
12168 else
12169 bpp = 8*3;
12170
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012171
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012172 pipe_config->pipe_bpp = bpp;
12173
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012174 state = pipe_config->base.state;
12175
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012176 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012177 for_each_connector_in_state(state, connector, connector_state, i) {
12178 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012179 continue;
12180
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012181 connected_sink_compute_bpp(to_intel_connector(connector),
12182 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012183 }
12184
12185 return bpp;
12186}
12187
Daniel Vetter644db712013-09-19 14:53:58 +020012188static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12189{
12190 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12191 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012192 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012193 mode->crtc_hdisplay, mode->crtc_hsync_start,
12194 mode->crtc_hsync_end, mode->crtc_htotal,
12195 mode->crtc_vdisplay, mode->crtc_vsync_start,
12196 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12197}
12198
Daniel Vetterc0b03412013-05-28 12:05:54 +020012199static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012200 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012201 const char *context)
12202{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012203 struct drm_device *dev = crtc->base.dev;
12204 struct drm_plane *plane;
12205 struct intel_plane *intel_plane;
12206 struct intel_plane_state *state;
12207 struct drm_framebuffer *fb;
12208
12209 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12210 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012211
Jani Nikulada205632016-03-15 21:51:10 +020012212 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012213 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12214 pipe_config->pipe_bpp, pipe_config->dither);
12215 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12216 pipe_config->has_pch_encoder,
12217 pipe_config->fdi_lanes,
12218 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12219 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12220 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012221 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 +030012222 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012223 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012224 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12225 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12226 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012227
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012228 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 -070012229 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012230 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012231 pipe_config->dp_m2_n2.gmch_m,
12232 pipe_config->dp_m2_n2.gmch_n,
12233 pipe_config->dp_m2_n2.link_m,
12234 pipe_config->dp_m2_n2.link_n,
12235 pipe_config->dp_m2_n2.tu);
12236
Daniel Vetter55072d12014-11-20 16:10:28 +010012237 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12238 pipe_config->has_audio,
12239 pipe_config->has_infoframe);
12240
Daniel Vetterc0b03412013-05-28 12:05:54 +020012241 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012242 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012243 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012244 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12245 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012246 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012247 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12248 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012249 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12250 crtc->num_scalers,
12251 pipe_config->scaler_state.scaler_users,
12252 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012253 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12254 pipe_config->gmch_pfit.control,
12255 pipe_config->gmch_pfit.pgm_ratios,
12256 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012257 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012258 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012259 pipe_config->pch_pfit.size,
12260 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012261 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012262 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012263
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012264 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012265 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012266 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012267 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012268 pipe_config->ddi_pll_sel,
12269 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012270 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012271 pipe_config->dpll_hw_state.pll0,
12272 pipe_config->dpll_hw_state.pll1,
12273 pipe_config->dpll_hw_state.pll2,
12274 pipe_config->dpll_hw_state.pll3,
12275 pipe_config->dpll_hw_state.pll6,
12276 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012277 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012278 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012279 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012280 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012281 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12282 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12283 pipe_config->ddi_pll_sel,
12284 pipe_config->dpll_hw_state.ctrl1,
12285 pipe_config->dpll_hw_state.cfgcr1,
12286 pipe_config->dpll_hw_state.cfgcr2);
12287 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012288 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 +010012289 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012290 pipe_config->dpll_hw_state.wrpll,
12291 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012292 } else {
12293 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12294 "fp0: 0x%x, fp1: 0x%x\n",
12295 pipe_config->dpll_hw_state.dpll,
12296 pipe_config->dpll_hw_state.dpll_md,
12297 pipe_config->dpll_hw_state.fp0,
12298 pipe_config->dpll_hw_state.fp1);
12299 }
12300
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012301 DRM_DEBUG_KMS("planes on this crtc\n");
12302 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12303 intel_plane = to_intel_plane(plane);
12304 if (intel_plane->pipe != crtc->pipe)
12305 continue;
12306
12307 state = to_intel_plane_state(plane->state);
12308 fb = state->base.fb;
12309 if (!fb) {
12310 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12311 "disabled, scaler_id = %d\n",
12312 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12313 plane->base.id, intel_plane->pipe,
12314 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12315 drm_plane_index(plane), state->scaler_id);
12316 continue;
12317 }
12318
12319 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12320 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12321 plane->base.id, intel_plane->pipe,
12322 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12323 drm_plane_index(plane));
12324 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12325 fb->base.id, fb->width, fb->height, fb->pixel_format);
12326 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12327 state->scaler_id,
12328 state->src.x1 >> 16, state->src.y1 >> 16,
12329 drm_rect_width(&state->src) >> 16,
12330 drm_rect_height(&state->src) >> 16,
12331 state->dst.x1, state->dst.y1,
12332 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12333 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012334}
12335
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012336static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012337{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012338 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012339 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012340 unsigned int used_ports = 0;
12341
12342 /*
12343 * Walk the connector list instead of the encoder
12344 * list to detect the problem on ddi platforms
12345 * where there's just one encoder per digital port.
12346 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012347 drm_for_each_connector(connector, dev) {
12348 struct drm_connector_state *connector_state;
12349 struct intel_encoder *encoder;
12350
12351 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12352 if (!connector_state)
12353 connector_state = connector->state;
12354
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012355 if (!connector_state->best_encoder)
12356 continue;
12357
12358 encoder = to_intel_encoder(connector_state->best_encoder);
12359
12360 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012361
12362 switch (encoder->type) {
12363 unsigned int port_mask;
12364 case INTEL_OUTPUT_UNKNOWN:
12365 if (WARN_ON(!HAS_DDI(dev)))
12366 break;
12367 case INTEL_OUTPUT_DISPLAYPORT:
12368 case INTEL_OUTPUT_HDMI:
12369 case INTEL_OUTPUT_EDP:
12370 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12371
12372 /* the same port mustn't appear more than once */
12373 if (used_ports & port_mask)
12374 return false;
12375
12376 used_ports |= port_mask;
12377 default:
12378 break;
12379 }
12380 }
12381
12382 return true;
12383}
12384
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012385static void
12386clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12387{
12388 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012389 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012390 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012391 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012392 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012393 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012394
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012395 /* FIXME: before the switch to atomic started, a new pipe_config was
12396 * kzalloc'd. Code that depends on any field being zero should be
12397 * fixed, so that the crtc_state can be safely duplicated. For now,
12398 * only fields that are know to not cause problems are preserved. */
12399
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012400 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012401 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012402 shared_dpll = crtc_state->shared_dpll;
12403 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012404 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012405 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012406
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012407 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012408
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012409 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012410 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012411 crtc_state->shared_dpll = shared_dpll;
12412 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012413 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012414 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012415}
12416
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012417static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012418intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012419 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012420{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012421 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012422 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012423 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012424 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012425 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012426 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012427 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012428
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012429 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012430
Daniel Vettere143a212013-07-04 12:01:15 +020012431 pipe_config->cpu_transcoder =
12432 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012433
Imre Deak2960bc92013-07-30 13:36:32 +030012434 /*
12435 * Sanitize sync polarity flags based on requested ones. If neither
12436 * positive or negative polarity is requested, treat this as meaning
12437 * negative polarity.
12438 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012439 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012440 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012441 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012442
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012443 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012444 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012445 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012446
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012447 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12448 pipe_config);
12449 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012450 goto fail;
12451
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012452 /*
12453 * Determine the real pipe dimensions. Note that stereo modes can
12454 * increase the actual pipe size due to the frame doubling and
12455 * insertion of additional space for blanks between the frame. This
12456 * is stored in the crtc timings. We use the requested mode to do this
12457 * computation to clearly distinguish it from the adjusted mode, which
12458 * can be changed by the connectors in the below retry loop.
12459 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012460 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012461 &pipe_config->pipe_src_w,
12462 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012463
Daniel Vettere29c22c2013-02-21 00:00:16 +010012464encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012465 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012466 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012467 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012468
Daniel Vetter135c81b2013-07-21 21:37:09 +020012469 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012470 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12471 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012472
Daniel Vetter7758a112012-07-08 19:40:39 +020012473 /* Pass our mode to the connectors and the CRTC to give them a chance to
12474 * adjust it according to limitations or connector properties, and also
12475 * a chance to reject the mode entirely.
12476 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012477 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012478 if (connector_state->crtc != crtc)
12479 continue;
12480
12481 encoder = to_intel_encoder(connector_state->best_encoder);
12482
Daniel Vetterefea6e82013-07-21 21:36:59 +020012483 if (!(encoder->compute_config(encoder, pipe_config))) {
12484 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012485 goto fail;
12486 }
12487 }
12488
Daniel Vetterff9a6752013-06-01 17:16:21 +020012489 /* Set default port clock if not overwritten by the encoder. Needs to be
12490 * done afterwards in case the encoder adjusts the mode. */
12491 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012492 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012493 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012494
Daniel Vettera43f6e02013-06-07 23:10:32 +020012495 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012496 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012497 DRM_DEBUG_KMS("CRTC fixup failed\n");
12498 goto fail;
12499 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012500
12501 if (ret == RETRY) {
12502 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12503 ret = -EINVAL;
12504 goto fail;
12505 }
12506
12507 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12508 retry = false;
12509 goto encoder_retry;
12510 }
12511
Daniel Vettere8fa4272015-08-12 11:43:34 +020012512 /* Dithering seems to not pass-through bits correctly when it should, so
12513 * only enable it on 6bpc panels. */
12514 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012515 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012516 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012517
Daniel Vetter7758a112012-07-08 19:40:39 +020012518fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012519 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012520}
12521
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012522static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012523intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012524{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012525 struct drm_crtc *crtc;
12526 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012527 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012528
Ville Syrjälä76688512014-01-10 11:28:06 +020012529 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012530 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012531 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012532
12533 /* Update hwmode for vblank functions */
12534 if (crtc->state->active)
12535 crtc->hwmode = crtc->state->adjusted_mode;
12536 else
12537 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012538
12539 /*
12540 * Update legacy state to satisfy fbc code. This can
12541 * be removed when fbc uses the atomic state.
12542 */
12543 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12544 struct drm_plane_state *plane_state = crtc->primary->state;
12545
12546 crtc->primary->fb = plane_state->fb;
12547 crtc->x = plane_state->src_x >> 16;
12548 crtc->y = plane_state->src_y >> 16;
12549 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012550 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012551}
12552
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012553static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012554{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012555 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012556
12557 if (clock1 == clock2)
12558 return true;
12559
12560 if (!clock1 || !clock2)
12561 return false;
12562
12563 diff = abs(clock1 - clock2);
12564
12565 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12566 return true;
12567
12568 return false;
12569}
12570
Daniel Vetter25c5b262012-07-08 22:08:04 +020012571#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12572 list_for_each_entry((intel_crtc), \
12573 &(dev)->mode_config.crtc_list, \
12574 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012575 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012576
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012577static bool
12578intel_compare_m_n(unsigned int m, unsigned int n,
12579 unsigned int m2, unsigned int n2,
12580 bool exact)
12581{
12582 if (m == m2 && n == n2)
12583 return true;
12584
12585 if (exact || !m || !n || !m2 || !n2)
12586 return false;
12587
12588 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12589
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012590 if (n > n2) {
12591 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012592 m2 <<= 1;
12593 n2 <<= 1;
12594 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012595 } else if (n < n2) {
12596 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012597 m <<= 1;
12598 n <<= 1;
12599 }
12600 }
12601
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012602 if (n != n2)
12603 return false;
12604
12605 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012606}
12607
12608static bool
12609intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12610 struct intel_link_m_n *m2_n2,
12611 bool adjust)
12612{
12613 if (m_n->tu == m2_n2->tu &&
12614 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12615 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12616 intel_compare_m_n(m_n->link_m, m_n->link_n,
12617 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12618 if (adjust)
12619 *m2_n2 = *m_n;
12620
12621 return true;
12622 }
12623
12624 return false;
12625}
12626
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012627static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012628intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012629 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012630 struct intel_crtc_state *pipe_config,
12631 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012632{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012633 bool ret = true;
12634
12635#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12636 do { \
12637 if (!adjust) \
12638 DRM_ERROR(fmt, ##__VA_ARGS__); \
12639 else \
12640 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12641 } while (0)
12642
Daniel Vetter66e985c2013-06-05 13:34:20 +020012643#define PIPE_CONF_CHECK_X(name) \
12644 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012645 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012646 "(expected 0x%08x, found 0x%08x)\n", \
12647 current_config->name, \
12648 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012650 }
12651
Daniel Vetter08a24032013-04-19 11:25:34 +020012652#define PIPE_CONF_CHECK_I(name) \
12653 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012654 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012655 "(expected %i, found %i)\n", \
12656 current_config->name, \
12657 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012658 ret = false; \
12659 }
12660
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012661#define PIPE_CONF_CHECK_P(name) \
12662 if (current_config->name != pipe_config->name) { \
12663 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12664 "(expected %p, found %p)\n", \
12665 current_config->name, \
12666 pipe_config->name); \
12667 ret = false; \
12668 }
12669
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012670#define PIPE_CONF_CHECK_M_N(name) \
12671 if (!intel_compare_link_m_n(&current_config->name, \
12672 &pipe_config->name,\
12673 adjust)) { \
12674 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12675 "(expected tu %i gmch %i/%i link %i/%i, " \
12676 "found tu %i, gmch %i/%i link %i/%i)\n", \
12677 current_config->name.tu, \
12678 current_config->name.gmch_m, \
12679 current_config->name.gmch_n, \
12680 current_config->name.link_m, \
12681 current_config->name.link_n, \
12682 pipe_config->name.tu, \
12683 pipe_config->name.gmch_m, \
12684 pipe_config->name.gmch_n, \
12685 pipe_config->name.link_m, \
12686 pipe_config->name.link_n); \
12687 ret = false; \
12688 }
12689
12690#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12691 if (!intel_compare_link_m_n(&current_config->name, \
12692 &pipe_config->name, adjust) && \
12693 !intel_compare_link_m_n(&current_config->alt_name, \
12694 &pipe_config->name, adjust)) { \
12695 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12696 "(expected tu %i gmch %i/%i link %i/%i, " \
12697 "or tu %i gmch %i/%i link %i/%i, " \
12698 "found tu %i, gmch %i/%i link %i/%i)\n", \
12699 current_config->name.tu, \
12700 current_config->name.gmch_m, \
12701 current_config->name.gmch_n, \
12702 current_config->name.link_m, \
12703 current_config->name.link_n, \
12704 current_config->alt_name.tu, \
12705 current_config->alt_name.gmch_m, \
12706 current_config->alt_name.gmch_n, \
12707 current_config->alt_name.link_m, \
12708 current_config->alt_name.link_n, \
12709 pipe_config->name.tu, \
12710 pipe_config->name.gmch_m, \
12711 pipe_config->name.gmch_n, \
12712 pipe_config->name.link_m, \
12713 pipe_config->name.link_n); \
12714 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012715 }
12716
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012717/* This is required for BDW+ where there is only one set of registers for
12718 * switching between high and low RR.
12719 * This macro can be used whenever a comparison has to be made between one
12720 * hw state and multiple sw state variables.
12721 */
12722#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12723 if ((current_config->name != pipe_config->name) && \
12724 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012725 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012726 "(expected %i or %i, found %i)\n", \
12727 current_config->name, \
12728 current_config->alt_name, \
12729 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012730 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012731 }
12732
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012733#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12734 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012735 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012736 "(expected %i, found %i)\n", \
12737 current_config->name & (mask), \
12738 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012739 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012740 }
12741
Ville Syrjälä5e550652013-09-06 23:29:07 +030012742#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12743 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012744 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012745 "(expected %i, found %i)\n", \
12746 current_config->name, \
12747 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012748 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012749 }
12750
Daniel Vetterbb760062013-06-06 14:55:52 +020012751#define PIPE_CONF_QUIRK(quirk) \
12752 ((current_config->quirks | pipe_config->quirks) & (quirk))
12753
Daniel Vettereccb1402013-05-22 00:50:22 +020012754 PIPE_CONF_CHECK_I(cpu_transcoder);
12755
Daniel Vetter08a24032013-04-19 11:25:34 +020012756 PIPE_CONF_CHECK_I(has_pch_encoder);
12757 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012758 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012759
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012760 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012761 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012762
12763 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012764 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012765
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012766 if (current_config->has_drrs)
12767 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12768 } else
12769 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012770
Jani Nikulaa65347b2015-11-27 12:21:46 +020012771 PIPE_CONF_CHECK_I(has_dsi_encoder);
12772
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012773 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12774 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12775 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12776 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12777 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012779
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12783 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012786
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012787 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012788 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012789 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012790 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012791 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012792 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012793
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012794 PIPE_CONF_CHECK_I(has_audio);
12795
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012796 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012797 DRM_MODE_FLAG_INTERLACE);
12798
Daniel Vetterbb760062013-06-06 14:55:52 +020012799 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012800 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012801 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012802 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012803 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012804 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012805 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012806 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012807 DRM_MODE_FLAG_NVSYNC);
12808 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012809
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012810 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012811 /* pfit ratios are autocomputed by the hw on gen4+ */
12812 if (INTEL_INFO(dev)->gen < 4)
12813 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012814 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012815
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012816 if (!adjust) {
12817 PIPE_CONF_CHECK_I(pipe_src_w);
12818 PIPE_CONF_CHECK_I(pipe_src_h);
12819
12820 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12821 if (current_config->pch_pfit.enabled) {
12822 PIPE_CONF_CHECK_X(pch_pfit.pos);
12823 PIPE_CONF_CHECK_X(pch_pfit.size);
12824 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012825
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012826 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12827 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012828
Jesse Barnese59150d2014-01-07 13:30:45 -080012829 /* BDW+ don't expose a synchronous way to read the state */
12830 if (IS_HASWELL(dev))
12831 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012832
Ville Syrjälä282740f2013-09-04 18:30:03 +030012833 PIPE_CONF_CHECK_I(double_wide);
12834
Daniel Vetter26804af2014-06-25 22:01:55 +030012835 PIPE_CONF_CHECK_X(ddi_pll_sel);
12836
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012837 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012838 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012839 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012840 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12841 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012842 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012843 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012844 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12845 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12846 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012847
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012848 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12849 PIPE_CONF_CHECK_I(pipe_bpp);
12850
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012851 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012852 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012853
Daniel Vetter66e985c2013-06-05 13:34:20 +020012854#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012855#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012856#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012857#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012858#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012859#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012860#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012861#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012862
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012863 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012864}
12865
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012866static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12867 const struct intel_crtc_state *pipe_config)
12868{
12869 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012870 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012871 &pipe_config->fdi_m_n);
12872 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12873
12874 /*
12875 * FDI already provided one idea for the dotclock.
12876 * Yell if the encoder disagrees.
12877 */
12878 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12879 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12880 fdi_dotclock, dotclock);
12881 }
12882}
12883
Damien Lespiau08db6652014-11-04 17:06:52 +000012884static void check_wm_state(struct drm_device *dev)
12885{
12886 struct drm_i915_private *dev_priv = dev->dev_private;
12887 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12888 struct intel_crtc *intel_crtc;
12889 int plane;
12890
12891 if (INTEL_INFO(dev)->gen < 9)
12892 return;
12893
12894 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12895 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12896
12897 for_each_intel_crtc(dev, intel_crtc) {
12898 struct skl_ddb_entry *hw_entry, *sw_entry;
12899 const enum pipe pipe = intel_crtc->pipe;
12900
12901 if (!intel_crtc->active)
12902 continue;
12903
12904 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012905 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012906 hw_entry = &hw_ddb.plane[pipe][plane];
12907 sw_entry = &sw_ddb->plane[pipe][plane];
12908
12909 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12910 continue;
12911
12912 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12913 "(expected (%u,%u), found (%u,%u))\n",
12914 pipe_name(pipe), plane + 1,
12915 sw_entry->start, sw_entry->end,
12916 hw_entry->start, hw_entry->end);
12917 }
12918
12919 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012920 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12921 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012922
12923 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12924 continue;
12925
12926 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12927 "(expected (%u,%u), found (%u,%u))\n",
12928 pipe_name(pipe),
12929 sw_entry->start, sw_entry->end,
12930 hw_entry->start, hw_entry->end);
12931 }
12932}
12933
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012934static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012935check_connector_state(struct drm_device *dev,
12936 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012937{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012938 struct drm_connector_state *old_conn_state;
12939 struct drm_connector *connector;
12940 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012941
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012942 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12943 struct drm_encoder *encoder = connector->encoder;
12944 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012945
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012946 /* This also checks the encoder/connector hw state with the
12947 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012948 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012949
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012950 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012951 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012952 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012953}
12954
12955static void
12956check_encoder_state(struct drm_device *dev)
12957{
12958 struct intel_encoder *encoder;
12959 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012960
Damien Lespiaub2784e12014-08-05 11:29:37 +010012961 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012962 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012963 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012964
12965 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12966 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012967 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012968
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012969 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012970 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012971 continue;
12972 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012973
12974 I915_STATE_WARN(connector->base.state->crtc !=
12975 encoder->base.crtc,
12976 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012977 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012978
Rob Clarke2c719b2014-12-15 13:56:32 -050012979 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012980 "encoder's enabled state mismatch "
12981 "(expected %i, found %i)\n",
12982 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012983
12984 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012985 bool active;
12986
12987 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012988 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012989 "encoder detached but still enabled on pipe %c.\n",
12990 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012991 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012992 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012993}
12994
12995static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012996check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012997{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012998 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012999 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013000 struct drm_crtc_state *old_crtc_state;
13001 struct drm_crtc *crtc;
13002 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013003
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013004 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
13005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13006 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020013007 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013008
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013009 if (!needs_modeset(crtc->state) &&
13010 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013011 continue;
13012
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013013 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13014 pipe_config = to_intel_crtc_state(old_crtc_state);
13015 memset(pipe_config, 0, sizeof(*pipe_config));
13016 pipe_config->base.crtc = crtc;
13017 pipe_config->base.state = old_state;
13018
13019 DRM_DEBUG_KMS("[CRTC:%d]\n",
13020 crtc->base.id);
13021
13022 active = dev_priv->display.get_pipe_config(intel_crtc,
13023 pipe_config);
13024
13025 /* hw state is inconsistent with the pipe quirk */
13026 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13027 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13028 active = crtc->state->active;
13029
13030 I915_STATE_WARN(crtc->state->active != active,
13031 "crtc active state doesn't match with hw state "
13032 "(expected %i, found %i)\n", crtc->state->active, active);
13033
13034 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
13035 "transitional active state does not match atomic hw state "
13036 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13037
13038 for_each_encoder_on_crtc(dev, crtc, encoder) {
13039 enum pipe pipe;
13040
13041 active = encoder->get_hw_state(encoder, &pipe);
13042 I915_STATE_WARN(active != crtc->state->active,
13043 "[ENCODER:%i] active %i with crtc active %i\n",
13044 encoder->base.base.id, active, crtc->state->active);
13045
13046 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13047 "Encoder connected to wrong pipe %c\n",
13048 pipe_name(pipe));
13049
13050 if (active)
13051 encoder->get_config(encoder, pipe_config);
13052 }
13053
13054 if (!crtc->state->active)
13055 continue;
13056
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013057 intel_pipe_config_sanity_check(dev_priv, pipe_config);
13058
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013059 sw_config = to_intel_crtc_state(crtc->state);
13060 if (!intel_pipe_config_compare(dev, sw_config,
13061 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050013062 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013063 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013064 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013065 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013066 "[sw state]");
13067 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013068 }
13069}
13070
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013071static void
13072check_shared_dpll_state(struct drm_device *dev)
13073{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013074 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013075 struct intel_crtc *crtc;
13076 struct intel_dpll_hw_state dpll_hw_state;
13077 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013078
13079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013080 struct intel_shared_dpll *pll =
13081 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010013082 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020013083 bool active;
13084
13085 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13086
13087 DRM_DEBUG_KMS("%s\n", pll->name);
13088
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020013089 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020013090
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010013091 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13092 "more active pll users than references: %x vs %x\n",
13093 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020013094
13095 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010013096 I915_STATE_WARN(!pll->on && pll->active_mask,
13097 "pll in active use but not on in sw tracking\n");
13098 I915_STATE_WARN(pll->on && !pll->active_mask,
13099 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020013100 I915_STATE_WARN(pll->on != active,
13101 "pll on state mismatch (expected %i, found %i)\n",
13102 pll->on, active);
13103 }
Daniel Vetter53589012013-06-05 13:34:16 +020013104
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013105 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013106 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010013107 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
13108 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
13109 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020013110 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010013111
13112 I915_STATE_WARN(pll->active_mask != active_crtcs,
13113 "pll active crtcs mismatch (expected %x, found %x)\n",
13114 pll->active_mask, active_crtcs);
13115 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
13116 "pll enabled crtcs mismatch (expected %x, found %x)\n",
13117 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013118
Rob Clarke2c719b2014-12-15 13:56:32 -050013119 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020013120 sizeof(dpll_hw_state)),
13121 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020013122 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013123}
13124
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013125static void
13126intel_modeset_check_state(struct drm_device *dev,
13127 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013128{
Damien Lespiau08db6652014-11-04 17:06:52 +000013129 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013130 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013131 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013132 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013133 check_shared_dpll_state(dev);
13134}
13135
Ville Syrjälä80715b22014-05-15 20:23:23 +030013136static void update_scanline_offset(struct intel_crtc *crtc)
13137{
13138 struct drm_device *dev = crtc->base.dev;
13139
13140 /*
13141 * The scanline counter increments at the leading edge of hsync.
13142 *
13143 * On most platforms it starts counting from vtotal-1 on the
13144 * first active line. That means the scanline counter value is
13145 * always one less than what we would expect. Ie. just after
13146 * start of vblank, which also occurs at start of hsync (on the
13147 * last active line), the scanline counter will read vblank_start-1.
13148 *
13149 * On gen2 the scanline counter starts counting from 1 instead
13150 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13151 * to keep the value positive), instead of adding one.
13152 *
13153 * On HSW+ the behaviour of the scanline counter depends on the output
13154 * type. For DP ports it behaves like most other platforms, but on HDMI
13155 * there's an extra 1 line difference. So we need to add two instead of
13156 * one to the value.
13157 */
13158 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013159 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013160 int vtotal;
13161
Ville Syrjälä124abe02015-09-08 13:40:45 +030013162 vtotal = adjusted_mode->crtc_vtotal;
13163 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013164 vtotal /= 2;
13165
13166 crtc->scanline_offset = vtotal - 1;
13167 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013168 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013169 crtc->scanline_offset = 2;
13170 } else
13171 crtc->scanline_offset = 1;
13172}
13173
Maarten Lankhorstad421372015-06-15 12:33:42 +020013174static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013175{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013176 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013177 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013178 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013179 struct drm_crtc *crtc;
13180 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013181 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013182
13183 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013184 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013185
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013186 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013188 struct intel_shared_dpll *old_dpll =
13189 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013190
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013191 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013192 continue;
13193
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013194 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013195
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013196 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013197 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013198
Maarten Lankhorstad421372015-06-15 12:33:42 +020013199 if (!shared_dpll)
13200 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13201
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013202 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013203 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013204}
13205
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013206/*
13207 * This implements the workaround described in the "notes" section of the mode
13208 * set sequence documentation. When going from no pipes or single pipe to
13209 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13210 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13211 */
13212static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13213{
13214 struct drm_crtc_state *crtc_state;
13215 struct intel_crtc *intel_crtc;
13216 struct drm_crtc *crtc;
13217 struct intel_crtc_state *first_crtc_state = NULL;
13218 struct intel_crtc_state *other_crtc_state = NULL;
13219 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13220 int i;
13221
13222 /* look at all crtc's that are going to be enabled in during modeset */
13223 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13224 intel_crtc = to_intel_crtc(crtc);
13225
13226 if (!crtc_state->active || !needs_modeset(crtc_state))
13227 continue;
13228
13229 if (first_crtc_state) {
13230 other_crtc_state = to_intel_crtc_state(crtc_state);
13231 break;
13232 } else {
13233 first_crtc_state = to_intel_crtc_state(crtc_state);
13234 first_pipe = intel_crtc->pipe;
13235 }
13236 }
13237
13238 /* No workaround needed? */
13239 if (!first_crtc_state)
13240 return 0;
13241
13242 /* w/a possibly needed, check how many crtc's are already enabled. */
13243 for_each_intel_crtc(state->dev, intel_crtc) {
13244 struct intel_crtc_state *pipe_config;
13245
13246 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13247 if (IS_ERR(pipe_config))
13248 return PTR_ERR(pipe_config);
13249
13250 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13251
13252 if (!pipe_config->base.active ||
13253 needs_modeset(&pipe_config->base))
13254 continue;
13255
13256 /* 2 or more enabled crtcs means no need for w/a */
13257 if (enabled_pipe != INVALID_PIPE)
13258 return 0;
13259
13260 enabled_pipe = intel_crtc->pipe;
13261 }
13262
13263 if (enabled_pipe != INVALID_PIPE)
13264 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13265 else if (other_crtc_state)
13266 other_crtc_state->hsw_workaround_pipe = first_pipe;
13267
13268 return 0;
13269}
13270
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013271static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13272{
13273 struct drm_crtc *crtc;
13274 struct drm_crtc_state *crtc_state;
13275 int ret = 0;
13276
13277 /* add all active pipes to the state */
13278 for_each_crtc(state->dev, crtc) {
13279 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13280 if (IS_ERR(crtc_state))
13281 return PTR_ERR(crtc_state);
13282
13283 if (!crtc_state->active || needs_modeset(crtc_state))
13284 continue;
13285
13286 crtc_state->mode_changed = true;
13287
13288 ret = drm_atomic_add_affected_connectors(state, crtc);
13289 if (ret)
13290 break;
13291
13292 ret = drm_atomic_add_affected_planes(state, crtc);
13293 if (ret)
13294 break;
13295 }
13296
13297 return ret;
13298}
13299
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013300static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013301{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013302 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13303 struct drm_i915_private *dev_priv = state->dev->dev_private;
13304 struct drm_crtc *crtc;
13305 struct drm_crtc_state *crtc_state;
13306 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013307
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013308 if (!check_digital_port_conflicts(state)) {
13309 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13310 return -EINVAL;
13311 }
13312
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013313 intel_state->modeset = true;
13314 intel_state->active_crtcs = dev_priv->active_crtcs;
13315
13316 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13317 if (crtc_state->active)
13318 intel_state->active_crtcs |= 1 << i;
13319 else
13320 intel_state->active_crtcs &= ~(1 << i);
13321 }
13322
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013323 /*
13324 * See if the config requires any additional preparation, e.g.
13325 * to adjust global state with pipes off. We need to do this
13326 * here so we can get the modeset_pipe updated config for the new
13327 * mode set on this crtc. For other crtcs we need to use the
13328 * adjusted_mode bits in the crtc directly.
13329 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013330 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013331 ret = dev_priv->display.modeset_calc_cdclk(state);
13332
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013333 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013334 ret = intel_modeset_all_pipes(state);
13335
13336 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013337 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013338
13339 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13340 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013341 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013342 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013343
Maarten Lankhorstad421372015-06-15 12:33:42 +020013344 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013345
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013346 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013347 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013348
Maarten Lankhorstad421372015-06-15 12:33:42 +020013349 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013350}
13351
Matt Roperaa363132015-09-24 15:53:18 -070013352/*
13353 * Handle calculation of various watermark data at the end of the atomic check
13354 * phase. The code here should be run after the per-crtc and per-plane 'check'
13355 * handlers to ensure that all derived state has been updated.
13356 */
13357static void calc_watermark_data(struct drm_atomic_state *state)
13358{
13359 struct drm_device *dev = state->dev;
13360 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13361 struct drm_crtc *crtc;
13362 struct drm_crtc_state *cstate;
13363 struct drm_plane *plane;
13364 struct drm_plane_state *pstate;
13365
13366 /*
13367 * Calculate watermark configuration details now that derived
13368 * plane/crtc state is all properly updated.
13369 */
13370 drm_for_each_crtc(crtc, dev) {
13371 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13372 crtc->state;
13373
13374 if (cstate->active)
13375 intel_state->wm_config.num_pipes_active++;
13376 }
13377 drm_for_each_legacy_plane(plane, dev) {
13378 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13379 plane->state;
13380
13381 if (!to_intel_plane_state(pstate)->visible)
13382 continue;
13383
13384 intel_state->wm_config.sprites_enabled = true;
13385 if (pstate->crtc_w != pstate->src_w >> 16 ||
13386 pstate->crtc_h != pstate->src_h >> 16)
13387 intel_state->wm_config.sprites_scaled = true;
13388 }
13389}
13390
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013391/**
13392 * intel_atomic_check - validate state object
13393 * @dev: drm device
13394 * @state: state to validate
13395 */
13396static int intel_atomic_check(struct drm_device *dev,
13397 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013398{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013399 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013400 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013401 struct drm_crtc *crtc;
13402 struct drm_crtc_state *crtc_state;
13403 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013404 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013405
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013406 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013407 if (ret)
13408 return ret;
13409
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013410 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013411 struct intel_crtc_state *pipe_config =
13412 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013413
13414 /* Catch I915_MODE_FLAG_INHERITED */
13415 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13416 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013417
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013418 if (!crtc_state->enable) {
13419 if (needs_modeset(crtc_state))
13420 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013421 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013422 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013423
Daniel Vetter26495482015-07-15 14:15:52 +020013424 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013425 continue;
13426
Daniel Vetter26495482015-07-15 14:15:52 +020013427 /* FIXME: For only active_changed we shouldn't need to do any
13428 * state recomputation at all. */
13429
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013430 ret = drm_atomic_add_affected_connectors(state, crtc);
13431 if (ret)
13432 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013433
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013434 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013435 if (ret)
13436 return ret;
13437
Jani Nikula73831232015-11-19 10:26:30 +020013438 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013439 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013440 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013441 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013442 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013443 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013444 }
13445
13446 if (needs_modeset(crtc_state)) {
13447 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013448
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013449 ret = drm_atomic_add_affected_planes(state, crtc);
13450 if (ret)
13451 return ret;
13452 }
13453
Daniel Vetter26495482015-07-15 14:15:52 +020013454 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13455 needs_modeset(crtc_state) ?
13456 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013457 }
13458
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013459 if (any_ms) {
13460 ret = intel_modeset_checks(state);
13461
13462 if (ret)
13463 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013464 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013465 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013466
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013467 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013468 if (ret)
13469 return ret;
13470
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013471 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013472 calc_watermark_data(state);
13473
13474 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013475}
13476
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013477static int intel_atomic_prepare_commit(struct drm_device *dev,
13478 struct drm_atomic_state *state,
13479 bool async)
13480{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013481 struct drm_i915_private *dev_priv = dev->dev_private;
13482 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013483 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013484 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013485 struct drm_crtc *crtc;
13486 int i, ret;
13487
13488 if (async) {
13489 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13490 return -EINVAL;
13491 }
13492
13493 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13494 ret = intel_crtc_wait_for_pending_flips(crtc);
13495 if (ret)
13496 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013497
13498 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13499 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013500 }
13501
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013502 ret = mutex_lock_interruptible(&dev->struct_mutex);
13503 if (ret)
13504 return ret;
13505
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013506 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013507 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13508 u32 reset_counter;
13509
13510 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13511 mutex_unlock(&dev->struct_mutex);
13512
13513 for_each_plane_in_state(state, plane, plane_state, i) {
13514 struct intel_plane_state *intel_plane_state =
13515 to_intel_plane_state(plane_state);
13516
13517 if (!intel_plane_state->wait_req)
13518 continue;
13519
13520 ret = __i915_wait_request(intel_plane_state->wait_req,
13521 reset_counter, true,
13522 NULL, NULL);
13523
13524 /* Swallow -EIO errors to allow updates during hw lockup. */
13525 if (ret == -EIO)
13526 ret = 0;
13527
13528 if (ret)
13529 break;
13530 }
13531
13532 if (!ret)
13533 return 0;
13534
13535 mutex_lock(&dev->struct_mutex);
13536 drm_atomic_helper_cleanup_planes(dev, state);
13537 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013538
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013539 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013540 return ret;
13541}
13542
Maarten Lankhorste8861672016-02-24 11:24:26 +010013543static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13544 struct drm_i915_private *dev_priv,
13545 unsigned crtc_mask)
13546{
13547 unsigned last_vblank_count[I915_MAX_PIPES];
13548 enum pipe pipe;
13549 int ret;
13550
13551 if (!crtc_mask)
13552 return;
13553
13554 for_each_pipe(dev_priv, pipe) {
13555 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13556
13557 if (!((1 << pipe) & crtc_mask))
13558 continue;
13559
13560 ret = drm_crtc_vblank_get(crtc);
13561 if (WARN_ON(ret != 0)) {
13562 crtc_mask &= ~(1 << pipe);
13563 continue;
13564 }
13565
13566 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13567 }
13568
13569 for_each_pipe(dev_priv, pipe) {
13570 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13571 long lret;
13572
13573 if (!((1 << pipe) & crtc_mask))
13574 continue;
13575
13576 lret = wait_event_timeout(dev->vblank[pipe].queue,
13577 last_vblank_count[pipe] !=
13578 drm_crtc_vblank_count(crtc),
13579 msecs_to_jiffies(50));
13580
13581 WARN_ON(!lret);
13582
13583 drm_crtc_vblank_put(crtc);
13584 }
13585}
13586
13587static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13588{
13589 /* fb updated, need to unpin old fb */
13590 if (crtc_state->fb_changed)
13591 return true;
13592
13593 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013594 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013595 return true;
13596
13597 /*
13598 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013599 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013600 * but added for clarity.
13601 */
13602 if (crtc_state->disable_cxsr)
13603 return true;
13604
13605 return false;
13606}
13607
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013608/**
13609 * intel_atomic_commit - commit validated state object
13610 * @dev: DRM device
13611 * @state: the top-level driver state object
13612 * @async: asynchronous commit
13613 *
13614 * This function commits a top-level state object that has been validated
13615 * with drm_atomic_helper_check().
13616 *
13617 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13618 * we can only handle plane-related operations and do not yet support
13619 * asynchronous commit.
13620 *
13621 * RETURNS
13622 * Zero for success or -errno.
13623 */
13624static int intel_atomic_commit(struct drm_device *dev,
13625 struct drm_atomic_state *state,
13626 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013627{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013628 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013629 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013630 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013631 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013632 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013633 int ret = 0, i;
13634 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013635 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013636 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013637
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013638 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013639 if (ret) {
13640 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013641 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013642 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013643
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013644 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013645 dev_priv->wm.config = intel_state->wm_config;
13646 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013647
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013648 if (intel_state->modeset) {
13649 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13650 sizeof(intel_state->min_pixclk));
13651 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013652 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013653
13654 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013655 }
13656
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013657 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13659
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013660 if (needs_modeset(crtc->state) ||
13661 to_intel_crtc_state(crtc->state)->update_pipe) {
13662 hw_check = true;
13663
13664 put_domains[to_intel_crtc(crtc)->pipe] =
13665 modeset_get_crtc_power_domains(crtc,
13666 to_intel_crtc_state(crtc->state));
13667 }
13668
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013669 if (!needs_modeset(crtc->state))
13670 continue;
13671
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013672 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013673
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013674 if (old_crtc_state->active) {
13675 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013676 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013677 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013678 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013679 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013680
13681 /*
13682 * Underruns don't always raise
13683 * interrupts, so check manually.
13684 */
13685 intel_check_cpu_fifo_underruns(dev_priv);
13686 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013687
13688 if (!crtc->state->active)
13689 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013690 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013691 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013692
Daniel Vetterea9d7582012-07-10 10:42:52 +020013693 /* Only after disabling all output pipelines that will be changed can we
13694 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013695 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013696
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013697 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013698 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013699
13700 if (dev_priv->display.modeset_commit_cdclk &&
13701 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13702 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013703 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013704
Daniel Vettera6778b32012-07-02 09:56:42 +020013705 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013706 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13708 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013709 struct intel_crtc_state *pipe_config =
13710 to_intel_crtc_state(crtc->state);
13711 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013712
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013713 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013714 update_scanline_offset(to_intel_crtc(crtc));
13715 dev_priv->display.crtc_enable(crtc);
13716 }
13717
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013718 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013719 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013720
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013721 if (crtc->state->active &&
13722 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013723 intel_fbc_enable(intel_crtc);
13724
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013725 if (crtc->state->active &&
13726 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013727 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013728
Maarten Lankhorste8861672016-02-24 11:24:26 +010013729 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13730 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013731 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013732
Daniel Vettera6778b32012-07-02 09:56:42 +020013733 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013734
Maarten Lankhorste8861672016-02-24 11:24:26 +010013735 if (!state->legacy_cursor_update)
13736 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013737
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013738 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010013739 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorste8861672016-02-24 11:24:26 +010013740
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013741 if (put_domains[i])
13742 modeset_put_power_domains(dev_priv, put_domains[i]);
13743 }
13744
13745 if (intel_state->modeset)
13746 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13747
Matt Ropered4a6a72016-02-23 17:20:13 -080013748 /*
13749 * Now that the vblank has passed, we can go ahead and program the
13750 * optimal watermarks on platforms that need two-step watermark
13751 * programming.
13752 *
13753 * TODO: Move this (and other cleanup) to an async worker eventually.
13754 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013755 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013756 intel_cstate = to_intel_crtc_state(crtc->state);
13757
13758 if (dev_priv->display.optimize_watermarks)
13759 dev_priv->display.optimize_watermarks(intel_cstate);
13760 }
13761
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013762 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013763 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013764 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013765
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013766 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013767 intel_modeset_check_state(dev, state);
13768
13769 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013770
Mika Kuoppala75714942015-12-16 09:26:48 +020013771 /* As one of the primary mmio accessors, KMS has a high likelihood
13772 * of triggering bugs in unclaimed access. After we finish
13773 * modesetting, see if an error has been flagged, and if so
13774 * enable debugging for the next modeset - and hope we catch
13775 * the culprit.
13776 *
13777 * XXX note that we assume display power is on at this point.
13778 * This might hold true now but we need to add pm helper to check
13779 * unclaimed only when the hardware is on, as atomic commits
13780 * can happen also when the device is completely off.
13781 */
13782 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13783
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013784 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013785}
13786
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013787void intel_crtc_restore_mode(struct drm_crtc *crtc)
13788{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013789 struct drm_device *dev = crtc->dev;
13790 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013791 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013792 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013793
13794 state = drm_atomic_state_alloc(dev);
13795 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013796 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013797 crtc->base.id);
13798 return;
13799 }
13800
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013801 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013802
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013803retry:
13804 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13805 ret = PTR_ERR_OR_ZERO(crtc_state);
13806 if (!ret) {
13807 if (!crtc_state->active)
13808 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013809
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013810 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013811 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013812 }
13813
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013814 if (ret == -EDEADLK) {
13815 drm_atomic_state_clear(state);
13816 drm_modeset_backoff(state->acquire_ctx);
13817 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013818 }
13819
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013820 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013821out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013822 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013823}
13824
Daniel Vetter25c5b262012-07-08 22:08:04 +020013825#undef for_each_intel_crtc_masked
13826
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013827static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013828 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013829 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013830 .destroy = intel_crtc_destroy,
13831 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013832 .atomic_duplicate_state = intel_crtc_duplicate_state,
13833 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013834};
13835
Matt Roper6beb8c232014-12-01 15:40:14 -080013836/**
13837 * intel_prepare_plane_fb - Prepare fb for usage on plane
13838 * @plane: drm plane to prepare for
13839 * @fb: framebuffer to prepare for presentation
13840 *
13841 * Prepares a framebuffer for usage on a display plane. Generally this
13842 * involves pinning the underlying object and updating the frontbuffer tracking
13843 * bits. Some older platforms need special physical address handling for
13844 * cursor planes.
13845 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013846 * Must be called with struct_mutex held.
13847 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013848 * Returns 0 on success, negative error code on failure.
13849 */
13850int
13851intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013852 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013853{
13854 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013855 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013856 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013857 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013858 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013859 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013860
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013861 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013862 return 0;
13863
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013864 if (old_obj) {
13865 struct drm_crtc_state *crtc_state =
13866 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13867
13868 /* Big Hammer, we also need to ensure that any pending
13869 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13870 * current scanout is retired before unpinning the old
13871 * framebuffer. Note that we rely on userspace rendering
13872 * into the buffer attached to the pipe they are waiting
13873 * on. If not, userspace generates a GPU hang with IPEHR
13874 * point to the MI_WAIT_FOR_EVENT.
13875 *
13876 * This should only fail upon a hung GPU, in which case we
13877 * can safely continue.
13878 */
13879 if (needs_modeset(crtc_state))
13880 ret = i915_gem_object_wait_rendering(old_obj, true);
13881
13882 /* Swallow -EIO errors to allow updates during hw lockup. */
13883 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013884 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013885 }
13886
Alex Goins3c28ff22015-11-25 18:43:39 -080013887 /* For framebuffer backed by dmabuf, wait for fence */
13888 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013889 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013890
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013891 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13892 false, true,
13893 MAX_SCHEDULE_TIMEOUT);
13894 if (lret == -ERESTARTSYS)
13895 return lret;
13896
13897 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013898 }
13899
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013900 if (!obj) {
13901 ret = 0;
13902 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013903 INTEL_INFO(dev)->cursor_needs_physical) {
13904 int align = IS_I830(dev) ? 16 * 1024 : 256;
13905 ret = i915_gem_object_attach_phys(obj, align);
13906 if (ret)
13907 DRM_DEBUG_KMS("failed to attach phys object\n");
13908 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013909 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013910 }
13911
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013912 if (ret == 0) {
13913 if (obj) {
13914 struct intel_plane_state *plane_state =
13915 to_intel_plane_state(new_state);
13916
13917 i915_gem_request_assign(&plane_state->wait_req,
13918 obj->last_write_req);
13919 }
13920
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013921 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013922 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013923
Matt Roper6beb8c232014-12-01 15:40:14 -080013924 return ret;
13925}
13926
Matt Roper38f3ce32014-12-02 07:45:25 -080013927/**
13928 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13929 * @plane: drm plane to clean up for
13930 * @fb: old framebuffer that was on plane
13931 *
13932 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013933 *
13934 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013935 */
13936void
13937intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013938 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013939{
13940 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013941 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013942 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013943 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13944 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013945
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013946 old_intel_state = to_intel_plane_state(old_state);
13947
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013948 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013949 return;
13950
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013951 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13952 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013953 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013954
13955 /* prepare_fb aborted? */
13956 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13957 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13958 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013959
13960 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013961}
13962
Chandra Konduru6156a452015-04-27 13:48:39 -070013963int
13964skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13965{
13966 int max_scale;
13967 struct drm_device *dev;
13968 struct drm_i915_private *dev_priv;
13969 int crtc_clock, cdclk;
13970
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013971 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013972 return DRM_PLANE_HELPER_NO_SCALING;
13973
13974 dev = intel_crtc->base.dev;
13975 dev_priv = dev->dev_private;
13976 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013977 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013978
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013979 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013980 return DRM_PLANE_HELPER_NO_SCALING;
13981
13982 /*
13983 * skl max scale is lower of:
13984 * close to 3 but not 3, -1 is for that purpose
13985 * or
13986 * cdclk/crtc_clock
13987 */
13988 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13989
13990 return max_scale;
13991}
13992
Matt Roper465c1202014-05-29 08:06:54 -070013993static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013994intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013995 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013996 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013997{
Matt Roper2b875c22014-12-01 15:40:13 -080013998 struct drm_crtc *crtc = state->base.crtc;
13999 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014000 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014001 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14002 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014003
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014004 if (INTEL_INFO(plane->dev)->gen >= 9) {
14005 /* use scaler when colorkey is not required */
14006 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14007 min_scale = 1;
14008 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14009 }
Sonika Jindald8106362015-04-10 14:37:28 +053014010 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014011 }
Sonika Jindald8106362015-04-10 14:37:28 +053014012
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014013 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14014 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014015 min_scale, max_scale,
14016 can_position, true,
14017 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014018}
14019
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014020static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14021 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014022{
14023 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080014024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014025 struct intel_crtc_state *old_intel_state =
14026 to_intel_crtc_state(old_crtc_state);
14027 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030014028
Matt Roperc34c9ee2014-12-23 10:41:50 -080014029 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020014030 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020014031
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014032 if (modeset)
14033 return;
14034
14035 if (to_intel_crtc_state(crtc->state)->update_pipe)
14036 intel_update_pipe_config(intel_crtc, old_intel_state);
14037 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020014038 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014039}
14040
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014041static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14042 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014043{
Matt Roper32b7eee2014-12-24 07:59:06 -080014044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014045
Maarten Lankhorst62852622015-09-23 16:29:38 +020014046 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014047}
14048
Matt Ropercf4c7c12014-12-04 10:27:42 -080014049/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014050 * intel_plane_destroy - destroy a plane
14051 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014052 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014053 * Common destruction function for all types of planes (primary, cursor,
14054 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014055 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014056void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014057{
14058 struct intel_plane *intel_plane = to_intel_plane(plane);
14059 drm_plane_cleanup(plane);
14060 kfree(intel_plane);
14061}
14062
Matt Roper65a3fea2015-01-21 16:35:42 -080014063const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014064 .update_plane = drm_atomic_helper_update_plane,
14065 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014066 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014067 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014068 .atomic_get_property = intel_plane_atomic_get_property,
14069 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014070 .atomic_duplicate_state = intel_plane_duplicate_state,
14071 .atomic_destroy_state = intel_plane_destroy_state,
14072
Matt Roper465c1202014-05-29 08:06:54 -070014073};
14074
14075static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14076 int pipe)
14077{
14078 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080014079 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070014080 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014081 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070014082
14083 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14084 if (primary == NULL)
14085 return NULL;
14086
Matt Roper8e7d6882015-01-21 16:35:41 -080014087 state = intel_create_plane_state(&primary->base);
14088 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014089 kfree(primary);
14090 return NULL;
14091 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014092 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014093
Matt Roper465c1202014-05-29 08:06:54 -070014094 primary->can_scale = false;
14095 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014096 if (INTEL_INFO(dev)->gen >= 9) {
14097 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014098 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014099 }
Matt Roper465c1202014-05-29 08:06:54 -070014100 primary->pipe = pipe;
14101 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014102 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014103 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014104 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14105 primary->plane = !pipe;
14106
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014107 if (INTEL_INFO(dev)->gen >= 9) {
14108 intel_primary_formats = skl_primary_formats;
14109 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014110
14111 primary->update_plane = skylake_update_primary_plane;
14112 primary->disable_plane = skylake_disable_primary_plane;
14113 } else if (HAS_PCH_SPLIT(dev)) {
14114 intel_primary_formats = i965_primary_formats;
14115 num_formats = ARRAY_SIZE(i965_primary_formats);
14116
14117 primary->update_plane = ironlake_update_primary_plane;
14118 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014119 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014120 intel_primary_formats = i965_primary_formats;
14121 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014122
14123 primary->update_plane = i9xx_update_primary_plane;
14124 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014125 } else {
14126 intel_primary_formats = i8xx_primary_formats;
14127 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014128
14129 primary->update_plane = i9xx_update_primary_plane;
14130 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014131 }
14132
14133 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014134 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014135 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014136 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014137
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014138 if (INTEL_INFO(dev)->gen >= 4)
14139 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014140
Matt Roperea2c67b2014-12-23 10:41:52 -080014141 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14142
Matt Roper465c1202014-05-29 08:06:54 -070014143 return &primary->base;
14144}
14145
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014146void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14147{
14148 if (!dev->mode_config.rotation_property) {
14149 unsigned long flags = BIT(DRM_ROTATE_0) |
14150 BIT(DRM_ROTATE_180);
14151
14152 if (INTEL_INFO(dev)->gen >= 9)
14153 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14154
14155 dev->mode_config.rotation_property =
14156 drm_mode_create_rotation_property(dev, flags);
14157 }
14158 if (dev->mode_config.rotation_property)
14159 drm_object_attach_property(&plane->base.base,
14160 dev->mode_config.rotation_property,
14161 plane->base.state->rotation);
14162}
14163
Matt Roper3d7d6512014-06-10 08:28:13 -070014164static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014165intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014166 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014167 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014168{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014169 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014170 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014171 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014172 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014173 unsigned stride;
14174 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014175
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014176 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14177 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014178 DRM_PLANE_HELPER_NO_SCALING,
14179 DRM_PLANE_HELPER_NO_SCALING,
14180 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014181 if (ret)
14182 return ret;
14183
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014184 /* if we want to turn off the cursor ignore width and height */
14185 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014186 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014187
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014188 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014189 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014190 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14191 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014192 return -EINVAL;
14193 }
14194
Matt Roperea2c67b2014-12-23 10:41:52 -080014195 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14196 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014197 DRM_DEBUG_KMS("buffer is too small\n");
14198 return -ENOMEM;
14199 }
14200
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014201 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014202 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014203 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014204 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014205
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014206 /*
14207 * There's something wrong with the cursor on CHV pipe C.
14208 * If it straddles the left edge of the screen then
14209 * moving it away from the edge or disabling it often
14210 * results in a pipe underrun, and often that can lead to
14211 * dead pipe (constant underrun reported, and it scans
14212 * out just a solid color). To recover from that, the
14213 * display power well must be turned off and on again.
14214 * Refuse the put the cursor into that compromised position.
14215 */
14216 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14217 state->visible && state->base.crtc_x < 0) {
14218 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14219 return -EINVAL;
14220 }
14221
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014222 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014223}
14224
Matt Roperf4a2cf22014-12-01 15:40:12 -080014225static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014226intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014227 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014228{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14230
14231 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014232 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014233}
14234
14235static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014236intel_update_cursor_plane(struct drm_plane *plane,
14237 const struct intel_crtc_state *crtc_state,
14238 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014239{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014240 struct drm_crtc *crtc = crtc_state->base.crtc;
14241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014242 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014243 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014244 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014245
Matt Roperf4a2cf22014-12-01 15:40:12 -080014246 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014247 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014248 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014249 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014250 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014251 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014252
Gustavo Padovana912f122014-12-01 15:40:10 -080014253 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014254 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014255}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014256
Matt Roper3d7d6512014-06-10 08:28:13 -070014257static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14258 int pipe)
14259{
14260 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014261 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014262
14263 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14264 if (cursor == NULL)
14265 return NULL;
14266
Matt Roper8e7d6882015-01-21 16:35:41 -080014267 state = intel_create_plane_state(&cursor->base);
14268 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014269 kfree(cursor);
14270 return NULL;
14271 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014272 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014273
Matt Roper3d7d6512014-06-10 08:28:13 -070014274 cursor->can_scale = false;
14275 cursor->max_downscale = 1;
14276 cursor->pipe = pipe;
14277 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014278 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014279 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014280 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014281 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014282
14283 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014284 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014285 intel_cursor_formats,
14286 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014287 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014288
14289 if (INTEL_INFO(dev)->gen >= 4) {
14290 if (!dev->mode_config.rotation_property)
14291 dev->mode_config.rotation_property =
14292 drm_mode_create_rotation_property(dev,
14293 BIT(DRM_ROTATE_0) |
14294 BIT(DRM_ROTATE_180));
14295 if (dev->mode_config.rotation_property)
14296 drm_object_attach_property(&cursor->base.base,
14297 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014298 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014299 }
14300
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014301 if (INTEL_INFO(dev)->gen >=9)
14302 state->scaler_id = -1;
14303
Matt Roperea2c67b2014-12-23 10:41:52 -080014304 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14305
Matt Roper3d7d6512014-06-10 08:28:13 -070014306 return &cursor->base;
14307}
14308
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014309static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14310 struct intel_crtc_state *crtc_state)
14311{
14312 int i;
14313 struct intel_scaler *intel_scaler;
14314 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14315
14316 for (i = 0; i < intel_crtc->num_scalers; i++) {
14317 intel_scaler = &scaler_state->scalers[i];
14318 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014319 intel_scaler->mode = PS_SCALER_MODE_DYN;
14320 }
14321
14322 scaler_state->scaler_id = -1;
14323}
14324
Hannes Ederb358d0a2008-12-18 21:18:47 +010014325static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014326{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014327 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014328 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014329 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014330 struct drm_plane *primary = NULL;
14331 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014332 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014333
Daniel Vetter955382f2013-09-19 14:05:45 +020014334 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014335 if (intel_crtc == NULL)
14336 return;
14337
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014338 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14339 if (!crtc_state)
14340 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014341 intel_crtc->config = crtc_state;
14342 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014343 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014344
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014345 /* initialize shared scalers */
14346 if (INTEL_INFO(dev)->gen >= 9) {
14347 if (pipe == PIPE_C)
14348 intel_crtc->num_scalers = 1;
14349 else
14350 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14351
14352 skl_init_scalers(dev, intel_crtc, crtc_state);
14353 }
14354
Matt Roper465c1202014-05-29 08:06:54 -070014355 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014356 if (!primary)
14357 goto fail;
14358
14359 cursor = intel_cursor_plane_create(dev, pipe);
14360 if (!cursor)
14361 goto fail;
14362
Matt Roper465c1202014-05-29 08:06:54 -070014363 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014364 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014365 if (ret)
14366 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014367
14368 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014369 for (i = 0; i < 256; i++) {
14370 intel_crtc->lut_r[i] = i;
14371 intel_crtc->lut_g[i] = i;
14372 intel_crtc->lut_b[i] = i;
14373 }
14374
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014375 /*
14376 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014377 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014378 */
Jesse Barnes80824002009-09-10 15:28:06 -070014379 intel_crtc->pipe = pipe;
14380 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014381 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014382 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014383 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014384 }
14385
Chris Wilson4b0e3332014-05-30 16:35:26 +030014386 intel_crtc->cursor_base = ~0;
14387 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014388 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014389
Ville Syrjälä852eb002015-06-24 22:00:07 +030014390 intel_crtc->wm.cxsr_allowed = true;
14391
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014392 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14393 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14394 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14395 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14396
Jesse Barnes79e53942008-11-07 14:24:08 -080014397 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014398
14399 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014400 return;
14401
14402fail:
14403 if (primary)
14404 drm_plane_cleanup(primary);
14405 if (cursor)
14406 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014407 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014408 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014409}
14410
Jesse Barnes752aa882013-10-31 18:55:49 +020014411enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14412{
14413 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014414 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014415
Rob Clark51fd3712013-11-19 12:10:12 -050014416 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014417
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014418 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014419 return INVALID_PIPE;
14420
14421 return to_intel_crtc(encoder->crtc)->pipe;
14422}
14423
Carl Worth08d7b3d2009-04-29 14:43:54 -070014424int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014425 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014426{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014427 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014428 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014429 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014430
Rob Clark7707e652014-07-17 23:30:04 -040014431 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014432
Rob Clark7707e652014-07-17 23:30:04 -040014433 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014434 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014435 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014436 }
14437
Rob Clark7707e652014-07-17 23:30:04 -040014438 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014439 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014440
Daniel Vetterc05422d2009-08-11 16:05:30 +020014441 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014442}
14443
Daniel Vetter66a92782012-07-12 20:08:18 +020014444static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014445{
Daniel Vetter66a92782012-07-12 20:08:18 +020014446 struct drm_device *dev = encoder->base.dev;
14447 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014448 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014449 int entry = 0;
14450
Damien Lespiaub2784e12014-08-05 11:29:37 +010014451 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014452 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014453 index_mask |= (1 << entry);
14454
Jesse Barnes79e53942008-11-07 14:24:08 -080014455 entry++;
14456 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014457
Jesse Barnes79e53942008-11-07 14:24:08 -080014458 return index_mask;
14459}
14460
Chris Wilson4d302442010-12-14 19:21:29 +000014461static bool has_edp_a(struct drm_device *dev)
14462{
14463 struct drm_i915_private *dev_priv = dev->dev_private;
14464
14465 if (!IS_MOBILE(dev))
14466 return false;
14467
14468 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14469 return false;
14470
Damien Lespiaue3589902014-02-07 19:12:50 +000014471 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014472 return false;
14473
14474 return true;
14475}
14476
Jesse Barnes84b4e042014-06-25 08:24:29 -070014477static bool intel_crt_present(struct drm_device *dev)
14478{
14479 struct drm_i915_private *dev_priv = dev->dev_private;
14480
Damien Lespiau884497e2013-12-03 13:56:23 +000014481 if (INTEL_INFO(dev)->gen >= 9)
14482 return false;
14483
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014484 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014485 return false;
14486
14487 if (IS_CHERRYVIEW(dev))
14488 return false;
14489
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014490 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14491 return false;
14492
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014493 /* DDI E can't be used if DDI A requires 4 lanes */
14494 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14495 return false;
14496
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014497 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014498 return false;
14499
14500 return true;
14501}
14502
Jesse Barnes79e53942008-11-07 14:24:08 -080014503static void intel_setup_outputs(struct drm_device *dev)
14504{
Eric Anholt725e30a2009-01-22 13:01:02 -080014505 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014506 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014507 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014508
Daniel Vetterc9093352013-06-06 22:22:47 +020014509 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014510
Jesse Barnes84b4e042014-06-25 08:24:29 -070014511 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014512 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014513
Vandana Kannanc776eb22014-08-19 12:05:01 +053014514 if (IS_BROXTON(dev)) {
14515 /*
14516 * FIXME: Broxton doesn't support port detection via the
14517 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14518 * detect the ports.
14519 */
14520 intel_ddi_init(dev, PORT_A);
14521 intel_ddi_init(dev, PORT_B);
14522 intel_ddi_init(dev, PORT_C);
14523 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014524 int found;
14525
Jesse Barnesde31fac2015-03-06 15:53:32 -080014526 /*
14527 * Haswell uses DDI functions to detect digital outputs.
14528 * On SKL pre-D0 the strap isn't connected, so we assume
14529 * it's there.
14530 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014531 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014532 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014533 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014534 intel_ddi_init(dev, PORT_A);
14535
14536 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14537 * register */
14538 found = I915_READ(SFUSE_STRAP);
14539
14540 if (found & SFUSE_STRAP_DDIB_DETECTED)
14541 intel_ddi_init(dev, PORT_B);
14542 if (found & SFUSE_STRAP_DDIC_DETECTED)
14543 intel_ddi_init(dev, PORT_C);
14544 if (found & SFUSE_STRAP_DDID_DETECTED)
14545 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014546 /*
14547 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14548 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014549 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014550 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14551 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14552 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14553 intel_ddi_init(dev, PORT_E);
14554
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014555 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014556 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014557 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014558
14559 if (has_edp_a(dev))
14560 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014561
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014562 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014563 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014564 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014565 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014566 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014567 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014568 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014569 }
14570
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014571 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014572 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014573
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014574 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014575 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014576
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014577 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014578 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014579
Daniel Vetter270b3042012-10-27 15:52:05 +020014580 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014581 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014582 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014583 /*
14584 * The DP_DETECTED bit is the latched state of the DDC
14585 * SDA pin at boot. However since eDP doesn't require DDC
14586 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14587 * eDP ports may have been muxed to an alternate function.
14588 * Thus we can't rely on the DP_DETECTED bit alone to detect
14589 * eDP ports. Consult the VBT as well as DP_DETECTED to
14590 * detect eDP ports.
14591 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014592 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014593 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014594 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14595 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014596 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014597 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014598
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014599 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014600 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014601 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14602 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014603 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014604 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014605
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014606 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014607 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014608 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14609 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14610 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14611 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014612 }
14613
Jani Nikula3cfca972013-08-27 15:12:26 +030014614 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014615 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014616 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014617
Paulo Zanonie2debe92013-02-18 19:00:27 -030014618 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014619 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014620 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014621 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014622 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014623 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014624 }
Ma Ling27185ae2009-08-24 13:50:23 +080014625
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014626 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014627 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014628 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014629
14630 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014631
Paulo Zanonie2debe92013-02-18 19:00:27 -030014632 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014633 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014634 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014635 }
Ma Ling27185ae2009-08-24 13:50:23 +080014636
Paulo Zanonie2debe92013-02-18 19:00:27 -030014637 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014638
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014639 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014640 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014641 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014642 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014643 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014644 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014645 }
Ma Ling27185ae2009-08-24 13:50:23 +080014646
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014647 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014648 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014649 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014650 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014651 intel_dvo_init(dev);
14652
Zhenyu Wang103a1962009-11-27 11:44:36 +080014653 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014654 intel_tv_init(dev);
14655
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014656 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014657
Damien Lespiaub2784e12014-08-05 11:29:37 +010014658 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014659 encoder->base.possible_crtcs = encoder->crtc_mask;
14660 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014661 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014662 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014663
Paulo Zanonidde86e22012-12-01 12:04:25 -020014664 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014665
14666 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014667}
14668
14669static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14670{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014671 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014672 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014673
Daniel Vetteref2d6332014-02-10 18:00:38 +010014674 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014675 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014676 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014677 drm_gem_object_unreference(&intel_fb->obj->base);
14678 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014679 kfree(intel_fb);
14680}
14681
14682static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014683 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014684 unsigned int *handle)
14685{
14686 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014687 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014688
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014689 if (obj->userptr.mm) {
14690 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14691 return -EINVAL;
14692 }
14693
Chris Wilson05394f32010-11-08 19:18:58 +000014694 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014695}
14696
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014697static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14698 struct drm_file *file,
14699 unsigned flags, unsigned color,
14700 struct drm_clip_rect *clips,
14701 unsigned num_clips)
14702{
14703 struct drm_device *dev = fb->dev;
14704 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14705 struct drm_i915_gem_object *obj = intel_fb->obj;
14706
14707 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014708 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014709 mutex_unlock(&dev->struct_mutex);
14710
14711 return 0;
14712}
14713
Jesse Barnes79e53942008-11-07 14:24:08 -080014714static const struct drm_framebuffer_funcs intel_fb_funcs = {
14715 .destroy = intel_user_framebuffer_destroy,
14716 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014717 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014718};
14719
Damien Lespiaub3218032015-02-27 11:15:18 +000014720static
14721u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14722 uint32_t pixel_format)
14723{
14724 u32 gen = INTEL_INFO(dev)->gen;
14725
14726 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014727 int cpp = drm_format_plane_cpp(pixel_format, 0);
14728
Damien Lespiaub3218032015-02-27 11:15:18 +000014729 /* "The stride in bytes must not exceed the of the size of 8K
14730 * pixels and 32K bytes."
14731 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014732 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014733 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014734 return 32*1024;
14735 } else if (gen >= 4) {
14736 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14737 return 16*1024;
14738 else
14739 return 32*1024;
14740 } else if (gen >= 3) {
14741 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14742 return 8*1024;
14743 else
14744 return 16*1024;
14745 } else {
14746 /* XXX DSPC is limited to 4k tiled */
14747 return 8*1024;
14748 }
14749}
14750
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014751static int intel_framebuffer_init(struct drm_device *dev,
14752 struct intel_framebuffer *intel_fb,
14753 struct drm_mode_fb_cmd2 *mode_cmd,
14754 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014755{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014756 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014757 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014758 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014759 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014760
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014761 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14762
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014763 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14764 /* Enforce that fb modifier and tiling mode match, but only for
14765 * X-tiled. This is needed for FBC. */
14766 if (!!(obj->tiling_mode == I915_TILING_X) !=
14767 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14768 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14769 return -EINVAL;
14770 }
14771 } else {
14772 if (obj->tiling_mode == I915_TILING_X)
14773 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14774 else if (obj->tiling_mode == I915_TILING_Y) {
14775 DRM_DEBUG("No Y tiling for legacy addfb\n");
14776 return -EINVAL;
14777 }
14778 }
14779
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014780 /* Passed in modifier sanity checking. */
14781 switch (mode_cmd->modifier[0]) {
14782 case I915_FORMAT_MOD_Y_TILED:
14783 case I915_FORMAT_MOD_Yf_TILED:
14784 if (INTEL_INFO(dev)->gen < 9) {
14785 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14786 mode_cmd->modifier[0]);
14787 return -EINVAL;
14788 }
14789 case DRM_FORMAT_MOD_NONE:
14790 case I915_FORMAT_MOD_X_TILED:
14791 break;
14792 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014793 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14794 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014795 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014796 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014797
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014798 stride_alignment = intel_fb_stride_alignment(dev_priv,
14799 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014800 mode_cmd->pixel_format);
14801 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14802 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14803 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014804 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014805 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014806
Damien Lespiaub3218032015-02-27 11:15:18 +000014807 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14808 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014809 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014810 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14811 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014812 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014813 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014814 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014815 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014816
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014817 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014818 mode_cmd->pitches[0] != obj->stride) {
14819 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14820 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014821 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014822 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014823
Ville Syrjälä57779d02012-10-31 17:50:14 +020014824 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014825 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014826 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014827 case DRM_FORMAT_RGB565:
14828 case DRM_FORMAT_XRGB8888:
14829 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014830 break;
14831 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014832 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014833 DRM_DEBUG("unsupported pixel format: %s\n",
14834 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014835 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014836 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014837 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014838 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014839 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14840 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014841 DRM_DEBUG("unsupported pixel format: %s\n",
14842 drm_get_format_name(mode_cmd->pixel_format));
14843 return -EINVAL;
14844 }
14845 break;
14846 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014847 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014848 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014849 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014850 DRM_DEBUG("unsupported pixel format: %s\n",
14851 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014852 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014853 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014854 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014855 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014856 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014857 DRM_DEBUG("unsupported pixel format: %s\n",
14858 drm_get_format_name(mode_cmd->pixel_format));
14859 return -EINVAL;
14860 }
14861 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014862 case DRM_FORMAT_YUYV:
14863 case DRM_FORMAT_UYVY:
14864 case DRM_FORMAT_YVYU:
14865 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014866 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014867 DRM_DEBUG("unsupported pixel format: %s\n",
14868 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014869 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014870 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014871 break;
14872 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014873 DRM_DEBUG("unsupported pixel format: %s\n",
14874 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014875 return -EINVAL;
14876 }
14877
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014878 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14879 if (mode_cmd->offsets[0] != 0)
14880 return -EINVAL;
14881
Damien Lespiauec2c9812015-01-20 12:51:45 +000014882 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014883 mode_cmd->pixel_format,
14884 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014885 /* FIXME drm helper for size checks (especially planar formats)? */
14886 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14887 return -EINVAL;
14888
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014889 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14890 intel_fb->obj = obj;
14891
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014892 intel_fill_fb_info(dev_priv, &intel_fb->base);
14893
Jesse Barnes79e53942008-11-07 14:24:08 -080014894 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14895 if (ret) {
14896 DRM_ERROR("framebuffer init failed %d\n", ret);
14897 return ret;
14898 }
14899
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014900 intel_fb->obj->framebuffer_references++;
14901
Jesse Barnes79e53942008-11-07 14:24:08 -080014902 return 0;
14903}
14904
Jesse Barnes79e53942008-11-07 14:24:08 -080014905static struct drm_framebuffer *
14906intel_user_framebuffer_create(struct drm_device *dev,
14907 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014908 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014909{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014910 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014911 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014912 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014913
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014914 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014915 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014916 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014917 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014918
Daniel Vetter92907cb2015-11-23 09:04:05 +010014919 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014920 if (IS_ERR(fb))
14921 drm_gem_object_unreference_unlocked(&obj->base);
14922
14923 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014924}
14925
Daniel Vetter06957262015-08-10 13:34:08 +020014926#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014927static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014928{
14929}
14930#endif
14931
Jesse Barnes79e53942008-11-07 14:24:08 -080014932static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014933 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014934 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014935 .atomic_check = intel_atomic_check,
14936 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014937 .atomic_state_alloc = intel_atomic_state_alloc,
14938 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014939};
14940
Imre Deak88212942016-03-16 13:38:53 +020014941/**
14942 * intel_init_display_hooks - initialize the display modesetting hooks
14943 * @dev_priv: device private
14944 */
14945void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014946{
Imre Deak88212942016-03-16 13:38:53 +020014947 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014948 dev_priv->display.find_dpll = g4x_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014949 else if (IS_CHERRYVIEW(dev_priv))
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014950 dev_priv->display.find_dpll = chv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014951 else if (IS_VALLEYVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014952 dev_priv->display.find_dpll = vlv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014953 else if (IS_PINEVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014954 dev_priv->display.find_dpll = pnv_find_best_dpll;
14955 else
14956 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14957
Imre Deak88212942016-03-16 13:38:53 +020014958 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014959 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014960 dev_priv->display.get_initial_plane_config =
14961 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014962 dev_priv->display.crtc_compute_clock =
14963 haswell_crtc_compute_clock;
14964 dev_priv->display.crtc_enable = haswell_crtc_enable;
14965 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014966 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014967 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014968 dev_priv->display.get_initial_plane_config =
14969 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014970 dev_priv->display.crtc_compute_clock =
14971 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014972 dev_priv->display.crtc_enable = haswell_crtc_enable;
14973 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014974 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014975 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014976 dev_priv->display.get_initial_plane_config =
14977 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014978 dev_priv->display.crtc_compute_clock =
14979 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014980 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14981 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014982 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014983 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014984 dev_priv->display.get_initial_plane_config =
14985 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014986 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014987 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14988 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014989 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014990 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014991 dev_priv->display.get_initial_plane_config =
14992 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014993 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014994 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14995 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014996 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014997
Jesse Barnese70236a2009-09-21 10:42:27 -070014998 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014999 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015000 dev_priv->display.get_display_clock_speed =
15001 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015002 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015003 dev_priv->display.get_display_clock_speed =
15004 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015005 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015006 dev_priv->display.get_display_clock_speed =
15007 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015008 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015009 dev_priv->display.get_display_clock_speed =
15010 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015011 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015012 dev_priv->display.get_display_clock_speed =
15013 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015014 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015015 dev_priv->display.get_display_clock_speed =
15016 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015017 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15018 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015019 dev_priv->display.get_display_clock_speed =
15020 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015021 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015022 dev_priv->display.get_display_clock_speed =
15023 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015024 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015025 dev_priv->display.get_display_clock_speed =
15026 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015027 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015028 dev_priv->display.get_display_clock_speed =
15029 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015030 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015031 dev_priv->display.get_display_clock_speed =
15032 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015033 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015034 dev_priv->display.get_display_clock_speed =
15035 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015036 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015037 dev_priv->display.get_display_clock_speed =
15038 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015039 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015040 dev_priv->display.get_display_clock_speed =
15041 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015042 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015043 dev_priv->display.get_display_clock_speed =
15044 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015045 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015046 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015047 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015048 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015049 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015050 dev_priv->display.get_display_clock_speed =
15051 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015052 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015053
Imre Deak88212942016-03-16 13:38:53 +020015054 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015055 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015056 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015057 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015058 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015059 /* FIXME: detect B0+ stepping and use auto training */
15060 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015061 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015062 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015063 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015064 dev_priv->display.modeset_commit_cdclk =
15065 broadwell_modeset_commit_cdclk;
15066 dev_priv->display.modeset_calc_cdclk =
15067 broadwell_modeset_calc_cdclk;
15068 }
Imre Deak88212942016-03-16 13:38:53 +020015069 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015070 dev_priv->display.modeset_commit_cdclk =
15071 valleyview_modeset_commit_cdclk;
15072 dev_priv->display.modeset_calc_cdclk =
15073 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015074 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015075 dev_priv->display.modeset_commit_cdclk =
15076 broxton_modeset_commit_cdclk;
15077 dev_priv->display.modeset_calc_cdclk =
15078 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015079 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015080
Imre Deak88212942016-03-16 13:38:53 +020015081 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015082 case 2:
15083 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15084 break;
15085
15086 case 3:
15087 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15088 break;
15089
15090 case 4:
15091 case 5:
15092 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15093 break;
15094
15095 case 6:
15096 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15097 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015098 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015099 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015100 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15101 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015102 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015103 /* Drop through - unsupported since execlist only. */
15104 default:
15105 /* Default just returns -ENODEV to indicate unsupported */
15106 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015107 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015108}
15109
Jesse Barnesb690e962010-07-19 13:53:12 -070015110/*
15111 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15112 * resume, or other times. This quirk makes sure that's the case for
15113 * affected systems.
15114 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015115static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015116{
15117 struct drm_i915_private *dev_priv = dev->dev_private;
15118
15119 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015120 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015121}
15122
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015123static void quirk_pipeb_force(struct drm_device *dev)
15124{
15125 struct drm_i915_private *dev_priv = dev->dev_private;
15126
15127 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15128 DRM_INFO("applying pipe b force quirk\n");
15129}
15130
Keith Packard435793d2011-07-12 14:56:22 -070015131/*
15132 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15133 */
15134static void quirk_ssc_force_disable(struct drm_device *dev)
15135{
15136 struct drm_i915_private *dev_priv = dev->dev_private;
15137 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015138 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015139}
15140
Carsten Emde4dca20e2012-03-15 15:56:26 +010015141/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015142 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15143 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015144 */
15145static void quirk_invert_brightness(struct drm_device *dev)
15146{
15147 struct drm_i915_private *dev_priv = dev->dev_private;
15148 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015149 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015150}
15151
Scot Doyle9c72cc62014-07-03 23:27:50 +000015152/* Some VBT's incorrectly indicate no backlight is present */
15153static void quirk_backlight_present(struct drm_device *dev)
15154{
15155 struct drm_i915_private *dev_priv = dev->dev_private;
15156 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15157 DRM_INFO("applying backlight present quirk\n");
15158}
15159
Jesse Barnesb690e962010-07-19 13:53:12 -070015160struct intel_quirk {
15161 int device;
15162 int subsystem_vendor;
15163 int subsystem_device;
15164 void (*hook)(struct drm_device *dev);
15165};
15166
Egbert Eich5f85f172012-10-14 15:46:38 +020015167/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15168struct intel_dmi_quirk {
15169 void (*hook)(struct drm_device *dev);
15170 const struct dmi_system_id (*dmi_id_list)[];
15171};
15172
15173static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15174{
15175 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15176 return 1;
15177}
15178
15179static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15180 {
15181 .dmi_id_list = &(const struct dmi_system_id[]) {
15182 {
15183 .callback = intel_dmi_reverse_brightness,
15184 .ident = "NCR Corporation",
15185 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15186 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15187 },
15188 },
15189 { } /* terminating entry */
15190 },
15191 .hook = quirk_invert_brightness,
15192 },
15193};
15194
Ben Widawskyc43b5632012-04-16 14:07:40 -070015195static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015196 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15197 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15198
Jesse Barnesb690e962010-07-19 13:53:12 -070015199 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15200 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15201
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015202 /* 830 needs to leave pipe A & dpll A up */
15203 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15204
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015205 /* 830 needs to leave pipe B & dpll B up */
15206 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15207
Keith Packard435793d2011-07-12 14:56:22 -070015208 /* Lenovo U160 cannot use SSC on LVDS */
15209 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015210
15211 /* Sony Vaio Y cannot use SSC on LVDS */
15212 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015213
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015214 /* Acer Aspire 5734Z must invert backlight brightness */
15215 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15216
15217 /* Acer/eMachines G725 */
15218 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15219
15220 /* Acer/eMachines e725 */
15221 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15222
15223 /* Acer/Packard Bell NCL20 */
15224 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15225
15226 /* Acer Aspire 4736Z */
15227 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015228
15229 /* Acer Aspire 5336 */
15230 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015231
15232 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15233 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015234
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015235 /* Acer C720 Chromebook (Core i3 4005U) */
15236 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15237
jens steinb2a96012014-10-28 20:25:53 +010015238 /* Apple Macbook 2,1 (Core 2 T7400) */
15239 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15240
Jani Nikula1b9448b2015-11-05 11:49:59 +020015241 /* Apple Macbook 4,1 */
15242 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15243
Scot Doyled4967d82014-07-03 23:27:52 +000015244 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15245 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015246
15247 /* HP Chromebook 14 (Celeron 2955U) */
15248 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015249
15250 /* Dell Chromebook 11 */
15251 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015252
15253 /* Dell Chromebook 11 (2015 version) */
15254 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015255};
15256
15257static void intel_init_quirks(struct drm_device *dev)
15258{
15259 struct pci_dev *d = dev->pdev;
15260 int i;
15261
15262 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15263 struct intel_quirk *q = &intel_quirks[i];
15264
15265 if (d->device == q->device &&
15266 (d->subsystem_vendor == q->subsystem_vendor ||
15267 q->subsystem_vendor == PCI_ANY_ID) &&
15268 (d->subsystem_device == q->subsystem_device ||
15269 q->subsystem_device == PCI_ANY_ID))
15270 q->hook(dev);
15271 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015272 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15273 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15274 intel_dmi_quirks[i].hook(dev);
15275 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015276}
15277
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015278/* Disable the VGA plane that we never use */
15279static void i915_disable_vga(struct drm_device *dev)
15280{
15281 struct drm_i915_private *dev_priv = dev->dev_private;
15282 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015283 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015284
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015285 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015286 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015287 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015288 sr1 = inb(VGA_SR_DATA);
15289 outb(sr1 | 1<<5, VGA_SR_DATA);
15290 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15291 udelay(300);
15292
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015293 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015294 POSTING_READ(vga_reg);
15295}
15296
Daniel Vetterf8175862012-04-10 15:50:11 +020015297void intel_modeset_init_hw(struct drm_device *dev)
15298{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015299 struct drm_i915_private *dev_priv = dev->dev_private;
15300
Ville Syrjäläb6283052015-06-03 15:45:07 +030015301 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015302
15303 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15304
Daniel Vetterf8175862012-04-10 15:50:11 +020015305 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015306 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015307}
15308
Matt Roperd93c0372015-12-03 11:37:41 -080015309/*
15310 * Calculate what we think the watermarks should be for the state we've read
15311 * out of the hardware and then immediately program those watermarks so that
15312 * we ensure the hardware settings match our internal state.
15313 *
15314 * We can calculate what we think WM's should be by creating a duplicate of the
15315 * current state (which was constructed during hardware readout) and running it
15316 * through the atomic check code to calculate new watermark values in the
15317 * state object.
15318 */
15319static void sanitize_watermarks(struct drm_device *dev)
15320{
15321 struct drm_i915_private *dev_priv = to_i915(dev);
15322 struct drm_atomic_state *state;
15323 struct drm_crtc *crtc;
15324 struct drm_crtc_state *cstate;
15325 struct drm_modeset_acquire_ctx ctx;
15326 int ret;
15327 int i;
15328
15329 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015330 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015331 return;
15332
15333 /*
15334 * We need to hold connection_mutex before calling duplicate_state so
15335 * that the connector loop is protected.
15336 */
15337 drm_modeset_acquire_init(&ctx, 0);
15338retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015339 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015340 if (ret == -EDEADLK) {
15341 drm_modeset_backoff(&ctx);
15342 goto retry;
15343 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015344 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015345 }
15346
15347 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15348 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015349 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015350
Matt Ropered4a6a72016-02-23 17:20:13 -080015351 /*
15352 * Hardware readout is the only time we don't want to calculate
15353 * intermediate watermarks (since we don't trust the current
15354 * watermarks).
15355 */
15356 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15357
Matt Roperd93c0372015-12-03 11:37:41 -080015358 ret = intel_atomic_check(dev, state);
15359 if (ret) {
15360 /*
15361 * If we fail here, it means that the hardware appears to be
15362 * programmed in a way that shouldn't be possible, given our
15363 * understanding of watermark requirements. This might mean a
15364 * mistake in the hardware readout code or a mistake in the
15365 * watermark calculations for a given platform. Raise a WARN
15366 * so that this is noticeable.
15367 *
15368 * If this actually happens, we'll have to just leave the
15369 * BIOS-programmed watermarks untouched and hope for the best.
15370 */
15371 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015372 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015373 }
15374
15375 /* Write calculated watermark values back */
15376 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15377 for_each_crtc_in_state(state, crtc, cstate, i) {
15378 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15379
Matt Ropered4a6a72016-02-23 17:20:13 -080015380 cs->wm.need_postvbl_update = true;
15381 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015382 }
15383
15384 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015385fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015386 drm_modeset_drop_locks(&ctx);
15387 drm_modeset_acquire_fini(&ctx);
15388}
15389
Jesse Barnes79e53942008-11-07 14:24:08 -080015390void intel_modeset_init(struct drm_device *dev)
15391{
Jesse Barnes652c3932009-08-17 13:31:43 -070015392 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015393 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015394 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015395 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015396
15397 drm_mode_config_init(dev);
15398
15399 dev->mode_config.min_width = 0;
15400 dev->mode_config.min_height = 0;
15401
Dave Airlie019d96c2011-09-29 16:20:42 +010015402 dev->mode_config.preferred_depth = 24;
15403 dev->mode_config.prefer_shadow = 1;
15404
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015405 dev->mode_config.allow_fb_modifiers = true;
15406
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015407 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015408
Jesse Barnesb690e962010-07-19 13:53:12 -070015409 intel_init_quirks(dev);
15410
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015411 intel_init_pm(dev);
15412
Ben Widawskye3c74752013-04-05 13:12:39 -070015413 if (INTEL_INFO(dev)->num_pipes == 0)
15414 return;
15415
Lukas Wunner69f92f62015-07-15 13:57:35 +020015416 /*
15417 * There may be no VBT; and if the BIOS enabled SSC we can
15418 * just keep using it to avoid unnecessary flicker. Whereas if the
15419 * BIOS isn't using it, don't assume it will work even if the VBT
15420 * indicates as much.
15421 */
15422 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15423 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15424 DREF_SSC1_ENABLE);
15425
15426 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15427 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15428 bios_lvds_use_ssc ? "en" : "dis",
15429 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15430 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15431 }
15432 }
15433
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015434 if (IS_GEN2(dev)) {
15435 dev->mode_config.max_width = 2048;
15436 dev->mode_config.max_height = 2048;
15437 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015438 dev->mode_config.max_width = 4096;
15439 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015440 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015441 dev->mode_config.max_width = 8192;
15442 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015443 }
Damien Lespiau068be562014-03-28 14:17:49 +000015444
Ville Syrjälädc41c152014-08-13 11:57:05 +030015445 if (IS_845G(dev) || IS_I865G(dev)) {
15446 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15447 dev->mode_config.cursor_height = 1023;
15448 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015449 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15450 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15451 } else {
15452 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15453 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15454 }
15455
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015456 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015457
Zhao Yakui28c97732009-10-09 11:39:41 +080015458 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015459 INTEL_INFO(dev)->num_pipes,
15460 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015461
Damien Lespiau055e3932014-08-18 13:49:10 +010015462 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015463 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015464 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015465 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015466 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015467 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015468 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015469 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015470 }
15471
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015472 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015473 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015474 intel_update_cdclk(dev);
15475
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015476 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015477
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015478 /* Just disable it once at startup */
15479 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015480 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015481
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015482 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015483 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015484 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015485
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015486 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015487 struct intel_initial_plane_config plane_config = {};
15488
Jesse Barnes46f297f2014-03-07 08:57:48 -080015489 if (!crtc->active)
15490 continue;
15491
Jesse Barnes46f297f2014-03-07 08:57:48 -080015492 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015493 * Note that reserving the BIOS fb up front prevents us
15494 * from stuffing other stolen allocations like the ring
15495 * on top. This prevents some ugliness at boot time, and
15496 * can even allow for smooth boot transitions if the BIOS
15497 * fb is large enough for the active pipe configuration.
15498 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015499 dev_priv->display.get_initial_plane_config(crtc,
15500 &plane_config);
15501
15502 /*
15503 * If the fb is shared between multiple heads, we'll
15504 * just get the first one.
15505 */
15506 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015507 }
Matt Roperd93c0372015-12-03 11:37:41 -080015508
15509 /*
15510 * Make sure hardware watermarks really match the state we read out.
15511 * Note that we need to do this after reconstructing the BIOS fb's
15512 * since the watermark calculation done here will use pstate->fb.
15513 */
15514 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015515}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015516
Daniel Vetter7fad7982012-07-04 17:51:47 +020015517static void intel_enable_pipe_a(struct drm_device *dev)
15518{
15519 struct intel_connector *connector;
15520 struct drm_connector *crt = NULL;
15521 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015522 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015523
15524 /* We can't just switch on the pipe A, we need to set things up with a
15525 * proper mode and output configuration. As a gross hack, enable pipe A
15526 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015527 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015528 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15529 crt = &connector->base;
15530 break;
15531 }
15532 }
15533
15534 if (!crt)
15535 return;
15536
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015537 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015538 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015539}
15540
Daniel Vetterfa555832012-10-10 23:14:00 +020015541static bool
15542intel_check_plane_mapping(struct intel_crtc *crtc)
15543{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015544 struct drm_device *dev = crtc->base.dev;
15545 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015546 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015547
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015548 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015549 return true;
15550
Ville Syrjälä649636e2015-09-22 19:50:01 +030015551 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015552
15553 if ((val & DISPLAY_PLANE_ENABLE) &&
15554 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15555 return false;
15556
15557 return true;
15558}
15559
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015560static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15561{
15562 struct drm_device *dev = crtc->base.dev;
15563 struct intel_encoder *encoder;
15564
15565 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15566 return true;
15567
15568 return false;
15569}
15570
Ville Syrjälädd756192016-02-17 21:28:45 +020015571static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15572{
15573 struct drm_device *dev = encoder->base.dev;
15574 struct intel_connector *connector;
15575
15576 for_each_connector_on_encoder(dev, &encoder->base, connector)
15577 return true;
15578
15579 return false;
15580}
15581
Daniel Vetter24929352012-07-02 20:28:59 +020015582static void intel_sanitize_crtc(struct intel_crtc *crtc)
15583{
15584 struct drm_device *dev = crtc->base.dev;
15585 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015586 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015587
Daniel Vetter24929352012-07-02 20:28:59 +020015588 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015589 if (!transcoder_is_dsi(cpu_transcoder)) {
15590 i915_reg_t reg = PIPECONF(cpu_transcoder);
15591
15592 I915_WRITE(reg,
15593 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15594 }
Daniel Vetter24929352012-07-02 20:28:59 +020015595
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015596 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015597 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015598 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015599 struct intel_plane *plane;
15600
Daniel Vetter96256042015-02-13 21:03:42 +010015601 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015602
15603 /* Disable everything but the primary plane */
15604 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15605 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15606 continue;
15607
15608 plane->disable_plane(&plane->base, &crtc->base);
15609 }
Daniel Vetter96256042015-02-13 21:03:42 +010015610 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015611
Daniel Vetter24929352012-07-02 20:28:59 +020015612 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015613 * disable the crtc (and hence change the state) if it is wrong. Note
15614 * that gen4+ has a fixed plane -> pipe mapping. */
15615 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015616 bool plane;
15617
Daniel Vetter24929352012-07-02 20:28:59 +020015618 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15619 crtc->base.base.id);
15620
15621 /* Pipe has the wrong plane attached and the plane is active.
15622 * Temporarily change the plane mapping and disable everything
15623 * ... */
15624 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015625 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015626 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015627 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015628 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015629 }
Daniel Vetter24929352012-07-02 20:28:59 +020015630
Daniel Vetter7fad7982012-07-04 17:51:47 +020015631 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15632 crtc->pipe == PIPE_A && !crtc->active) {
15633 /* BIOS forgot to enable pipe A, this mostly happens after
15634 * resume. Force-enable the pipe to fix this, the update_dpms
15635 * call below we restore the pipe to the right state, but leave
15636 * the required bits on. */
15637 intel_enable_pipe_a(dev);
15638 }
15639
Daniel Vetter24929352012-07-02 20:28:59 +020015640 /* Adjust the state of the output pipe according to whether we
15641 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015642 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015643 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015644
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015645 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015646 /*
15647 * We start out with underrun reporting disabled to avoid races.
15648 * For correct bookkeeping mark this on active crtcs.
15649 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015650 * Also on gmch platforms we dont have any hardware bits to
15651 * disable the underrun reporting. Which means we need to start
15652 * out with underrun reporting disabled also on inactive pipes,
15653 * since otherwise we'll complain about the garbage we read when
15654 * e.g. coming up after runtime pm.
15655 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015656 * No protection against concurrent access is required - at
15657 * worst a fifo underrun happens which also sets this to false.
15658 */
15659 crtc->cpu_fifo_underrun_disabled = true;
15660 crtc->pch_fifo_underrun_disabled = true;
15661 }
Daniel Vetter24929352012-07-02 20:28:59 +020015662}
15663
15664static void intel_sanitize_encoder(struct intel_encoder *encoder)
15665{
15666 struct intel_connector *connector;
15667 struct drm_device *dev = encoder->base.dev;
15668
15669 /* We need to check both for a crtc link (meaning that the
15670 * encoder is active and trying to read from a pipe) and the
15671 * pipe itself being active. */
15672 bool has_active_crtc = encoder->base.crtc &&
15673 to_intel_crtc(encoder->base.crtc)->active;
15674
Ville Syrjälädd756192016-02-17 21:28:45 +020015675 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015676 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15677 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015678 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015679
15680 /* Connector is active, but has no active pipe. This is
15681 * fallout from our resume register restoring. Disable
15682 * the encoder manually again. */
15683 if (encoder->base.crtc) {
15684 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15685 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015686 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015687 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015688 if (encoder->post_disable)
15689 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015690 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015691 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015692
15693 /* Inconsistent output/port/pipe state happens presumably due to
15694 * a bug in one of the get_hw_state functions. Or someplace else
15695 * in our code, like the register restore mess on resume. Clamp
15696 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015697 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015698 if (connector->encoder != encoder)
15699 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015700 connector->base.dpms = DRM_MODE_DPMS_OFF;
15701 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015702 }
15703 }
15704 /* Enabled encoders without active connectors will be fixed in
15705 * the crtc fixup. */
15706}
15707
Imre Deak04098752014-02-18 00:02:16 +020015708void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015709{
15710 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015711 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015712
Imre Deak04098752014-02-18 00:02:16 +020015713 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15714 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15715 i915_disable_vga(dev);
15716 }
15717}
15718
15719void i915_redisable_vga(struct drm_device *dev)
15720{
15721 struct drm_i915_private *dev_priv = dev->dev_private;
15722
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015723 /* This function can be called both from intel_modeset_setup_hw_state or
15724 * at a very early point in our resume sequence, where the power well
15725 * structures are not yet restored. Since this function is at a very
15726 * paranoid "someone might have enabled VGA while we were not looking"
15727 * level, just check if the power well is enabled instead of trying to
15728 * follow the "don't touch the power well if we don't need it" policy
15729 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015730 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015731 return;
15732
Imre Deak04098752014-02-18 00:02:16 +020015733 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015734
15735 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015736}
15737
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015738static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015739{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015740 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015741
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015742 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015743}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015744
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015745/* FIXME read out full plane state for all planes */
15746static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015747{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015748 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015749 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015750 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015751
Matt Roper19b8d382015-09-24 15:53:17 -070015752 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015753 primary_get_hw_state(to_intel_plane(primary));
15754
15755 if (plane_state->visible)
15756 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015757}
15758
Daniel Vetter30e984d2013-06-05 13:34:17 +020015759static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015760{
15761 struct drm_i915_private *dev_priv = dev->dev_private;
15762 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015763 struct intel_crtc *crtc;
15764 struct intel_encoder *encoder;
15765 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015766 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015767
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015768 dev_priv->active_crtcs = 0;
15769
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015770 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015771 struct intel_crtc_state *crtc_state = crtc->config;
15772 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015773
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015774 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15775 memset(crtc_state, 0, sizeof(*crtc_state));
15776 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015777
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015778 crtc_state->base.active = crtc_state->base.enable =
15779 dev_priv->display.get_pipe_config(crtc, crtc_state);
15780
15781 crtc->base.enabled = crtc_state->base.enable;
15782 crtc->active = crtc_state->base.active;
15783
15784 if (crtc_state->base.active) {
15785 dev_priv->active_crtcs |= 1 << crtc->pipe;
15786
15787 if (IS_BROADWELL(dev_priv)) {
15788 pixclk = ilk_pipe_pixel_rate(crtc_state);
15789
15790 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15791 if (crtc_state->ips_enabled)
15792 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15793 } else if (IS_VALLEYVIEW(dev_priv) ||
15794 IS_CHERRYVIEW(dev_priv) ||
15795 IS_BROXTON(dev_priv))
15796 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15797 else
15798 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15799 }
15800
15801 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015802
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015803 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015804
15805 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15806 crtc->base.base.id,
15807 crtc->active ? "enabled" : "disabled");
15808 }
15809
Daniel Vetter53589012013-06-05 13:34:16 +020015810 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15811 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15812
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015813 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15814 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015815 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015816 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015817 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015818 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015819 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015820 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015821
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015822 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015823 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015824 }
15825
Damien Lespiaub2784e12014-08-05 11:29:37 +010015826 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015827 pipe = 0;
15828
15829 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015830 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15831 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015832 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015833 } else {
15834 encoder->base.crtc = NULL;
15835 }
15836
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015837 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015838 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015839 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015840 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015841 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015842 }
15843
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015844 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015845 if (connector->get_hw_state(connector)) {
15846 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015847
15848 encoder = connector->encoder;
15849 connector->base.encoder = &encoder->base;
15850
15851 if (encoder->base.crtc &&
15852 encoder->base.crtc->state->active) {
15853 /*
15854 * This has to be done during hardware readout
15855 * because anything calling .crtc_disable may
15856 * rely on the connector_mask being accurate.
15857 */
15858 encoder->base.crtc->state->connector_mask |=
15859 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015860 encoder->base.crtc->state->encoder_mask |=
15861 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015862 }
15863
Daniel Vetter24929352012-07-02 20:28:59 +020015864 } else {
15865 connector->base.dpms = DRM_MODE_DPMS_OFF;
15866 connector->base.encoder = NULL;
15867 }
15868 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15869 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015870 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015871 connector->base.encoder ? "enabled" : "disabled");
15872 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015873
15874 for_each_intel_crtc(dev, crtc) {
15875 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15876
15877 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15878 if (crtc->base.state->active) {
15879 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15880 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15881 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15882
15883 /*
15884 * The initial mode needs to be set in order to keep
15885 * the atomic core happy. It wants a valid mode if the
15886 * crtc's enabled, so we do the above call.
15887 *
15888 * At this point some state updated by the connectors
15889 * in their ->detect() callback has not run yet, so
15890 * no recalculation can be done yet.
15891 *
15892 * Even if we could do a recalculation and modeset
15893 * right now it would cause a double modeset if
15894 * fbdev or userspace chooses a different initial mode.
15895 *
15896 * If that happens, someone indicated they wanted a
15897 * mode change, which means it's safe to do a full
15898 * recalculation.
15899 */
15900 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015901
15902 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15903 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015904 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015905
15906 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015907 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015908}
15909
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015910/* Scan out the current hw modeset state,
15911 * and sanitizes it to the current state
15912 */
15913static void
15914intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015915{
15916 struct drm_i915_private *dev_priv = dev->dev_private;
15917 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015918 struct intel_crtc *crtc;
15919 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015920 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015921
15922 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015923
15924 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015925 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015926 intel_sanitize_encoder(encoder);
15927 }
15928
Damien Lespiau055e3932014-08-18 13:49:10 +010015929 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015930 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15931 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015932 intel_dump_pipe_config(crtc, crtc->config,
15933 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015934 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015935
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015936 intel_modeset_update_connector_atomic_state(dev);
15937
Daniel Vetter35c95372013-07-17 06:55:04 +020015938 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15939 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15940
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015941 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015942 continue;
15943
15944 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15945
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015946 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015947 pll->on = false;
15948 }
15949
Wayne Boyer666a4532015-12-09 12:29:35 -080015950 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015951 vlv_wm_get_hw_state(dev);
15952 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015953 skl_wm_get_hw_state(dev);
15954 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015955 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015956
15957 for_each_intel_crtc(dev, crtc) {
15958 unsigned long put_domains;
15959
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015960 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015961 if (WARN_ON(put_domains))
15962 modeset_put_power_domains(dev_priv, put_domains);
15963 }
15964 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015965
15966 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015967}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015968
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015969void intel_display_resume(struct drm_device *dev)
15970{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015971 struct drm_i915_private *dev_priv = to_i915(dev);
15972 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15973 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015974 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015975 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015976
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015977 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015978
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015979 /*
15980 * This is a cludge because with real atomic modeset mode_config.mutex
15981 * won't be taken. Unfortunately some probed state like
15982 * audio_codec_enable is still protected by mode_config.mutex, so lock
15983 * it here for now.
15984 */
15985 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015986 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015987
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015988retry:
15989 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015990
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015991 if (ret == 0 && !setup) {
15992 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015993
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015994 intel_modeset_setup_hw_state(dev);
15995 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015996 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015997
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015998 if (ret == 0 && state) {
15999 struct drm_crtc_state *crtc_state;
16000 struct drm_crtc *crtc;
16001 int i;
16002
16003 state->acquire_ctx = &ctx;
16004
16005 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16006 /*
16007 * Force recalculation even if we restore
16008 * current state. With fast modeset this may not result
16009 * in a modeset when the state is compatible.
16010 */
16011 crtc_state->mode_changed = true;
16012 }
16013
16014 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016015 }
16016
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016017 if (ret == -EDEADLK) {
16018 drm_modeset_backoff(&ctx);
16019 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016020 }
16021
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016022 drm_modeset_drop_locks(&ctx);
16023 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016024 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016025
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016026 if (ret) {
16027 DRM_ERROR("Restoring old state failed with %i\n", ret);
16028 drm_atomic_state_free(state);
16029 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016030}
16031
16032void intel_modeset_gem_init(struct drm_device *dev)
16033{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016034 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016035 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016036 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016037
Imre Deakae484342014-03-31 15:10:44 +030016038 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030016039
Chris Wilson1833b132012-05-09 11:56:28 +010016040 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016041
16042 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016043
16044 /*
16045 * Make sure any fbs we allocated at startup are properly
16046 * pinned & fenced. When we do the allocation it's too early
16047 * for this.
16048 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016049 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016050 obj = intel_fb_obj(c->primary->fb);
16051 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016052 continue;
16053
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016054 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016055 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16056 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016057 mutex_unlock(&dev->struct_mutex);
16058 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016059 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16060 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016061 drm_framebuffer_unreference(c->primary->fb);
16062 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016063 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016064 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016065 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016066 }
16067 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016068
16069 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016070}
16071
Imre Deak4932e2c2014-02-11 17:12:48 +020016072void intel_connector_unregister(struct intel_connector *intel_connector)
16073{
16074 struct drm_connector *connector = &intel_connector->base;
16075
16076 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016077 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016078}
16079
Jesse Barnes79e53942008-11-07 14:24:08 -080016080void intel_modeset_cleanup(struct drm_device *dev)
16081{
Jesse Barnes652c3932009-08-17 13:31:43 -070016082 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016083 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016084
Imre Deak2eb52522014-11-19 15:30:05 +020016085 intel_disable_gt_powersave(dev);
16086
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016087 intel_backlight_unregister(dev);
16088
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016089 /*
16090 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016091 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016092 * experience fancy races otherwise.
16093 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016094 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016095
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016096 /*
16097 * Due to the hpd irq storm handling the hotplug work can re-arm the
16098 * poll handlers. Hence disable polling after hpd handling is shut down.
16099 */
Keith Packardf87ea762010-10-03 19:36:26 -070016100 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016101
Jesse Barnes723bfd72010-10-07 16:01:13 -070016102 intel_unregister_dsm_handler();
16103
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016104 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016105
Chris Wilson1630fe72011-07-08 12:22:42 +010016106 /* flush any delayed tasks or pending work */
16107 flush_scheduled_work();
16108
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016109 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016110 for_each_intel_connector(dev, connector)
16111 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016112
Jesse Barnes79e53942008-11-07 14:24:08 -080016113 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016114
16115 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016116
Imre Deakae484342014-03-31 15:10:44 +030016117 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016118
16119 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016120}
16121
Dave Airlie28d52042009-09-21 14:33:58 +100016122/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016123 * Return which encoder is currently attached for connector.
16124 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016125struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016126{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016127 return &intel_attached_encoder(connector)->base;
16128}
Jesse Barnes79e53942008-11-07 14:24:08 -080016129
Chris Wilsondf0e9242010-09-09 16:20:55 +010016130void intel_connector_attach_encoder(struct intel_connector *connector,
16131 struct intel_encoder *encoder)
16132{
16133 connector->encoder = encoder;
16134 drm_mode_connector_attach_encoder(&connector->base,
16135 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016136}
Dave Airlie28d52042009-09-21 14:33:58 +100016137
16138/*
16139 * set vga decode state - true == enable VGA decode
16140 */
16141int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16142{
16143 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016144 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016145 u16 gmch_ctrl;
16146
Chris Wilson75fa0412014-02-07 18:37:02 -020016147 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16148 DRM_ERROR("failed to read control word\n");
16149 return -EIO;
16150 }
16151
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016152 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16153 return 0;
16154
Dave Airlie28d52042009-09-21 14:33:58 +100016155 if (state)
16156 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16157 else
16158 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016159
16160 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16161 DRM_ERROR("failed to write control word\n");
16162 return -EIO;
16163 }
16164
Dave Airlie28d52042009-09-21 14:33:58 +100016165 return 0;
16166}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016167
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016168struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016169
16170 u32 power_well_driver;
16171
Chris Wilson63b66e52013-08-08 15:12:06 +020016172 int num_transcoders;
16173
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016174 struct intel_cursor_error_state {
16175 u32 control;
16176 u32 position;
16177 u32 base;
16178 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016179 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016180
16181 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016182 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016183 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016184 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016185 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016186
16187 struct intel_plane_error_state {
16188 u32 control;
16189 u32 stride;
16190 u32 size;
16191 u32 pos;
16192 u32 addr;
16193 u32 surface;
16194 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016195 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016196
16197 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016198 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016199 enum transcoder cpu_transcoder;
16200
16201 u32 conf;
16202
16203 u32 htotal;
16204 u32 hblank;
16205 u32 hsync;
16206 u32 vtotal;
16207 u32 vblank;
16208 u32 vsync;
16209 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016210};
16211
16212struct intel_display_error_state *
16213intel_display_capture_error_state(struct drm_device *dev)
16214{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016215 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016216 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016217 int transcoders[] = {
16218 TRANSCODER_A,
16219 TRANSCODER_B,
16220 TRANSCODER_C,
16221 TRANSCODER_EDP,
16222 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016223 int i;
16224
Chris Wilson63b66e52013-08-08 15:12:06 +020016225 if (INTEL_INFO(dev)->num_pipes == 0)
16226 return NULL;
16227
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016228 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016229 if (error == NULL)
16230 return NULL;
16231
Imre Deak190be112013-11-25 17:15:31 +020016232 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016233 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16234
Damien Lespiau055e3932014-08-18 13:49:10 +010016235 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016236 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016237 __intel_display_power_is_enabled(dev_priv,
16238 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016239 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016240 continue;
16241
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016242 error->cursor[i].control = I915_READ(CURCNTR(i));
16243 error->cursor[i].position = I915_READ(CURPOS(i));
16244 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016245
16246 error->plane[i].control = I915_READ(DSPCNTR(i));
16247 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016248 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016249 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016250 error->plane[i].pos = I915_READ(DSPPOS(i));
16251 }
Paulo Zanonica291362013-03-06 20:03:14 -030016252 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16253 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016254 if (INTEL_INFO(dev)->gen >= 4) {
16255 error->plane[i].surface = I915_READ(DSPSURF(i));
16256 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16257 }
16258
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016259 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016260
Sonika Jindal3abfce72014-07-21 15:23:43 +053016261 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016262 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016263 }
16264
Jani Nikula4d1de972016-03-18 17:05:42 +020016265 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016266 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16267 if (HAS_DDI(dev_priv->dev))
16268 error->num_transcoders++; /* Account for eDP. */
16269
16270 for (i = 0; i < error->num_transcoders; i++) {
16271 enum transcoder cpu_transcoder = transcoders[i];
16272
Imre Deakddf9c532013-11-27 22:02:02 +020016273 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016274 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016275 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016276 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016277 continue;
16278
Chris Wilson63b66e52013-08-08 15:12:06 +020016279 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16280
16281 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16282 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16283 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16284 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16285 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16286 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16287 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016288 }
16289
16290 return error;
16291}
16292
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016293#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16294
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016295void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016296intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016297 struct drm_device *dev,
16298 struct intel_display_error_state *error)
16299{
Damien Lespiau055e3932014-08-18 13:49:10 +010016300 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016301 int i;
16302
Chris Wilson63b66e52013-08-08 15:12:06 +020016303 if (!error)
16304 return;
16305
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016306 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016307 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016308 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016309 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016310 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016311 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016312 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016313 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016314 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016315 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016316
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016317 err_printf(m, "Plane [%d]:\n", i);
16318 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16319 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016320 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016321 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16322 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016323 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016324 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016325 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016326 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016327 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16328 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016329 }
16330
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016331 err_printf(m, "Cursor [%d]:\n", i);
16332 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16333 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16334 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016335 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016336
16337 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016338 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016339 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016340 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016341 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016342 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16343 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16344 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16345 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16346 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16347 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16348 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16349 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016350}