blob: 894091fe9dabf34cec1b2847c2f6eac6099f838d [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080047#include <linux/reservation.h>
48#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Matt Roper465c1202014-05-29 08:06:54 -070050/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010051static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010052 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070054 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010055 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070056};
57
58/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010059static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010060 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010064 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
72 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010073 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070076 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053077 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070081};
82
Matt Roper3d7d6512014-06-10 08:28:13 -070083/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020089 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030090static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020091 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030092
Jesse Barneseb1bfe82014-02-12 12:26:25 -080093static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020097static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +020099static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200100static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700101 struct intel_link_m_n *m_n,
102 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200103static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200104static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200105static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200106static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200107 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200108static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200109 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200110static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
111static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700112static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
113 struct intel_crtc_state *crtc_state);
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +0200114static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200118static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200172static int
173intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200174{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200175 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200176}
177
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200178static int
179intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300180{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200181 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
182 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183}
184
185static int
186intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
187{
Jani Nikula79e50a42015-08-26 10:58:20 +0300188 uint32_t clkcfg;
189
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300195 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200196 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300197 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200198 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200200 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200204 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 }
212}
213
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214static void intel_update_rawclk(struct drm_i915_private *dev_priv)
215{
216 if (HAS_PCH_SPLIT(dev_priv))
217 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
218 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
219 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
220 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
221 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
222 else
223 return; /* no rawclk on other platforms, or no need to know it */
224
225 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
226}
227
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300228static void intel_update_czclk(struct drm_i915_private *dev_priv)
229{
Wayne Boyer666a4532015-12-09 12:29:35 -0800230 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300231 return;
232
233 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
234 CCK_CZ_CLOCK_CONTROL);
235
236 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
237}
238
Chris Wilson021357a2010-09-07 20:54:59 +0100239static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200240intel_fdi_link_freq(struct drm_i915_private *dev_priv,
241 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100242{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200243 if (HAS_DDI(dev_priv))
244 return pipe_config->port_clock; /* SPLL */
245 else if (IS_GEN5(dev_priv))
246 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200247 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200248 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100249}
250
Daniel Vetter5d536e22013-07-06 12:52:06 +0200251static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200253 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200254 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
Daniel Vetter5d536e22013-07-06 12:52:06 +0200264static const intel_limit_t intel_limits_i8xx_dvo = {
265 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200266 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200267 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 4 },
275};
276
Keith Packarde4b36692009-06-05 19:22:17 -0700277static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200279 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200280 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700288};
Eric Anholt273e27c2011-03-30 13:01:10 -0700289
Keith Packarde4b36692009-06-05 19:22:17 -0700290static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .p2 = { .dot_limit = 200000,
300 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700301};
302
303static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400310 .p = { .min = 7, .max = 98 },
311 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .p2 = { .dot_limit = 112000,
313 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
Eric Anholt273e27c2011-03-30 13:01:10 -0700316
Keith Packarde4b36692009-06-05 19:22:17 -0700317static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 25000, .max = 270000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 17, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 10, .max = 30 },
325 .p1 = { .min = 1, .max = 3},
326 .p2 = { .dot_limit = 270000,
327 .p2_slow = 10,
328 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800329 },
Keith Packarde4b36692009-06-05 19:22:17 -0700330};
331
332static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700333 .dot = { .min = 22000, .max = 400000 },
334 .vco = { .min = 1750000, .max = 3500000},
335 .n = { .min = 1, .max = 4 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 16, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 5, .max = 80 },
340 .p1 = { .min = 1, .max = 8},
341 .p2 = { .dot_limit = 165000,
342 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700343};
344
345static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .dot = { .min = 20000, .max = 115000 },
347 .vco = { .min = 1750000, .max = 3500000 },
348 .n = { .min = 1, .max = 3 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 17, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 0,
355 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800356 },
Keith Packarde4b36692009-06-05 19:22:17 -0700357};
358
359static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .dot = { .min = 80000, .max = 224000 },
361 .vco = { .min = 1750000, .max = 3500000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 104, .max = 138 },
364 .m1 = { .min = 17, .max = 23 },
365 .m2 = { .min = 5, .max = 11 },
366 .p = { .min = 14, .max = 42 },
367 .p1 = { .min = 2, .max = 6 },
368 .p2 = { .dot_limit = 0,
369 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800370 },
Keith Packarde4b36692009-06-05 19:22:17 -0700371};
372
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500373static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400374 .dot = { .min = 20000, .max = 400000},
375 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400377 .n = { .min = 3, .max = 6 },
378 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700379 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 5, .max = 80 },
383 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700384 .p2 = { .dot_limit = 200000,
385 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700386};
387
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500388static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .dot = { .min = 20000, .max = 400000 },
390 .vco = { .min = 1700000, .max = 3500000 },
391 .n = { .min = 3, .max = 6 },
392 .m = { .min = 2, .max = 256 },
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 7, .max = 112 },
396 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700397 .p2 = { .dot_limit = 112000,
398 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700399};
400
Eric Anholt273e27c2011-03-30 13:01:10 -0700401/* Ironlake / Sandybridge
402 *
403 * We calculate clock using (register_value + 2) for N/M1/M2, so here
404 * the range value for them is (actual_value - 2).
405 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800406static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 5 },
410 .m = { .min = 79, .max = 127 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 5, .max = 80 },
414 .p1 = { .min = 1, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700417};
418
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800419static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 118 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 28, .max = 112 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430};
431
432static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 127 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 14, .max = 56 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800443};
444
Eric Anholt273e27c2011-03-30 13:01:10 -0700445/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800446static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 2 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400454 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800457};
458
459static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 3 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800470};
471
Ville Syrjälädc730512013-09-24 21:26:30 +0300472static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200480 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700481 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700482 .m1 = { .min = 2, .max = 3 },
483 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300484 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300485 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700486};
487
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300488static const intel_limit_t intel_limits_chv = {
489 /*
490 * These are the data rate limits (measured in fast clocks)
491 * since those are the strictest limits we have. The fast
492 * clock and actual rate limits are more relaxed, so checking
493 * them would make no difference.
494 */
495 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200496 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300497 .n = { .min = 1, .max = 1 },
498 .m1 = { .min = 2, .max = 2 },
499 .m2 = { .min = 24 << 22, .max = 175 << 22 },
500 .p1 = { .min = 2, .max = 4 },
501 .p2 = { .p2_slow = 1, .p2_fast = 14 },
502};
503
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200504static const intel_limit_t intel_limits_bxt = {
505 /* FIXME: find real dot limits */
506 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530507 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 /* FIXME: find real m2 limits */
511 .m2 = { .min = 2 << 22, .max = 255 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 20 },
514};
515
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200516static bool
517needs_modeset(struct drm_crtc_state *state)
518{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200519 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200520}
521
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300522/**
523 * Returns whether any output on the specified pipe is of the specified type
524 */
Damien Lespiau40935612014-10-29 11:16:59 +0000525bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300526{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300527 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300528 struct intel_encoder *encoder;
529
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300530 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300531 if (encoder->type == type)
532 return true;
533
534 return false;
535}
536
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200537/**
538 * Returns whether any output on the specified pipe will have the specified
539 * type after a staged modeset is complete, i.e., the same as
540 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
541 * encoder->crtc.
542 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200543static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
544 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200545{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300547 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200549 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200551
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300552 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (connector_state->crtc != crtc_state->base.crtc)
554 continue;
555
556 num_connectors++;
557
558 encoder = to_intel_encoder(connector_state->best_encoder);
559 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 }
562
563 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200564
565 return false;
566}
567
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200568static const intel_limit_t *
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200569intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800570{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200571 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800572 const intel_limit_t *limit;
573
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200574 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100575 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700576 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800577 else
Keith Packarde4b36692009-06-05 19:22:17 -0700578 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200579 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
580 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700581 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200582 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700583 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800584 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700585 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800586
587 return limit;
588}
589
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200590static const intel_limit_t *
591intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800592{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200593 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 const intel_limit_t *limit;
595
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200596 if (IS_BROXTON(dev))
597 limit = &intel_limits_bxt;
Ander Conselvan de Oliveira8f0d5b92016-03-21 18:00:04 +0200598 else if (WARN_ON(HAS_PCH_SPLIT(dev)))
599 limit = NULL;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800600 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200601 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500602 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200603 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500604 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800605 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500606 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300607 } else if (IS_CHERRYVIEW(dev)) {
608 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700609 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300610 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100611 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200612 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100613 limit = &intel_limits_i9xx_lvds;
614 else
615 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200617 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700618 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200619 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700620 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200621 else
622 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 }
624 return limit;
625}
626
Imre Deakdccbea32015-06-22 23:35:51 +0300627/*
628 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
629 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
630 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
631 * The helpers' return value is the rate of the clock that is fed to the
632 * display engine's pipe which can be the above fast dot clock rate or a
633 * divided-down version of it.
634 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500635/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300636static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800637{
Shaohua Li21778322009-02-23 15:19:16 +0800638 clock->m = clock->m2 + 2;
639 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200640 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300641 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300642 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
643 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300644
645 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800646}
647
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200648static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
649{
650 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
651}
652
Imre Deakdccbea32015-06-22 23:35:51 +0300653static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800654{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200655 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200657 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300658 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300659 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
660 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300661
662 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800663}
664
Imre Deakdccbea32015-06-22 23:35:51 +0300665static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300666{
667 clock->m = clock->m1 * clock->m2;
668 clock->p = clock->p1 * clock->p2;
669 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300670 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300671 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
672 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300673
674 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300675}
676
Imre Deakdccbea32015-06-22 23:35:51 +0300677int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300678{
679 clock->m = clock->m1 * clock->m2;
680 clock->p = clock->p1 * clock->p2;
681 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300682 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300683 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
684 clock->n << 22);
685 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300686
687 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300688}
689
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800690#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800691/**
692 * Returns whether the given set of divisors are valid for a given refclk with
693 * the given connectors.
694 */
695
Chris Wilson1b894b52010-12-14 20:04:54 +0000696static bool intel_PLL_is_valid(struct drm_device *dev,
697 const intel_limit_t *limit,
698 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800699{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300700 if (clock->n < limit->n.min || limit->n.max < clock->n)
701 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400703 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400705 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400707 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300708
Wayne Boyer666a4532015-12-09 12:29:35 -0800709 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
710 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300711 if (clock->m1 <= clock->m2)
712 INTELPllInvalid("m1 <= m2\n");
713
Wayne Boyer666a4532015-12-09 12:29:35 -0800714 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300715 if (clock->p < limit->p.min || limit->p.max < clock->p)
716 INTELPllInvalid("p out of range\n");
717 if (clock->m < limit->m.min || limit->m.max < clock->m)
718 INTELPllInvalid("m out of range\n");
719 }
720
Jesse Barnes79e53942008-11-07 14:24:08 -0800721 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400722 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800723 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
724 * connector, etc., rather than just a single range.
725 */
726 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400727 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800728
729 return true;
730}
731
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300732static int
733i9xx_select_p2_div(const intel_limit_t *limit,
734 const struct intel_crtc_state *crtc_state,
735 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800736{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300737 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800738
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200739 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800740 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100741 * For LVDS just rely on its current settings for dual-channel.
742 * We haven't figured out how to reliably set up different
743 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800744 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100745 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300746 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800747 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300748 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800749 } else {
750 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300751 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300753 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800754 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300755}
756
757static bool
758i9xx_find_best_dpll(const intel_limit_t *limit,
759 struct intel_crtc_state *crtc_state,
760 int target, int refclk, intel_clock_t *match_clock,
761 intel_clock_t *best_clock)
762{
763 struct drm_device *dev = crtc_state->base.crtc->dev;
764 intel_clock_t clock;
765 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800766
Akshay Joshi0206e352011-08-16 15:34:10 -0400767 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800768
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300769 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
770
Zhao Yakui42158662009-11-20 11:24:18 +0800771 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
772 clock.m1++) {
773 for (clock.m2 = limit->m2.min;
774 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200775 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800776 break;
777 for (clock.n = limit->n.min;
778 clock.n <= limit->n.max; clock.n++) {
779 for (clock.p1 = limit->p1.min;
780 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800781 int this_err;
782
Imre Deakdccbea32015-06-22 23:35:51 +0300783 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000784 if (!intel_PLL_is_valid(dev, limit,
785 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800786 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800787 if (match_clock &&
788 clock.p != match_clock->p)
789 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800790
791 this_err = abs(clock.dot - target);
792 if (this_err < err) {
793 *best_clock = clock;
794 err = this_err;
795 }
796 }
797 }
798 }
799 }
800
801 return (err != target);
802}
803
Ma Lingd4906092009-03-18 20:13:27 +0800804static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200805pnv_find_best_dpll(const intel_limit_t *limit,
806 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200807 int target, int refclk, intel_clock_t *match_clock,
808 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200809{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300810 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200811 intel_clock_t clock;
812 int err = target;
813
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200814 memset(best_clock, 0, sizeof(*best_clock));
815
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300816 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
817
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200818 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
819 clock.m1++) {
820 for (clock.m2 = limit->m2.min;
821 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200822 for (clock.n = limit->n.min;
823 clock.n <= limit->n.max; clock.n++) {
824 for (clock.p1 = limit->p1.min;
825 clock.p1 <= limit->p1.max; clock.p1++) {
826 int this_err;
827
Imre Deakdccbea32015-06-22 23:35:51 +0300828 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800829 if (!intel_PLL_is_valid(dev, limit,
830 &clock))
831 continue;
832 if (match_clock &&
833 clock.p != match_clock->p)
834 continue;
835
836 this_err = abs(clock.dot - target);
837 if (this_err < err) {
838 *best_clock = clock;
839 err = this_err;
840 }
841 }
842 }
843 }
844 }
845
846 return (err != target);
847}
848
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200849/*
850 * Returns a set of divisors for the desired target clock with the given
851 * refclk, or FALSE. The returned values represent the clock equation:
852 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
853 */
Ma Lingd4906092009-03-18 20:13:27 +0800854static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200855g4x_find_best_dpll(const intel_limit_t *limit,
856 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200857 int target, int refclk, intel_clock_t *match_clock,
858 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800859{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300860 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800861 intel_clock_t clock;
862 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300863 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400864 /* approximately equals target * 0.00585 */
865 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800866
867 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300868
869 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
870
Ma Lingd4906092009-03-18 20:13:27 +0800871 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200872 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800873 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200874 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800875 for (clock.m1 = limit->m1.max;
876 clock.m1 >= limit->m1.min; clock.m1--) {
877 for (clock.m2 = limit->m2.max;
878 clock.m2 >= limit->m2.min; clock.m2--) {
879 for (clock.p1 = limit->p1.max;
880 clock.p1 >= limit->p1.min; clock.p1--) {
881 int this_err;
882
Imre Deakdccbea32015-06-22 23:35:51 +0300883 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000884 if (!intel_PLL_is_valid(dev, limit,
885 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800886 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000887
888 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800889 if (this_err < err_most) {
890 *best_clock = clock;
891 err_most = this_err;
892 max_n = clock.n;
893 found = true;
894 }
895 }
896 }
897 }
898 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800899 return found;
900}
Ma Lingd4906092009-03-18 20:13:27 +0800901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902/*
903 * Check if the calculated PLL configuration is more optimal compared to the
904 * best configuration and error found so far. Return the calculated error.
905 */
906static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
907 const intel_clock_t *calculated_clock,
908 const intel_clock_t *best_clock,
909 unsigned int best_error_ppm,
910 unsigned int *error_ppm)
911{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200912 /*
913 * For CHV ignore the error and consider only the P value.
914 * Prefer a bigger P value based on HW requirements.
915 */
916 if (IS_CHERRYVIEW(dev)) {
917 *error_ppm = 0;
918
919 return calculated_clock->p > best_clock->p;
920 }
921
Imre Deak24be4e42015-03-17 11:40:04 +0200922 if (WARN_ON_ONCE(!target_freq))
923 return false;
924
Imre Deakd5dd62b2015-03-17 11:40:03 +0200925 *error_ppm = div_u64(1000000ULL *
926 abs(target_freq - calculated_clock->dot),
927 target_freq);
928 /*
929 * Prefer a better P value over a better (smaller) error if the error
930 * is small. Ensure this preference for future configurations too by
931 * setting the error to 0.
932 */
933 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
934 *error_ppm = 0;
935
936 return true;
937 }
938
939 return *error_ppm + 10 < best_error_ppm;
940}
941
Zhenyu Wang2c072452009-06-05 15:38:42 +0800942static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200943vlv_find_best_dpll(const intel_limit_t *limit,
944 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200945 int target, int refclk, intel_clock_t *match_clock,
946 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700947{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200948 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300949 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300950 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300951 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300952 /* min update 19.2 MHz */
953 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300954 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700955
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956 target *= 5; /* fast clock */
957
958 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700959
960 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300961 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300963 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300964 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300965 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700966 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300967 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200968 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300969
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300970 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
971 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300972
Imre Deakdccbea32015-06-22 23:35:51 +0300973 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300974
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300975 if (!intel_PLL_is_valid(dev, limit,
976 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300977 continue;
978
Imre Deakd5dd62b2015-03-17 11:40:03 +0200979 if (!vlv_PLL_is_optimal(dev, target,
980 &clock,
981 best_clock,
982 bestppm, &ppm))
983 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300984
Imre Deakd5dd62b2015-03-17 11:40:03 +0200985 *best_clock = clock;
986 bestppm = ppm;
987 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700988 }
989 }
990 }
991 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700992
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300993 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700994}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700995
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300996static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200997chv_find_best_dpll(const intel_limit_t *limit,
998 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300999 int target, int refclk, intel_clock_t *match_clock,
1000 intel_clock_t *best_clock)
1001{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001002 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001003 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001004 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001005 intel_clock_t clock;
1006 uint64_t m2;
1007 int found = false;
1008
1009 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001010 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001011
1012 /*
1013 * Based on hardware doc, the n always set to 1, and m1 always
1014 * set to 2. If requires to support 200Mhz refclk, we need to
1015 * revisit this because n may not 1 anymore.
1016 */
1017 clock.n = 1, clock.m1 = 2;
1018 target *= 5; /* fast clock */
1019
1020 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1021 for (clock.p2 = limit->p2.p2_fast;
1022 clock.p2 >= limit->p2.p2_slow;
1023 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001024 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001025
1026 clock.p = clock.p1 * clock.p2;
1027
1028 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1029 clock.n) << 22, refclk * clock.m1);
1030
1031 if (m2 > INT_MAX/clock.m1)
1032 continue;
1033
1034 clock.m2 = m2;
1035
Imre Deakdccbea32015-06-22 23:35:51 +03001036 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001037
1038 if (!intel_PLL_is_valid(dev, limit, &clock))
1039 continue;
1040
Imre Deak9ca3ba02015-03-17 11:40:05 +02001041 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1042 best_error_ppm, &error_ppm))
1043 continue;
1044
1045 *best_clock = clock;
1046 best_error_ppm = error_ppm;
1047 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001048 }
1049 }
1050
1051 return found;
1052}
1053
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001054bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1055 intel_clock_t *best_clock)
1056{
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02001057 int refclk = i9xx_get_refclk(crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001058
1059 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1060 target_clock, refclk, NULL, best_clock);
1061}
1062
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001063bool intel_crtc_active(struct drm_crtc *crtc)
1064{
1065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1066
1067 /* Be paranoid as we can arrive here with only partial
1068 * state retrieved from the hardware during setup.
1069 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001070 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001071 * as Haswell has gained clock readout/fastboot support.
1072 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001073 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001074 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001075 *
1076 * FIXME: The intel_crtc->active here should be switched to
1077 * crtc->state->active once we have proper CRTC states wired up
1078 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001079 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001080 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001081 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001082}
1083
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001084enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1085 enum pipe pipe)
1086{
1087 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001090 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001091}
1092
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001093static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1094{
1095 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001096 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001097 u32 line1, line2;
1098 u32 line_mask;
1099
1100 if (IS_GEN2(dev))
1101 line_mask = DSL_LINEMASK_GEN2;
1102 else
1103 line_mask = DSL_LINEMASK_GEN3;
1104
1105 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001106 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001107 line2 = I915_READ(reg) & line_mask;
1108
1109 return line1 == line2;
1110}
1111
Keith Packardab7ad7f2010-10-03 00:33:06 -07001112/*
1113 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001114 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 *
1116 * After disabling a pipe, we can't wait for vblank in the usual way,
1117 * spinning on the vblank interrupt status bit, since we won't actually
1118 * see an interrupt when the pipe is disabled.
1119 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001120 * On Gen4 and above:
1121 * wait for the pipe register state bit to turn off
1122 *
1123 * Otherwise:
1124 * wait for the display line value to settle (it usually
1125 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001126 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001127 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001128static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001129{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001130 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001131 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001132 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001133 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001134
Keith Packardab7ad7f2010-10-03 00:33:06 -07001135 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001136 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001137
Keith Packardab7ad7f2010-10-03 00:33:06 -07001138 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001139 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1140 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001141 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001142 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001143 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001144 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001145 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001146 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001147}
1148
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001150void assert_pll(struct drm_i915_private *dev_priv,
1151 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001152{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001153 u32 val;
1154 bool cur_state;
1155
Ville Syrjälä649636e2015-09-22 19:50:01 +03001156 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001157 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001158 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001159 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001160 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001162
Jani Nikula23538ef2013-08-27 15:12:22 +03001163/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001164void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001165{
1166 u32 val;
1167 bool cur_state;
1168
Ville Syrjäläa5805162015-05-26 20:42:30 +03001169 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001170 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001171 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001172
1173 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001175 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001176 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001177}
Jani Nikula23538ef2013-08-27 15:12:22 +03001178
Jesse Barnes040484a2011-01-03 12:14:26 -08001179static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1180 enum pipe pipe, bool state)
1181{
Jesse Barnes040484a2011-01-03 12:14:26 -08001182 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001183 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1184 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001185
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001186 if (HAS_DDI(dev_priv->dev)) {
1187 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001188 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001189 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001190 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001191 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001192 cur_state = !!(val & FDI_TX_ENABLE);
1193 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001194 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001195 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001196 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001197}
1198#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1199#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1200
1201static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1202 enum pipe pipe, bool state)
1203{
Jesse Barnes040484a2011-01-03 12:14:26 -08001204 u32 val;
1205 bool cur_state;
1206
Ville Syrjälä649636e2015-09-22 19:50:01 +03001207 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001208 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001209 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001210 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001211 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001212}
1213#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1214#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1215
1216static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1217 enum pipe pipe)
1218{
Jesse Barnes040484a2011-01-03 12:14:26 -08001219 u32 val;
1220
1221 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001222 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001223 return;
1224
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001225 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001226 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001227 return;
1228
Ville Syrjälä649636e2015-09-22 19:50:01 +03001229 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001230 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 -08001231}
1232
Daniel Vetter55607e82013-06-16 21:42:39 +02001233void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1234 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001235{
Jesse Barnes040484a2011-01-03 12:14:26 -08001236 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001237 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001238
Ville Syrjälä649636e2015-09-22 19:50:01 +03001239 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001243 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetterb680c372014-09-19 18:27:27 +02001246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001249 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001250 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001253 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001254
Jani Nikulabedd4db2014-08-22 15:04:13 +03001255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001268 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 } else {
1273 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 locked = false;
1282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001285 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286}
1287
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
Paulo Zanonid9d82082014-02-27 16:30:56 -03001294 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001295 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001296 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001301 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001309 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001310 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1311 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001312 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001314 /* if we need the pipe quirk it must be always on */
1315 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1316 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001317 state = true;
1318
Imre Deak4feed0e2016-02-12 18:55:14 +02001319 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1320 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001321 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001322 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001323
1324 intel_display_power_put(dev_priv, power_domain);
1325 } else {
1326 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001327 }
1328
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001331 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332}
1333
Chris Wilson931872f2012-01-16 23:01:13 +00001334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001336{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001337 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001338 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001341 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001343 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001344 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345}
1346
Chris Wilson931872f2012-01-16 23:01:13 +00001347#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1348#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1349
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1351 enum pipe pipe)
1352{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001353 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001354 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355
Ville Syrjälä653e1022013-06-04 13:49:05 +03001356 /* Primary planes are fixed to pipes on gen4+ */
1357 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001358 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001360 "plane %c assertion failure, should be disabled but not\n",
1361 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001362 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001363 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001364
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001366 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001367 u32 val = I915_READ(DSPCNTR(i));
1368 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001370 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001371 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1372 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373 }
1374}
1375
Jesse Barnes19332d72013-03-28 09:55:38 -07001376static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1377 enum pipe pipe)
1378{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001379 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001380 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001381
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001382 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001383 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001384 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001385 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001386 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1387 sprite, pipe_name(pipe));
1388 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001389 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001390 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001391 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001393 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001394 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001395 }
1396 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001397 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001398 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001399 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001400 plane_name(pipe), pipe_name(pipe));
1401 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001402 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001403 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001404 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1405 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001406 }
1407}
1408
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001409static void assert_vblank_disabled(struct drm_crtc *crtc)
1410{
Rob Clarke2c719b2014-12-15 13:56:32 -05001411 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001412 drm_crtc_vblank_put(crtc);
1413}
1414
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001415void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1416 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001417{
Jesse Barnes92f25842011-01-04 15:09:34 -08001418 u32 val;
1419 bool enabled;
1420
Ville Syrjälä649636e2015-09-22 19:50:01 +03001421 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001422 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001424 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1425 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001426}
1427
Keith Packard4e634382011-08-06 10:39:45 -07001428static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1429 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001430{
1431 if ((val & DP_PORT_EN) == 0)
1432 return false;
1433
1434 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001435 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001436 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1437 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001438 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1439 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1440 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001441 } else {
1442 if ((val & DP_PIPE_MASK) != (pipe << 30))
1443 return false;
1444 }
1445 return true;
1446}
1447
Keith Packard1519b992011-08-06 10:35:34 -07001448static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1449 enum pipe pipe, u32 val)
1450{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001451 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001452 return false;
1453
1454 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001455 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001456 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001457 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1458 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1459 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001460 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001461 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001462 return false;
1463 }
1464 return true;
1465}
1466
1467static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1468 enum pipe pipe, u32 val)
1469{
1470 if ((val & LVDS_PORT_EN) == 0)
1471 return false;
1472
1473 if (HAS_PCH_CPT(dev_priv->dev)) {
1474 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1475 return false;
1476 } else {
1477 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1478 return false;
1479 }
1480 return true;
1481}
1482
1483static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1484 enum pipe pipe, u32 val)
1485{
1486 if ((val & ADPA_DAC_ENABLE) == 0)
1487 return false;
1488 if (HAS_PCH_CPT(dev_priv->dev)) {
1489 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1490 return false;
1491 } else {
1492 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1493 return false;
1494 }
1495 return true;
1496}
1497
Jesse Barnes291906f2011-02-02 12:28:03 -08001498static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001499 enum pipe pipe, i915_reg_t reg,
1500 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001501{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001502 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001503 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001504 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001505 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001506
Rob Clarke2c719b2014-12-15 13:56:32 -05001507 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001508 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001509 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001510}
1511
1512static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001513 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001514{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001515 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001516 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001517 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001518 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001519
Rob Clarke2c719b2014-12-15 13:56:32 -05001520 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001521 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001522 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001523}
1524
1525static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1526 enum pipe pipe)
1527{
Jesse Barnes291906f2011-02-02 12:28:03 -08001528 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001529
Keith Packardf0575e92011-07-25 22:12:43 -07001530 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1531 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1532 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001533
Ville Syrjälä649636e2015-09-22 19:50:01 +03001534 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001535 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001536 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001537 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001538
Ville Syrjälä649636e2015-09-22 19:50:01 +03001539 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001540 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001541 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001542 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001543
Paulo Zanonie2debe92013-02-18 19:00:27 -03001544 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1545 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1546 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001547}
1548
Ville Syrjäläd288f652014-10-28 13:20:22 +02001549static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001550 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001551{
Daniel Vetter426115c2013-07-11 22:13:42 +02001552 struct drm_device *dev = crtc->base.dev;
1553 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001554 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001555 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001556
Daniel Vetter426115c2013-07-11 22:13:42 +02001557 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001558
Daniel Vetter87442f72013-06-06 00:52:17 +02001559 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001560 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001561 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001562
Daniel Vetter426115c2013-07-11 22:13:42 +02001563 I915_WRITE(reg, dpll);
1564 POSTING_READ(reg);
1565 udelay(150);
1566
1567 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1568 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1569
Ville Syrjäläd288f652014-10-28 13:20:22 +02001570 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001571 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001572
1573 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001574 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001575 POSTING_READ(reg);
1576 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001577 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001578 POSTING_READ(reg);
1579 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001580 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001581 POSTING_READ(reg);
1582 udelay(150); /* wait for warmup */
1583}
1584
Ville Syrjäläd288f652014-10-28 13:20:22 +02001585static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001586 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001587{
1588 struct drm_device *dev = crtc->base.dev;
1589 struct drm_i915_private *dev_priv = dev->dev_private;
1590 int pipe = crtc->pipe;
1591 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592 u32 tmp;
1593
1594 assert_pipe_disabled(dev_priv, crtc->pipe);
1595
Ville Syrjäläa5805162015-05-26 20:42:30 +03001596 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597
1598 /* Enable back the 10bit clock to display controller */
1599 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1600 tmp |= DPIO_DCLKP_EN;
1601 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1602
Ville Syrjälä54433e92015-05-26 20:42:31 +03001603 mutex_unlock(&dev_priv->sb_lock);
1604
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001605 /*
1606 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1607 */
1608 udelay(1);
1609
1610 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001611 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001612
1613 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001614 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001615 DRM_ERROR("PLL %d failed to lock\n", pipe);
1616
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001617 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001618 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001619 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001620}
1621
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001622static int intel_num_dvo_pipes(struct drm_device *dev)
1623{
1624 struct intel_crtc *crtc;
1625 int count = 0;
1626
1627 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001628 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001629 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001630
1631 return count;
1632}
1633
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001634static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001635{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636 struct drm_device *dev = crtc->base.dev;
1637 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001638 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001639 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001640
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001641 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642
1643 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001644 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001645
1646 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 if (IS_MOBILE(dev) && !IS_I830(dev))
1648 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001649
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001650 /* Enable DVO 2x clock on both PLLs if necessary */
1651 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1652 /*
1653 * It appears to be important that we don't enable this
1654 * for the current pipe before otherwise configuring the
1655 * PLL. No idea how this should be handled if multiple
1656 * DVO outputs are enabled simultaneosly.
1657 */
1658 dpll |= DPLL_DVO_2X_MODE;
1659 I915_WRITE(DPLL(!crtc->pipe),
1660 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1661 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001662
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001663 /*
1664 * Apparently we need to have VGA mode enabled prior to changing
1665 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1666 * dividers, even though the register value does change.
1667 */
1668 I915_WRITE(reg, 0);
1669
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001670 I915_WRITE(reg, dpll);
1671
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001672 /* Wait for the clocks to stabilize. */
1673 POSTING_READ(reg);
1674 udelay(150);
1675
1676 if (INTEL_INFO(dev)->gen >= 4) {
1677 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001678 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001679 } else {
1680 /* The pixel multiplier can only be updated once the
1681 * DPLL is enabled and the clocks are stable.
1682 *
1683 * So write it again.
1684 */
1685 I915_WRITE(reg, dpll);
1686 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001687
1688 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001689 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001690 POSTING_READ(reg);
1691 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001692 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001693 POSTING_READ(reg);
1694 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001695 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001696 POSTING_READ(reg);
1697 udelay(150); /* wait for warmup */
1698}
1699
1700/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001701 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 * @dev_priv: i915 private structure
1703 * @pipe: pipe PLL to disable
1704 *
1705 * Disable the PLL for @pipe, making sure the pipe is off first.
1706 *
1707 * Note! This is for pre-ILK only.
1708 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001709static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001711 struct drm_device *dev = crtc->base.dev;
1712 struct drm_i915_private *dev_priv = dev->dev_private;
1713 enum pipe pipe = crtc->pipe;
1714
1715 /* Disable DVO 2x clock on both PLLs if necessary */
1716 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001717 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001718 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001719 I915_WRITE(DPLL(PIPE_B),
1720 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1721 I915_WRITE(DPLL(PIPE_A),
1722 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1723 }
1724
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001725 /* Don't disable pipe or pipe PLLs if needed */
1726 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1727 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001728 return;
1729
1730 /* Make sure the pipe isn't still relying on us */
1731 assert_pipe_disabled(dev_priv, pipe);
1732
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001733 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001734 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001735}
1736
Jesse Barnesf6071162013-10-01 10:41:38 -07001737static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1738{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001739 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001740
1741 /* Make sure the pipe isn't still relying on us */
1742 assert_pipe_disabled(dev_priv, pipe);
1743
Imre Deake5cbfbf2014-01-09 17:08:16 +02001744 /*
1745 * Leave integrated clock source and reference clock enabled for pipe B.
1746 * The latter is needed for VGA hotplug / manual detection.
1747 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001748 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001749 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001750 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001751 I915_WRITE(DPLL(pipe), val);
1752 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001753
1754}
1755
1756static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001758 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001759 u32 val;
1760
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001761 /* Make sure the pipe isn't still relying on us */
1762 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001763
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001764 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001765 val = DPLL_SSC_REF_CLK_CHV |
1766 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001767 if (pipe != PIPE_A)
1768 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1769 I915_WRITE(DPLL(pipe), val);
1770 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001771
Ville Syrjäläa5805162015-05-26 20:42:30 +03001772 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001773
1774 /* Disable 10bit clock to display controller */
1775 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1776 val &= ~DPIO_DCLKP_EN;
1777 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1778
Ville Syrjäläa5805162015-05-26 20:42:30 +03001779 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001780}
1781
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001782void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001783 struct intel_digital_port *dport,
1784 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001785{
1786 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001787 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001788
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001789 switch (dport->port) {
1790 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001791 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001792 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001793 break;
1794 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001795 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001796 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001797 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001798 break;
1799 case PORT_D:
1800 port_mask = DPLL_PORTD_READY_MASK;
1801 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001802 break;
1803 default:
1804 BUG();
1805 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001806
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001807 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1808 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1809 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001810}
1811
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001812static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1813 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001814{
Daniel Vetter23670b322012-11-01 09:15:30 +01001815 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001816 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001818 i915_reg_t reg;
1819 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001820
1821 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001822 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001823
1824 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001825 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001826
1827 /* FDI must be feeding us bits for PCH ports */
1828 assert_fdi_tx_enabled(dev_priv, pipe);
1829 assert_fdi_rx_enabled(dev_priv, pipe);
1830
Daniel Vetter23670b322012-11-01 09:15:30 +01001831 if (HAS_PCH_CPT(dev)) {
1832 /* Workaround: Set the timing override bit before enabling the
1833 * pch transcoder. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001838 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001839
Daniel Vetterab9412b2013-05-03 11:49:46 +02001840 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001841 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001842 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001843
1844 if (HAS_PCH_IBX(dev_priv->dev)) {
1845 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001846 * Make the BPC in transcoder be consistent with
1847 * that in pipeconf reg. For HDMI we must use 8bpc
1848 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001849 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001850 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001851 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1852 val |= PIPECONF_8BPC;
1853 else
1854 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001855 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001856
1857 val &= ~TRANS_INTERLACE_MASK;
1858 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001859 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001860 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001861 val |= TRANS_LEGACY_INTERLACED_ILK;
1862 else
1863 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001864 else
1865 val |= TRANS_PROGRESSIVE;
1866
Jesse Barnes040484a2011-01-03 12:14:26 -08001867 I915_WRITE(reg, val | TRANS_ENABLE);
1868 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001869 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001870}
1871
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001872static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001873 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001874{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001876
1877 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001878 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001879
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001880 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001881 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001882 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001883
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001884 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001885 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001886 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001887 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001888
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001889 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001890 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001891
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001892 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1893 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001894 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001895 else
1896 val |= TRANS_PROGRESSIVE;
1897
Daniel Vetterab9412b2013-05-03 11:49:46 +02001898 I915_WRITE(LPT_TRANSCONF, val);
1899 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001900 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001901}
1902
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001903static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1904 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001905{
Daniel Vetter23670b322012-11-01 09:15:30 +01001906 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001907 i915_reg_t reg;
1908 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001909
1910 /* FDI relies on the transcoder */
1911 assert_fdi_tx_disabled(dev_priv, pipe);
1912 assert_fdi_rx_disabled(dev_priv, pipe);
1913
Jesse Barnes291906f2011-02-02 12:28:03 -08001914 /* Ports must be off as well */
1915 assert_pch_ports_disabled(dev_priv, pipe);
1916
Daniel Vetterab9412b2013-05-03 11:49:46 +02001917 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001918 val = I915_READ(reg);
1919 val &= ~TRANS_ENABLE;
1920 I915_WRITE(reg, val);
1921 /* wait for PCH transcoder off, transcoder state */
1922 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001923 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924
Ville Syrjäläc4656132015-10-29 21:25:56 +02001925 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001926 /* Workaround: Clear the timing override chicken bit again. */
1927 reg = TRANS_CHICKEN2(pipe);
1928 val = I915_READ(reg);
1929 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1930 I915_WRITE(reg, val);
1931 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001932}
1933
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001934static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001935{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936 u32 val;
1937
Daniel Vetterab9412b2013-05-03 11:49:46 +02001938 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001939 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001941 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001942 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001943 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001944
1945 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001946 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001947 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001948 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001949}
1950
1951/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001952 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001953 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001955 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001958static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001959{
Paulo Zanoni03722642014-01-17 13:51:09 -02001960 struct drm_device *dev = crtc->base.dev;
1961 struct drm_i915_private *dev_priv = dev->dev_private;
1962 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001963 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001964 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001965 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001966 u32 val;
1967
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001968 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1969
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001970 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001971 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001972 assert_sprites_disabled(dev_priv, pipe);
1973
Paulo Zanoni681e5812012-12-06 11:12:38 -02001974 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001975 pch_transcoder = TRANSCODER_A;
1976 else
1977 pch_transcoder = pipe;
1978
Jesse Barnesb24e7172011-01-04 15:09:30 -08001979 /*
1980 * A pipe without a PLL won't actually be able to drive bits from
1981 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1982 * need the check.
1983 */
Imre Deak50360402015-01-16 00:55:16 -08001984 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001985 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001986 assert_dsi_pll_enabled(dev_priv);
1987 else
1988 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001989 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001990 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001991 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001992 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001993 assert_fdi_tx_pll_enabled(dev_priv,
1994 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001995 }
1996 /* FIXME: assert CPU port conditions for SNB+ */
1997 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001998
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001999 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002000 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002001 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002002 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2003 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002004 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002005 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002006
2007 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002008 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002009
2010 /*
2011 * Until the pipe starts DSL will read as 0, which would cause
2012 * an apparent vblank timestamp jump, which messes up also the
2013 * frame count when it's derived from the timestamps. So let's
2014 * wait for the pipe to start properly before we call
2015 * drm_crtc_vblank_on()
2016 */
2017 if (dev->max_vblank_count == 0 &&
2018 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2019 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020}
2021
2022/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002023 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002024 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002025 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002026 * Disable the pipe of @crtc, making sure that various hardware
2027 * specific requirements are met, if applicable, e.g. plane
2028 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 *
2030 * Will wait until the pipe has shut down before returning.
2031 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002032static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002034 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002035 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002036 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002037 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038 u32 val;
2039
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002040 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2041
Jesse Barnesb24e7172011-01-04 15:09:30 -08002042 /*
2043 * Make sure planes won't keep trying to pump pixels to us,
2044 * or we might hang the display.
2045 */
2046 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002047 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002048 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002050 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002052 if ((val & PIPECONF_ENABLE) == 0)
2053 return;
2054
Ville Syrjälä67adc642014-08-15 01:21:57 +03002055 /*
2056 * Double wide has implications for planes
2057 * so best keep it disabled when not needed.
2058 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002059 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002060 val &= ~PIPECONF_DOUBLE_WIDE;
2061
2062 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002063 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2064 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002065 val &= ~PIPECONF_ENABLE;
2066
2067 I915_WRITE(reg, val);
2068 if ((val & PIPECONF_ENABLE) == 0)
2069 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002070}
2071
Chris Wilson693db182013-03-05 14:52:39 +00002072static bool need_vtd_wa(struct drm_device *dev)
2073{
2074#ifdef CONFIG_INTEL_IOMMU
2075 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2076 return true;
2077#endif
2078 return false;
2079}
2080
Ville Syrjälä832be822016-01-12 21:08:33 +02002081static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2082{
2083 return IS_GEN2(dev_priv) ? 2048 : 4096;
2084}
2085
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002086static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2087 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002088{
2089 switch (fb_modifier) {
2090 case DRM_FORMAT_MOD_NONE:
2091 return cpp;
2092 case I915_FORMAT_MOD_X_TILED:
2093 if (IS_GEN2(dev_priv))
2094 return 128;
2095 else
2096 return 512;
2097 case I915_FORMAT_MOD_Y_TILED:
2098 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2099 return 128;
2100 else
2101 return 512;
2102 case I915_FORMAT_MOD_Yf_TILED:
2103 switch (cpp) {
2104 case 1:
2105 return 64;
2106 case 2:
2107 case 4:
2108 return 128;
2109 case 8:
2110 case 16:
2111 return 256;
2112 default:
2113 MISSING_CASE(cpp);
2114 return cpp;
2115 }
2116 break;
2117 default:
2118 MISSING_CASE(fb_modifier);
2119 return cpp;
2120 }
2121}
2122
Ville Syrjälä832be822016-01-12 21:08:33 +02002123unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2124 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002125{
Ville Syrjälä832be822016-01-12 21:08:33 +02002126 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2127 return 1;
2128 else
2129 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002130 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002131}
2132
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002133/* Return the tile dimensions in pixel units */
2134static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2135 unsigned int *tile_width,
2136 unsigned int *tile_height,
2137 uint64_t fb_modifier,
2138 unsigned int cpp)
2139{
2140 unsigned int tile_width_bytes =
2141 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2142
2143 *tile_width = tile_width_bytes / cpp;
2144 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2145}
2146
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002147unsigned int
2148intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002149 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002150{
Ville Syrjälä832be822016-01-12 21:08:33 +02002151 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2152 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2153
2154 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002155}
2156
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002157unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2158{
2159 unsigned int size = 0;
2160 int i;
2161
2162 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2163 size += rot_info->plane[i].width * rot_info->plane[i].height;
2164
2165 return size;
2166}
2167
Daniel Vetter75c82a52015-10-14 16:51:04 +02002168static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002169intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2170 const struct drm_framebuffer *fb,
2171 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002172{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002173 if (intel_rotation_90_or_270(rotation)) {
2174 *view = i915_ggtt_view_rotated;
2175 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2176 } else {
2177 *view = i915_ggtt_view_normal;
2178 }
2179}
2180
2181static void
2182intel_fill_fb_info(struct drm_i915_private *dev_priv,
2183 struct drm_framebuffer *fb)
2184{
2185 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002186 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002187
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002188 tile_size = intel_tile_size(dev_priv);
2189
2190 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002191 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2192 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002193
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002194 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2195 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002196
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002197 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002198 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002199 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2200 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002201
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002202 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002203 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2204 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002205 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002206}
2207
Ville Syrjälä603525d2016-01-12 21:08:37 +02002208static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002209{
2210 if (INTEL_INFO(dev_priv)->gen >= 9)
2211 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002212 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002213 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002214 return 128 * 1024;
2215 else if (INTEL_INFO(dev_priv)->gen >= 4)
2216 return 4 * 1024;
2217 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002218 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002219}
2220
Ville Syrjälä603525d2016-01-12 21:08:37 +02002221static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2222 uint64_t fb_modifier)
2223{
2224 switch (fb_modifier) {
2225 case DRM_FORMAT_MOD_NONE:
2226 return intel_linear_alignment(dev_priv);
2227 case I915_FORMAT_MOD_X_TILED:
2228 if (INTEL_INFO(dev_priv)->gen >= 9)
2229 return 256 * 1024;
2230 return 0;
2231 case I915_FORMAT_MOD_Y_TILED:
2232 case I915_FORMAT_MOD_Yf_TILED:
2233 return 1 * 1024 * 1024;
2234 default:
2235 MISSING_CASE(fb_modifier);
2236 return 0;
2237 }
2238}
2239
Chris Wilson127bd2a2010-07-23 23:32:05 +01002240int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002241intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2242 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002243{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002244 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002245 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002246 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002247 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002248 u32 alignment;
2249 int ret;
2250
Matt Roperebcdd392014-07-09 16:22:11 -07002251 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2252
Ville Syrjälä603525d2016-01-12 21:08:37 +02002253 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002254
Ville Syrjälä3465c582016-02-15 22:54:43 +02002255 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002256
Chris Wilson693db182013-03-05 14:52:39 +00002257 /* Note that the w/a also requires 64 PTE of padding following the
2258 * bo. We currently fill all unused PTE with the shadow page and so
2259 * we should always have valid PTE following the scanout preventing
2260 * the VT-d warning.
2261 */
2262 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2263 alignment = 256 * 1024;
2264
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002265 /*
2266 * Global gtt pte registers are special registers which actually forward
2267 * writes to a chunk of system memory. Which means that there is no risk
2268 * that the register values disappear as soon as we call
2269 * intel_runtime_pm_put(), so it is correct to wrap only the
2270 * pin/unpin/fence and not more.
2271 */
2272 intel_runtime_pm_get(dev_priv);
2273
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002274 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2275 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002276 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002277 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002278
2279 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2280 * fence, whereas 965+ only requires a fence if using
2281 * framebuffer compression. For simplicity, we always install
2282 * a fence as the cost is not that onerous.
2283 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002284 if (view.type == I915_GGTT_VIEW_NORMAL) {
2285 ret = i915_gem_object_get_fence(obj);
2286 if (ret == -EDEADLK) {
2287 /*
2288 * -EDEADLK means there are no free fences
2289 * no pending flips.
2290 *
2291 * This is propagated to atomic, but it uses
2292 * -EDEADLK to force a locking recovery, so
2293 * change the returned error to -EBUSY.
2294 */
2295 ret = -EBUSY;
2296 goto err_unpin;
2297 } else if (ret)
2298 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002299
Vivek Kasireddy98072162015-10-29 18:54:38 -07002300 i915_gem_object_pin_fence(obj);
2301 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002302
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002303 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002304 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002305
2306err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002307 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002308err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002309 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002310 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002311}
2312
Ville Syrjälä3465c582016-02-15 22:54:43 +02002313static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002314{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002315 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002316 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002317
Matt Roperebcdd392014-07-09 16:22:11 -07002318 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2319
Ville Syrjälä3465c582016-02-15 22:54:43 +02002320 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002321
Vivek Kasireddy98072162015-10-29 18:54:38 -07002322 if (view.type == I915_GGTT_VIEW_NORMAL)
2323 i915_gem_object_unpin_fence(obj);
2324
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002325 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002326}
2327
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002328/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002329 * Adjust the tile offset by moving the difference into
2330 * the x/y offsets.
2331 *
2332 * Input tile dimensions and pitch must already be
2333 * rotated to match x and y, and in pixel units.
2334 */
2335static u32 intel_adjust_tile_offset(int *x, int *y,
2336 unsigned int tile_width,
2337 unsigned int tile_height,
2338 unsigned int tile_size,
2339 unsigned int pitch_tiles,
2340 u32 old_offset,
2341 u32 new_offset)
2342{
2343 unsigned int tiles;
2344
2345 WARN_ON(old_offset & (tile_size - 1));
2346 WARN_ON(new_offset & (tile_size - 1));
2347 WARN_ON(new_offset > old_offset);
2348
2349 tiles = (old_offset - new_offset) / tile_size;
2350
2351 *y += tiles / pitch_tiles * tile_height;
2352 *x += tiles % pitch_tiles * tile_width;
2353
2354 return new_offset;
2355}
2356
2357/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002358 * Computes the linear offset to the base tile and adjusts
2359 * x, y. bytes per pixel is assumed to be a power-of-two.
2360 *
2361 * In the 90/270 rotated case, x and y are assumed
2362 * to be already rotated to match the rotated GTT view, and
2363 * pitch is the tile_height aligned framebuffer height.
2364 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002365u32 intel_compute_tile_offset(int *x, int *y,
2366 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002367 unsigned int pitch,
2368 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002369{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002370 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2371 uint64_t fb_modifier = fb->modifier[plane];
2372 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002373 u32 offset, offset_aligned, alignment;
2374
2375 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2376 if (alignment)
2377 alignment--;
2378
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002379 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002380 unsigned int tile_size, tile_width, tile_height;
2381 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002382
Ville Syrjäläd8433102016-01-12 21:08:35 +02002383 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002384 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2385 fb_modifier, cpp);
2386
2387 if (intel_rotation_90_or_270(rotation)) {
2388 pitch_tiles = pitch / tile_height;
2389 swap(tile_width, tile_height);
2390 } else {
2391 pitch_tiles = pitch / (tile_width * cpp);
2392 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002393
Ville Syrjäläd8433102016-01-12 21:08:35 +02002394 tile_rows = *y / tile_height;
2395 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002396
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002397 tiles = *x / tile_width;
2398 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002399
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002400 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2401 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002402
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002403 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2404 tile_size, pitch_tiles,
2405 offset, offset_aligned);
2406 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002407 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002408 offset_aligned = offset & ~alignment;
2409
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002410 *y = (offset & alignment) / pitch;
2411 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002412 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002413
2414 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002415}
2416
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002417static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002418{
2419 switch (format) {
2420 case DISPPLANE_8BPP:
2421 return DRM_FORMAT_C8;
2422 case DISPPLANE_BGRX555:
2423 return DRM_FORMAT_XRGB1555;
2424 case DISPPLANE_BGRX565:
2425 return DRM_FORMAT_RGB565;
2426 default:
2427 case DISPPLANE_BGRX888:
2428 return DRM_FORMAT_XRGB8888;
2429 case DISPPLANE_RGBX888:
2430 return DRM_FORMAT_XBGR8888;
2431 case DISPPLANE_BGRX101010:
2432 return DRM_FORMAT_XRGB2101010;
2433 case DISPPLANE_RGBX101010:
2434 return DRM_FORMAT_XBGR2101010;
2435 }
2436}
2437
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002438static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2439{
2440 switch (format) {
2441 case PLANE_CTL_FORMAT_RGB_565:
2442 return DRM_FORMAT_RGB565;
2443 default:
2444 case PLANE_CTL_FORMAT_XRGB_8888:
2445 if (rgb_order) {
2446 if (alpha)
2447 return DRM_FORMAT_ABGR8888;
2448 else
2449 return DRM_FORMAT_XBGR8888;
2450 } else {
2451 if (alpha)
2452 return DRM_FORMAT_ARGB8888;
2453 else
2454 return DRM_FORMAT_XRGB8888;
2455 }
2456 case PLANE_CTL_FORMAT_XRGB_2101010:
2457 if (rgb_order)
2458 return DRM_FORMAT_XBGR2101010;
2459 else
2460 return DRM_FORMAT_XRGB2101010;
2461 }
2462}
2463
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002464static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002465intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2466 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002467{
2468 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002469 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470 struct drm_i915_gem_object *obj = NULL;
2471 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002472 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002473 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2474 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2475 PAGE_SIZE);
2476
2477 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Chris Wilsonff2652e2014-03-10 08:07:02 +00002479 if (plane_config->size == 0)
2480 return false;
2481
Paulo Zanoni3badb492015-09-23 12:52:23 -03002482 /* If the FB is too big, just don't use it since fbdev is not very
2483 * important and we should probably use that space with FBC or other
2484 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002485 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002486 return false;
2487
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002488 mutex_lock(&dev->struct_mutex);
2489
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002490 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2491 base_aligned,
2492 base_aligned,
2493 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002494 if (!obj) {
2495 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002496 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002497 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002498
Damien Lespiau49af4492015-01-20 12:51:44 +00002499 obj->tiling_mode = plane_config->tiling;
2500 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002501 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002503 mode_cmd.pixel_format = fb->pixel_format;
2504 mode_cmd.width = fb->width;
2505 mode_cmd.height = fb->height;
2506 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002507 mode_cmd.modifier[0] = fb->modifier[0];
2508 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002509
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002510 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002511 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512 DRM_DEBUG_KMS("intel fb init failed\n");
2513 goto out_unref_obj;
2514 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002515
Jesse Barnes46f297f2014-03-07 08:57:48 -08002516 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002517
Daniel Vetterf6936e22015-03-26 12:17:05 +01002518 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520
2521out_unref_obj:
2522 drm_gem_object_unreference(&obj->base);
2523 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002524 return false;
2525}
2526
Matt Roperafd65eb2015-02-03 13:10:04 -08002527/* Update plane->state->fb to match plane->fb after driver-internal updates */
2528static void
2529update_state_fb(struct drm_plane *plane)
2530{
2531 if (plane->fb == plane->state->fb)
2532 return;
2533
2534 if (plane->state->fb)
2535 drm_framebuffer_unreference(plane->state->fb);
2536 plane->state->fb = plane->fb;
2537 if (plane->state->fb)
2538 drm_framebuffer_reference(plane->state->fb);
2539}
2540
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002541static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002542intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2543 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544{
2545 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002546 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002547 struct drm_crtc *c;
2548 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002549 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002551 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002552 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2553 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002554 struct intel_plane_state *intel_state =
2555 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557
Damien Lespiau2d140302015-02-05 17:22:18 +00002558 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return;
2560
Daniel Vetterf6936e22015-03-26 12:17:05 +01002561 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002562 fb = &plane_config->fb->base;
2563 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002564 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565
Damien Lespiau2d140302015-02-05 17:22:18 +00002566 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567
2568 /*
2569 * Failed to alloc the obj, check to see if we should share
2570 * an fb with another CRTC instead
2571 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002572 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002573 i = to_intel_crtc(c);
2574
2575 if (c == &intel_crtc->base)
2576 continue;
2577
Matt Roper2ff8fde2014-07-08 07:50:07 -07002578 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 continue;
2580
Daniel Vetter88595ac2015-03-26 12:42:24 +01002581 fb = c->primary->fb;
2582 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 continue;
2584
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002586 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 drm_framebuffer_reference(fb);
2588 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589 }
2590 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591
Matt Roper200757f2015-12-03 11:37:36 -08002592 /*
2593 * We've failed to reconstruct the BIOS FB. Current display state
2594 * indicates that the primary plane is visible, but has a NULL FB,
2595 * which will lead to problems later if we don't fix it up. The
2596 * simplest solution is to just disable the primary plane now and
2597 * pretend the BIOS never had it enabled.
2598 */
2599 to_intel_plane_state(plane_state)->visible = false;
2600 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002601 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002602 intel_plane->disable_plane(primary, &intel_crtc->base);
2603
Daniel Vetter88595ac2015-03-26 12:42:24 +01002604 return;
2605
2606valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002607 plane_state->src_x = 0;
2608 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002609 plane_state->src_w = fb->width << 16;
2610 plane_state->src_h = fb->height << 16;
2611
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002612 plane_state->crtc_x = 0;
2613 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002614 plane_state->crtc_w = fb->width;
2615 plane_state->crtc_h = fb->height;
2616
Matt Roper0a8d8a82015-12-03 11:37:38 -08002617 intel_state->src.x1 = plane_state->src_x;
2618 intel_state->src.y1 = plane_state->src_y;
2619 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2620 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2621 intel_state->dst.x1 = plane_state->crtc_x;
2622 intel_state->dst.y1 = plane_state->crtc_y;
2623 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2624 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2625
Daniel Vetter88595ac2015-03-26 12:42:24 +01002626 obj = intel_fb_obj(fb);
2627 if (obj->tiling_mode != I915_TILING_NONE)
2628 dev_priv->preserve_bios_swizzle = true;
2629
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002630 drm_framebuffer_reference(fb);
2631 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002632 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002633 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002634 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002635}
2636
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002637static void i9xx_update_primary_plane(struct drm_plane *primary,
2638 const struct intel_crtc_state *crtc_state,
2639 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002640{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002642 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2644 struct drm_framebuffer *fb = plane_state->base.fb;
2645 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002646 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002647 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002648 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002649 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002650 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002651 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002652 int x = plane_state->src.x1 >> 16;
2653 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002654
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002655 dspcntr = DISPPLANE_GAMMA_ENABLE;
2656
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002657 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002658
2659 if (INTEL_INFO(dev)->gen < 4) {
2660 if (intel_crtc->pipe == PIPE_B)
2661 dspcntr |= DISPPLANE_SEL_PIPE_B;
2662
2663 /* pipesrc and dspsize control the size that is scaled from,
2664 * which should always be the user's requested size.
2665 */
2666 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002667 ((crtc_state->pipe_src_h - 1) << 16) |
2668 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002669 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002670 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2671 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002672 ((crtc_state->pipe_src_h - 1) << 16) |
2673 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002674 I915_WRITE(PRIMPOS(plane), 0);
2675 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 }
2677
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 switch (fb->pixel_format) {
2679 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002680 dspcntr |= DISPPLANE_8BPP;
2681 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002684 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 case DRM_FORMAT_RGB565:
2686 dspcntr |= DISPPLANE_BGRX565;
2687 break;
2688 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002689 dspcntr |= DISPPLANE_BGRX888;
2690 break;
2691 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002692 dspcntr |= DISPPLANE_RGBX888;
2693 break;
2694 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_BGRX101010;
2696 break;
2697 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002699 break;
2700 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002701 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002702 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002704 if (INTEL_INFO(dev)->gen >= 4 &&
2705 obj->tiling_mode != I915_TILING_NONE)
2706 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002707
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002708 if (IS_G4X(dev))
2709 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2710
Ville Syrjäläac484962016-01-20 21:05:26 +02002711 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002712
Daniel Vetterc2c75132012-07-05 12:17:30 +02002713 if (INTEL_INFO(dev)->gen >= 4) {
2714 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002715 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002716 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002717 linear_offset -= intel_crtc->dspaddr_offset;
2718 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002719 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002720 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002722 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302723 dspcntr |= DISPPLANE_ROTATE_180;
2724
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002725 x += (crtc_state->pipe_src_w - 1);
2726 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302727
2728 /* Finding the last pixel of the last line of the display
2729 data and adding to linear_offset*/
2730 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002731 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002732 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302733 }
2734
Paulo Zanoni2db33662015-09-14 15:20:03 -03002735 intel_crtc->adjusted_x = x;
2736 intel_crtc->adjusted_y = y;
2737
Sonika Jindal48404c12014-08-22 14:06:04 +05302738 I915_WRITE(reg, dspcntr);
2739
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002740 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002741 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002742 I915_WRITE(DSPSURF(plane),
2743 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002744 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002745 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002746 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002747 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002749}
2750
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751static void i9xx_disable_primary_plane(struct drm_plane *primary,
2752 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753{
2754 struct drm_device *dev = crtc->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002757 int plane = intel_crtc->plane;
2758
2759 I915_WRITE(DSPCNTR(plane), 0);
2760 if (INTEL_INFO(dev_priv)->gen >= 4)
2761 I915_WRITE(DSPSURF(plane), 0);
2762 else
2763 I915_WRITE(DSPADDR(plane), 0);
2764 POSTING_READ(DSPCNTR(plane));
2765}
2766
2767static void ironlake_update_primary_plane(struct drm_plane *primary,
2768 const struct intel_crtc_state *crtc_state,
2769 const struct intel_plane_state *plane_state)
2770{
2771 struct drm_device *dev = primary->dev;
2772 struct drm_i915_private *dev_priv = dev->dev_private;
2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2774 struct drm_framebuffer *fb = plane_state->base.fb;
2775 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002777 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002779 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002780 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002781 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002782 int x = plane_state->src.x1 >> 16;
2783 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002785 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002786 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002787
2788 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2789 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2790
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 switch (fb->pixel_format) {
2792 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793 dspcntr |= DISPPLANE_8BPP;
2794 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002795 case DRM_FORMAT_RGB565:
2796 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002798 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002799 dspcntr |= DISPPLANE_BGRX888;
2800 break;
2801 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 dspcntr |= DISPPLANE_RGBX888;
2803 break;
2804 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_BGRX101010;
2806 break;
2807 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002809 break;
2810 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002811 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 }
2813
2814 if (obj->tiling_mode != I915_TILING_NONE)
2815 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002817 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002818 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819
Ville Syrjäläac484962016-01-20 21:05:26 +02002820 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002821 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002822 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002823 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002824 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002825 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302826 dspcntr |= DISPPLANE_ROTATE_180;
2827
2828 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002829 x += (crtc_state->pipe_src_w - 1);
2830 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302831
2832 /* Finding the last pixel of the last line of the display
2833 data and adding to linear_offset*/
2834 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002835 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002836 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302837 }
2838 }
2839
Paulo Zanoni2db33662015-09-14 15:20:03 -03002840 intel_crtc->adjusted_x = x;
2841 intel_crtc->adjusted_y = y;
2842
Sonika Jindal48404c12014-08-22 14:06:04 +05302843 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002844
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002845 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002846 I915_WRITE(DSPSURF(plane),
2847 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002848 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002849 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2850 } else {
2851 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2852 I915_WRITE(DSPLINOFF(plane), linear_offset);
2853 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002854 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002855}
2856
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002857u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2858 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002859{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002860 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2861 return 64;
2862 } else {
2863 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002864
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002865 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002866 }
2867}
2868
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002869u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2870 struct drm_i915_gem_object *obj,
2871 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002872{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002873 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002874 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002875 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002876
Ville Syrjäläe7941292016-01-19 18:23:17 +02002877 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002878 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002879
Daniel Vetterce7f1722015-10-14 16:51:06 +02002880 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002881 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002882 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002883 return -1;
2884
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002885 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002886
2887 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002888 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002889 PAGE_SIZE;
2890 }
2891
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002892 WARN_ON(upper_32_bits(offset));
2893
2894 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002895}
2896
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002897static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2898{
2899 struct drm_device *dev = intel_crtc->base.dev;
2900 struct drm_i915_private *dev_priv = dev->dev_private;
2901
2902 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2903 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2904 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002905}
2906
Chandra Kondurua1b22782015-04-07 15:28:45 -07002907/*
2908 * This function detaches (aka. unbinds) unused scalers in hardware
2909 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002910static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002912 struct intel_crtc_scaler_state *scaler_state;
2913 int i;
2914
Chandra Kondurua1b22782015-04-07 15:28:45 -07002915 scaler_state = &intel_crtc->config->scaler_state;
2916
2917 /* loop through and disable scalers that aren't in use */
2918 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002919 if (!scaler_state->scalers[i].in_use)
2920 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002921 }
2922}
2923
Chandra Konduru6156a452015-04-27 13:48:39 -07002924u32 skl_plane_ctl_format(uint32_t pixel_format)
2925{
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002927 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 /*
2936 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2937 * to be already pre-multiplied. We need to add a knob (or a different
2938 * DRM_FORMAT) for user-space to configure that.
2939 */
2940 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002959 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002961
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963}
2964
2965u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2966{
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 switch (fb_modifier) {
2968 case DRM_FORMAT_MOD_NONE:
2969 break;
2970 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 default:
2977 MISSING_CASE(fb_modifier);
2978 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002979
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981}
2982
2983u32 skl_plane_ctl_rotation(unsigned int rotation)
2984{
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 switch (rotation) {
2986 case BIT(DRM_ROTATE_0):
2987 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302988 /*
2989 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2990 * while i915 HW rotation is clockwise, thats why this swapping.
2991 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302993 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302997 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 default:
2999 MISSING_CASE(rotation);
3000 }
3001
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003002 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003003}
3004
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003005static void skylake_update_primary_plane(struct drm_plane *plane,
3006 const struct intel_crtc_state *crtc_state,
3007 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003008{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003010 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3012 struct drm_framebuffer *fb = plane_state->base.fb;
3013 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303015 u32 plane_ctl, stride_div, stride;
3016 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003017 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303018 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003019 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 int scaler_id = plane_state->scaler_id;
3021 int src_x = plane_state->src.x1 >> 16;
3022 int src_y = plane_state->src.y1 >> 16;
3023 int src_w = drm_rect_width(&plane_state->src) >> 16;
3024 int src_h = drm_rect_height(&plane_state->src) >> 16;
3025 int dst_x = plane_state->dst.x1;
3026 int dst_y = plane_state->dst.y1;
3027 int dst_w = drm_rect_width(&plane_state->dst);
3028 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003029
3030 plane_ctl = PLANE_CTL_ENABLE |
3031 PLANE_CTL_PIPE_GAMMA_ENABLE |
3032 PLANE_CTL_PIPE_CSC_ENABLE;
3033
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3035 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003036 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003037 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003038
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003039 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003040 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003041 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003043 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003044
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303045 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003046 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3047
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003049 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303050 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003051 x_offset = stride * tile_height - src_y - src_h;
3052 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003053 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054 } else {
3055 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003056 x_offset = src_x;
3057 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003058 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303059 }
3060 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003061
Paulo Zanoni2db33662015-09-14 15:20:03 -03003062 intel_crtc->adjusted_x = x_offset;
3063 intel_crtc->adjusted_y = y_offset;
3064
Damien Lespiau70d21f02013-07-03 21:06:04 +01003065 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3067 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3068 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003069
3070 if (scaler_id >= 0) {
3071 uint32_t ps_ctrl = 0;
3072
3073 WARN_ON(!dst_w || !dst_h);
3074 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3075 crtc_state->scaler_state.scalers[scaler_id].mode;
3076 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3077 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3078 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3079 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3080 I915_WRITE(PLANE_POS(pipe, 0), 0);
3081 } else {
3082 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3083 }
3084
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003085 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003086
3087 POSTING_READ(PLANE_SURF(pipe, 0));
3088}
3089
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003090static void skylake_disable_primary_plane(struct drm_plane *primary,
3091 struct drm_crtc *crtc)
3092{
3093 struct drm_device *dev = crtc->dev;
3094 struct drm_i915_private *dev_priv = dev->dev_private;
3095 int pipe = to_intel_crtc(crtc)->pipe;
3096
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003097 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3098 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3099 POSTING_READ(PLANE_SURF(pipe, 0));
3100}
3101
Jesse Barnes17638cd2011-06-24 12:19:23 -07003102/* Assume fb object is pinned & idle & fenced and just update base pointers */
3103static int
3104intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3105 int x, int y, enum mode_set_atomic state)
3106{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003107 /* Support for kgdboc is disabled, this needs a major rework. */
3108 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003109
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003111}
3112
Ville Syrjälä75147472014-11-24 18:28:11 +02003113static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003114{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003115 struct drm_crtc *crtc;
3116
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003117 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3119 enum plane plane = intel_crtc->plane;
3120
3121 intel_prepare_page_flip(dev, plane);
3122 intel_finish_page_flip_plane(dev, plane);
3123 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003124}
3125
3126static void intel_update_primary_planes(struct drm_device *dev)
3127{
Ville Syrjälä75147472014-11-24 18:28:11 +02003128 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003129
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003130 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003131 struct intel_plane *plane = to_intel_plane(crtc->primary);
3132 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003133
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003134 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003135 plane_state = to_intel_plane_state(plane->base.state);
3136
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003137 if (plane_state->visible)
3138 plane->update_plane(&plane->base,
3139 to_intel_crtc_state(crtc->state),
3140 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003141
3142 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143 }
3144}
3145
Ville Syrjälä75147472014-11-24 18:28:11 +02003146void intel_prepare_reset(struct drm_device *dev)
3147{
3148 /* no reset support for gen2 */
3149 if (IS_GEN2(dev))
3150 return;
3151
3152 /* reset doesn't touch the display */
3153 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3154 return;
3155
3156 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003157 /*
3158 * Disabling the crtcs gracefully seems nicer. Also the
3159 * g33 docs say we should at least disable all the planes.
3160 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003161 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003162}
3163
3164void intel_finish_reset(struct drm_device *dev)
3165{
3166 struct drm_i915_private *dev_priv = to_i915(dev);
3167
3168 /*
3169 * Flips in the rings will be nuked by the reset,
3170 * so complete all pending flips so that user space
3171 * will get its events and not get stuck.
3172 */
3173 intel_complete_page_flips(dev);
3174
3175 /* no reset support for gen2 */
3176 if (IS_GEN2(dev))
3177 return;
3178
3179 /* reset doesn't touch the display */
3180 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3181 /*
3182 * Flips in the rings have been nuked by the reset,
3183 * so update the base address of all primary
3184 * planes to the the last fb to make sure we're
3185 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003186 *
3187 * FIXME: Atomic will make this obsolete since we won't schedule
3188 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003189 */
3190 intel_update_primary_planes(dev);
3191 return;
3192 }
3193
3194 /*
3195 * The display has been reset as well,
3196 * so need a full re-initialization.
3197 */
3198 intel_runtime_pm_disable_interrupts(dev_priv);
3199 intel_runtime_pm_enable_interrupts(dev_priv);
3200
3201 intel_modeset_init_hw(dev);
3202
3203 spin_lock_irq(&dev_priv->irq_lock);
3204 if (dev_priv->display.hpd_irq_setup)
3205 dev_priv->display.hpd_irq_setup(dev);
3206 spin_unlock_irq(&dev_priv->irq_lock);
3207
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003208 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003209
3210 intel_hpd_init(dev_priv);
3211
3212 drm_modeset_unlock_all(dev);
3213}
3214
Chris Wilson7d5e3792014-03-04 13:15:08 +00003215static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3216{
3217 struct drm_device *dev = crtc->dev;
3218 struct drm_i915_private *dev_priv = dev->dev_private;
3219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003220 bool pending;
3221
3222 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3223 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3224 return false;
3225
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003226 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003227 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003228 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003229
3230 return pending;
3231}
3232
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003233static void intel_update_pipe_config(struct intel_crtc *crtc,
3234 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235{
3236 struct drm_device *dev = crtc->base.dev;
3237 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003238 struct intel_crtc_state *pipe_config =
3239 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003240
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003241 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3242 crtc->base.mode = crtc->base.state->mode;
3243
3244 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3245 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3246 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003247
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003248 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003249 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003250
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003251 /*
3252 * Update pipe size and adjust fitter if needed: the reason for this is
3253 * that in compute_mode_changes we check the native mode (not the pfit
3254 * mode) to see if we can flip rather than do a full mode set. In the
3255 * fastboot case, we'll flip, but if we don't update the pipesrc and
3256 * pfit state, we'll end up with a big fb scanned out into the wrong
3257 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003258 */
3259
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003260 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003261 ((pipe_config->pipe_src_w - 1) << 16) |
3262 (pipe_config->pipe_src_h - 1));
3263
3264 /* on skylake this is done by detaching scalers */
3265 if (INTEL_INFO(dev)->gen >= 9) {
3266 skl_detach_scalers(crtc);
3267
3268 if (pipe_config->pch_pfit.enabled)
3269 skylake_pfit_enable(crtc);
3270 } else if (HAS_PCH_SPLIT(dev)) {
3271 if (pipe_config->pch_pfit.enabled)
3272 ironlake_pfit_enable(crtc);
3273 else if (old_crtc_state->pch_pfit.enabled)
3274 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003275 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003276}
3277
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003278static void intel_fdi_normal_train(struct drm_crtc *crtc)
3279{
3280 struct drm_device *dev = crtc->dev;
3281 struct drm_i915_private *dev_priv = dev->dev_private;
3282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3283 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003284 i915_reg_t reg;
3285 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003286
3287 /* enable normal train */
3288 reg = FDI_TX_CTL(pipe);
3289 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003290 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003291 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3292 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003293 } else {
3294 temp &= ~FDI_LINK_TRAIN_NONE;
3295 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003296 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003297 I915_WRITE(reg, temp);
3298
3299 reg = FDI_RX_CTL(pipe);
3300 temp = I915_READ(reg);
3301 if (HAS_PCH_CPT(dev)) {
3302 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3303 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3304 } else {
3305 temp &= ~FDI_LINK_TRAIN_NONE;
3306 temp |= FDI_LINK_TRAIN_NONE;
3307 }
3308 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3309
3310 /* wait one idle pattern time */
3311 POSTING_READ(reg);
3312 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003313
3314 /* IVB wants error correction enabled */
3315 if (IS_IVYBRIDGE(dev))
3316 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3317 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003318}
3319
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003320/* The FDI link training functions for ILK/Ibexpeak. */
3321static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3322{
3323 struct drm_device *dev = crtc->dev;
3324 struct drm_i915_private *dev_priv = dev->dev_private;
3325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3326 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003327 i915_reg_t reg;
3328 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003329
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003330 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003331 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003332
Adam Jacksone1a44742010-06-25 15:32:14 -04003333 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3334 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003335 reg = FDI_RX_IMR(pipe);
3336 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003337 temp &= ~FDI_RX_SYMBOL_LOCK;
3338 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003339 I915_WRITE(reg, temp);
3340 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003341 udelay(150);
3342
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003343 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 reg = FDI_TX_CTL(pipe);
3345 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003346 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003347 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351
Chris Wilson5eddb702010-09-11 13:48:45 +01003352 reg = FDI_RX_CTL(pipe);
3353 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354 temp &= ~FDI_LINK_TRAIN_NONE;
3355 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003356 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3357
3358 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359 udelay(150);
3360
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003361 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003362 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3363 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3364 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003365
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003367 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3370
3371 if ((temp & FDI_RX_BIT_LOCK)) {
3372 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 break;
3375 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003377 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379
3380 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 reg = FDI_TX_CTL(pipe);
3382 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 temp &= ~FDI_LINK_TRAIN_NONE;
3384 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003385 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 reg = FDI_RX_CTL(pipe);
3388 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 temp &= ~FDI_LINK_TRAIN_NONE;
3390 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 I915_WRITE(reg, temp);
3392
3393 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 udelay(150);
3395
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003397 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3400
3401 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 DRM_DEBUG_KMS("FDI train 2 done.\n");
3404 break;
3405 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003407 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409
3410 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003411
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412}
3413
Akshay Joshi0206e352011-08-16 15:34:10 -04003414static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3416 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3417 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3418 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3419};
3420
3421/* The FDI link training functions for SNB/Cougarpoint. */
3422static void gen6_fdi_link_train(struct drm_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3427 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003428 i915_reg_t reg;
3429 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3432 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 reg = FDI_RX_IMR(pipe);
3434 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003435 temp &= ~FDI_RX_SYMBOL_LOCK;
3436 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 I915_WRITE(reg, temp);
3438
3439 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003440 udelay(150);
3441
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 reg = FDI_TX_CTL(pipe);
3444 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003445 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003446 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 temp &= ~FDI_LINK_TRAIN_NONE;
3448 temp |= FDI_LINK_TRAIN_PATTERN_1;
3449 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3450 /* SNB-B */
3451 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453
Daniel Vetterd74cf322012-10-26 10:58:13 +02003454 I915_WRITE(FDI_RX_MISC(pipe),
3455 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3456
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 reg = FDI_RX_CTL(pipe);
3458 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 if (HAS_PCH_CPT(dev)) {
3460 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3461 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3462 } else {
3463 temp &= ~FDI_LINK_TRAIN_NONE;
3464 temp |= FDI_LINK_TRAIN_PATTERN_1;
3465 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3467
3468 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 udelay(150);
3470
Akshay Joshi0206e352011-08-16 15:34:10 -04003471 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = FDI_TX_CTL(pipe);
3473 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3475 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp);
3477
3478 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 udelay(500);
3480
Sean Paulfa37d392012-03-02 12:53:39 -05003481 for (retry = 0; retry < 5; retry++) {
3482 reg = FDI_RX_IIR(pipe);
3483 temp = I915_READ(reg);
3484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3485 if (temp & FDI_RX_BIT_LOCK) {
3486 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3487 DRM_DEBUG_KMS("FDI train 1 done.\n");
3488 break;
3489 }
3490 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 }
Sean Paulfa37d392012-03-02 12:53:39 -05003492 if (retry < 5)
3493 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 }
3495 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003497
3498 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 reg = FDI_TX_CTL(pipe);
3500 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003501 temp &= ~FDI_LINK_TRAIN_NONE;
3502 temp |= FDI_LINK_TRAIN_PATTERN_2;
3503 if (IS_GEN6(dev)) {
3504 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3505 /* SNB-B */
3506 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3507 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 reg = FDI_RX_CTL(pipe);
3511 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 if (HAS_PCH_CPT(dev)) {
3513 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3514 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3515 } else {
3516 temp &= ~FDI_LINK_TRAIN_NONE;
3517 temp |= FDI_LINK_TRAIN_PATTERN_2;
3518 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 I915_WRITE(reg, temp);
3520
3521 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 udelay(150);
3523
Akshay Joshi0206e352011-08-16 15:34:10 -04003524 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003525 reg = FDI_TX_CTL(pipe);
3526 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3528 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 I915_WRITE(reg, temp);
3530
3531 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 udelay(500);
3533
Sean Paulfa37d392012-03-02 12:53:39 -05003534 for (retry = 0; retry < 5; retry++) {
3535 reg = FDI_RX_IIR(pipe);
3536 temp = I915_READ(reg);
3537 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3538 if (temp & FDI_RX_SYMBOL_LOCK) {
3539 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3540 DRM_DEBUG_KMS("FDI train 2 done.\n");
3541 break;
3542 }
3543 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 }
Sean Paulfa37d392012-03-02 12:53:39 -05003545 if (retry < 5)
3546 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547 }
3548 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003550
3551 DRM_DEBUG_KMS("FDI train done.\n");
3552}
3553
Jesse Barnes357555c2011-04-28 15:09:55 -07003554/* Manual link training for Ivy Bridge A0 parts */
3555static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3556{
3557 struct drm_device *dev = crtc->dev;
3558 struct drm_i915_private *dev_priv = dev->dev_private;
3559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3560 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003561 i915_reg_t reg;
3562 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003563
3564 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3565 for train result */
3566 reg = FDI_RX_IMR(pipe);
3567 temp = I915_READ(reg);
3568 temp &= ~FDI_RX_SYMBOL_LOCK;
3569 temp &= ~FDI_RX_BIT_LOCK;
3570 I915_WRITE(reg, temp);
3571
3572 POSTING_READ(reg);
3573 udelay(150);
3574
Daniel Vetter01a415f2012-10-27 15:58:40 +02003575 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3576 I915_READ(FDI_RX_IIR(pipe)));
3577
Jesse Barnes139ccd32013-08-19 11:04:55 -07003578 /* Try each vswing and preemphasis setting twice before moving on */
3579 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3580 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003581 reg = FDI_TX_CTL(pipe);
3582 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003583 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3584 temp &= ~FDI_TX_ENABLE;
3585 I915_WRITE(reg, temp);
3586
3587 reg = FDI_RX_CTL(pipe);
3588 temp = I915_READ(reg);
3589 temp &= ~FDI_LINK_TRAIN_AUTO;
3590 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3591 temp &= ~FDI_RX_ENABLE;
3592 I915_WRITE(reg, temp);
3593
3594 /* enable CPU FDI TX and PCH FDI RX */
3595 reg = FDI_TX_CTL(pipe);
3596 temp = I915_READ(reg);
3597 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003598 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003599 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003600 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003601 temp |= snb_b_fdi_train_param[j/2];
3602 temp |= FDI_COMPOSITE_SYNC;
3603 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3604
3605 I915_WRITE(FDI_RX_MISC(pipe),
3606 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3607
3608 reg = FDI_RX_CTL(pipe);
3609 temp = I915_READ(reg);
3610 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3611 temp |= FDI_COMPOSITE_SYNC;
3612 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3613
3614 POSTING_READ(reg);
3615 udelay(1); /* should be 0.5us */
3616
3617 for (i = 0; i < 4; i++) {
3618 reg = FDI_RX_IIR(pipe);
3619 temp = I915_READ(reg);
3620 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3621
3622 if (temp & FDI_RX_BIT_LOCK ||
3623 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3624 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3625 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3626 i);
3627 break;
3628 }
3629 udelay(1); /* should be 0.5us */
3630 }
3631 if (i == 4) {
3632 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3633 continue;
3634 }
3635
3636 /* Train 2 */
3637 reg = FDI_TX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3640 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3641 I915_WRITE(reg, temp);
3642
3643 reg = FDI_RX_CTL(pipe);
3644 temp = I915_READ(reg);
3645 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3646 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003647 I915_WRITE(reg, temp);
3648
3649 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003651
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 for (i = 0; i < 4; i++) {
3653 reg = FDI_RX_IIR(pipe);
3654 temp = I915_READ(reg);
3655 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003656
Jesse Barnes139ccd32013-08-19 11:04:55 -07003657 if (temp & FDI_RX_SYMBOL_LOCK ||
3658 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3659 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3660 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3661 i);
3662 goto train_done;
3663 }
3664 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003665 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003666 if (i == 4)
3667 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003668 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003669
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003671 DRM_DEBUG_KMS("FDI train done.\n");
3672}
3673
Daniel Vetter88cefb62012-08-12 19:27:14 +02003674static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003675{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003676 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003677 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003678 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003679 i915_reg_t reg;
3680 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003681
Jesse Barnes0e23b992010-09-10 11:10:00 -07003682 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003683 reg = FDI_RX_CTL(pipe);
3684 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003685 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003686 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003687 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3689
3690 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003691 udelay(200);
3692
3693 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003694 temp = I915_READ(reg);
3695 I915_WRITE(reg, temp | FDI_PCDCLK);
3696
3697 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003698 udelay(200);
3699
Paulo Zanoni20749732012-11-23 15:30:38 -02003700 /* Enable CPU FDI TX PLL, always on for Ironlake */
3701 reg = FDI_TX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3704 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003705
Paulo Zanoni20749732012-11-23 15:30:38 -02003706 POSTING_READ(reg);
3707 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003708 }
3709}
3710
Daniel Vetter88cefb62012-08-12 19:27:14 +02003711static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3712{
3713 struct drm_device *dev = intel_crtc->base.dev;
3714 struct drm_i915_private *dev_priv = dev->dev_private;
3715 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003716 i915_reg_t reg;
3717 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003718
3719 /* Switch from PCDclk to Rawclk */
3720 reg = FDI_RX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3723
3724 /* Disable CPU FDI TX PLL */
3725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3728
3729 POSTING_READ(reg);
3730 udelay(100);
3731
3732 reg = FDI_RX_CTL(pipe);
3733 temp = I915_READ(reg);
3734 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3735
3736 /* Wait for the clocks to turn off. */
3737 POSTING_READ(reg);
3738 udelay(100);
3739}
3740
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003741static void ironlake_fdi_disable(struct drm_crtc *crtc)
3742{
3743 struct drm_device *dev = crtc->dev;
3744 struct drm_i915_private *dev_priv = dev->dev_private;
3745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3746 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003747 i915_reg_t reg;
3748 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003749
3750 /* disable CPU FDI tx and PCH FDI rx */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3754 POSTING_READ(reg);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003759 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003760 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3761
3762 POSTING_READ(reg);
3763 udelay(100);
3764
3765 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003766 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003767 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003768
3769 /* still set train pattern 1 */
3770 reg = FDI_TX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 temp &= ~FDI_LINK_TRAIN_NONE;
3773 temp |= FDI_LINK_TRAIN_PATTERN_1;
3774 I915_WRITE(reg, temp);
3775
3776 reg = FDI_RX_CTL(pipe);
3777 temp = I915_READ(reg);
3778 if (HAS_PCH_CPT(dev)) {
3779 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3780 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3781 } else {
3782 temp &= ~FDI_LINK_TRAIN_NONE;
3783 temp |= FDI_LINK_TRAIN_PATTERN_1;
3784 }
3785 /* BPC in FDI rx is consistent with that in PIPECONF */
3786 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003787 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003788 I915_WRITE(reg, temp);
3789
3790 POSTING_READ(reg);
3791 udelay(100);
3792}
3793
Chris Wilson5dce5b932014-01-20 10:17:36 +00003794bool intel_has_pending_fb_unpin(struct drm_device *dev)
3795{
3796 struct intel_crtc *crtc;
3797
3798 /* Note that we don't need to be called with mode_config.lock here
3799 * as our list of CRTC objects is static for the lifetime of the
3800 * device and so cannot disappear as we iterate. Similarly, we can
3801 * happily treat the predicates as racy, atomic checks as userspace
3802 * cannot claim and pin a new fb without at least acquring the
3803 * struct_mutex and so serialising with us.
3804 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003805 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003806 if (atomic_read(&crtc->unpin_work_count) == 0)
3807 continue;
3808
3809 if (crtc->unpin_work)
3810 intel_wait_for_vblank(dev, crtc->pipe);
3811
3812 return true;
3813 }
3814
3815 return false;
3816}
3817
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003818static void page_flip_completed(struct intel_crtc *intel_crtc)
3819{
3820 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3821 struct intel_unpin_work *work = intel_crtc->unpin_work;
3822
3823 /* ensure that the unpin work is consistent wrt ->pending. */
3824 smp_rmb();
3825 intel_crtc->unpin_work = NULL;
3826
3827 if (work->event)
3828 drm_send_vblank_event(intel_crtc->base.dev,
3829 intel_crtc->pipe,
3830 work->event);
3831
3832 drm_crtc_vblank_put(&intel_crtc->base);
3833
3834 wake_up_all(&dev_priv->pending_flip_queue);
3835 queue_work(dev_priv->wq, &work->work);
3836
3837 trace_i915_flip_complete(intel_crtc->plane,
3838 work->pending_flip_obj);
3839}
3840
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003841static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003842{
Chris Wilson0f911282012-04-17 10:05:38 +01003843 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003844 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003845 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003846
Daniel Vetter2c10d572012-12-20 21:24:07 +01003847 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003848
3849 ret = wait_event_interruptible_timeout(
3850 dev_priv->pending_flip_queue,
3851 !intel_crtc_has_pending_flip(crtc),
3852 60*HZ);
3853
3854 if (ret < 0)
3855 return ret;
3856
3857 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003859
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003860 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003861 if (intel_crtc->unpin_work) {
3862 WARN_ONCE(1, "Removing stuck page flip\n");
3863 page_flip_completed(intel_crtc);
3864 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003865 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003866 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003867
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003868 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003869}
3870
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003871static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3872{
3873 u32 temp;
3874
3875 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3876
3877 mutex_lock(&dev_priv->sb_lock);
3878
3879 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3880 temp |= SBI_SSCCTL_DISABLE;
3881 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3882
3883 mutex_unlock(&dev_priv->sb_lock);
3884}
3885
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886/* Program iCLKIP clock to the desired frequency */
3887static void lpt_program_iclkip(struct drm_crtc *crtc)
3888{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003889 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003890 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3892 u32 temp;
3893
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003894 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003895
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003896 /* The iCLK virtual clock root frequency is in MHz,
3897 * but the adjusted_mode->crtc_clock in in KHz. To get the
3898 * divisors, it is necessary to divide one by another, so we
3899 * convert the virtual clock precision to KHz here for higher
3900 * precision.
3901 */
3902 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003903 u32 iclk_virtual_root_freq = 172800 * 1000;
3904 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003905 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003907 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3908 clock << auxdiv);
3909 divsel = (desired_divisor / iclk_pi_range) - 2;
3910 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003911
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003912 /*
3913 * Near 20MHz is a corner case which is
3914 * out of range for the 7-bit divisor
3915 */
3916 if (divsel <= 0x7f)
3917 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003918 }
3919
3920 /* This should not happen with any sane values */
3921 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3922 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3923 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3924 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3925
3926 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 +03003927 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928 auxdiv,
3929 divsel,
3930 phasedir,
3931 phaseinc);
3932
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003933 mutex_lock(&dev_priv->sb_lock);
3934
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003936 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3938 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3939 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3940 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3941 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3942 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003943 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003944
3945 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3948 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003949 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950
3951 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003952 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003954 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003956 mutex_unlock(&dev_priv->sb_lock);
3957
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 /* Wait for initialization time */
3959 udelay(24);
3960
3961 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3962}
3963
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003964int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3965{
3966 u32 divsel, phaseinc, auxdiv;
3967 u32 iclk_virtual_root_freq = 172800 * 1000;
3968 u32 iclk_pi_range = 64;
3969 u32 desired_divisor;
3970 u32 temp;
3971
3972 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3973 return 0;
3974
3975 mutex_lock(&dev_priv->sb_lock);
3976
3977 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3978 if (temp & SBI_SSCCTL_DISABLE) {
3979 mutex_unlock(&dev_priv->sb_lock);
3980 return 0;
3981 }
3982
3983 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3984 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3985 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3986 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3987 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3988
3989 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3990 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3991 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3992
3993 mutex_unlock(&dev_priv->sb_lock);
3994
3995 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3996
3997 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3998 desired_divisor << auxdiv);
3999}
4000
Daniel Vetter275f01b22013-05-03 11:49:47 +02004001static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4002 enum pipe pch_transcoder)
4003{
4004 struct drm_device *dev = crtc->base.dev;
4005 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004006 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004007
4008 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4009 I915_READ(HTOTAL(cpu_transcoder)));
4010 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4011 I915_READ(HBLANK(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4013 I915_READ(HSYNC(cpu_transcoder)));
4014
4015 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4016 I915_READ(VTOTAL(cpu_transcoder)));
4017 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4018 I915_READ(VBLANK(cpu_transcoder)));
4019 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4020 I915_READ(VSYNC(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4022 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4023}
4024
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004025static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004026{
4027 struct drm_i915_private *dev_priv = dev->dev_private;
4028 uint32_t temp;
4029
4030 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004031 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004032 return;
4033
4034 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4035 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4036
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004037 temp &= ~FDI_BC_BIFURCATION_SELECT;
4038 if (enable)
4039 temp |= FDI_BC_BIFURCATION_SELECT;
4040
4041 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004042 I915_WRITE(SOUTH_CHICKEN1, temp);
4043 POSTING_READ(SOUTH_CHICKEN1);
4044}
4045
4046static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4047{
4048 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004049
4050 switch (intel_crtc->pipe) {
4051 case PIPE_A:
4052 break;
4053 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004054 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004055 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004056 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004057 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004058
4059 break;
4060 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004061 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004062
4063 break;
4064 default:
4065 BUG();
4066 }
4067}
4068
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004069/* Return which DP Port should be selected for Transcoder DP control */
4070static enum port
4071intel_trans_dp_port_sel(struct drm_crtc *crtc)
4072{
4073 struct drm_device *dev = crtc->dev;
4074 struct intel_encoder *encoder;
4075
4076 for_each_encoder_on_crtc(dev, crtc, encoder) {
4077 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4078 encoder->type == INTEL_OUTPUT_EDP)
4079 return enc_to_dig_port(&encoder->base)->port;
4080 }
4081
4082 return -1;
4083}
4084
Jesse Barnesf67a5592011-01-05 10:31:48 -08004085/*
4086 * Enable PCH resources required for PCH ports:
4087 * - PCH PLLs
4088 * - FDI training & RX/TX
4089 * - update transcoder timings
4090 * - DP transcoding bits
4091 * - transcoder
4092 */
4093static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004094{
4095 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004096 struct drm_i915_private *dev_priv = dev->dev_private;
4097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4098 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004099 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004100
Daniel Vetterab9412b2013-05-03 11:49:46 +02004101 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004102
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004103 if (IS_IVYBRIDGE(dev))
4104 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4105
Daniel Vettercd986ab2012-10-26 10:58:12 +02004106 /* Write the TU size bits before fdi link training, so that error
4107 * detection works. */
4108 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4109 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4110
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004111 /*
4112 * Sometimes spurious CPU pipe underruns happen during FDI
4113 * training, at least with VGA+HDMI cloning. Suppress them.
4114 */
4115 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4116
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004118 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004119
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004120 /* We need to program the right clock selection before writing the pixel
4121 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004122 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004123 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004124
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004126 temp |= TRANS_DPLL_ENABLE(pipe);
4127 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004128 if (intel_crtc->config->shared_dpll ==
4129 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004130 temp |= sel;
4131 else
4132 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004134 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004136 /* XXX: pch pll's can be enabled any time before we enable the PCH
4137 * transcoder, and we actually should do this to not upset any PCH
4138 * transcoder that already use the clock when we share it.
4139 *
4140 * Note that enable_shared_dpll tries to do the right thing, but
4141 * get_shared_dpll unconditionally resets the pll - we need that to have
4142 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004143 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004144
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004145 /* set transcoder timing, panel must allow it */
4146 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004147 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004148
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004149 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004150
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004151 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4152
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004154 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004155 const struct drm_display_mode *adjusted_mode =
4156 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004157 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004158 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004159 temp = I915_READ(reg);
4160 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004161 TRANS_DP_SYNC_MASK |
4162 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004163 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004164 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004166 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004167 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004168 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004169 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170
4171 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004172 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004173 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004175 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004176 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004178 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180 break;
4181 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004182 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183 }
4184
Chris Wilson5eddb702010-09-11 13:48:45 +01004185 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186 }
4187
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004188 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004189}
4190
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004191static void lpt_pch_enable(struct drm_crtc *crtc)
4192{
4193 struct drm_device *dev = crtc->dev;
4194 struct drm_i915_private *dev_priv = dev->dev_private;
4195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004196 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004197
Daniel Vetterab9412b2013-05-03 11:49:46 +02004198 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004200 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004201
Paulo Zanoni0540e482012-10-31 18:12:40 -02004202 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004203 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004204
Paulo Zanoni937bb612012-10-31 18:12:47 -02004205 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004206}
4207
Daniel Vettera1520312013-05-03 11:49:50 +02004208static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004209{
4210 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004211 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004212 u32 temp;
4213
4214 temp = I915_READ(dslreg);
4215 udelay(500);
4216 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004217 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004218 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004219 }
4220}
4221
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222static int
4223skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4224 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4225 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004227 struct intel_crtc_scaler_state *scaler_state =
4228 &crtc_state->scaler_state;
4229 struct intel_crtc *intel_crtc =
4230 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004232
4233 need_scaling = intel_rotation_90_or_270(rotation) ?
4234 (src_h != dst_w || src_w != dst_h):
4235 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004236
4237 /*
4238 * if plane is being disabled or scaler is no more required or force detach
4239 * - free scaler binded to this plane/crtc
4240 * - in order to do this, update crtc->scaler_usage
4241 *
4242 * Here scaler state in crtc_state is set free so that
4243 * scaler can be assigned to other user. Actual register
4244 * update to free the scaler is done in plane/panel-fit programming.
4245 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4246 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004247 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004248 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 scaler_state->scalers[*scaler_id].in_use = 0;
4251
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004252 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4253 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4254 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255 scaler_state->scaler_users);
4256 *scaler_id = -1;
4257 }
4258 return 0;
4259 }
4260
4261 /* range checks */
4262 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4263 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4264
4265 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4266 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004267 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004268 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004270 return -EINVAL;
4271 }
4272
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273 /* mark this plane as a scaler user in crtc_state */
4274 scaler_state->scaler_users |= (1 << scaler_user);
4275 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4276 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4277 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4278 scaler_state->scaler_users);
4279
4280 return 0;
4281}
4282
4283/**
4284 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4285 *
4286 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004287 *
4288 * Return
4289 * 0 - scaler_usage updated successfully
4290 * error - requested scaling cannot be supported or other error condition
4291 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004292int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293{
4294 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004295 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004296
4297 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4298 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4299
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004300 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004301 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004302 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004303 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004304}
4305
4306/**
4307 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4308 *
4309 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004310 * @plane_state: atomic plane state to update
4311 *
4312 * Return
4313 * 0 - scaler_usage updated successfully
4314 * error - requested scaling cannot be supported or other error condition
4315 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004316static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4317 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004318{
4319
4320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004321 struct intel_plane *intel_plane =
4322 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004323 struct drm_framebuffer *fb = plane_state->base.fb;
4324 int ret;
4325
4326 bool force_detach = !fb || !plane_state->visible;
4327
4328 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4329 intel_plane->base.base.id, intel_crtc->pipe,
4330 drm_plane_index(&intel_plane->base));
4331
4332 ret = skl_update_scaler(crtc_state, force_detach,
4333 drm_plane_index(&intel_plane->base),
4334 &plane_state->scaler_id,
4335 plane_state->base.rotation,
4336 drm_rect_width(&plane_state->src) >> 16,
4337 drm_rect_height(&plane_state->src) >> 16,
4338 drm_rect_width(&plane_state->dst),
4339 drm_rect_height(&plane_state->dst));
4340
4341 if (ret || plane_state->scaler_id < 0)
4342 return ret;
4343
Chandra Kondurua1b22782015-04-07 15:28:45 -07004344 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004345 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004346 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004347 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 return -EINVAL;
4349 }
4350
4351 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004352 switch (fb->pixel_format) {
4353 case DRM_FORMAT_RGB565:
4354 case DRM_FORMAT_XBGR8888:
4355 case DRM_FORMAT_XRGB8888:
4356 case DRM_FORMAT_ABGR8888:
4357 case DRM_FORMAT_ARGB8888:
4358 case DRM_FORMAT_XRGB2101010:
4359 case DRM_FORMAT_XBGR2101010:
4360 case DRM_FORMAT_YUYV:
4361 case DRM_FORMAT_YVYU:
4362 case DRM_FORMAT_UYVY:
4363 case DRM_FORMAT_VYUY:
4364 break;
4365 default:
4366 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4367 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4368 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 }
4370
Chandra Kondurua1b22782015-04-07 15:28:45 -07004371 return 0;
4372}
4373
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004374static void skylake_scaler_disable(struct intel_crtc *crtc)
4375{
4376 int i;
4377
4378 for (i = 0; i < crtc->num_scalers; i++)
4379 skl_detach_scaler(crtc, i);
4380}
4381
4382static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004383{
4384 struct drm_device *dev = crtc->base.dev;
4385 struct drm_i915_private *dev_priv = dev->dev_private;
4386 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004387 struct intel_crtc_scaler_state *scaler_state =
4388 &crtc->config->scaler_state;
4389
4390 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004392 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004393 int id;
4394
4395 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4396 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4397 return;
4398 }
4399
4400 id = scaler_state->scaler_id;
4401 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4402 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4403 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4404 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4405
4406 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004407 }
4408}
4409
Jesse Barnesb074cec2013-04-25 12:55:02 -07004410static void ironlake_pfit_enable(struct intel_crtc *crtc)
4411{
4412 struct drm_device *dev = crtc->base.dev;
4413 struct drm_i915_private *dev_priv = dev->dev_private;
4414 int pipe = crtc->pipe;
4415
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004416 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004417 /* Force use of hard-coded filter coefficients
4418 * as some pre-programmed values are broken,
4419 * e.g. x201.
4420 */
4421 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4422 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4423 PF_PIPE_SEL_IVB(pipe));
4424 else
4425 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004426 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4427 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004428 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004429}
4430
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004431void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004432{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004433 struct drm_device *dev = crtc->base.dev;
4434 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004436 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004437 return;
4438
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004439 /* We can only enable IPS after we enable a plane and wait for a vblank */
4440 intel_wait_for_vblank(dev, crtc->pipe);
4441
Paulo Zanonid77e4532013-09-24 13:52:55 -03004442 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004443 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004444 mutex_lock(&dev_priv->rps.hw_lock);
4445 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4446 mutex_unlock(&dev_priv->rps.hw_lock);
4447 /* Quoting Art Runyan: "its not safe to expect any particular
4448 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004449 * mailbox." Moreover, the mailbox may return a bogus state,
4450 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004451 */
4452 } else {
4453 I915_WRITE(IPS_CTL, IPS_ENABLE);
4454 /* The bit only becomes 1 in the next vblank, so this wait here
4455 * is essentially intel_wait_for_vblank. If we don't have this
4456 * and don't wait for vblanks until the end of crtc_enable, then
4457 * the HW state readout code will complain that the expected
4458 * IPS_CTL value is not the one we read. */
4459 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4460 DRM_ERROR("Timed out waiting for IPS enable\n");
4461 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004462}
4463
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004464void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004465{
4466 struct drm_device *dev = crtc->base.dev;
4467 struct drm_i915_private *dev_priv = dev->dev_private;
4468
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004469 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004470 return;
4471
4472 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004473 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004474 mutex_lock(&dev_priv->rps.hw_lock);
4475 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4476 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004477 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4478 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4479 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004480 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004481 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004482 POSTING_READ(IPS_CTL);
4483 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004484
4485 /* We need to wait for a vblank before we can disable the plane. */
4486 intel_wait_for_vblank(dev, crtc->pipe);
4487}
4488
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004489static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004490{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004491 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004492 struct drm_device *dev = intel_crtc->base.dev;
4493 struct drm_i915_private *dev_priv = dev->dev_private;
4494
4495 mutex_lock(&dev->struct_mutex);
4496 dev_priv->mm.interruptible = false;
4497 (void) intel_overlay_switch_off(intel_crtc->overlay);
4498 dev_priv->mm.interruptible = true;
4499 mutex_unlock(&dev->struct_mutex);
4500 }
4501
4502 /* Let userspace switch the overlay on again. In most cases userspace
4503 * has to recompute where to put it anyway.
4504 */
4505}
4506
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004507/**
4508 * intel_post_enable_primary - Perform operations after enabling primary plane
4509 * @crtc: the CRTC whose primary plane was just enabled
4510 *
4511 * Performs potentially sleeping operations that must be done after the primary
4512 * plane is enabled, such as updating FBC and IPS. Note that this may be
4513 * called due to an explicit primary plane update, or due to an implicit
4514 * re-enable that is caused when a sprite plane is updated to no longer
4515 * completely hide the primary plane.
4516 */
4517static void
4518intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004519{
4520 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004521 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4523 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004524
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004525 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004526 * FIXME IPS should be fine as long as one plane is
4527 * enabled, but in practice it seems to have problems
4528 * when going from primary only to sprite only and vice
4529 * versa.
4530 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004531 hsw_enable_ips(intel_crtc);
4532
Daniel Vetterf99d7062014-06-19 16:01:59 +02004533 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004534 * Gen2 reports pipe underruns whenever all planes are disabled.
4535 * So don't enable underrun reporting before at least some planes
4536 * are enabled.
4537 * FIXME: Need to fix the logic to work when we turn off all planes
4538 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004539 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004540 if (IS_GEN2(dev))
4541 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4542
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004543 /* Underruns don't always raise interrupts, so check manually. */
4544 intel_check_cpu_fifo_underruns(dev_priv);
4545 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004546}
4547
Ville Syrjälä2622a082016-03-09 19:07:26 +02004548/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004549static void
4550intel_pre_disable_primary(struct drm_crtc *crtc)
4551{
4552 struct drm_device *dev = crtc->dev;
4553 struct drm_i915_private *dev_priv = dev->dev_private;
4554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4555 int pipe = intel_crtc->pipe;
4556
4557 /*
4558 * Gen2 reports pipe underruns whenever all planes are disabled.
4559 * So diasble underrun reporting before all the planes get disabled.
4560 * FIXME: Need to fix the logic to work when we turn off all planes
4561 * but leave the pipe running.
4562 */
4563 if (IS_GEN2(dev))
4564 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4565
4566 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004567 * FIXME IPS should be fine as long as one plane is
4568 * enabled, but in practice it seems to have problems
4569 * when going from primary only to sprite only and vice
4570 * versa.
4571 */
4572 hsw_disable_ips(intel_crtc);
4573}
4574
4575/* FIXME get rid of this and use pre_plane_update */
4576static void
4577intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4578{
4579 struct drm_device *dev = crtc->dev;
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4582 int pipe = intel_crtc->pipe;
4583
4584 intel_pre_disable_primary(crtc);
4585
4586 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004587 * Vblank time updates from the shadow to live plane control register
4588 * are blocked if the memory self-refresh mode is active at that
4589 * moment. So to make sure the plane gets truly disabled, disable
4590 * first the self-refresh mode. The self-refresh enable bit in turn
4591 * will be checked/applied by the HW only at the next frame start
4592 * event which is after the vblank start event, so we need to have a
4593 * wait-for-vblank between disabling the plane and the pipe.
4594 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004595 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004596 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004597 dev_priv->wm.vlv.cxsr = false;
4598 intel_wait_for_vblank(dev, pipe);
4599 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004600}
4601
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004602static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004603{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004604 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4605 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004606 struct intel_crtc_state *pipe_config =
4607 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004608 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004609 struct drm_plane *primary = crtc->base.primary;
4610 struct drm_plane_state *old_pri_state =
4611 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004612
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004613 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004614
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004615 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004616
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004617 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004618 intel_update_watermarks(&crtc->base);
4619
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004620 if (old_pri_state) {
4621 struct intel_plane_state *primary_state =
4622 to_intel_plane_state(primary->state);
4623 struct intel_plane_state *old_primary_state =
4624 to_intel_plane_state(old_pri_state);
4625
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004626 intel_fbc_post_update(crtc);
4627
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004628 if (primary_state->visible &&
4629 (needs_modeset(&pipe_config->base) ||
4630 !old_primary_state->visible))
4631 intel_post_enable_primary(&crtc->base);
4632 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004633}
4634
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004635static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004636{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004637 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004638 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004639 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004640 struct intel_crtc_state *pipe_config =
4641 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004642 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4643 struct drm_plane *primary = crtc->base.primary;
4644 struct drm_plane_state *old_pri_state =
4645 drm_atomic_get_existing_plane_state(old_state, primary);
4646 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004647
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004648 if (old_pri_state) {
4649 struct intel_plane_state *primary_state =
4650 to_intel_plane_state(primary->state);
4651 struct intel_plane_state *old_primary_state =
4652 to_intel_plane_state(old_pri_state);
4653
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004654 intel_fbc_pre_update(crtc);
4655
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004656 if (old_primary_state->visible &&
4657 (modeset || !primary_state->visible))
4658 intel_pre_disable_primary(&crtc->base);
4659 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004660
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004661 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004662 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004663
Ville Syrjälä2622a082016-03-09 19:07:26 +02004664 /*
4665 * Vblank time updates from the shadow to live plane control register
4666 * are blocked if the memory self-refresh mode is active at that
4667 * moment. So to make sure the plane gets truly disabled, disable
4668 * first the self-refresh mode. The self-refresh enable bit in turn
4669 * will be checked/applied by the HW only at the next frame start
4670 * event which is after the vblank start event, so we need to have a
4671 * wait-for-vblank between disabling the plane and the pipe.
4672 */
4673 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004674 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004675 dev_priv->wm.vlv.cxsr = false;
4676 intel_wait_for_vblank(dev, crtc->pipe);
4677 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004678 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004679
Matt Ropered4a6a72016-02-23 17:20:13 -08004680 /*
4681 * IVB workaround: must disable low power watermarks for at least
4682 * one frame before enabling scaling. LP watermarks can be re-enabled
4683 * when scaling is disabled.
4684 *
4685 * WaCxSRDisabledForSpriteScaling:ivb
4686 */
4687 if (pipe_config->disable_lp_wm) {
4688 ilk_disable_lp_wm(dev);
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690 }
4691
4692 /*
4693 * If we're doing a modeset, we're done. No need to do any pre-vblank
4694 * watermark programming here.
4695 */
4696 if (needs_modeset(&pipe_config->base))
4697 return;
4698
4699 /*
4700 * For platforms that support atomic watermarks, program the
4701 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4702 * will be the intermediate values that are safe for both pre- and
4703 * post- vblank; when vblank happens, the 'active' values will be set
4704 * to the final 'target' values and we'll do this again to get the
4705 * optimal watermarks. For gen9+ platforms, the values we program here
4706 * will be the final target values which will get automatically latched
4707 * at vblank time; no further programming will be necessary.
4708 *
4709 * If a platform hasn't been transitioned to atomic watermarks yet,
4710 * we'll continue to update watermarks the old way, if flags tell
4711 * us to.
4712 */
4713 if (dev_priv->display.initial_watermarks != NULL)
4714 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004715 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004716 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004717}
4718
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004719static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004720{
4721 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004723 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004724 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004725
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004726 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004727
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004728 drm_for_each_plane_mask(p, dev, plane_mask)
4729 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004730
Daniel Vetterf99d7062014-06-19 16:01:59 +02004731 /*
4732 * FIXME: Once we grow proper nuclear flip support out of this we need
4733 * to compute the mask of flip planes precisely. For the time being
4734 * consider this a flip to a NULL plane.
4735 */
4736 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004737}
4738
Jesse Barnesf67a5592011-01-05 10:31:48 -08004739static void ironlake_crtc_enable(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004744 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004745 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004746
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004747 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004748 return;
4749
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004750 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004751 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4752
4753 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004754 intel_prepare_shared_dpll(intel_crtc);
4755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004756 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304757 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004758
4759 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004760 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004762 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004763 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004765 }
4766
4767 ironlake_set_pipeconf(crtc);
4768
Jesse Barnesf67a5592011-01-05 10:31:48 -08004769 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004770
Daniel Vettera72e4c92014-09-30 10:56:47 +02004771 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004772
Daniel Vetterf6736a12013-06-05 13:34:30 +02004773 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004774 if (encoder->pre_enable)
4775 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004776
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004777 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004778 /* Note: FDI PLL enabling _must_ be done before we enable the
4779 * cpu pipes, hence this is separate from all the other fdi/pch
4780 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004781 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004782 } else {
4783 assert_fdi_tx_disabled(dev_priv, pipe);
4784 assert_fdi_rx_disabled(dev_priv, pipe);
4785 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004786
Jesse Barnesb074cec2013-04-25 12:55:02 -07004787 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004788
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004789 /*
4790 * On ILK+ LUT must be loaded before the pipe is running but with
4791 * clocks enabled
4792 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004793 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004794
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004795 if (dev_priv->display.initial_watermarks != NULL)
4796 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004797 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004798
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004799 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004800 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004801
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004802 assert_vblank_disabled(crtc);
4803 drm_crtc_vblank_on(crtc);
4804
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004805 for_each_encoder_on_crtc(dev, crtc, encoder)
4806 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004807
4808 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004809 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004810
4811 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4812 if (intel_crtc->config->has_pch_encoder)
4813 intel_wait_for_vblank(dev, pipe);
4814 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004815}
4816
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004817/* IPS only exists on ULT machines and is tied to pipe A. */
4818static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4819{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004820 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004821}
4822
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004823static void haswell_crtc_enable(struct drm_crtc *crtc)
4824{
4825 struct drm_device *dev = crtc->dev;
4826 struct drm_i915_private *dev_priv = dev->dev_private;
4827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4828 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004829 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004830 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004831 struct intel_crtc_state *pipe_config =
4832 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004833
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004834 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004835 return;
4836
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004837 if (intel_crtc->config->has_pch_encoder)
4838 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4839 false);
4840
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004841 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004842 intel_enable_shared_dpll(intel_crtc);
4843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004844 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304845 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004846
Jani Nikula4d1de972016-03-18 17:05:42 +02004847 if (!intel_crtc->config->has_dsi_encoder)
4848 intel_set_pipe_timings(intel_crtc);
4849
Jani Nikulabc58be62016-03-18 17:05:39 +02004850 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004851
Jani Nikula4d1de972016-03-18 17:05:42 +02004852 if (cpu_transcoder != TRANSCODER_EDP &&
4853 !transcoder_is_dsi(cpu_transcoder)) {
4854 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004855 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004856 }
4857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004858 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004859 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004860 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004861 }
4862
Jani Nikula4d1de972016-03-18 17:05:42 +02004863 if (!intel_crtc->config->has_dsi_encoder)
4864 haswell_set_pipeconf(crtc);
4865
Jani Nikula391bf042016-03-18 17:05:40 +02004866 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004867
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004868 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004869
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004871
Daniel Vetter6b698512015-11-28 11:05:39 +01004872 if (intel_crtc->config->has_pch_encoder)
4873 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4874 else
4875 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4876
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304877 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878 if (encoder->pre_enable)
4879 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304880 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004881
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004882 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004883 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004884
Jani Nikulaa65347b2015-11-27 12:21:46 +02004885 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304886 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004887
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004888 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004889 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004890 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004891 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004892
4893 /*
4894 * On ILK+ LUT must be loaded before the pipe is running but with
4895 * clocks enabled
4896 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004897 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004898
Paulo Zanoni1f544382012-10-24 11:32:00 -02004899 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004900 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304901 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004903 if (dev_priv->display.initial_watermarks != NULL)
4904 dev_priv->display.initial_watermarks(pipe_config);
4905 else
4906 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004907
4908 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4909 if (!intel_crtc->config->has_dsi_encoder)
4910 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004913 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914
Jani Nikulaa65347b2015-11-27 12:21:46 +02004915 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004916 intel_ddi_set_vc_payload_alloc(crtc, true);
4917
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004918 assert_vblank_disabled(crtc);
4919 drm_crtc_vblank_on(crtc);
4920
Jani Nikula8807e552013-08-30 19:40:32 +03004921 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004922 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004923 intel_opregion_notify_encoder(encoder, true);
4924 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925
Daniel Vetter6b698512015-11-28 11:05:39 +01004926 if (intel_crtc->config->has_pch_encoder) {
4927 intel_wait_for_vblank(dev, pipe);
4928 intel_wait_for_vblank(dev, pipe);
4929 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004930 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4931 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004932 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004933
Paulo Zanonie4916942013-09-20 16:21:19 -03004934 /* If we change the relative order between pipe/planes enabling, we need
4935 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004936 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4937 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4938 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4939 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4940 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004941}
4942
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004943static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004944{
4945 struct drm_device *dev = crtc->base.dev;
4946 struct drm_i915_private *dev_priv = dev->dev_private;
4947 int pipe = crtc->pipe;
4948
4949 /* To avoid upsetting the power well on haswell only disable the pfit if
4950 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004951 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004952 I915_WRITE(PF_CTL(pipe), 0);
4953 I915_WRITE(PF_WIN_POS(pipe), 0);
4954 I915_WRITE(PF_WIN_SZ(pipe), 0);
4955 }
4956}
4957
Jesse Barnes6be4a602010-09-10 10:26:01 -07004958static void ironlake_crtc_disable(struct drm_crtc *crtc)
4959{
4960 struct drm_device *dev = crtc->dev;
4961 struct drm_i915_private *dev_priv = dev->dev_private;
4962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004963 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004966 if (intel_crtc->config->has_pch_encoder)
4967 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4968
Daniel Vetterea9d7582012-07-10 10:42:52 +02004969 for_each_encoder_on_crtc(dev, crtc, encoder)
4970 encoder->disable(encoder);
4971
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004972 drm_crtc_vblank_off(crtc);
4973 assert_vblank_disabled(crtc);
4974
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004975 /*
4976 * Sometimes spurious CPU pipe underruns happen when the
4977 * pipe is already disabled, but FDI RX/TX is still enabled.
4978 * Happens at least with VGA+HDMI cloning. Suppress them.
4979 */
4980 if (intel_crtc->config->has_pch_encoder)
4981 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4982
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004983 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004985 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004986
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004987 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004988 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004989 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4990 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004991
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004992 for_each_encoder_on_crtc(dev, crtc, encoder)
4993 if (encoder->post_disable)
4994 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004996 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004997 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005000 i915_reg_t reg;
5001 u32 temp;
5002
Daniel Vetterd925c592013-06-05 13:34:04 +02005003 /* disable TRANS_DP_CTL */
5004 reg = TRANS_DP_CTL(pipe);
5005 temp = I915_READ(reg);
5006 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5007 TRANS_DP_PORT_SEL_MASK);
5008 temp |= TRANS_DP_PORT_SEL_NONE;
5009 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005010
Daniel Vetterd925c592013-06-05 13:34:04 +02005011 /* disable DPLL_SEL */
5012 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005013 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005014 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005015 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005016
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005019
5020 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005021}
5022
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023static void haswell_crtc_disable(struct drm_crtc *crtc)
5024{
5025 struct drm_device *dev = crtc->dev;
5026 struct drm_i915_private *dev_priv = dev->dev_private;
5027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5028 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005029 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005031 if (intel_crtc->config->has_pch_encoder)
5032 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5033 false);
5034
Jani Nikula8807e552013-08-30 19:40:32 +03005035 for_each_encoder_on_crtc(dev, crtc, encoder) {
5036 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005038 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005040 drm_crtc_vblank_off(crtc);
5041 assert_vblank_disabled(crtc);
5042
Jani Nikula4d1de972016-03-18 17:05:42 +02005043 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5044 if (!intel_crtc->config->has_dsi_encoder)
5045 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005047 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005048 intel_ddi_set_vc_payload_alloc(crtc, false);
5049
Jani Nikulaa65347b2015-11-27 12:21:46 +02005050 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305051 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005053 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005054 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005055 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005056 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057
Jani Nikulaa65347b2015-11-27 12:21:46 +02005058 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305059 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060
Imre Deak97b040a2014-06-25 22:01:50 +03005061 for_each_encoder_on_crtc(dev, crtc, encoder)
5062 if (encoder->post_disable)
5063 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005064
Ville Syrjälä92966a32015-12-08 16:05:48 +02005065 if (intel_crtc->config->has_pch_encoder) {
5066 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005067 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005068 intel_ddi_fdi_disable(crtc);
5069
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005070 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5071 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005072 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073}
5074
Jesse Barnes2dd24552013-04-25 12:55:01 -07005075static void i9xx_pfit_enable(struct intel_crtc *crtc)
5076{
5077 struct drm_device *dev = crtc->base.dev;
5078 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005079 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005080
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005081 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005082 return;
5083
Daniel Vetterc0b03412013-05-28 12:05:54 +02005084 /*
5085 * The panel fitter should only be adjusted whilst the pipe is disabled,
5086 * according to register description and PRM.
5087 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005088 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5089 assert_pipe_disabled(dev_priv, crtc->pipe);
5090
Jesse Barnesb074cec2013-04-25 12:55:02 -07005091 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5092 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005093
5094 /* Border color in case we don't scale up to the full screen. Black by
5095 * default, change to something else for debugging. */
5096 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005097}
5098
Dave Airlied05410f2014-06-05 13:22:59 +10005099static enum intel_display_power_domain port_to_power_domain(enum port port)
5100{
5101 switch (port) {
5102 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005103 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005104 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005105 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005106 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005107 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005108 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005109 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005110 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005111 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005112 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005113 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005114 return POWER_DOMAIN_PORT_OTHER;
5115 }
5116}
5117
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005118static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5119{
5120 switch (port) {
5121 case PORT_A:
5122 return POWER_DOMAIN_AUX_A;
5123 case PORT_B:
5124 return POWER_DOMAIN_AUX_B;
5125 case PORT_C:
5126 return POWER_DOMAIN_AUX_C;
5127 case PORT_D:
5128 return POWER_DOMAIN_AUX_D;
5129 case PORT_E:
5130 /* FIXME: Check VBT for actual wiring of PORT E */
5131 return POWER_DOMAIN_AUX_D;
5132 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005133 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005134 return POWER_DOMAIN_AUX_A;
5135 }
5136}
5137
Imre Deak319be8a2014-03-04 19:22:57 +02005138enum intel_display_power_domain
5139intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005140{
Imre Deak319be8a2014-03-04 19:22:57 +02005141 struct drm_device *dev = intel_encoder->base.dev;
5142 struct intel_digital_port *intel_dig_port;
5143
5144 switch (intel_encoder->type) {
5145 case INTEL_OUTPUT_UNKNOWN:
5146 /* Only DDI platforms should ever use this output type */
5147 WARN_ON_ONCE(!HAS_DDI(dev));
5148 case INTEL_OUTPUT_DISPLAYPORT:
5149 case INTEL_OUTPUT_HDMI:
5150 case INTEL_OUTPUT_EDP:
5151 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005152 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005153 case INTEL_OUTPUT_DP_MST:
5154 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5155 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005156 case INTEL_OUTPUT_ANALOG:
5157 return POWER_DOMAIN_PORT_CRT;
5158 case INTEL_OUTPUT_DSI:
5159 return POWER_DOMAIN_PORT_DSI;
5160 default:
5161 return POWER_DOMAIN_PORT_OTHER;
5162 }
5163}
5164
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005165enum intel_display_power_domain
5166intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5167{
5168 struct drm_device *dev = intel_encoder->base.dev;
5169 struct intel_digital_port *intel_dig_port;
5170
5171 switch (intel_encoder->type) {
5172 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005173 case INTEL_OUTPUT_HDMI:
5174 /*
5175 * Only DDI platforms should ever use these output types.
5176 * We can get here after the HDMI detect code has already set
5177 * the type of the shared encoder. Since we can't be sure
5178 * what's the status of the given connectors, play safe and
5179 * run the DP detection too.
5180 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005181 WARN_ON_ONCE(!HAS_DDI(dev));
5182 case INTEL_OUTPUT_DISPLAYPORT:
5183 case INTEL_OUTPUT_EDP:
5184 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5185 return port_to_aux_power_domain(intel_dig_port->port);
5186 case INTEL_OUTPUT_DP_MST:
5187 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5188 return port_to_aux_power_domain(intel_dig_port->port);
5189 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005190 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005191 return POWER_DOMAIN_AUX_A;
5192 }
5193}
5194
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005195static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5196 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005197{
5198 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005199 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5201 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005203 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005204
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005205 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005206 return 0;
5207
Imre Deak77d22dc2014-03-05 16:20:52 +02005208 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5209 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005210 if (crtc_state->pch_pfit.enabled ||
5211 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005212 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5213
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005214 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5215 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5216
Imre Deak319be8a2014-03-04 19:22:57 +02005217 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005218 }
Imre Deak319be8a2014-03-04 19:22:57 +02005219
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005220 if (crtc_state->shared_dpll)
5221 mask |= BIT(POWER_DOMAIN_PLLS);
5222
Imre Deak77d22dc2014-03-05 16:20:52 +02005223 return mask;
5224}
5225
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005226static unsigned long
5227modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5228 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005229{
5230 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5232 enum intel_display_power_domain domain;
5233 unsigned long domains, new_domains, old_domains;
5234
5235 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005236 intel_crtc->enabled_power_domains = new_domains =
5237 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005238
5239 domains = new_domains & ~old_domains;
5240
5241 for_each_power_domain(domain, domains)
5242 intel_display_power_get(dev_priv, domain);
5243
5244 return old_domains & ~new_domains;
5245}
5246
5247static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5248 unsigned long domains)
5249{
5250 enum intel_display_power_domain domain;
5251
5252 for_each_power_domain(domain, domains)
5253 intel_display_power_put(dev_priv, domain);
5254}
5255
Mika Kaholaadafdc62015-08-18 14:36:59 +03005256static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5257{
5258 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5259
5260 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5261 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5262 return max_cdclk_freq;
5263 else if (IS_CHERRYVIEW(dev_priv))
5264 return max_cdclk_freq*95/100;
5265 else if (INTEL_INFO(dev_priv)->gen < 4)
5266 return 2*max_cdclk_freq*90/100;
5267 else
5268 return max_cdclk_freq*90/100;
5269}
5270
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005271static void intel_update_max_cdclk(struct drm_device *dev)
5272{
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005275 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005276 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5277
5278 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5279 dev_priv->max_cdclk_freq = 675000;
5280 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5281 dev_priv->max_cdclk_freq = 540000;
5282 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5283 dev_priv->max_cdclk_freq = 450000;
5284 else
5285 dev_priv->max_cdclk_freq = 337500;
5286 } else if (IS_BROADWELL(dev)) {
5287 /*
5288 * FIXME with extra cooling we can allow
5289 * 540 MHz for ULX and 675 Mhz for ULT.
5290 * How can we know if extra cooling is
5291 * available? PCI ID, VTB, something else?
5292 */
5293 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5294 dev_priv->max_cdclk_freq = 450000;
5295 else if (IS_BDW_ULX(dev))
5296 dev_priv->max_cdclk_freq = 450000;
5297 else if (IS_BDW_ULT(dev))
5298 dev_priv->max_cdclk_freq = 540000;
5299 else
5300 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005301 } else if (IS_CHERRYVIEW(dev)) {
5302 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005303 } else if (IS_VALLEYVIEW(dev)) {
5304 dev_priv->max_cdclk_freq = 400000;
5305 } else {
5306 /* otherwise assume cdclk is fixed */
5307 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5308 }
5309
Mika Kaholaadafdc62015-08-18 14:36:59 +03005310 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5311
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005312 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5313 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005314
5315 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5316 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005317}
5318
5319static void intel_update_cdclk(struct drm_device *dev)
5320{
5321 struct drm_i915_private *dev_priv = dev->dev_private;
5322
5323 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5324 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5325 dev_priv->cdclk_freq);
5326
5327 /*
5328 * Program the gmbus_freq based on the cdclk frequency.
5329 * BSpec erroneously claims we should aim for 4MHz, but
5330 * in fact 1MHz is the correct frequency.
5331 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005332 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005333 /*
5334 * Program the gmbus_freq based on the cdclk frequency.
5335 * BSpec erroneously claims we should aim for 4MHz, but
5336 * in fact 1MHz is the correct frequency.
5337 */
5338 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5339 }
5340
5341 if (dev_priv->max_cdclk_freq == 0)
5342 intel_update_max_cdclk(dev);
5343}
5344
Damien Lespiau70d0c572015-06-04 18:21:29 +01005345static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305346{
5347 struct drm_i915_private *dev_priv = dev->dev_private;
5348 uint32_t divider;
5349 uint32_t ratio;
5350 uint32_t current_freq;
5351 int ret;
5352
5353 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5354 switch (frequency) {
5355 case 144000:
5356 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5357 ratio = BXT_DE_PLL_RATIO(60);
5358 break;
5359 case 288000:
5360 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5361 ratio = BXT_DE_PLL_RATIO(60);
5362 break;
5363 case 384000:
5364 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5365 ratio = BXT_DE_PLL_RATIO(60);
5366 break;
5367 case 576000:
5368 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5369 ratio = BXT_DE_PLL_RATIO(60);
5370 break;
5371 case 624000:
5372 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5373 ratio = BXT_DE_PLL_RATIO(65);
5374 break;
5375 case 19200:
5376 /*
5377 * Bypass frequency with DE PLL disabled. Init ratio, divider
5378 * to suppress GCC warning.
5379 */
5380 ratio = 0;
5381 divider = 0;
5382 break;
5383 default:
5384 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5385
5386 return;
5387 }
5388
5389 mutex_lock(&dev_priv->rps.hw_lock);
5390 /* Inform power controller of upcoming frequency change */
5391 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5392 0x80000000);
5393 mutex_unlock(&dev_priv->rps.hw_lock);
5394
5395 if (ret) {
5396 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5397 ret, frequency);
5398 return;
5399 }
5400
5401 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5402 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5403 current_freq = current_freq * 500 + 1000;
5404
5405 /*
5406 * DE PLL has to be disabled when
5407 * - setting to 19.2MHz (bypass, PLL isn't used)
5408 * - before setting to 624MHz (PLL needs toggling)
5409 * - before setting to any frequency from 624MHz (PLL needs toggling)
5410 */
5411 if (frequency == 19200 || frequency == 624000 ||
5412 current_freq == 624000) {
5413 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5414 /* Timeout 200us */
5415 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5416 1))
5417 DRM_ERROR("timout waiting for DE PLL unlock\n");
5418 }
5419
5420 if (frequency != 19200) {
5421 uint32_t val;
5422
5423 val = I915_READ(BXT_DE_PLL_CTL);
5424 val &= ~BXT_DE_PLL_RATIO_MASK;
5425 val |= ratio;
5426 I915_WRITE(BXT_DE_PLL_CTL, val);
5427
5428 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5429 /* Timeout 200us */
5430 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5431 DRM_ERROR("timeout waiting for DE PLL lock\n");
5432
5433 val = I915_READ(CDCLK_CTL);
5434 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5435 val |= divider;
5436 /*
5437 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5438 * enable otherwise.
5439 */
5440 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5441 if (frequency >= 500000)
5442 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5443
5444 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5445 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5446 val |= (frequency - 1000) / 500;
5447 I915_WRITE(CDCLK_CTL, val);
5448 }
5449
5450 mutex_lock(&dev_priv->rps.hw_lock);
5451 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5452 DIV_ROUND_UP(frequency, 25000));
5453 mutex_unlock(&dev_priv->rps.hw_lock);
5454
5455 if (ret) {
5456 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5457 ret, frequency);
5458 return;
5459 }
5460
Damien Lespiaua47871b2015-06-04 18:21:34 +01005461 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305462}
5463
5464void broxton_init_cdclk(struct drm_device *dev)
5465{
5466 struct drm_i915_private *dev_priv = dev->dev_private;
5467 uint32_t val;
5468
5469 /*
5470 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5471 * or else the reset will hang because there is no PCH to respond.
5472 * Move the handshake programming to initialization sequence.
5473 * Previously was left up to BIOS.
5474 */
5475 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5476 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5477 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5478
5479 /* Enable PG1 for cdclk */
5480 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5481
5482 /* check if cd clock is enabled */
5483 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5484 DRM_DEBUG_KMS("Display already initialized\n");
5485 return;
5486 }
5487
5488 /*
5489 * FIXME:
5490 * - The initial CDCLK needs to be read from VBT.
5491 * Need to make this change after VBT has changes for BXT.
5492 * - check if setting the max (or any) cdclk freq is really necessary
5493 * here, it belongs to modeset time
5494 */
5495 broxton_set_cdclk(dev, 624000);
5496
5497 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005498 POSTING_READ(DBUF_CTL);
5499
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305500 udelay(10);
5501
5502 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5503 DRM_ERROR("DBuf power enable timeout!\n");
5504}
5505
5506void broxton_uninit_cdclk(struct drm_device *dev)
5507{
5508 struct drm_i915_private *dev_priv = dev->dev_private;
5509
5510 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005511 POSTING_READ(DBUF_CTL);
5512
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305513 udelay(10);
5514
5515 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5516 DRM_ERROR("DBuf power disable timeout!\n");
5517
5518 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5519 broxton_set_cdclk(dev, 19200);
5520
5521 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5522}
5523
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005524static const struct skl_cdclk_entry {
5525 unsigned int freq;
5526 unsigned int vco;
5527} skl_cdclk_frequencies[] = {
5528 { .freq = 308570, .vco = 8640 },
5529 { .freq = 337500, .vco = 8100 },
5530 { .freq = 432000, .vco = 8640 },
5531 { .freq = 450000, .vco = 8100 },
5532 { .freq = 540000, .vco = 8100 },
5533 { .freq = 617140, .vco = 8640 },
5534 { .freq = 675000, .vco = 8100 },
5535};
5536
5537static unsigned int skl_cdclk_decimal(unsigned int freq)
5538{
5539 return (freq - 1000) / 500;
5540}
5541
5542static unsigned int skl_cdclk_get_vco(unsigned int freq)
5543{
5544 unsigned int i;
5545
5546 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5547 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5548
5549 if (e->freq == freq)
5550 return e->vco;
5551 }
5552
5553 return 8100;
5554}
5555
5556static void
5557skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5558{
5559 unsigned int min_freq;
5560 u32 val;
5561
5562 /* select the minimum CDCLK before enabling DPLL 0 */
5563 val = I915_READ(CDCLK_CTL);
5564 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5565 val |= CDCLK_FREQ_337_308;
5566
5567 if (required_vco == 8640)
5568 min_freq = 308570;
5569 else
5570 min_freq = 337500;
5571
5572 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5573
5574 I915_WRITE(CDCLK_CTL, val);
5575 POSTING_READ(CDCLK_CTL);
5576
5577 /*
5578 * We always enable DPLL0 with the lowest link rate possible, but still
5579 * taking into account the VCO required to operate the eDP panel at the
5580 * desired frequency. The usual DP link rates operate with a VCO of
5581 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5582 * The modeset code is responsible for the selection of the exact link
5583 * rate later on, with the constraint of choosing a frequency that
5584 * works with required_vco.
5585 */
5586 val = I915_READ(DPLL_CTRL1);
5587
5588 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5589 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5590 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5591 if (required_vco == 8640)
5592 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5593 SKL_DPLL0);
5594 else
5595 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5596 SKL_DPLL0);
5597
5598 I915_WRITE(DPLL_CTRL1, val);
5599 POSTING_READ(DPLL_CTRL1);
5600
5601 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5602
5603 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5604 DRM_ERROR("DPLL0 not locked\n");
5605}
5606
5607static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5608{
5609 int ret;
5610 u32 val;
5611
5612 /* inform PCU we want to change CDCLK */
5613 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5614 mutex_lock(&dev_priv->rps.hw_lock);
5615 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5616 mutex_unlock(&dev_priv->rps.hw_lock);
5617
5618 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5619}
5620
5621static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5622{
5623 unsigned int i;
5624
5625 for (i = 0; i < 15; i++) {
5626 if (skl_cdclk_pcu_ready(dev_priv))
5627 return true;
5628 udelay(10);
5629 }
5630
5631 return false;
5632}
5633
5634static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5635{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005636 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005637 u32 freq_select, pcu_ack;
5638
5639 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5640
5641 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5642 DRM_ERROR("failed to inform PCU about cdclk change\n");
5643 return;
5644 }
5645
5646 /* set CDCLK_CTL */
5647 switch(freq) {
5648 case 450000:
5649 case 432000:
5650 freq_select = CDCLK_FREQ_450_432;
5651 pcu_ack = 1;
5652 break;
5653 case 540000:
5654 freq_select = CDCLK_FREQ_540;
5655 pcu_ack = 2;
5656 break;
5657 case 308570:
5658 case 337500:
5659 default:
5660 freq_select = CDCLK_FREQ_337_308;
5661 pcu_ack = 0;
5662 break;
5663 case 617140:
5664 case 675000:
5665 freq_select = CDCLK_FREQ_675_617;
5666 pcu_ack = 3;
5667 break;
5668 }
5669
5670 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5671 POSTING_READ(CDCLK_CTL);
5672
5673 /* inform PCU of the change */
5674 mutex_lock(&dev_priv->rps.hw_lock);
5675 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5676 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005677
5678 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005679}
5680
5681void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5682{
5683 /* disable DBUF power */
5684 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5685 POSTING_READ(DBUF_CTL);
5686
5687 udelay(10);
5688
5689 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5690 DRM_ERROR("DBuf power disable timeout\n");
5691
Imre Deakab96c1ee2015-11-04 19:24:18 +02005692 /* disable DPLL0 */
5693 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5694 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5695 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005696}
5697
5698void skl_init_cdclk(struct drm_i915_private *dev_priv)
5699{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005700 unsigned int required_vco;
5701
Gary Wang39d9b852015-08-28 16:40:34 +08005702 /* DPLL0 not enabled (happens on early BIOS versions) */
5703 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5704 /* enable DPLL0 */
5705 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5706 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005707 }
5708
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005709 /* set CDCLK to the frequency the BIOS chose */
5710 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5711
5712 /* enable DBUF power */
5713 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5714 POSTING_READ(DBUF_CTL);
5715
5716 udelay(10);
5717
5718 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5719 DRM_ERROR("DBuf power enable timeout\n");
5720}
5721
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305722int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5723{
5724 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5725 uint32_t cdctl = I915_READ(CDCLK_CTL);
5726 int freq = dev_priv->skl_boot_cdclk;
5727
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305728 /*
5729 * check if the pre-os intialized the display
5730 * There is SWF18 scratchpad register defined which is set by the
5731 * pre-os which can be used by the OS drivers to check the status
5732 */
5733 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5734 goto sanitize;
5735
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305736 /* Is PLL enabled and locked ? */
5737 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5738 goto sanitize;
5739
5740 /* DPLL okay; verify the cdclock
5741 *
5742 * Noticed in some instances that the freq selection is correct but
5743 * decimal part is programmed wrong from BIOS where pre-os does not
5744 * enable display. Verify the same as well.
5745 */
5746 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5747 /* All well; nothing to sanitize */
5748 return false;
5749sanitize:
5750 /*
5751 * As of now initialize with max cdclk till
5752 * we get dynamic cdclk support
5753 * */
5754 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5755 skl_init_cdclk(dev_priv);
5756
5757 /* we did have to sanitize */
5758 return true;
5759}
5760
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761/* Adjust CDclk dividers to allow high res or save power if possible */
5762static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5763{
5764 struct drm_i915_private *dev_priv = dev->dev_private;
5765 u32 val, cmd;
5766
Vandana Kannan164dfd22014-11-24 13:37:41 +05305767 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5768 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005769
Ville Syrjälädfcab172014-06-13 13:37:47 +03005770 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005771 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005772 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773 cmd = 1;
5774 else
5775 cmd = 0;
5776
5777 mutex_lock(&dev_priv->rps.hw_lock);
5778 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5779 val &= ~DSPFREQGUAR_MASK;
5780 val |= (cmd << DSPFREQGUAR_SHIFT);
5781 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5782 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5783 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5784 50)) {
5785 DRM_ERROR("timed out waiting for CDclk change\n");
5786 }
5787 mutex_unlock(&dev_priv->rps.hw_lock);
5788
Ville Syrjälä54433e92015-05-26 20:42:31 +03005789 mutex_lock(&dev_priv->sb_lock);
5790
Ville Syrjälädfcab172014-06-13 13:37:47 +03005791 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005792 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005794 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796 /* adjust cdclk divider */
5797 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005798 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005799 val |= divider;
5800 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005801
5802 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005803 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005804 50))
5805 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005806 }
5807
Jesse Barnes30a970c2013-11-04 13:48:12 -08005808 /* adjust self-refresh exit latency value */
5809 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5810 val &= ~0x7f;
5811
5812 /*
5813 * For high bandwidth configs, we set a higher latency in the bunit
5814 * so that the core display fetch happens in time to avoid underruns.
5815 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005816 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005817 val |= 4500 / 250; /* 4.5 usec */
5818 else
5819 val |= 3000 / 250; /* 3.0 usec */
5820 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005821
Ville Syrjäläa5805162015-05-26 20:42:30 +03005822 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005823
Ville Syrjäläb6283052015-06-03 15:45:07 +03005824 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825}
5826
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005827static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5828{
5829 struct drm_i915_private *dev_priv = dev->dev_private;
5830 u32 val, cmd;
5831
Vandana Kannan164dfd22014-11-24 13:37:41 +05305832 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5833 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005834
5835 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005836 case 333333:
5837 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005838 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005839 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840 break;
5841 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005842 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005843 return;
5844 }
5845
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005846 /*
5847 * Specs are full of misinformation, but testing on actual
5848 * hardware has shown that we just need to write the desired
5849 * CCK divider into the Punit register.
5850 */
5851 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5852
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005853 mutex_lock(&dev_priv->rps.hw_lock);
5854 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5855 val &= ~DSPFREQGUAR_MASK_CHV;
5856 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5857 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5858 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5859 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5860 50)) {
5861 DRM_ERROR("timed out waiting for CDclk change\n");
5862 }
5863 mutex_unlock(&dev_priv->rps.hw_lock);
5864
Ville Syrjäläb6283052015-06-03 15:45:07 +03005865 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005866}
5867
Jesse Barnes30a970c2013-11-04 13:48:12 -08005868static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5869 int max_pixclk)
5870{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005871 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005872 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005873
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874 /*
5875 * Really only a few cases to deal with, as only 4 CDclks are supported:
5876 * 200MHz
5877 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005878 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005879 * 400MHz (VLV only)
5880 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5881 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005882 *
5883 * We seem to get an unstable or solid color picture at 200MHz.
5884 * Not sure what's wrong. For now use 200MHz only when all pipes
5885 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005887 if (!IS_CHERRYVIEW(dev_priv) &&
5888 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005889 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005890 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005891 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005892 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005893 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005894 else
5895 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005896}
5897
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305898static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5899 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305901 /*
5902 * FIXME:
5903 * - remove the guardband, it's not needed on BXT
5904 * - set 19.2MHz bypass frequency if there are no active pipes
5905 */
5906 if (max_pixclk > 576000*9/10)
5907 return 624000;
5908 else if (max_pixclk > 384000*9/10)
5909 return 576000;
5910 else if (max_pixclk > 288000*9/10)
5911 return 384000;
5912 else if (max_pixclk > 144000*9/10)
5913 return 288000;
5914 else
5915 return 144000;
5916}
5917
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005918/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005919static int intel_mode_max_pixclk(struct drm_device *dev,
5920 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005921{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005922 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5923 struct drm_i915_private *dev_priv = dev->dev_private;
5924 struct drm_crtc *crtc;
5925 struct drm_crtc_state *crtc_state;
5926 unsigned max_pixclk = 0, i;
5927 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005928
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005929 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5930 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005931
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005932 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5933 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005934
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005935 if (crtc_state->enable)
5936 pixclk = crtc_state->adjusted_mode.crtc_clock;
5937
5938 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939 }
5940
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005941 for_each_pipe(dev_priv, pipe)
5942 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5943
Jesse Barnes30a970c2013-11-04 13:48:12 -08005944 return max_pixclk;
5945}
5946
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005947static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005948{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949 struct drm_device *dev = state->dev;
5950 struct drm_i915_private *dev_priv = dev->dev_private;
5951 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005952 struct intel_atomic_state *intel_state =
5953 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005954
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005955 if (max_pixclk < 0)
5956 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005957
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005958 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005959 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305960
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005961 if (!intel_state->active_crtcs)
5962 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5963
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005964 return 0;
5965}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005966
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005967static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5968{
5969 struct drm_device *dev = state->dev;
5970 struct drm_i915_private *dev_priv = dev->dev_private;
5971 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005972 struct intel_atomic_state *intel_state =
5973 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005974
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005975 if (max_pixclk < 0)
5976 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005977
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005978 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005979 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005980
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005981 if (!intel_state->active_crtcs)
5982 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5983
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005984 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005985}
5986
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005987static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5988{
5989 unsigned int credits, default_credits;
5990
5991 if (IS_CHERRYVIEW(dev_priv))
5992 default_credits = PFI_CREDIT(12);
5993 else
5994 default_credits = PFI_CREDIT(8);
5995
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005996 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005997 /* CHV suggested value is 31 or 63 */
5998 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005999 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006000 else
6001 credits = PFI_CREDIT(15);
6002 } else {
6003 credits = default_credits;
6004 }
6005
6006 /*
6007 * WA - write default credits before re-programming
6008 * FIXME: should we also set the resend bit here?
6009 */
6010 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6011 default_credits);
6012
6013 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6014 credits | PFI_CREDIT_RESEND);
6015
6016 /*
6017 * FIXME is this guaranteed to clear
6018 * immediately or should we poll for it?
6019 */
6020 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6021}
6022
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006023static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006024{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006025 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006027 struct intel_atomic_state *old_intel_state =
6028 to_intel_atomic_state(old_state);
6029 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006031 /*
6032 * FIXME: We can end up here with all power domains off, yet
6033 * with a CDCLK frequency other than the minimum. To account
6034 * for this take the PIPE-A power domain, which covers the HW
6035 * blocks needed for the following programming. This can be
6036 * removed once it's guaranteed that we get here either with
6037 * the minimum CDCLK set, or the required power domains
6038 * enabled.
6039 */
6040 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006041
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006042 if (IS_CHERRYVIEW(dev))
6043 cherryview_set_cdclk(dev, req_cdclk);
6044 else
6045 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006046
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006047 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006048
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006049 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006050}
6051
Jesse Barnes89b667f2013-04-18 14:51:36 -07006052static void valleyview_crtc_enable(struct drm_crtc *crtc)
6053{
6054 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006055 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6057 struct intel_encoder *encoder;
6058 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006059
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006060 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061 return;
6062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006063 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306064 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006065
6066 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006067 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006068
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006069 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6070 struct drm_i915_private *dev_priv = dev->dev_private;
6071
6072 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6073 I915_WRITE(CHV_CANVAS(pipe), 0);
6074 }
6075
Daniel Vetter5b18e572014-04-24 23:55:06 +02006076 i9xx_set_pipeconf(intel_crtc);
6077
Jesse Barnes89b667f2013-04-18 14:51:36 -07006078 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006079
Daniel Vettera72e4c92014-09-30 10:56:47 +02006080 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006081
Jesse Barnes89b667f2013-04-18 14:51:36 -07006082 for_each_encoder_on_crtc(dev, crtc, encoder)
6083 if (encoder->pre_pll_enable)
6084 encoder->pre_pll_enable(encoder);
6085
Jani Nikulaa65347b2015-11-27 12:21:46 +02006086 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006087 if (IS_CHERRYVIEW(dev)) {
6088 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006089 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006090 } else {
6091 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006092 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006093 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006094 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006095
6096 for_each_encoder_on_crtc(dev, crtc, encoder)
6097 if (encoder->pre_enable)
6098 encoder->pre_enable(encoder);
6099
Jesse Barnes2dd24552013-04-25 12:55:01 -07006100 i9xx_pfit_enable(intel_crtc);
6101
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006102 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006103
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006104 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006105 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006106
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006107 assert_vblank_disabled(crtc);
6108 drm_crtc_vblank_on(crtc);
6109
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006110 for_each_encoder_on_crtc(dev, crtc, encoder)
6111 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006112}
6113
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006114static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6115{
6116 struct drm_device *dev = crtc->base.dev;
6117 struct drm_i915_private *dev_priv = dev->dev_private;
6118
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006119 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6120 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006121}
6122
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006123static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006124{
6125 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006126 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006128 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006129 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006130
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006131 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006132 return;
6133
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006134 i9xx_set_pll_dividers(intel_crtc);
6135
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006136 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306137 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006138
6139 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006140 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006141
Daniel Vetter5b18e572014-04-24 23:55:06 +02006142 i9xx_set_pipeconf(intel_crtc);
6143
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006144 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006145
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006146 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006147 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006148
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006149 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006150 if (encoder->pre_enable)
6151 encoder->pre_enable(encoder);
6152
Daniel Vetterf6736a12013-06-05 13:34:30 +02006153 i9xx_enable_pll(intel_crtc);
6154
Jesse Barnes2dd24552013-04-25 12:55:01 -07006155 i9xx_pfit_enable(intel_crtc);
6156
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006157 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006158
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006159 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006160 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006161
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006162 assert_vblank_disabled(crtc);
6163 drm_crtc_vblank_on(crtc);
6164
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006165 for_each_encoder_on_crtc(dev, crtc, encoder)
6166 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006167}
6168
Daniel Vetter87476d62013-04-11 16:29:06 +02006169static void i9xx_pfit_disable(struct intel_crtc *crtc)
6170{
6171 struct drm_device *dev = crtc->base.dev;
6172 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006173
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006174 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006175 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006176
6177 assert_pipe_disabled(dev_priv, crtc->pipe);
6178
Daniel Vetter328d8e82013-05-08 10:36:31 +02006179 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6180 I915_READ(PFIT_CONTROL));
6181 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006182}
6183
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006184static void i9xx_crtc_disable(struct drm_crtc *crtc)
6185{
6186 struct drm_device *dev = crtc->dev;
6187 struct drm_i915_private *dev_priv = dev->dev_private;
6188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006189 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006190 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006191
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006192 /*
6193 * On gen2 planes are double buffered but the pipe isn't, so we must
6194 * wait for planes to fully turn off before disabling the pipe.
6195 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006196 if (IS_GEN2(dev))
6197 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006198
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006199 for_each_encoder_on_crtc(dev, crtc, encoder)
6200 encoder->disable(encoder);
6201
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006202 drm_crtc_vblank_off(crtc);
6203 assert_vblank_disabled(crtc);
6204
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006205 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006206
Daniel Vetter87476d62013-04-11 16:29:06 +02006207 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006208
Jesse Barnes89b667f2013-04-18 14:51:36 -07006209 for_each_encoder_on_crtc(dev, crtc, encoder)
6210 if (encoder->post_disable)
6211 encoder->post_disable(encoder);
6212
Jani Nikulaa65347b2015-11-27 12:21:46 +02006213 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006214 if (IS_CHERRYVIEW(dev))
6215 chv_disable_pll(dev_priv, pipe);
6216 else if (IS_VALLEYVIEW(dev))
6217 vlv_disable_pll(dev_priv, pipe);
6218 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006219 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006220 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006221
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 if (encoder->post_pll_disable)
6224 encoder->post_pll_disable(encoder);
6225
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006226 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006227 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006228}
6229
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006230static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006231{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006232 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006234 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006235 enum intel_display_power_domain domain;
6236 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006237
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006238 if (!intel_crtc->active)
6239 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006240
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006241 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006242 WARN_ON(intel_crtc->unpin_work);
6243
Ville Syrjälä2622a082016-03-09 19:07:26 +02006244 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006245
6246 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6247 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006248 }
6249
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006250 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006251
6252 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6253 crtc->base.id);
6254
6255 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6256 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006257 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006258 crtc->enabled = false;
6259 crtc->state->connector_mask = 0;
6260 crtc->state->encoder_mask = 0;
6261
6262 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6263 encoder->base.crtc = NULL;
6264
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006265 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006266 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006267 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006268
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006269 domains = intel_crtc->enabled_power_domains;
6270 for_each_power_domain(domain, domains)
6271 intel_display_power_put(dev_priv, domain);
6272 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006273
6274 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6275 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006276}
6277
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006278/*
6279 * turn all crtc's off, but do not adjust state
6280 * This has to be paired with a call to intel_modeset_setup_hw_state.
6281 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006282int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006283{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006284 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006285 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006286 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006287
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006288 state = drm_atomic_helper_suspend(dev);
6289 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006290 if (ret)
6291 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006292 else
6293 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006294 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006295}
6296
Chris Wilsonea5b2132010-08-04 13:50:23 +01006297void intel_encoder_destroy(struct drm_encoder *encoder)
6298{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006299 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006300
Chris Wilsonea5b2132010-08-04 13:50:23 +01006301 drm_encoder_cleanup(encoder);
6302 kfree(intel_encoder);
6303}
6304
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006305/* Cross check the actual hw state with our own modeset state tracking (and it's
6306 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006307static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006308{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006309 struct drm_crtc *crtc = connector->base.state->crtc;
6310
6311 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6312 connector->base.base.id,
6313 connector->base.name);
6314
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006315 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006316 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006317 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006318
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006319 I915_STATE_WARN(!crtc,
6320 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006321
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006322 if (!crtc)
6323 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006324
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 I915_STATE_WARN(!crtc->state->active,
6326 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006328 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006329 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006330
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006331 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006332 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006333
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006334 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006335 "attached encoder crtc differs from connector crtc\n");
6336 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006337 I915_STATE_WARN(crtc && crtc->state->active,
6338 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006339 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6340 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006341 }
6342}
6343
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006344int intel_connector_init(struct intel_connector *connector)
6345{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006346 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006347
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006348 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006349 return -ENOMEM;
6350
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006351 return 0;
6352}
6353
6354struct intel_connector *intel_connector_alloc(void)
6355{
6356 struct intel_connector *connector;
6357
6358 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6359 if (!connector)
6360 return NULL;
6361
6362 if (intel_connector_init(connector) < 0) {
6363 kfree(connector);
6364 return NULL;
6365 }
6366
6367 return connector;
6368}
6369
Daniel Vetterf0947c32012-07-02 13:10:34 +02006370/* Simple connector->get_hw_state implementation for encoders that support only
6371 * one connector and no cloning and hence the encoder state determines the state
6372 * of the connector. */
6373bool intel_connector_get_hw_state(struct intel_connector *connector)
6374{
Daniel Vetter24929352012-07-02 20:28:59 +02006375 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006376 struct intel_encoder *encoder = connector->encoder;
6377
6378 return encoder->get_hw_state(encoder, &pipe);
6379}
6380
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006381static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006382{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006383 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6384 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006385
6386 return 0;
6387}
6388
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006390 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006392 struct drm_atomic_state *state = pipe_config->base.state;
6393 struct intel_crtc *other_crtc;
6394 struct intel_crtc_state *other_crtc_state;
6395
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006396 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6397 pipe_name(pipe), pipe_config->fdi_lanes);
6398 if (pipe_config->fdi_lanes > 4) {
6399 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6400 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402 }
6403
Paulo Zanonibafb6552013-11-02 21:07:44 -07006404 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006405 if (pipe_config->fdi_lanes > 2) {
6406 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6407 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006409 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411 }
6412 }
6413
6414 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416
6417 /* Ivybridge 3 pipe is really complicated */
6418 switch (pipe) {
6419 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006421 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 if (pipe_config->fdi_lanes <= 2)
6423 return 0;
6424
6425 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6426 other_crtc_state =
6427 intel_atomic_get_crtc_state(state, other_crtc);
6428 if (IS_ERR(other_crtc_state))
6429 return PTR_ERR(other_crtc_state);
6430
6431 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6433 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006435 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006438 if (pipe_config->fdi_lanes > 2) {
6439 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6440 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006441 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006442 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443
6444 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6445 other_crtc_state =
6446 intel_atomic_get_crtc_state(state, other_crtc);
6447 if (IS_ERR(other_crtc_state))
6448 return PTR_ERR(other_crtc_state);
6449
6450 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006451 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006455 default:
6456 BUG();
6457 }
6458}
6459
Daniel Vettere29c22c2013-02-21 00:00:16 +01006460#define RETRY 1
6461static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006462 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006463{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006465 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006466 int lane, link_bw, fdi_dotclock, ret;
6467 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006468
Daniel Vettere29c22c2013-02-21 00:00:16 +01006469retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006470 /* FDI is a binary signal running at ~2.7GHz, encoding
6471 * each output octet as 10 bits. The actual frequency
6472 * is stored as a divider into a 100MHz clock, and the
6473 * mode pixel clock is stored in units of 1KHz.
6474 * Hence the bw of each lane in terms of the mode signal
6475 * is:
6476 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006477 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006478
Damien Lespiau241bfc32013-09-25 16:45:37 +01006479 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006480
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006481 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006482 pipe_config->pipe_bpp);
6483
6484 pipe_config->fdi_lanes = lane;
6485
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006486 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006487 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006488
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006489 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006491 pipe_config->pipe_bpp -= 2*3;
6492 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6493 pipe_config->pipe_bpp);
6494 needs_recompute = true;
6495 pipe_config->bw_constrained = true;
6496
6497 goto retry;
6498 }
6499
6500 if (needs_recompute)
6501 return RETRY;
6502
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006504}
6505
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006506static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6507 struct intel_crtc_state *pipe_config)
6508{
6509 if (pipe_config->pipe_bpp > 24)
6510 return false;
6511
6512 /* HSW can handle pixel rate up to cdclk? */
6513 if (IS_HASWELL(dev_priv->dev))
6514 return true;
6515
6516 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006517 * We compare against max which means we must take
6518 * the increased cdclk requirement into account when
6519 * calculating the new cdclk.
6520 *
6521 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006522 */
6523 return ilk_pipe_pixel_rate(pipe_config) <=
6524 dev_priv->max_cdclk_freq * 95 / 100;
6525}
6526
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006527static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006528 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006529{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006530 struct drm_device *dev = crtc->base.dev;
6531 struct drm_i915_private *dev_priv = dev->dev_private;
6532
Jani Nikulad330a952014-01-21 11:24:25 +02006533 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006534 hsw_crtc_supports_ips(crtc) &&
6535 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006536}
6537
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006538static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6539{
6540 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6541
6542 /* GDG double wide on either pipe, otherwise pipe A only */
6543 return INTEL_INFO(dev_priv)->gen < 4 &&
6544 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6545}
6546
Daniel Vettera43f6e02013-06-07 23:10:32 +02006547static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006548 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006549{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006550 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006551 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006552 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006553
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006554 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006555 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006556 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006557
6558 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006559 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006560 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006561 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006562 if (intel_crtc_supports_double_wide(crtc) &&
6563 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006564 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006565 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006566 }
6567
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006568 if (adjusted_mode->crtc_clock > clock_limit) {
6569 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6570 adjusted_mode->crtc_clock, clock_limit,
6571 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006572 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006573 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006574 }
Chris Wilson89749352010-09-12 18:25:19 +01006575
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006576 /*
6577 * Pipe horizontal size must be even in:
6578 * - DVO ganged mode
6579 * - LVDS dual channel mode
6580 * - Double wide pipe
6581 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006582 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006583 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6584 pipe_config->pipe_src_w &= ~1;
6585
Damien Lespiau8693a822013-05-03 18:48:11 +01006586 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6587 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006588 */
6589 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006590 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006591 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006592
Damien Lespiauf5adf942013-06-24 18:29:34 +01006593 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006594 hsw_compute_ips_config(crtc, pipe_config);
6595
Daniel Vetter877d48d2013-04-19 11:24:43 +02006596 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006597 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006598
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006599 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006600}
6601
Ville Syrjälä1652d192015-03-31 14:12:01 +03006602static int skylake_get_display_clock_speed(struct drm_device *dev)
6603{
6604 struct drm_i915_private *dev_priv = to_i915(dev);
6605 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6606 uint32_t cdctl = I915_READ(CDCLK_CTL);
6607 uint32_t linkrate;
6608
Damien Lespiau414355a2015-06-04 18:21:31 +01006609 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006610 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006611
6612 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6613 return 540000;
6614
6615 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006616 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006617
Damien Lespiau71cd8422015-04-30 16:39:17 +01006618 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6619 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006620 /* vco 8640 */
6621 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6622 case CDCLK_FREQ_450_432:
6623 return 432000;
6624 case CDCLK_FREQ_337_308:
6625 return 308570;
6626 case CDCLK_FREQ_675_617:
6627 return 617140;
6628 default:
6629 WARN(1, "Unknown cd freq selection\n");
6630 }
6631 } else {
6632 /* vco 8100 */
6633 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6634 case CDCLK_FREQ_450_432:
6635 return 450000;
6636 case CDCLK_FREQ_337_308:
6637 return 337500;
6638 case CDCLK_FREQ_675_617:
6639 return 675000;
6640 default:
6641 WARN(1, "Unknown cd freq selection\n");
6642 }
6643 }
6644
6645 /* error case, do as if DPLL0 isn't enabled */
6646 return 24000;
6647}
6648
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006649static int broxton_get_display_clock_speed(struct drm_device *dev)
6650{
6651 struct drm_i915_private *dev_priv = to_i915(dev);
6652 uint32_t cdctl = I915_READ(CDCLK_CTL);
6653 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6654 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6655 int cdclk;
6656
6657 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6658 return 19200;
6659
6660 cdclk = 19200 * pll_ratio / 2;
6661
6662 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6663 case BXT_CDCLK_CD2X_DIV_SEL_1:
6664 return cdclk; /* 576MHz or 624MHz */
6665 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6666 return cdclk * 2 / 3; /* 384MHz */
6667 case BXT_CDCLK_CD2X_DIV_SEL_2:
6668 return cdclk / 2; /* 288MHz */
6669 case BXT_CDCLK_CD2X_DIV_SEL_4:
6670 return cdclk / 4; /* 144MHz */
6671 }
6672
6673 /* error case, do as if DE PLL isn't enabled */
6674 return 19200;
6675}
6676
Ville Syrjälä1652d192015-03-31 14:12:01 +03006677static int broadwell_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = dev->dev_private;
6680 uint32_t lcpll = I915_READ(LCPLL_CTL);
6681 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6682
6683 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6684 return 800000;
6685 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6686 return 450000;
6687 else if (freq == LCPLL_CLK_FREQ_450)
6688 return 450000;
6689 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6690 return 540000;
6691 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6692 return 337500;
6693 else
6694 return 675000;
6695}
6696
6697static int haswell_get_display_clock_speed(struct drm_device *dev)
6698{
6699 struct drm_i915_private *dev_priv = dev->dev_private;
6700 uint32_t lcpll = I915_READ(LCPLL_CTL);
6701 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6702
6703 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6704 return 800000;
6705 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6706 return 450000;
6707 else if (freq == LCPLL_CLK_FREQ_450)
6708 return 450000;
6709 else if (IS_HSW_ULT(dev))
6710 return 337500;
6711 else
6712 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006713}
6714
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006715static int valleyview_get_display_clock_speed(struct drm_device *dev)
6716{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006717 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6718 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006719}
6720
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006721static int ilk_get_display_clock_speed(struct drm_device *dev)
6722{
6723 return 450000;
6724}
6725
Jesse Barnese70236a2009-09-21 10:42:27 -07006726static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006727{
Jesse Barnese70236a2009-09-21 10:42:27 -07006728 return 400000;
6729}
Jesse Barnes79e53942008-11-07 14:24:08 -08006730
Jesse Barnese70236a2009-09-21 10:42:27 -07006731static int i915_get_display_clock_speed(struct drm_device *dev)
6732{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006733 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006734}
Jesse Barnes79e53942008-11-07 14:24:08 -08006735
Jesse Barnese70236a2009-09-21 10:42:27 -07006736static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6737{
6738 return 200000;
6739}
Jesse Barnes79e53942008-11-07 14:24:08 -08006740
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006741static int pnv_get_display_clock_speed(struct drm_device *dev)
6742{
6743 u16 gcfgc = 0;
6744
6745 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6746
6747 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6748 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006749 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006750 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006751 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006752 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006753 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006754 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6755 return 200000;
6756 default:
6757 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6758 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006759 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006760 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006761 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006762 }
6763}
6764
Jesse Barnese70236a2009-09-21 10:42:27 -07006765static int i915gm_get_display_clock_speed(struct drm_device *dev)
6766{
6767 u16 gcfgc = 0;
6768
6769 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6770
6771 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006773 else {
6774 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6775 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006776 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006777 default:
6778 case GC_DISPLAY_CLOCK_190_200_MHZ:
6779 return 190000;
6780 }
6781 }
6782}
Jesse Barnes79e53942008-11-07 14:24:08 -08006783
Jesse Barnese70236a2009-09-21 10:42:27 -07006784static int i865_get_display_clock_speed(struct drm_device *dev)
6785{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006786 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006787}
6788
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006789static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006790{
6791 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006792
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006793 /*
6794 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6795 * encoding is different :(
6796 * FIXME is this the right way to detect 852GM/852GMV?
6797 */
6798 if (dev->pdev->revision == 0x1)
6799 return 133333;
6800
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006801 pci_bus_read_config_word(dev->pdev->bus,
6802 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6803
Jesse Barnese70236a2009-09-21 10:42:27 -07006804 /* Assume that the hardware is in the high speed state. This
6805 * should be the default.
6806 */
6807 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6808 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006809 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006810 case GC_CLOCK_100_200:
6811 return 200000;
6812 case GC_CLOCK_166_250:
6813 return 250000;
6814 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006815 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006816 case GC_CLOCK_133_266:
6817 case GC_CLOCK_133_266_2:
6818 case GC_CLOCK_166_266:
6819 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006820 }
6821
6822 /* Shouldn't happen */
6823 return 0;
6824}
6825
6826static int i830_get_display_clock_speed(struct drm_device *dev)
6827{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006828 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006829}
6830
Ville Syrjälä34edce22015-05-22 11:22:33 +03006831static unsigned int intel_hpll_vco(struct drm_device *dev)
6832{
6833 struct drm_i915_private *dev_priv = dev->dev_private;
6834 static const unsigned int blb_vco[8] = {
6835 [0] = 3200000,
6836 [1] = 4000000,
6837 [2] = 5333333,
6838 [3] = 4800000,
6839 [4] = 6400000,
6840 };
6841 static const unsigned int pnv_vco[8] = {
6842 [0] = 3200000,
6843 [1] = 4000000,
6844 [2] = 5333333,
6845 [3] = 4800000,
6846 [4] = 2666667,
6847 };
6848 static const unsigned int cl_vco[8] = {
6849 [0] = 3200000,
6850 [1] = 4000000,
6851 [2] = 5333333,
6852 [3] = 6400000,
6853 [4] = 3333333,
6854 [5] = 3566667,
6855 [6] = 4266667,
6856 };
6857 static const unsigned int elk_vco[8] = {
6858 [0] = 3200000,
6859 [1] = 4000000,
6860 [2] = 5333333,
6861 [3] = 4800000,
6862 };
6863 static const unsigned int ctg_vco[8] = {
6864 [0] = 3200000,
6865 [1] = 4000000,
6866 [2] = 5333333,
6867 [3] = 6400000,
6868 [4] = 2666667,
6869 [5] = 4266667,
6870 };
6871 const unsigned int *vco_table;
6872 unsigned int vco;
6873 uint8_t tmp = 0;
6874
6875 /* FIXME other chipsets? */
6876 if (IS_GM45(dev))
6877 vco_table = ctg_vco;
6878 else if (IS_G4X(dev))
6879 vco_table = elk_vco;
6880 else if (IS_CRESTLINE(dev))
6881 vco_table = cl_vco;
6882 else if (IS_PINEVIEW(dev))
6883 vco_table = pnv_vco;
6884 else if (IS_G33(dev))
6885 vco_table = blb_vco;
6886 else
6887 return 0;
6888
6889 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6890
6891 vco = vco_table[tmp & 0x7];
6892 if (vco == 0)
6893 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6894 else
6895 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6896
6897 return vco;
6898}
6899
6900static int gm45_get_display_clock_speed(struct drm_device *dev)
6901{
6902 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6903 uint16_t tmp = 0;
6904
6905 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6906
6907 cdclk_sel = (tmp >> 12) & 0x1;
6908
6909 switch (vco) {
6910 case 2666667:
6911 case 4000000:
6912 case 5333333:
6913 return cdclk_sel ? 333333 : 222222;
6914 case 3200000:
6915 return cdclk_sel ? 320000 : 228571;
6916 default:
6917 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6918 return 222222;
6919 }
6920}
6921
6922static int i965gm_get_display_clock_speed(struct drm_device *dev)
6923{
6924 static const uint8_t div_3200[] = { 16, 10, 8 };
6925 static const uint8_t div_4000[] = { 20, 12, 10 };
6926 static const uint8_t div_5333[] = { 24, 16, 14 };
6927 const uint8_t *div_table;
6928 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6929 uint16_t tmp = 0;
6930
6931 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6932
6933 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6934
6935 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6936 goto fail;
6937
6938 switch (vco) {
6939 case 3200000:
6940 div_table = div_3200;
6941 break;
6942 case 4000000:
6943 div_table = div_4000;
6944 break;
6945 case 5333333:
6946 div_table = div_5333;
6947 break;
6948 default:
6949 goto fail;
6950 }
6951
6952 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6953
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006954fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006955 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6956 return 200000;
6957}
6958
6959static int g33_get_display_clock_speed(struct drm_device *dev)
6960{
6961 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6962 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6963 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6964 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6965 const uint8_t *div_table;
6966 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6967 uint16_t tmp = 0;
6968
6969 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6970
6971 cdclk_sel = (tmp >> 4) & 0x7;
6972
6973 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6974 goto fail;
6975
6976 switch (vco) {
6977 case 3200000:
6978 div_table = div_3200;
6979 break;
6980 case 4000000:
6981 div_table = div_4000;
6982 break;
6983 case 4800000:
6984 div_table = div_4800;
6985 break;
6986 case 5333333:
6987 div_table = div_5333;
6988 break;
6989 default:
6990 goto fail;
6991 }
6992
6993 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6994
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006995fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006996 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6997 return 190476;
6998}
6999
Zhenyu Wang2c072452009-06-05 15:38:42 +08007000static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007001intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007002{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007003 while (*num > DATA_LINK_M_N_MASK ||
7004 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007005 *num >>= 1;
7006 *den >>= 1;
7007 }
7008}
7009
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007010static void compute_m_n(unsigned int m, unsigned int n,
7011 uint32_t *ret_m, uint32_t *ret_n)
7012{
7013 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7014 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7015 intel_reduce_m_n_ratio(ret_m, ret_n);
7016}
7017
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007018void
7019intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7020 int pixel_clock, int link_clock,
7021 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007022{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007023 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007024
7025 compute_m_n(bits_per_pixel * pixel_clock,
7026 link_clock * nlanes * 8,
7027 &m_n->gmch_m, &m_n->gmch_n);
7028
7029 compute_m_n(pixel_clock, link_clock,
7030 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007031}
7032
Chris Wilsona7615032011-01-12 17:04:08 +00007033static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7034{
Jani Nikulad330a952014-01-21 11:24:25 +02007035 if (i915.panel_use_ssc >= 0)
7036 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007037 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007038 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007039}
7040
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007041static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007042{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007043 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007044 struct drm_i915_private *dev_priv = dev->dev_private;
7045 int refclk;
7046
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007047 WARN_ON(!crtc_state->base.state);
7048
Wayne Boyer666a4532015-12-09 12:29:35 -08007049 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007050 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007051 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007052 intel_panel_use_ssc(dev_priv)) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007053 refclk = dev_priv->vbt.lvds_ssc_freq;
7054 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007055 } else if (!IS_GEN2(dev)) {
7056 refclk = 96000;
7057 } else {
7058 refclk = 48000;
7059 }
7060
7061 return refclk;
7062}
7063
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007064static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007065{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007066 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007067}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007068
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007069static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7070{
7071 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007072}
7073
Daniel Vetterf47709a2013-03-28 10:42:02 +01007074static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007075 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007076 intel_clock_t *reduced_clock)
7077{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007078 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007079 u32 fp, fp2 = 0;
7080
7081 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007082 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007083 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007084 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007085 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007086 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007087 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007088 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007089 }
7090
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007091 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007092
Daniel Vetterf47709a2013-03-28 10:42:02 +01007093 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007094 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007095 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007096 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007097 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007098 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007099 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007100 }
7101}
7102
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007103static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7104 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007105{
7106 u32 reg_val;
7107
7108 /*
7109 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7110 * and set it to a reasonable value instead.
7111 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007112 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113 reg_val &= 0xffffff00;
7114 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007115 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007116
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007117 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007118 reg_val &= 0x8cffffff;
7119 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007120 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007121
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007122 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007123 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007124 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007125
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007126 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007127 reg_val &= 0x00ffffff;
7128 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007129 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007130}
7131
Daniel Vetterb5518422013-05-03 11:49:48 +02007132static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7133 struct intel_link_m_n *m_n)
7134{
7135 struct drm_device *dev = crtc->base.dev;
7136 struct drm_i915_private *dev_priv = dev->dev_private;
7137 int pipe = crtc->pipe;
7138
Daniel Vettere3b95f12013-05-03 11:49:49 +02007139 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7140 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7141 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7142 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007143}
7144
7145static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007146 struct intel_link_m_n *m_n,
7147 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007148{
7149 struct drm_device *dev = crtc->base.dev;
7150 struct drm_i915_private *dev_priv = dev->dev_private;
7151 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007152 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007153
7154 if (INTEL_INFO(dev)->gen >= 5) {
7155 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7156 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7157 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7158 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007159 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7160 * for gen < 8) and if DRRS is supported (to make sure the
7161 * registers are not unnecessarily accessed).
7162 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307163 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007164 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007165 I915_WRITE(PIPE_DATA_M2(transcoder),
7166 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7167 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7168 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7169 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7170 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007171 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007172 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7173 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7174 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7175 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007176 }
7177}
7178
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307179void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007180{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307181 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7182
7183 if (m_n == M1_N1) {
7184 dp_m_n = &crtc->config->dp_m_n;
7185 dp_m2_n2 = &crtc->config->dp_m2_n2;
7186 } else if (m_n == M2_N2) {
7187
7188 /*
7189 * M2_N2 registers are not supported. Hence m2_n2 divider value
7190 * needs to be programmed into M1_N1.
7191 */
7192 dp_m_n = &crtc->config->dp_m2_n2;
7193 } else {
7194 DRM_ERROR("Unsupported divider value\n");
7195 return;
7196 }
7197
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007198 if (crtc->config->has_pch_encoder)
7199 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007200 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307201 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007202}
7203
Daniel Vetter251ac862015-06-18 10:30:24 +02007204static void vlv_compute_dpll(struct intel_crtc *crtc,
7205 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007206{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007207 u32 dpll, dpll_md;
7208
7209 /*
7210 * Enable DPIO clock input. We should never disable the reference
7211 * clock for pipe B, since VGA hotplug / manual detection depends
7212 * on it.
7213 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007214 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7215 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007216 /* We should never disable this, set it here for state tracking */
7217 if (crtc->pipe == PIPE_B)
7218 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7219 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007220 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007221
Ville Syrjäläd288f652014-10-28 13:20:22 +02007222 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007223 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007224 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007225}
7226
Ville Syrjäläd288f652014-10-28 13:20:22 +02007227static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007228 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007229{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007230 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007231 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007232 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007233 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007234 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007235 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007236
Ville Syrjäläa5805162015-05-26 20:42:30 +03007237 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007238
Ville Syrjäläd288f652014-10-28 13:20:22 +02007239 bestn = pipe_config->dpll.n;
7240 bestm1 = pipe_config->dpll.m1;
7241 bestm2 = pipe_config->dpll.m2;
7242 bestp1 = pipe_config->dpll.p1;
7243 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007244
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245 /* See eDP HDMI DPIO driver vbios notes doc */
7246
7247 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007248 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007249 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250
7251 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253
7254 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258
7259 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007260 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007261
7262 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007263 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7264 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7265 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007266 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007267
7268 /*
7269 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7270 * but we don't support that).
7271 * Note: don't use the DAC post divider as it seems unstable.
7272 */
7273 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007276 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007277 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007278
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007280 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007281 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7282 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007283 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007284 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007288
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007289 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007291 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293 0x0df40000);
7294 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007295 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007296 0x0df70000);
7297 } else { /* HDMI or VGA */
7298 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007299 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007300 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007301 0x0df70000);
7302 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007303 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007304 0x0df40000);
7305 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007306
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007307 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007308 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007309 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7310 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007311 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007313
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007314 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007315 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007316}
7317
Daniel Vetter251ac862015-06-18 10:30:24 +02007318static void chv_compute_dpll(struct intel_crtc *crtc,
7319 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007320{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007321 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7322 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007323 DPLL_VCO_ENABLE;
7324 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007325 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007326
Ville Syrjäläd288f652014-10-28 13:20:22 +02007327 pipe_config->dpll_hw_state.dpll_md =
7328 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007329}
7330
Ville Syrjäläd288f652014-10-28 13:20:22 +02007331static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007332 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007333{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007334 struct drm_device *dev = crtc->base.dev;
7335 struct drm_i915_private *dev_priv = dev->dev_private;
7336 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007337 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007338 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307339 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007340 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307341 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307342 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007343
Ville Syrjäläd288f652014-10-28 13:20:22 +02007344 bestn = pipe_config->dpll.n;
7345 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7346 bestm1 = pipe_config->dpll.m1;
7347 bestm2 = pipe_config->dpll.m2 >> 22;
7348 bestp1 = pipe_config->dpll.p1;
7349 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307350 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307351 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307352 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007353
7354 /*
7355 * Enable Refclk and SSC
7356 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007357 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007358 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007359
Ville Syrjäläa5805162015-05-26 20:42:30 +03007360 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007361
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007362 /* p1 and p2 divider */
7363 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7364 5 << DPIO_CHV_S1_DIV_SHIFT |
7365 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7366 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7367 1 << DPIO_CHV_K_DIV_SHIFT);
7368
7369 /* Feedback post-divider - m2 */
7370 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7371
7372 /* Feedback refclk divider - n and m1 */
7373 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7374 DPIO_CHV_M1_DIV_BY_2 |
7375 1 << DPIO_CHV_N_DIV_SHIFT);
7376
7377 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007378 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007379
7380 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307381 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7382 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7383 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7384 if (bestm2_frac)
7385 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7386 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007387
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307388 /* Program digital lock detect threshold */
7389 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7390 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7391 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7392 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7393 if (!bestm2_frac)
7394 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7395 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7396
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307398 if (vco == 5400000) {
7399 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7400 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7401 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7402 tribuf_calcntr = 0x9;
7403 } else if (vco <= 6200000) {
7404 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7405 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7406 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7407 tribuf_calcntr = 0x9;
7408 } else if (vco <= 6480000) {
7409 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7410 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7411 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7412 tribuf_calcntr = 0x8;
7413 } else {
7414 /* Not supported. Apply the same limits as in the max case */
7415 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7416 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7417 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7418 tribuf_calcntr = 0;
7419 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007420 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7421
Ville Syrjälä968040b2015-03-11 22:52:08 +02007422 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307423 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7424 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7425 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7426
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007427 /* AFC Recal */
7428 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7429 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7430 DPIO_AFC_RECAL);
7431
Ville Syrjäläa5805162015-05-26 20:42:30 +03007432 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007433}
7434
Ville Syrjäläd288f652014-10-28 13:20:22 +02007435/**
7436 * vlv_force_pll_on - forcibly enable just the PLL
7437 * @dev_priv: i915 private structure
7438 * @pipe: pipe PLL to enable
7439 * @dpll: PLL configuration
7440 *
7441 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7442 * in cases where we need the PLL enabled even when @pipe is not going to
7443 * be enabled.
7444 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007445int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7446 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007447{
7448 struct intel_crtc *crtc =
7449 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007450 struct intel_crtc_state *pipe_config;
7451
7452 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7453 if (!pipe_config)
7454 return -ENOMEM;
7455
7456 pipe_config->base.crtc = &crtc->base;
7457 pipe_config->pixel_multiplier = 1;
7458 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007459
7460 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007461 chv_compute_dpll(crtc, pipe_config);
7462 chv_prepare_pll(crtc, pipe_config);
7463 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007464 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007465 vlv_compute_dpll(crtc, pipe_config);
7466 vlv_prepare_pll(crtc, pipe_config);
7467 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007468 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007469
7470 kfree(pipe_config);
7471
7472 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007473}
7474
7475/**
7476 * vlv_force_pll_off - forcibly disable just the PLL
7477 * @dev_priv: i915 private structure
7478 * @pipe: pipe PLL to disable
7479 *
7480 * Disable the PLL for @pipe. To be used in cases where we need
7481 * the PLL enabled even when @pipe is not going to be enabled.
7482 */
7483void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7484{
7485 if (IS_CHERRYVIEW(dev))
7486 chv_disable_pll(to_i915(dev), pipe);
7487 else
7488 vlv_disable_pll(to_i915(dev), pipe);
7489}
7490
Daniel Vetter251ac862015-06-18 10:30:24 +02007491static void i9xx_compute_dpll(struct intel_crtc *crtc,
7492 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007493 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007494{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007495 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007496 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007497 u32 dpll;
7498 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007499 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007500
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007501 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307502
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007503 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7504 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007505
7506 dpll = DPLL_VGA_MODE_DIS;
7507
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007508 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509 dpll |= DPLLB_MODE_LVDS;
7510 else
7511 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007512
Daniel Vetteref1b4602013-06-01 17:17:04 +02007513 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007514 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007515 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007516 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007517
7518 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007519 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007520
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007521 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007522 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523
7524 /* compute bitmask from p1 value */
7525 if (IS_PINEVIEW(dev))
7526 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7527 else {
7528 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7529 if (IS_G4X(dev) && reduced_clock)
7530 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7531 }
7532 switch (clock->p2) {
7533 case 5:
7534 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7535 break;
7536 case 7:
7537 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7538 break;
7539 case 10:
7540 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7541 break;
7542 case 14:
7543 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7544 break;
7545 }
7546 if (INTEL_INFO(dev)->gen >= 4)
7547 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7548
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007549 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007551 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007552 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007553 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7554 else
7555 dpll |= PLL_REF_INPUT_DREFCLK;
7556
7557 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007558 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007559
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007560 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007561 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007562 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007563 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007564 }
7565}
7566
Daniel Vetter251ac862015-06-18 10:30:24 +02007567static void i8xx_compute_dpll(struct intel_crtc *crtc,
7568 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007569 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007571 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007572 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007574 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007575
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007576 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307577
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007578 dpll = DPLL_VGA_MODE_DIS;
7579
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007580 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007581 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7582 } else {
7583 if (clock->p1 == 2)
7584 dpll |= PLL_P1_DIVIDE_BY_TWO;
7585 else
7586 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7587 if (clock->p2 == 4)
7588 dpll |= PLL_P2_DIVIDE_BY_4;
7589 }
7590
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007591 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007592 dpll |= DPLL_DVO_2X_MODE;
7593
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007594 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007595 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007596 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7597 else
7598 dpll |= PLL_REF_INPUT_DREFCLK;
7599
7600 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007601 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007602}
7603
Daniel Vetter8a654f32013-06-01 17:16:22 +02007604static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605{
7606 struct drm_device *dev = intel_crtc->base.dev;
7607 struct drm_i915_private *dev_priv = dev->dev_private;
7608 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007609 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007610 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007611 uint32_t crtc_vtotal, crtc_vblank_end;
7612 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007613
7614 /* We need to be careful not to changed the adjusted mode, for otherwise
7615 * the hw state checker will get angry at the mismatch. */
7616 crtc_vtotal = adjusted_mode->crtc_vtotal;
7617 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007618
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007619 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007620 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007621 crtc_vtotal -= 1;
7622 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007623
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007624 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007625 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7626 else
7627 vsyncshift = adjusted_mode->crtc_hsync_start -
7628 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007629 if (vsyncshift < 0)
7630 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007631 }
7632
7633 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007634 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007635
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007636 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007637 (adjusted_mode->crtc_hdisplay - 1) |
7638 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007639 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007640 (adjusted_mode->crtc_hblank_start - 1) |
7641 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007642 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007643 (adjusted_mode->crtc_hsync_start - 1) |
7644 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7645
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007646 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007647 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007648 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007649 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007650 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007651 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007652 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007653 (adjusted_mode->crtc_vsync_start - 1) |
7654 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7655
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007656 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7657 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7658 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7659 * bits. */
7660 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7661 (pipe == PIPE_B || pipe == PIPE_C))
7662 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7663
Jani Nikulabc58be62016-03-18 17:05:39 +02007664}
7665
7666static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7667{
7668 struct drm_device *dev = intel_crtc->base.dev;
7669 struct drm_i915_private *dev_priv = dev->dev_private;
7670 enum pipe pipe = intel_crtc->pipe;
7671
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007672 /* pipesrc controls the size that is scaled from, which should
7673 * always be the user's requested size.
7674 */
7675 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007676 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7677 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007678}
7679
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007680static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007681 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007682{
7683 struct drm_device *dev = crtc->base.dev;
7684 struct drm_i915_private *dev_priv = dev->dev_private;
7685 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7686 uint32_t tmp;
7687
7688 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007689 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7690 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007691 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007692 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7693 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007694 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7696 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007697
7698 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007699 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7700 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007701 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007702 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7703 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007704 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007705 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7706 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007707
7708 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007709 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7710 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7711 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007712 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007713}
7714
7715static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7716 struct intel_crtc_state *pipe_config)
7717{
7718 struct drm_device *dev = crtc->base.dev;
7719 struct drm_i915_private *dev_priv = dev->dev_private;
7720 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007721
7722 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007723 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7724 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7725
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007726 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7727 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007728}
7729
Daniel Vetterf6a83282014-02-11 15:28:57 -08007730void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007731 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007732{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007733 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7734 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7735 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7736 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007737
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007738 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7739 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7740 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7741 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007742
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007743 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007744 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007745
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007746 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7747 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007748
7749 mode->hsync = drm_mode_hsync(mode);
7750 mode->vrefresh = drm_mode_vrefresh(mode);
7751 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007752}
7753
Daniel Vetter84b046f2013-02-19 18:48:54 +01007754static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7755{
7756 struct drm_device *dev = intel_crtc->base.dev;
7757 struct drm_i915_private *dev_priv = dev->dev_private;
7758 uint32_t pipeconf;
7759
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007760 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007761
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007762 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7763 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7764 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007765
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007766 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007767 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007768
Daniel Vetterff9ce462013-04-24 14:57:17 +02007769 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007770 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007771 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007772 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007773 pipeconf |= PIPECONF_DITHER_EN |
7774 PIPECONF_DITHER_TYPE_SP;
7775
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007776 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007777 case 18:
7778 pipeconf |= PIPECONF_6BPC;
7779 break;
7780 case 24:
7781 pipeconf |= PIPECONF_8BPC;
7782 break;
7783 case 30:
7784 pipeconf |= PIPECONF_10BPC;
7785 break;
7786 default:
7787 /* Case prevented by intel_choose_pipe_bpp_dither. */
7788 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007789 }
7790 }
7791
7792 if (HAS_PIPE_CXSR(dev)) {
7793 if (intel_crtc->lowfreq_avail) {
7794 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7795 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7796 } else {
7797 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007798 }
7799 }
7800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007801 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007802 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007803 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007804 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7805 else
7806 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7807 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007808 pipeconf |= PIPECONF_PROGRESSIVE;
7809
Wayne Boyer666a4532015-12-09 12:29:35 -08007810 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7811 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007812 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007813
Daniel Vetter84b046f2013-02-19 18:48:54 +01007814 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7815 POSTING_READ(PIPECONF(intel_crtc->pipe));
7816}
7817
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007818static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7819 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007820{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007821 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007822 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007823 int refclk;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007824 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007825 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08007826
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007827 memset(&crtc_state->dpll_hw_state, 0,
7828 sizeof(crtc_state->dpll_hw_state));
7829
Jani Nikulaa65347b2015-11-27 12:21:46 +02007830 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007831 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007832
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007833 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007834 refclk = i9xx_get_refclk(crtc_state);
Jani Nikulaf2335332013-09-13 11:03:09 +03007835
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007836 /*
7837 * Returns a set of divisors for the desired target clock with
7838 * the given refclk, or FALSE. The returned values represent
7839 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7840 * 2) / p1 / p2.
7841 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007842 limit = intel_limit(crtc_state, refclk);
7843 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007844 crtc_state->port_clock,
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02007845 refclk, NULL,
7846 &crtc_state->dpll);
Jani Nikulaf2335332013-09-13 11:03:09 +03007847 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007848 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7849 return -EINVAL;
7850 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007851 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007852
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007853 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007854 i8xx_compute_dpll(crtc, crtc_state, NULL);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007855 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007856 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007857 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007858 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007859 } else {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007860 i9xx_compute_dpll(crtc, crtc_state, NULL);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007861 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007862
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007863 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007864}
7865
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007866static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007867 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007868{
7869 struct drm_device *dev = crtc->base.dev;
7870 struct drm_i915_private *dev_priv = dev->dev_private;
7871 uint32_t tmp;
7872
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007873 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7874 return;
7875
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007876 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007877 if (!(tmp & PFIT_ENABLE))
7878 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007879
Daniel Vetter06922822013-07-11 13:35:40 +02007880 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007881 if (INTEL_INFO(dev)->gen < 4) {
7882 if (crtc->pipe != PIPE_B)
7883 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007884 } else {
7885 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7886 return;
7887 }
7888
Daniel Vetter06922822013-07-11 13:35:40 +02007889 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007890 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7891 if (INTEL_INFO(dev)->gen < 5)
7892 pipe_config->gmch_pfit.lvds_border_bits =
7893 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7894}
7895
Jesse Barnesacbec812013-09-20 11:29:32 -07007896static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007897 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007898{
7899 struct drm_device *dev = crtc->base.dev;
7900 struct drm_i915_private *dev_priv = dev->dev_private;
7901 int pipe = pipe_config->cpu_transcoder;
7902 intel_clock_t clock;
7903 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007904 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007905
Shobhit Kumarf573de52014-07-30 20:32:37 +05307906 /* In case of MIPI DPLL will not even be used */
7907 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7908 return;
7909
Ville Syrjäläa5805162015-05-26 20:42:30 +03007910 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007911 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007912 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007913
7914 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7915 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7916 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7917 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7918 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7919
Imre Deakdccbea32015-06-22 23:35:51 +03007920 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007921}
7922
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007923static void
7924i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7925 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007926{
7927 struct drm_device *dev = crtc->base.dev;
7928 struct drm_i915_private *dev_priv = dev->dev_private;
7929 u32 val, base, offset;
7930 int pipe = crtc->pipe, plane = crtc->plane;
7931 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007932 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007933 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007934 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007935
Damien Lespiau42a7b082015-02-05 19:35:13 +00007936 val = I915_READ(DSPCNTR(plane));
7937 if (!(val & DISPLAY_PLANE_ENABLE))
7938 return;
7939
Damien Lespiaud9806c92015-01-21 14:07:19 +00007940 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007941 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007942 DRM_DEBUG_KMS("failed to alloc fb\n");
7943 return;
7944 }
7945
Damien Lespiau1b842c82015-01-21 13:50:54 +00007946 fb = &intel_fb->base;
7947
Daniel Vetter18c52472015-02-10 17:16:09 +00007948 if (INTEL_INFO(dev)->gen >= 4) {
7949 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007950 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007951 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7952 }
7953 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007954
7955 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007956 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007957 fb->pixel_format = fourcc;
7958 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007959
7960 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007961 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007962 offset = I915_READ(DSPTILEOFF(plane));
7963 else
7964 offset = I915_READ(DSPLINOFF(plane));
7965 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7966 } else {
7967 base = I915_READ(DSPADDR(plane));
7968 }
7969 plane_config->base = base;
7970
7971 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007972 fb->width = ((val >> 16) & 0xfff) + 1;
7973 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007974
7975 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007976 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007977
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007978 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007979 fb->pixel_format,
7980 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007981
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007982 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007983
Damien Lespiau2844a922015-01-20 12:51:48 +00007984 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7985 pipe_name(pipe), plane, fb->width, fb->height,
7986 fb->bits_per_pixel, base, fb->pitches[0],
7987 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007988
Damien Lespiau2d140302015-02-05 17:22:18 +00007989 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007990}
7991
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007992static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007993 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007994{
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 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7999 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008000 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008001 int refclk = 100000;
8002
Ville Syrjäläa5805162015-05-26 20:42:30 +03008003 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008004 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8005 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8006 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8007 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008008 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008009 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008010
8011 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008012 clock.m2 = (pll_dw0 & 0xff) << 22;
8013 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8014 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008015 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8016 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8017 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8018
Imre Deakdccbea32015-06-22 23:35:51 +03008019 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008020}
8021
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008022static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008023 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008024{
8025 struct drm_device *dev = crtc->base.dev;
8026 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008027 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008028 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008029 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008030
Imre Deak17290502016-02-12 18:55:11 +02008031 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8032 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008033 return false;
8034
Daniel Vettere143a212013-07-04 12:01:15 +02008035 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008036 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008037
Imre Deak17290502016-02-12 18:55:11 +02008038 ret = false;
8039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008040 tmp = I915_READ(PIPECONF(crtc->pipe));
8041 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008042 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008043
Wayne Boyer666a4532015-12-09 12:29:35 -08008044 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008045 switch (tmp & PIPECONF_BPC_MASK) {
8046 case PIPECONF_6BPC:
8047 pipe_config->pipe_bpp = 18;
8048 break;
8049 case PIPECONF_8BPC:
8050 pipe_config->pipe_bpp = 24;
8051 break;
8052 case PIPECONF_10BPC:
8053 pipe_config->pipe_bpp = 30;
8054 break;
8055 default:
8056 break;
8057 }
8058 }
8059
Wayne Boyer666a4532015-12-09 12:29:35 -08008060 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8061 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008062 pipe_config->limited_color_range = true;
8063
Ville Syrjälä282740f2013-09-04 18:30:03 +03008064 if (INTEL_INFO(dev)->gen < 4)
8065 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8066
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008067 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008068 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008069
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008070 i9xx_get_pfit_config(crtc, pipe_config);
8071
Daniel Vetter6c49f242013-06-06 12:45:25 +02008072 if (INTEL_INFO(dev)->gen >= 4) {
8073 tmp = I915_READ(DPLL_MD(crtc->pipe));
8074 pipe_config->pixel_multiplier =
8075 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8076 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008077 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008078 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8079 tmp = I915_READ(DPLL(crtc->pipe));
8080 pipe_config->pixel_multiplier =
8081 ((tmp & SDVO_MULTIPLIER_MASK)
8082 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8083 } else {
8084 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8085 * port and will be fixed up in the encoder->get_config
8086 * function. */
8087 pipe_config->pixel_multiplier = 1;
8088 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008089 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008090 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008091 /*
8092 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8093 * on 830. Filter it out here so that we don't
8094 * report errors due to that.
8095 */
8096 if (IS_I830(dev))
8097 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8098
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008099 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8100 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008101 } else {
8102 /* Mask out read-only status bits. */
8103 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8104 DPLL_PORTC_READY_MASK |
8105 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008106 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008107
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008108 if (IS_CHERRYVIEW(dev))
8109 chv_crtc_clock_get(crtc, pipe_config);
8110 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008111 vlv_crtc_clock_get(crtc, pipe_config);
8112 else
8113 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008114
Ville Syrjälä0f646142015-08-26 19:39:18 +03008115 /*
8116 * Normally the dotclock is filled in by the encoder .get_config()
8117 * but in case the pipe is enabled w/o any ports we need a sane
8118 * default.
8119 */
8120 pipe_config->base.adjusted_mode.crtc_clock =
8121 pipe_config->port_clock / pipe_config->pixel_multiplier;
8122
Imre Deak17290502016-02-12 18:55:11 +02008123 ret = true;
8124
8125out:
8126 intel_display_power_put(dev_priv, power_domain);
8127
8128 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008129}
8130
Paulo Zanonidde86e22012-12-01 12:04:25 -02008131static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008132{
8133 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008134 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008135 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008136 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008137 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008138 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008139 bool has_ck505 = false;
8140 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008141
8142 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008143 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008144 switch (encoder->type) {
8145 case INTEL_OUTPUT_LVDS:
8146 has_panel = true;
8147 has_lvds = true;
8148 break;
8149 case INTEL_OUTPUT_EDP:
8150 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008151 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008152 has_cpu_edp = true;
8153 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008154 default:
8155 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008156 }
8157 }
8158
Keith Packard99eb6a02011-09-26 14:29:12 -07008159 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008160 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008161 can_ssc = has_ck505;
8162 } else {
8163 has_ck505 = false;
8164 can_ssc = true;
8165 }
8166
Imre Deak2de69052013-05-08 13:14:04 +03008167 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8168 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008169
8170 /* Ironlake: try to setup display ref clock before DPLL
8171 * enabling. This is only under driver's control after
8172 * PCH B stepping, previous chipset stepping should be
8173 * ignoring this setting.
8174 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008175 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008176
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008177 /* As we must carefully and slowly disable/enable each source in turn,
8178 * compute the final state we want first and check if we need to
8179 * make any changes at all.
8180 */
8181 final = val;
8182 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008183 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008184 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008185 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008186 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8187
8188 final &= ~DREF_SSC_SOURCE_MASK;
8189 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8190 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008191
Keith Packard199e5d72011-09-22 12:01:57 -07008192 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008193 final |= DREF_SSC_SOURCE_ENABLE;
8194
8195 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8196 final |= DREF_SSC1_ENABLE;
8197
8198 if (has_cpu_edp) {
8199 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8200 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8201 else
8202 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8203 } else
8204 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8205 } else {
8206 final |= DREF_SSC_SOURCE_DISABLE;
8207 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8208 }
8209
8210 if (final == val)
8211 return;
8212
8213 /* Always enable nonspread source */
8214 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8215
8216 if (has_ck505)
8217 val |= DREF_NONSPREAD_CK505_ENABLE;
8218 else
8219 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8220
8221 if (has_panel) {
8222 val &= ~DREF_SSC_SOURCE_MASK;
8223 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008224
Keith Packard199e5d72011-09-22 12:01:57 -07008225 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008226 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008227 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008228 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008229 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008230 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008231
8232 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008233 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008234 POSTING_READ(PCH_DREF_CONTROL);
8235 udelay(200);
8236
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008237 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008238
8239 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008240 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008241 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008242 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008243 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008244 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008245 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008246 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008247 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008248
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008249 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008250 POSTING_READ(PCH_DREF_CONTROL);
8251 udelay(200);
8252 } else {
8253 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8254
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008255 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008256
8257 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008258 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008259
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008261 POSTING_READ(PCH_DREF_CONTROL);
8262 udelay(200);
8263
8264 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008265 val &= ~DREF_SSC_SOURCE_MASK;
8266 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008267
8268 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008269 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008270
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008272 POSTING_READ(PCH_DREF_CONTROL);
8273 udelay(200);
8274 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275
8276 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008277}
8278
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008279static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008280{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008281 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008282
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008283 tmp = I915_READ(SOUTH_CHICKEN2);
8284 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8285 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008286
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008287 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8288 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8289 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008290
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008291 tmp = I915_READ(SOUTH_CHICKEN2);
8292 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8293 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008294
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008295 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8296 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8297 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008298}
8299
8300/* WaMPhyProgramming:hsw */
8301static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8302{
8303 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008304
8305 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8306 tmp &= ~(0xFF << 24);
8307 tmp |= (0x12 << 24);
8308 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8309
Paulo Zanonidde86e22012-12-01 12:04:25 -02008310 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8311 tmp |= (1 << 11);
8312 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8313
8314 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8315 tmp |= (1 << 11);
8316 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8317
Paulo Zanonidde86e22012-12-01 12:04:25 -02008318 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8319 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8320 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8321
8322 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8323 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8324 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8325
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008326 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8327 tmp &= ~(7 << 13);
8328 tmp |= (5 << 13);
8329 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008330
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008331 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8332 tmp &= ~(7 << 13);
8333 tmp |= (5 << 13);
8334 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008335
8336 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8337 tmp &= ~0xFF;
8338 tmp |= 0x1C;
8339 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8340
8341 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8342 tmp &= ~0xFF;
8343 tmp |= 0x1C;
8344 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8345
8346 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8347 tmp &= ~(0xFF << 16);
8348 tmp |= (0x1C << 16);
8349 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8350
8351 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8352 tmp &= ~(0xFF << 16);
8353 tmp |= (0x1C << 16);
8354 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8355
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008356 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8357 tmp |= (1 << 27);
8358 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008359
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008360 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8361 tmp |= (1 << 27);
8362 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008363
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008364 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8365 tmp &= ~(0xF << 28);
8366 tmp |= (4 << 28);
8367 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008369 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8370 tmp &= ~(0xF << 28);
8371 tmp |= (4 << 28);
8372 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008373}
8374
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008375/* Implements 3 different sequences from BSpec chapter "Display iCLK
8376 * Programming" based on the parameters passed:
8377 * - Sequence to enable CLKOUT_DP
8378 * - Sequence to enable CLKOUT_DP without spread
8379 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8380 */
8381static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8382 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008383{
8384 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008385 uint32_t reg, tmp;
8386
8387 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8388 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008389 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008390 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008391
Ville Syrjäläa5805162015-05-26 20:42:30 +03008392 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008393
8394 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8395 tmp &= ~SBI_SSCCTL_DISABLE;
8396 tmp |= SBI_SSCCTL_PATHALT;
8397 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8398
8399 udelay(24);
8400
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008401 if (with_spread) {
8402 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8403 tmp &= ~SBI_SSCCTL_PATHALT;
8404 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008405
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008406 if (with_fdi) {
8407 lpt_reset_fdi_mphy(dev_priv);
8408 lpt_program_fdi_mphy(dev_priv);
8409 }
8410 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008411
Ville Syrjäläc2699522015-08-27 23:55:59 +03008412 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008413 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8414 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8415 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008416
Ville Syrjäläa5805162015-05-26 20:42:30 +03008417 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418}
8419
Paulo Zanoni47701c32013-07-23 11:19:25 -03008420/* Sequence to disable CLKOUT_DP */
8421static void lpt_disable_clkout_dp(struct drm_device *dev)
8422{
8423 struct drm_i915_private *dev_priv = dev->dev_private;
8424 uint32_t reg, tmp;
8425
Ville Syrjäläa5805162015-05-26 20:42:30 +03008426 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008427
Ville Syrjäläc2699522015-08-27 23:55:59 +03008428 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008429 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8430 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8431 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8432
8433 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8434 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8435 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8436 tmp |= SBI_SSCCTL_PATHALT;
8437 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8438 udelay(32);
8439 }
8440 tmp |= SBI_SSCCTL_DISABLE;
8441 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8442 }
8443
Ville Syrjäläa5805162015-05-26 20:42:30 +03008444 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008445}
8446
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008447#define BEND_IDX(steps) ((50 + (steps)) / 5)
8448
8449static const uint16_t sscdivintphase[] = {
8450 [BEND_IDX( 50)] = 0x3B23,
8451 [BEND_IDX( 45)] = 0x3B23,
8452 [BEND_IDX( 40)] = 0x3C23,
8453 [BEND_IDX( 35)] = 0x3C23,
8454 [BEND_IDX( 30)] = 0x3D23,
8455 [BEND_IDX( 25)] = 0x3D23,
8456 [BEND_IDX( 20)] = 0x3E23,
8457 [BEND_IDX( 15)] = 0x3E23,
8458 [BEND_IDX( 10)] = 0x3F23,
8459 [BEND_IDX( 5)] = 0x3F23,
8460 [BEND_IDX( 0)] = 0x0025,
8461 [BEND_IDX( -5)] = 0x0025,
8462 [BEND_IDX(-10)] = 0x0125,
8463 [BEND_IDX(-15)] = 0x0125,
8464 [BEND_IDX(-20)] = 0x0225,
8465 [BEND_IDX(-25)] = 0x0225,
8466 [BEND_IDX(-30)] = 0x0325,
8467 [BEND_IDX(-35)] = 0x0325,
8468 [BEND_IDX(-40)] = 0x0425,
8469 [BEND_IDX(-45)] = 0x0425,
8470 [BEND_IDX(-50)] = 0x0525,
8471};
8472
8473/*
8474 * Bend CLKOUT_DP
8475 * steps -50 to 50 inclusive, in steps of 5
8476 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8477 * change in clock period = -(steps / 10) * 5.787 ps
8478 */
8479static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8480{
8481 uint32_t tmp;
8482 int idx = BEND_IDX(steps);
8483
8484 if (WARN_ON(steps % 5 != 0))
8485 return;
8486
8487 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8488 return;
8489
8490 mutex_lock(&dev_priv->sb_lock);
8491
8492 if (steps % 10 != 0)
8493 tmp = 0xAAAAAAAB;
8494 else
8495 tmp = 0x00000000;
8496 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8497
8498 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8499 tmp &= 0xffff0000;
8500 tmp |= sscdivintphase[idx];
8501 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8502
8503 mutex_unlock(&dev_priv->sb_lock);
8504}
8505
8506#undef BEND_IDX
8507
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008508static void lpt_init_pch_refclk(struct drm_device *dev)
8509{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008510 struct intel_encoder *encoder;
8511 bool has_vga = false;
8512
Damien Lespiaub2784e12014-08-05 11:29:37 +01008513 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008514 switch (encoder->type) {
8515 case INTEL_OUTPUT_ANALOG:
8516 has_vga = true;
8517 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008518 default:
8519 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008520 }
8521 }
8522
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008523 if (has_vga) {
8524 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008525 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008526 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008527 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008528 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008529}
8530
Paulo Zanonidde86e22012-12-01 12:04:25 -02008531/*
8532 * Initialize reference clocks when the driver loads
8533 */
8534void intel_init_pch_refclk(struct drm_device *dev)
8535{
8536 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8537 ironlake_init_pch_refclk(dev);
8538 else if (HAS_PCH_LPT(dev))
8539 lpt_init_pch_refclk(dev);
8540}
8541
Daniel Vetter6ff93602013-04-19 11:24:36 +02008542static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008543{
8544 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8546 int pipe = intel_crtc->pipe;
8547 uint32_t val;
8548
Daniel Vetter78114072013-06-13 00:54:57 +02008549 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008550
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008551 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008552 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008553 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008554 break;
8555 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008556 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008557 break;
8558 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008559 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008560 break;
8561 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008562 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008563 break;
8564 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008565 /* Case prevented by intel_choose_pipe_bpp_dither. */
8566 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008567 }
8568
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008569 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008570 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8571
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008572 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008573 val |= PIPECONF_INTERLACED_ILK;
8574 else
8575 val |= PIPECONF_PROGRESSIVE;
8576
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008577 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008578 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008579
Paulo Zanonic8203562012-09-12 10:06:29 -03008580 I915_WRITE(PIPECONF(pipe), val);
8581 POSTING_READ(PIPECONF(pipe));
8582}
8583
Daniel Vetter6ff93602013-04-19 11:24:36 +02008584static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008585{
Jani Nikula391bf042016-03-18 17:05:40 +02008586 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008588 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008589 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008590
Jani Nikula391bf042016-03-18 17:05:40 +02008591 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008592 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8593
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008594 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008595 val |= PIPECONF_INTERLACED_ILK;
8596 else
8597 val |= PIPECONF_PROGRESSIVE;
8598
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008599 I915_WRITE(PIPECONF(cpu_transcoder), val);
8600 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008601}
8602
Jani Nikula391bf042016-03-18 17:05:40 +02008603static void haswell_set_pipemisc(struct drm_crtc *crtc)
8604{
8605 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8607
8608 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8609 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008610
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008611 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008612 case 18:
8613 val |= PIPEMISC_DITHER_6_BPC;
8614 break;
8615 case 24:
8616 val |= PIPEMISC_DITHER_8_BPC;
8617 break;
8618 case 30:
8619 val |= PIPEMISC_DITHER_10_BPC;
8620 break;
8621 case 36:
8622 val |= PIPEMISC_DITHER_12_BPC;
8623 break;
8624 default:
8625 /* Case prevented by pipe_config_set_bpp. */
8626 BUG();
8627 }
8628
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008629 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008630 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8631
Jani Nikula391bf042016-03-18 17:05:40 +02008632 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008633 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008634}
8635
Paulo Zanonid4b19312012-11-29 11:29:32 -02008636int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8637{
8638 /*
8639 * Account for spread spectrum to avoid
8640 * oversubscribing the link. Max center spread
8641 * is 2.5%; use 5% for safety's sake.
8642 */
8643 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008644 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008645}
8646
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008647static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008648{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008649 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008650}
8651
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008652static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8653 struct intel_crtc_state *crtc_state,
8654 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008655{
8656 struct drm_crtc *crtc = &intel_crtc->base;
8657 struct drm_device *dev = crtc->dev;
8658 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008659 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008660 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008661 struct drm_connector_state *connector_state;
8662 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008663 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008664 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008665 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008666
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008667 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008668 if (connector_state->crtc != crtc_state->base.crtc)
8669 continue;
8670
8671 encoder = to_intel_encoder(connector_state->best_encoder);
8672
8673 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008674 case INTEL_OUTPUT_LVDS:
8675 is_lvds = true;
8676 break;
8677 case INTEL_OUTPUT_SDVO:
8678 case INTEL_OUTPUT_HDMI:
8679 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008680 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008681 default:
8682 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008683 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008684 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008685
Chris Wilsonc1858122010-12-03 21:35:48 +00008686 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008687 factor = 21;
8688 if (is_lvds) {
8689 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008690 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008691 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008692 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008693 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008694 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008695
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008696 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008697
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008698 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8699 fp |= FP_CB_TUNE;
8700
8701 if (reduced_clock) {
8702 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8703
8704 if (reduced_clock->m < factor * reduced_clock->n)
8705 fp2 |= FP_CB_TUNE;
8706 } else {
8707 fp2 = fp;
8708 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008709
Chris Wilson5eddb702010-09-11 13:48:45 +01008710 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008711
Eric Anholta07d6782011-03-30 13:01:08 -07008712 if (is_lvds)
8713 dpll |= DPLLB_MODE_LVDS;
8714 else
8715 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008716
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008717 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008718 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008719
8720 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008721 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008722 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008723 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008724
Eric Anholta07d6782011-03-30 13:01:08 -07008725 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008726 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008727 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008728 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008729
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008730 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008731 case 5:
8732 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8733 break;
8734 case 7:
8735 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8736 break;
8737 case 10:
8738 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8739 break;
8740 case 14:
8741 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8742 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008743 }
8744
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008745 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008746 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008747 else
8748 dpll |= PLL_REF_INPUT_DREFCLK;
8749
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008750 dpll |= DPLL_VCO_ENABLE;
8751
8752 crtc_state->dpll_hw_state.dpll = dpll;
8753 crtc_state->dpll_hw_state.fp0 = fp;
8754 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008755}
8756
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008757static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8758 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008759{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008760 struct drm_device *dev = crtc->base.dev;
8761 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008762 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008763 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008764 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008765 const intel_limit_t *limit;
8766 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008767
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008768 memset(&crtc_state->dpll_hw_state, 0,
8769 sizeof(crtc_state->dpll_hw_state));
8770
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008771 crtc->lowfreq_avail = false;
8772
8773 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8774 if (!crtc_state->has_pch_encoder)
8775 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008776
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008777 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8778 if (intel_panel_use_ssc(dev_priv)) {
8779 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8780 dev_priv->vbt.lvds_ssc_freq);
8781 refclk = dev_priv->vbt.lvds_ssc_freq;
8782 }
8783
8784 if (intel_is_dual_link_lvds(dev)) {
8785 if (refclk == 100000)
8786 limit = &intel_limits_ironlake_dual_lvds_100m;
8787 else
8788 limit = &intel_limits_ironlake_dual_lvds;
8789 } else {
8790 if (refclk == 100000)
8791 limit = &intel_limits_ironlake_single_lvds_100m;
8792 else
8793 limit = &intel_limits_ironlake_single_lvds;
8794 }
8795 } else {
8796 limit = &intel_limits_ironlake_dac;
8797 }
8798
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008799 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008800 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8801 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008802 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8803 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008804 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008805
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008806 ironlake_compute_dpll(crtc, crtc_state,
8807 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008808
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008809 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8810 if (pll == NULL) {
8811 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8812 pipe_name(crtc->pipe));
8813 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008814 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008815
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008816 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8817 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008818 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008819
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008820 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008821}
8822
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008823static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8824 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008825{
8826 struct drm_device *dev = crtc->base.dev;
8827 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008828 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008829
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008830 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8831 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8832 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8833 & ~TU_SIZE_MASK;
8834 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8835 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8836 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8837}
8838
8839static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8840 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008841 struct intel_link_m_n *m_n,
8842 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008843{
8844 struct drm_device *dev = crtc->base.dev;
8845 struct drm_i915_private *dev_priv = dev->dev_private;
8846 enum pipe pipe = crtc->pipe;
8847
8848 if (INTEL_INFO(dev)->gen >= 5) {
8849 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8850 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8851 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8852 & ~TU_SIZE_MASK;
8853 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8854 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8855 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008856 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8857 * gen < 8) and if DRRS is supported (to make sure the
8858 * registers are not unnecessarily read).
8859 */
8860 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008861 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008862 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8863 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8864 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8865 & ~TU_SIZE_MASK;
8866 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8867 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8868 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8869 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008870 } else {
8871 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8872 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8873 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8874 & ~TU_SIZE_MASK;
8875 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8876 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8877 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8878 }
8879}
8880
8881void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008882 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008883{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008884 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008885 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8886 else
8887 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008888 &pipe_config->dp_m_n,
8889 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008890}
8891
Daniel Vetter72419202013-04-04 13:28:53 +02008892static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008893 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008894{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008895 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008896 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008897}
8898
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008899static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008900 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008901{
8902 struct drm_device *dev = crtc->base.dev;
8903 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008904 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8905 uint32_t ps_ctrl = 0;
8906 int id = -1;
8907 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008908
Chandra Kondurua1b22782015-04-07 15:28:45 -07008909 /* find scaler attached to this pipe */
8910 for (i = 0; i < crtc->num_scalers; i++) {
8911 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8912 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8913 id = i;
8914 pipe_config->pch_pfit.enabled = true;
8915 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8916 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8917 break;
8918 }
8919 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008920
Chandra Kondurua1b22782015-04-07 15:28:45 -07008921 scaler_state->scaler_id = id;
8922 if (id >= 0) {
8923 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8924 } else {
8925 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008926 }
8927}
8928
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008929static void
8930skylake_get_initial_plane_config(struct intel_crtc *crtc,
8931 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008932{
8933 struct drm_device *dev = crtc->base.dev;
8934 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008935 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008936 int pipe = crtc->pipe;
8937 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008938 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008939 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008940 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008941
Damien Lespiaud9806c92015-01-21 14:07:19 +00008942 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008943 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008944 DRM_DEBUG_KMS("failed to alloc fb\n");
8945 return;
8946 }
8947
Damien Lespiau1b842c82015-01-21 13:50:54 +00008948 fb = &intel_fb->base;
8949
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008950 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008951 if (!(val & PLANE_CTL_ENABLE))
8952 goto error;
8953
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008954 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8955 fourcc = skl_format_to_fourcc(pixel_format,
8956 val & PLANE_CTL_ORDER_RGBX,
8957 val & PLANE_CTL_ALPHA_MASK);
8958 fb->pixel_format = fourcc;
8959 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8960
Damien Lespiau40f46282015-02-27 11:15:21 +00008961 tiling = val & PLANE_CTL_TILED_MASK;
8962 switch (tiling) {
8963 case PLANE_CTL_TILED_LINEAR:
8964 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8965 break;
8966 case PLANE_CTL_TILED_X:
8967 plane_config->tiling = I915_TILING_X;
8968 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8969 break;
8970 case PLANE_CTL_TILED_Y:
8971 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8972 break;
8973 case PLANE_CTL_TILED_YF:
8974 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8975 break;
8976 default:
8977 MISSING_CASE(tiling);
8978 goto error;
8979 }
8980
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008981 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8982 plane_config->base = base;
8983
8984 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8985
8986 val = I915_READ(PLANE_SIZE(pipe, 0));
8987 fb->height = ((val >> 16) & 0xfff) + 1;
8988 fb->width = ((val >> 0) & 0x1fff) + 1;
8989
8990 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02008991 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00008992 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008993 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8994
8995 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008996 fb->pixel_format,
8997 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008998
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008999 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009000
9001 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9002 pipe_name(pipe), fb->width, fb->height,
9003 fb->bits_per_pixel, base, fb->pitches[0],
9004 plane_config->size);
9005
Damien Lespiau2d140302015-02-05 17:22:18 +00009006 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009007 return;
9008
9009error:
9010 kfree(fb);
9011}
9012
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009013static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009014 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009015{
9016 struct drm_device *dev = crtc->base.dev;
9017 struct drm_i915_private *dev_priv = dev->dev_private;
9018 uint32_t tmp;
9019
9020 tmp = I915_READ(PF_CTL(crtc->pipe));
9021
9022 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009023 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009024 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9025 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009026
9027 /* We currently do not free assignements of panel fitters on
9028 * ivb/hsw (since we don't use the higher upscaling modes which
9029 * differentiates them) so just WARN about this case for now. */
9030 if (IS_GEN7(dev)) {
9031 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9032 PF_PIPE_SEL_IVB(crtc->pipe));
9033 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009034 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009035}
9036
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009037static void
9038ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9039 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009040{
9041 struct drm_device *dev = crtc->base.dev;
9042 struct drm_i915_private *dev_priv = dev->dev_private;
9043 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009044 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009045 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009046 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009047 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009048 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009049
Damien Lespiau42a7b082015-02-05 19:35:13 +00009050 val = I915_READ(DSPCNTR(pipe));
9051 if (!(val & DISPLAY_PLANE_ENABLE))
9052 return;
9053
Damien Lespiaud9806c92015-01-21 14:07:19 +00009054 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009055 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009056 DRM_DEBUG_KMS("failed to alloc fb\n");
9057 return;
9058 }
9059
Damien Lespiau1b842c82015-01-21 13:50:54 +00009060 fb = &intel_fb->base;
9061
Daniel Vetter18c52472015-02-10 17:16:09 +00009062 if (INTEL_INFO(dev)->gen >= 4) {
9063 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009064 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009065 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9066 }
9067 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009068
9069 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009070 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009071 fb->pixel_format = fourcc;
9072 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009073
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009074 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009075 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009076 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009077 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009078 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009079 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009080 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009081 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009082 }
9083 plane_config->base = base;
9084
9085 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009086 fb->width = ((val >> 16) & 0xfff) + 1;
9087 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009088
9089 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009090 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009091
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009092 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009093 fb->pixel_format,
9094 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009095
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009096 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009097
Damien Lespiau2844a922015-01-20 12:51:48 +00009098 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9099 pipe_name(pipe), fb->width, fb->height,
9100 fb->bits_per_pixel, base, fb->pitches[0],
9101 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009102
Damien Lespiau2d140302015-02-05 17:22:18 +00009103 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009104}
9105
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009106static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009107 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009108{
9109 struct drm_device *dev = crtc->base.dev;
9110 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009111 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009112 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009113 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009114
Imre Deak17290502016-02-12 18:55:11 +02009115 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9116 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009117 return false;
9118
Daniel Vettere143a212013-07-04 12:01:15 +02009119 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009120 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009121
Imre Deak17290502016-02-12 18:55:11 +02009122 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009123 tmp = I915_READ(PIPECONF(crtc->pipe));
9124 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009125 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009126
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009127 switch (tmp & PIPECONF_BPC_MASK) {
9128 case PIPECONF_6BPC:
9129 pipe_config->pipe_bpp = 18;
9130 break;
9131 case PIPECONF_8BPC:
9132 pipe_config->pipe_bpp = 24;
9133 break;
9134 case PIPECONF_10BPC:
9135 pipe_config->pipe_bpp = 30;
9136 break;
9137 case PIPECONF_12BPC:
9138 pipe_config->pipe_bpp = 36;
9139 break;
9140 default:
9141 break;
9142 }
9143
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009144 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9145 pipe_config->limited_color_range = true;
9146
Daniel Vetterab9412b2013-05-03 11:49:46 +02009147 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009148 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009149 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009150
Daniel Vetter88adfff2013-03-28 10:42:01 +01009151 pipe_config->has_pch_encoder = true;
9152
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009153 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9154 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9155 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009156
9157 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009158
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009159 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009160 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009161 } else {
9162 tmp = I915_READ(PCH_DPLL_SEL);
9163 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009164 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009165 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009166 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009167 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009168
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009169 pipe_config->shared_dpll =
9170 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9171 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009172
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009173 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9174 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009175
9176 tmp = pipe_config->dpll_hw_state.dpll;
9177 pipe_config->pixel_multiplier =
9178 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9179 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009180
9181 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009182 } else {
9183 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009184 }
9185
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009186 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009187 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009188
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009189 ironlake_get_pfit_config(crtc, pipe_config);
9190
Imre Deak17290502016-02-12 18:55:11 +02009191 ret = true;
9192
9193out:
9194 intel_display_power_put(dev_priv, power_domain);
9195
9196 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009197}
9198
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009199static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9200{
9201 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009202 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009203
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009204 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009205 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009206 pipe_name(crtc->pipe));
9207
Rob Clarke2c719b2014-12-15 13:56:32 -05009208 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9209 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009210 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9211 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009212 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9213 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009214 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009215 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009216 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009217 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009218 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009219 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009220 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009221 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009222 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009223
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009224 /*
9225 * In theory we can still leave IRQs enabled, as long as only the HPD
9226 * interrupts remain enabled. We used to check for that, but since it's
9227 * gen-specific and since we only disable LCPLL after we fully disable
9228 * the interrupts, the check below should be enough.
9229 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009230 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009231}
9232
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009233static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9234{
9235 struct drm_device *dev = dev_priv->dev;
9236
9237 if (IS_HASWELL(dev))
9238 return I915_READ(D_COMP_HSW);
9239 else
9240 return I915_READ(D_COMP_BDW);
9241}
9242
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009243static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9244{
9245 struct drm_device *dev = dev_priv->dev;
9246
9247 if (IS_HASWELL(dev)) {
9248 mutex_lock(&dev_priv->rps.hw_lock);
9249 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9250 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009251 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009252 mutex_unlock(&dev_priv->rps.hw_lock);
9253 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009254 I915_WRITE(D_COMP_BDW, val);
9255 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009256 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009257}
9258
9259/*
9260 * This function implements pieces of two sequences from BSpec:
9261 * - Sequence for display software to disable LCPLL
9262 * - Sequence for display software to allow package C8+
9263 * The steps implemented here are just the steps that actually touch the LCPLL
9264 * register. Callers should take care of disabling all the display engine
9265 * functions, doing the mode unset, fixing interrupts, etc.
9266 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009267static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9268 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009269{
9270 uint32_t val;
9271
9272 assert_can_disable_lcpll(dev_priv);
9273
9274 val = I915_READ(LCPLL_CTL);
9275
9276 if (switch_to_fclk) {
9277 val |= LCPLL_CD_SOURCE_FCLK;
9278 I915_WRITE(LCPLL_CTL, val);
9279
9280 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9281 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9282 DRM_ERROR("Switching to FCLK failed\n");
9283
9284 val = I915_READ(LCPLL_CTL);
9285 }
9286
9287 val |= LCPLL_PLL_DISABLE;
9288 I915_WRITE(LCPLL_CTL, val);
9289 POSTING_READ(LCPLL_CTL);
9290
9291 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9292 DRM_ERROR("LCPLL still locked\n");
9293
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009294 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009295 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009296 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009297 ndelay(100);
9298
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009299 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9300 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009301 DRM_ERROR("D_COMP RCOMP still in progress\n");
9302
9303 if (allow_power_down) {
9304 val = I915_READ(LCPLL_CTL);
9305 val |= LCPLL_POWER_DOWN_ALLOW;
9306 I915_WRITE(LCPLL_CTL, val);
9307 POSTING_READ(LCPLL_CTL);
9308 }
9309}
9310
9311/*
9312 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9313 * source.
9314 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009315static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009316{
9317 uint32_t val;
9318
9319 val = I915_READ(LCPLL_CTL);
9320
9321 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9322 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9323 return;
9324
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009325 /*
9326 * Make sure we're not on PC8 state before disabling PC8, otherwise
9327 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009328 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009329 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009330
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009331 if (val & LCPLL_POWER_DOWN_ALLOW) {
9332 val &= ~LCPLL_POWER_DOWN_ALLOW;
9333 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009334 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335 }
9336
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009337 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338 val |= D_COMP_COMP_FORCE;
9339 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009340 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009341
9342 val = I915_READ(LCPLL_CTL);
9343 val &= ~LCPLL_PLL_DISABLE;
9344 I915_WRITE(LCPLL_CTL, val);
9345
9346 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9347 DRM_ERROR("LCPLL not locked yet\n");
9348
9349 if (val & LCPLL_CD_SOURCE_FCLK) {
9350 val = I915_READ(LCPLL_CTL);
9351 val &= ~LCPLL_CD_SOURCE_FCLK;
9352 I915_WRITE(LCPLL_CTL, val);
9353
9354 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9355 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9356 DRM_ERROR("Switching back to LCPLL failed\n");
9357 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009358
Mika Kuoppala59bad942015-01-16 11:34:40 +02009359 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009360 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009361}
9362
Paulo Zanoni765dab672014-03-07 20:08:18 -03009363/*
9364 * Package states C8 and deeper are really deep PC states that can only be
9365 * reached when all the devices on the system allow it, so even if the graphics
9366 * device allows PC8+, it doesn't mean the system will actually get to these
9367 * states. Our driver only allows PC8+ when going into runtime PM.
9368 *
9369 * The requirements for PC8+ are that all the outputs are disabled, the power
9370 * well is disabled and most interrupts are disabled, and these are also
9371 * requirements for runtime PM. When these conditions are met, we manually do
9372 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9373 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9374 * hang the machine.
9375 *
9376 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9377 * the state of some registers, so when we come back from PC8+ we need to
9378 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9379 * need to take care of the registers kept by RC6. Notice that this happens even
9380 * if we don't put the device in PCI D3 state (which is what currently happens
9381 * because of the runtime PM support).
9382 *
9383 * For more, read "Display Sequences for Package C8" on the hardware
9384 * documentation.
9385 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009386void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009387{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009388 struct drm_device *dev = dev_priv->dev;
9389 uint32_t val;
9390
Paulo Zanonic67a4702013-08-19 13:18:09 -03009391 DRM_DEBUG_KMS("Enabling package C8+\n");
9392
Ville Syrjäläc2699522015-08-27 23:55:59 +03009393 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009394 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9395 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9396 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9397 }
9398
9399 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009400 hsw_disable_lcpll(dev_priv, true, true);
9401}
9402
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009403void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009404{
9405 struct drm_device *dev = dev_priv->dev;
9406 uint32_t val;
9407
Paulo Zanonic67a4702013-08-19 13:18:09 -03009408 DRM_DEBUG_KMS("Disabling package C8+\n");
9409
9410 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009411 lpt_init_pch_refclk(dev);
9412
Ville Syrjäläc2699522015-08-27 23:55:59 +03009413 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009414 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9415 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9416 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9417 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009418}
9419
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009420static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309421{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009422 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009423 struct intel_atomic_state *old_intel_state =
9424 to_intel_atomic_state(old_state);
9425 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309426
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009427 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309428}
9429
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009430/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009431static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009432{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009433 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9434 struct drm_i915_private *dev_priv = state->dev->dev_private;
9435 struct drm_crtc *crtc;
9436 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009437 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009438 unsigned max_pixel_rate = 0, i;
9439 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009440
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009441 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9442 sizeof(intel_state->min_pixclk));
9443
9444 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009445 int pixel_rate;
9446
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009447 crtc_state = to_intel_crtc_state(cstate);
9448 if (!crtc_state->base.enable) {
9449 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009450 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009451 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009452
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009453 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009454
9455 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009456 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009457 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9458
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009459 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009460 }
9461
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009462 for_each_pipe(dev_priv, pipe)
9463 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9464
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009465 return max_pixel_rate;
9466}
9467
9468static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9469{
9470 struct drm_i915_private *dev_priv = dev->dev_private;
9471 uint32_t val, data;
9472 int ret;
9473
9474 if (WARN((I915_READ(LCPLL_CTL) &
9475 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9476 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9477 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9478 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9479 "trying to change cdclk frequency with cdclk not enabled\n"))
9480 return;
9481
9482 mutex_lock(&dev_priv->rps.hw_lock);
9483 ret = sandybridge_pcode_write(dev_priv,
9484 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9485 mutex_unlock(&dev_priv->rps.hw_lock);
9486 if (ret) {
9487 DRM_ERROR("failed to inform pcode about cdclk change\n");
9488 return;
9489 }
9490
9491 val = I915_READ(LCPLL_CTL);
9492 val |= LCPLL_CD_SOURCE_FCLK;
9493 I915_WRITE(LCPLL_CTL, val);
9494
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009495 if (wait_for_us(I915_READ(LCPLL_CTL) &
9496 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009497 DRM_ERROR("Switching to FCLK failed\n");
9498
9499 val = I915_READ(LCPLL_CTL);
9500 val &= ~LCPLL_CLK_FREQ_MASK;
9501
9502 switch (cdclk) {
9503 case 450000:
9504 val |= LCPLL_CLK_FREQ_450;
9505 data = 0;
9506 break;
9507 case 540000:
9508 val |= LCPLL_CLK_FREQ_54O_BDW;
9509 data = 1;
9510 break;
9511 case 337500:
9512 val |= LCPLL_CLK_FREQ_337_5_BDW;
9513 data = 2;
9514 break;
9515 case 675000:
9516 val |= LCPLL_CLK_FREQ_675_BDW;
9517 data = 3;
9518 break;
9519 default:
9520 WARN(1, "invalid cdclk frequency\n");
9521 return;
9522 }
9523
9524 I915_WRITE(LCPLL_CTL, val);
9525
9526 val = I915_READ(LCPLL_CTL);
9527 val &= ~LCPLL_CD_SOURCE_FCLK;
9528 I915_WRITE(LCPLL_CTL, val);
9529
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009530 if (wait_for_us((I915_READ(LCPLL_CTL) &
9531 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009532 DRM_ERROR("Switching back to LCPLL failed\n");
9533
9534 mutex_lock(&dev_priv->rps.hw_lock);
9535 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9536 mutex_unlock(&dev_priv->rps.hw_lock);
9537
9538 intel_update_cdclk(dev);
9539
9540 WARN(cdclk != dev_priv->cdclk_freq,
9541 "cdclk requested %d kHz but got %d kHz\n",
9542 cdclk, dev_priv->cdclk_freq);
9543}
9544
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009545static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009546{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009547 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009548 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009549 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009550 int cdclk;
9551
9552 /*
9553 * FIXME should also account for plane ratio
9554 * once 64bpp pixel formats are supported.
9555 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009556 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009557 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009558 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009559 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009560 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009561 cdclk = 450000;
9562 else
9563 cdclk = 337500;
9564
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009565 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009566 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9567 cdclk, dev_priv->max_cdclk_freq);
9568 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 }
9570
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009571 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9572 if (!intel_state->active_crtcs)
9573 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009574
9575 return 0;
9576}
9577
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009578static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009579{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009580 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009581 struct intel_atomic_state *old_intel_state =
9582 to_intel_atomic_state(old_state);
9583 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009584
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009585 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009586}
9587
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009588static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9589 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009590{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009591 struct intel_encoder *intel_encoder =
9592 intel_ddi_get_crtc_new_encoder(crtc_state);
9593
9594 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9595 if (!intel_ddi_pll_select(crtc, crtc_state))
9596 return -EINVAL;
9597 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009598
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009599 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009600
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009601 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009602}
9603
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309604static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9605 enum port port,
9606 struct intel_crtc_state *pipe_config)
9607{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009608 enum intel_dpll_id id;
9609
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309610 switch (port) {
9611 case PORT_A:
9612 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009613 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309614 break;
9615 case PORT_B:
9616 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009617 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309618 break;
9619 case PORT_C:
9620 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009621 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309622 break;
9623 default:
9624 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009625 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309626 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009627
9628 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309629}
9630
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009631static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9632 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009633 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009634{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009635 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009636 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009637
9638 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9639 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9640
9641 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009642 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009643 id = DPLL_ID_SKL_DPLL0;
9644 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009645 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009646 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009647 break;
9648 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009649 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009650 break;
9651 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009652 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009653 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009654 default:
9655 MISSING_CASE(pipe_config->ddi_pll_sel);
9656 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009657 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009658
9659 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009660}
9661
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009662static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9663 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009664 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009665{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009666 enum intel_dpll_id id;
9667
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009668 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9669
9670 switch (pipe_config->ddi_pll_sel) {
9671 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009672 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009673 break;
9674 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009675 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009676 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009677 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009678 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009679 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009680 case PORT_CLK_SEL_LCPLL_810:
9681 id = DPLL_ID_LCPLL_810;
9682 break;
9683 case PORT_CLK_SEL_LCPLL_1350:
9684 id = DPLL_ID_LCPLL_1350;
9685 break;
9686 case PORT_CLK_SEL_LCPLL_2700:
9687 id = DPLL_ID_LCPLL_2700;
9688 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009689 default:
9690 MISSING_CASE(pipe_config->ddi_pll_sel);
9691 /* fall through */
9692 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009693 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009694 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009695
9696 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009697}
9698
Jani Nikulacf304292016-03-18 17:05:41 +02009699static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9700 struct intel_crtc_state *pipe_config,
9701 unsigned long *power_domain_mask)
9702{
9703 struct drm_device *dev = crtc->base.dev;
9704 struct drm_i915_private *dev_priv = dev->dev_private;
9705 enum intel_display_power_domain power_domain;
9706 u32 tmp;
9707
9708 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9709
9710 /*
9711 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9712 * consistency and less surprising code; it's in always on power).
9713 */
9714 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9715 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9716 enum pipe trans_edp_pipe;
9717 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9718 default:
9719 WARN(1, "unknown pipe linked to edp transcoder\n");
9720 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9721 case TRANS_DDI_EDP_INPUT_A_ON:
9722 trans_edp_pipe = PIPE_A;
9723 break;
9724 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9725 trans_edp_pipe = PIPE_B;
9726 break;
9727 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9728 trans_edp_pipe = PIPE_C;
9729 break;
9730 }
9731
9732 if (trans_edp_pipe == crtc->pipe)
9733 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9734 }
9735
9736 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9737 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9738 return false;
9739 *power_domain_mask |= BIT(power_domain);
9740
9741 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9742
9743 return tmp & PIPECONF_ENABLE;
9744}
9745
Jani Nikula4d1de972016-03-18 17:05:42 +02009746static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9747 struct intel_crtc_state *pipe_config,
9748 unsigned long *power_domain_mask)
9749{
9750 struct drm_device *dev = crtc->base.dev;
9751 struct drm_i915_private *dev_priv = dev->dev_private;
9752 enum intel_display_power_domain power_domain;
9753 enum port port;
9754 enum transcoder cpu_transcoder;
9755 u32 tmp;
9756
9757 pipe_config->has_dsi_encoder = false;
9758
9759 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9760 if (port == PORT_A)
9761 cpu_transcoder = TRANSCODER_DSI_A;
9762 else
9763 cpu_transcoder = TRANSCODER_DSI_C;
9764
9765 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9766 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9767 continue;
9768 *power_domain_mask |= BIT(power_domain);
9769
9770 /* XXX: this works for video mode only */
9771 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9772 if (!(tmp & DPI_ENABLE))
9773 continue;
9774
9775 tmp = I915_READ(MIPI_CTRL(port));
9776 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9777 continue;
9778
9779 pipe_config->cpu_transcoder = cpu_transcoder;
9780 pipe_config->has_dsi_encoder = true;
9781 break;
9782 }
9783
9784 return pipe_config->has_dsi_encoder;
9785}
9786
Daniel Vetter26804af2014-06-25 22:01:55 +03009787static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009788 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009789{
9790 struct drm_device *dev = crtc->base.dev;
9791 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009792 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009793 enum port port;
9794 uint32_t tmp;
9795
9796 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9797
9798 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9799
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009800 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009801 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309802 else if (IS_BROXTON(dev))
9803 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009804 else
9805 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009806
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009807 pll = pipe_config->shared_dpll;
9808 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009809 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9810 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009811 }
9812
Daniel Vetter26804af2014-06-25 22:01:55 +03009813 /*
9814 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9815 * DDI E. So just check whether this pipe is wired to DDI E and whether
9816 * the PCH transcoder is on.
9817 */
Damien Lespiauca370452013-12-03 13:56:24 +00009818 if (INTEL_INFO(dev)->gen < 9 &&
9819 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009820 pipe_config->has_pch_encoder = true;
9821
9822 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9823 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9824 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9825
9826 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9827 }
9828}
9829
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009830static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009831 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009832{
9833 struct drm_device *dev = crtc->base.dev;
9834 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009835 enum intel_display_power_domain power_domain;
9836 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009837 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009838
Imre Deak17290502016-02-12 18:55:11 +02009839 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9840 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009841 return false;
Imre Deak17290502016-02-12 18:55:11 +02009842 power_domain_mask = BIT(power_domain);
9843
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009844 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009845
Jani Nikulacf304292016-03-18 17:05:41 +02009846 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009847
Jani Nikula4d1de972016-03-18 17:05:42 +02009848 if (IS_BROXTON(dev_priv)) {
9849 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9850 &power_domain_mask);
9851 WARN_ON(active && pipe_config->has_dsi_encoder);
9852 if (pipe_config->has_dsi_encoder)
9853 active = true;
9854 }
9855
Jani Nikulacf304292016-03-18 17:05:41 +02009856 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009857 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009858
Jani Nikula4d1de972016-03-18 17:05:42 +02009859 if (!pipe_config->has_dsi_encoder) {
9860 haswell_get_ddi_port_state(crtc, pipe_config);
9861 intel_get_pipe_timings(crtc, pipe_config);
9862 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009863
Jani Nikulabc58be62016-03-18 17:05:39 +02009864 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009865
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009866 pipe_config->gamma_mode =
9867 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9868
Chandra Kondurua1b22782015-04-07 15:28:45 -07009869 if (INTEL_INFO(dev)->gen >= 9) {
9870 skl_init_scalers(dev, crtc, pipe_config);
9871 }
9872
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009873 if (INTEL_INFO(dev)->gen >= 9) {
9874 pipe_config->scaler_state.scaler_id = -1;
9875 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9876 }
9877
Imre Deak17290502016-02-12 18:55:11 +02009878 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9879 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9880 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009881 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009882 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009883 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009884 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009885 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009886
Jesse Barnese59150d2014-01-07 13:30:45 -08009887 if (IS_HASWELL(dev))
9888 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9889 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009890
Jani Nikula4d1de972016-03-18 17:05:42 +02009891 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9892 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009893 pipe_config->pixel_multiplier =
9894 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9895 } else {
9896 pipe_config->pixel_multiplier = 1;
9897 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009898
Imre Deak17290502016-02-12 18:55:11 +02009899out:
9900 for_each_power_domain(power_domain, power_domain_mask)
9901 intel_display_power_put(dev_priv, power_domain);
9902
Jani Nikulacf304292016-03-18 17:05:41 +02009903 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009904}
9905
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009906static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9907 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009908{
9909 struct drm_device *dev = crtc->dev;
9910 struct drm_i915_private *dev_priv = dev->dev_private;
9911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009912 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009913
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009914 if (plane_state && plane_state->visible) {
9915 unsigned int width = plane_state->base.crtc_w;
9916 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009917 unsigned int stride = roundup_pow_of_two(width) * 4;
9918
9919 switch (stride) {
9920 default:
9921 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9922 width, stride);
9923 stride = 256;
9924 /* fallthrough */
9925 case 256:
9926 case 512:
9927 case 1024:
9928 case 2048:
9929 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009930 }
9931
Ville Syrjälädc41c152014-08-13 11:57:05 +03009932 cntl |= CURSOR_ENABLE |
9933 CURSOR_GAMMA_ENABLE |
9934 CURSOR_FORMAT_ARGB |
9935 CURSOR_STRIDE(stride);
9936
9937 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009938 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009939
Ville Syrjälädc41c152014-08-13 11:57:05 +03009940 if (intel_crtc->cursor_cntl != 0 &&
9941 (intel_crtc->cursor_base != base ||
9942 intel_crtc->cursor_size != size ||
9943 intel_crtc->cursor_cntl != cntl)) {
9944 /* On these chipsets we can only modify the base/size/stride
9945 * whilst the cursor is disabled.
9946 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009947 I915_WRITE(CURCNTR(PIPE_A), 0);
9948 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +03009949 intel_crtc->cursor_cntl = 0;
9950 }
9951
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009952 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009953 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009954 intel_crtc->cursor_base = base;
9955 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009956
9957 if (intel_crtc->cursor_size != size) {
9958 I915_WRITE(CURSIZE, size);
9959 intel_crtc->cursor_size = size;
9960 }
9961
Chris Wilson4b0e3332014-05-30 16:35:26 +03009962 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009963 I915_WRITE(CURCNTR(PIPE_A), cntl);
9964 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +03009965 intel_crtc->cursor_cntl = cntl;
9966 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009967}
9968
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009969static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9970 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009971{
9972 struct drm_device *dev = crtc->dev;
9973 struct drm_i915_private *dev_priv = dev->dev_private;
9974 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9975 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +02009976 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009977
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009978 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +03009979 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009980 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309981 case 64:
9982 cntl |= CURSOR_MODE_64_ARGB_AX;
9983 break;
9984 case 128:
9985 cntl |= CURSOR_MODE_128_ARGB_AX;
9986 break;
9987 case 256:
9988 cntl |= CURSOR_MODE_256_ARGB_AX;
9989 break;
9990 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009991 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309992 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009993 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009994 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009995
Bob Paauwefc6f93b2015-08-31 14:03:30 -07009996 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009997 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009998
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009999 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10000 cntl |= CURSOR_ROTATE_180;
10001 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010002
Chris Wilson4b0e3332014-05-30 16:35:26 +030010003 if (intel_crtc->cursor_cntl != cntl) {
10004 I915_WRITE(CURCNTR(pipe), cntl);
10005 POSTING_READ(CURCNTR(pipe));
10006 intel_crtc->cursor_cntl = cntl;
10007 }
10008
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010009 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010010 I915_WRITE(CURBASE(pipe), base);
10011 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010012
10013 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010014}
10015
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010016/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010017static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010018 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010019{
10020 struct drm_device *dev = crtc->dev;
10021 struct drm_i915_private *dev_priv = dev->dev_private;
10022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10023 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010024 u32 base = intel_crtc->cursor_addr;
10025 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010026
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010027 if (plane_state) {
10028 int x = plane_state->base.crtc_x;
10029 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010030
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010031 if (x < 0) {
10032 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10033 x = -x;
10034 }
10035 pos |= x << CURSOR_X_SHIFT;
10036
10037 if (y < 0) {
10038 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10039 y = -y;
10040 }
10041 pos |= y << CURSOR_Y_SHIFT;
10042
10043 /* ILK+ do this automagically */
10044 if (HAS_GMCH_DISPLAY(dev) &&
10045 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10046 base += (plane_state->base.crtc_h *
10047 plane_state->base.crtc_w - 1) * 4;
10048 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010049 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010050
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010051 I915_WRITE(CURPOS(pipe), pos);
10052
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010053 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010054 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010055 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010056 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010057}
10058
Ville Syrjälädc41c152014-08-13 11:57:05 +030010059static bool cursor_size_ok(struct drm_device *dev,
10060 uint32_t width, uint32_t height)
10061{
10062 if (width == 0 || height == 0)
10063 return false;
10064
10065 /*
10066 * 845g/865g are special in that they are only limited by
10067 * the width of their cursors, the height is arbitrary up to
10068 * the precision of the register. Everything else requires
10069 * square cursors, limited to a few power-of-two sizes.
10070 */
10071 if (IS_845G(dev) || IS_I865G(dev)) {
10072 if ((width & 63) != 0)
10073 return false;
10074
10075 if (width > (IS_845G(dev) ? 64 : 512))
10076 return false;
10077
10078 if (height > 1023)
10079 return false;
10080 } else {
10081 switch (width | height) {
10082 case 256:
10083 case 128:
10084 if (IS_GEN2(dev))
10085 return false;
10086 case 64:
10087 break;
10088 default:
10089 return false;
10090 }
10091 }
10092
10093 return true;
10094}
10095
Jesse Barnes79e53942008-11-07 14:24:08 -080010096/* VESA 640x480x72Hz mode to set on the pipe */
10097static struct drm_display_mode load_detect_mode = {
10098 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10099 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10100};
10101
Daniel Vettera8bb6812014-02-10 18:00:39 +010010102struct drm_framebuffer *
10103__intel_framebuffer_create(struct drm_device *dev,
10104 struct drm_mode_fb_cmd2 *mode_cmd,
10105 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010106{
10107 struct intel_framebuffer *intel_fb;
10108 int ret;
10109
10110 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010111 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010112 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010113
10114 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010115 if (ret)
10116 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010117
10118 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010119
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010120err:
10121 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010122 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010123}
10124
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010125static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010126intel_framebuffer_create(struct drm_device *dev,
10127 struct drm_mode_fb_cmd2 *mode_cmd,
10128 struct drm_i915_gem_object *obj)
10129{
10130 struct drm_framebuffer *fb;
10131 int ret;
10132
10133 ret = i915_mutex_lock_interruptible(dev);
10134 if (ret)
10135 return ERR_PTR(ret);
10136 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10137 mutex_unlock(&dev->struct_mutex);
10138
10139 return fb;
10140}
10141
Chris Wilsond2dff872011-04-19 08:36:26 +010010142static u32
10143intel_framebuffer_pitch_for_width(int width, int bpp)
10144{
10145 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10146 return ALIGN(pitch, 64);
10147}
10148
10149static u32
10150intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10151{
10152 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010153 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010154}
10155
10156static struct drm_framebuffer *
10157intel_framebuffer_create_for_mode(struct drm_device *dev,
10158 struct drm_display_mode *mode,
10159 int depth, int bpp)
10160{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010161 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010162 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010163 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010164
10165 obj = i915_gem_alloc_object(dev,
10166 intel_framebuffer_size_for_mode(mode, bpp));
10167 if (obj == NULL)
10168 return ERR_PTR(-ENOMEM);
10169
10170 mode_cmd.width = mode->hdisplay;
10171 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010172 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10173 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010174 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010175
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010176 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10177 if (IS_ERR(fb))
10178 drm_gem_object_unreference_unlocked(&obj->base);
10179
10180 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010181}
10182
10183static struct drm_framebuffer *
10184mode_fits_in_fbdev(struct drm_device *dev,
10185 struct drm_display_mode *mode)
10186{
Daniel Vetter06957262015-08-10 13:34:08 +020010187#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010188 struct drm_i915_private *dev_priv = dev->dev_private;
10189 struct drm_i915_gem_object *obj;
10190 struct drm_framebuffer *fb;
10191
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010192 if (!dev_priv->fbdev)
10193 return NULL;
10194
10195 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010196 return NULL;
10197
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010198 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010199 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010200
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010201 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010202 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10203 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010204 return NULL;
10205
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010206 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010207 return NULL;
10208
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010209 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010210 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010211#else
10212 return NULL;
10213#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010214}
10215
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010216static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10217 struct drm_crtc *crtc,
10218 struct drm_display_mode *mode,
10219 struct drm_framebuffer *fb,
10220 int x, int y)
10221{
10222 struct drm_plane_state *plane_state;
10223 int hdisplay, vdisplay;
10224 int ret;
10225
10226 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10227 if (IS_ERR(plane_state))
10228 return PTR_ERR(plane_state);
10229
10230 if (mode)
10231 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10232 else
10233 hdisplay = vdisplay = 0;
10234
10235 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10236 if (ret)
10237 return ret;
10238 drm_atomic_set_fb_for_plane(plane_state, fb);
10239 plane_state->crtc_x = 0;
10240 plane_state->crtc_y = 0;
10241 plane_state->crtc_w = hdisplay;
10242 plane_state->crtc_h = vdisplay;
10243 plane_state->src_x = x << 16;
10244 plane_state->src_y = y << 16;
10245 plane_state->src_w = hdisplay << 16;
10246 plane_state->src_h = vdisplay << 16;
10247
10248 return 0;
10249}
10250
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010251bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010252 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010253 struct intel_load_detect_pipe *old,
10254 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010255{
10256 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010257 struct intel_encoder *intel_encoder =
10258 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010259 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010260 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010261 struct drm_crtc *crtc = NULL;
10262 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010263 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010264 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010265 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010266 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010267 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010268 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010269
Chris Wilsond2dff872011-04-19 08:36:26 +010010270 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010271 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010272 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010273
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010274 old->restore_state = NULL;
10275
Rob Clark51fd3712013-11-19 12:10:12 -050010276retry:
10277 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10278 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010279 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010280
Jesse Barnes79e53942008-11-07 14:24:08 -080010281 /*
10282 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010283 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010284 * - if the connector already has an assigned crtc, use it (but make
10285 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010286 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010287 * - try to find the first unused crtc that can drive this connector,
10288 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010289 */
10290
10291 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010292 if (connector->state->crtc) {
10293 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010294
Rob Clark51fd3712013-11-19 12:10:12 -050010295 ret = drm_modeset_lock(&crtc->mutex, ctx);
10296 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010297 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010298
10299 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010300 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010301 }
10302
10303 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010304 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010305 i++;
10306 if (!(encoder->possible_crtcs & (1 << i)))
10307 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010308
10309 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10310 if (ret)
10311 goto fail;
10312
10313 if (possible_crtc->state->enable) {
10314 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010315 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010316 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010317
10318 crtc = possible_crtc;
10319 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010320 }
10321
10322 /*
10323 * If we didn't find an unused CRTC, don't use any.
10324 */
10325 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010326 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010327 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010328 }
10329
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010330found:
10331 intel_crtc = to_intel_crtc(crtc);
10332
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010333 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10334 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010335 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010336
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010337 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010338 restore_state = drm_atomic_state_alloc(dev);
10339 if (!state || !restore_state) {
10340 ret = -ENOMEM;
10341 goto fail;
10342 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010343
10344 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010345 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010346
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010347 connector_state = drm_atomic_get_connector_state(state, connector);
10348 if (IS_ERR(connector_state)) {
10349 ret = PTR_ERR(connector_state);
10350 goto fail;
10351 }
10352
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010353 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10354 if (ret)
10355 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010356
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010357 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10358 if (IS_ERR(crtc_state)) {
10359 ret = PTR_ERR(crtc_state);
10360 goto fail;
10361 }
10362
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010363 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010364
Chris Wilson64927112011-04-20 07:25:26 +010010365 if (!mode)
10366 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010367
Chris Wilsond2dff872011-04-19 08:36:26 +010010368 /* We need a framebuffer large enough to accommodate all accesses
10369 * that the plane may generate whilst we perform load detection.
10370 * We can not rely on the fbcon either being present (we get called
10371 * during its initialisation to detect all boot displays, or it may
10372 * not even exist) or that it is large enough to satisfy the
10373 * requested mode.
10374 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010375 fb = mode_fits_in_fbdev(dev, mode);
10376 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010377 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010378 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010379 } else
10380 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010381 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010382 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010383 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010384 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010385
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010386 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10387 if (ret)
10388 goto fail;
10389
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010390 drm_framebuffer_unreference(fb);
10391
10392 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10393 if (ret)
10394 goto fail;
10395
10396 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10397 if (!ret)
10398 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10399 if (!ret)
10400 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10401 if (ret) {
10402 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10403 goto fail;
10404 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010405
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010406 ret = drm_atomic_commit(state);
10407 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010408 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010409 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010411
10412 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010413
Jesse Barnes79e53942008-11-07 14:24:08 -080010414 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010415 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010416 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010417
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010418fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010419 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010420 drm_atomic_state_free(restore_state);
10421 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010422
Rob Clark51fd3712013-11-19 12:10:12 -050010423 if (ret == -EDEADLK) {
10424 drm_modeset_backoff(ctx);
10425 goto retry;
10426 }
10427
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010428 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010429}
10430
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010431void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010432 struct intel_load_detect_pipe *old,
10433 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010434{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010435 struct intel_encoder *intel_encoder =
10436 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010437 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010438 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010439 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010440
Chris Wilsond2dff872011-04-19 08:36:26 +010010441 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010442 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010443 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010444
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010445 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010446 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010447
10448 ret = drm_atomic_commit(state);
10449 if (ret) {
10450 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10451 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010452 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010453}
10454
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010455static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010456 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010457{
10458 struct drm_i915_private *dev_priv = dev->dev_private;
10459 u32 dpll = pipe_config->dpll_hw_state.dpll;
10460
10461 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010462 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010463 else if (HAS_PCH_SPLIT(dev))
10464 return 120000;
10465 else if (!IS_GEN2(dev))
10466 return 96000;
10467 else
10468 return 48000;
10469}
10470
Jesse Barnes79e53942008-11-07 14:24:08 -080010471/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010472static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010473 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010474{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010475 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010476 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010477 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010478 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010479 u32 fp;
10480 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010481 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010482 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010483
10484 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010485 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010486 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010487 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010488
10489 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010490 if (IS_PINEVIEW(dev)) {
10491 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10492 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010493 } else {
10494 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10495 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10496 }
10497
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010498 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010499 if (IS_PINEVIEW(dev))
10500 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10501 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010502 else
10503 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010504 DPLL_FPA01_P1_POST_DIV_SHIFT);
10505
10506 switch (dpll & DPLL_MODE_MASK) {
10507 case DPLLB_MODE_DAC_SERIAL:
10508 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10509 5 : 10;
10510 break;
10511 case DPLLB_MODE_LVDS:
10512 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10513 7 : 14;
10514 break;
10515 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010516 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010517 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010518 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010519 }
10520
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010521 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010522 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010523 else
Imre Deakdccbea32015-06-22 23:35:51 +030010524 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010525 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010526 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010527 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010528
10529 if (is_lvds) {
10530 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10531 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010532
10533 if (lvds & LVDS_CLKB_POWER_UP)
10534 clock.p2 = 7;
10535 else
10536 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010537 } else {
10538 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10539 clock.p1 = 2;
10540 else {
10541 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10542 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10543 }
10544 if (dpll & PLL_P2_DIVIDE_BY_4)
10545 clock.p2 = 4;
10546 else
10547 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010548 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010549
Imre Deakdccbea32015-06-22 23:35:51 +030010550 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010551 }
10552
Ville Syrjälä18442d02013-09-13 16:00:08 +030010553 /*
10554 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010555 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010556 * encoder's get_config() function.
10557 */
Imre Deakdccbea32015-06-22 23:35:51 +030010558 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010559}
10560
Ville Syrjälä6878da02013-09-13 15:59:11 +030010561int intel_dotclock_calculate(int link_freq,
10562 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010563{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010564 /*
10565 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010566 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010567 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010568 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010569 *
10570 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010571 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010572 */
10573
Ville Syrjälä6878da02013-09-13 15:59:11 +030010574 if (!m_n->link_n)
10575 return 0;
10576
10577 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10578}
10579
Ville Syrjälä18442d02013-09-13 16:00:08 +030010580static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010581 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010582{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010583 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010584
10585 /* read out port_clock from the DPLL */
10586 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010587
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010588 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010589 * In case there is an active pipe without active ports,
10590 * we may need some idea for the dotclock anyway.
10591 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010592 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010593 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010594 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010595 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010596}
10597
10598/** Returns the currently programmed mode of the given pipe. */
10599struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10600 struct drm_crtc *crtc)
10601{
Jesse Barnes548f2452011-02-17 10:40:53 -080010602 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010604 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010605 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010606 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010607 int htot = I915_READ(HTOTAL(cpu_transcoder));
10608 int hsync = I915_READ(HSYNC(cpu_transcoder));
10609 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10610 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010611 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010612
10613 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10614 if (!mode)
10615 return NULL;
10616
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010617 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10618 if (!pipe_config) {
10619 kfree(mode);
10620 return NULL;
10621 }
10622
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010623 /*
10624 * Construct a pipe_config sufficient for getting the clock info
10625 * back out of crtc_clock_get.
10626 *
10627 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10628 * to use a real value here instead.
10629 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010630 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10631 pipe_config->pixel_multiplier = 1;
10632 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10633 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10634 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10635 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010636
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010637 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010638 mode->hdisplay = (htot & 0xffff) + 1;
10639 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10640 mode->hsync_start = (hsync & 0xffff) + 1;
10641 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10642 mode->vdisplay = (vtot & 0xffff) + 1;
10643 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10644 mode->vsync_start = (vsync & 0xffff) + 1;
10645 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10646
10647 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010648
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010649 kfree(pipe_config);
10650
Jesse Barnes79e53942008-11-07 14:24:08 -080010651 return mode;
10652}
10653
Chris Wilsonf047e392012-07-21 12:31:41 +010010654void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010655{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010656 struct drm_i915_private *dev_priv = dev->dev_private;
10657
Chris Wilsonf62a0072014-02-21 17:55:39 +000010658 if (dev_priv->mm.busy)
10659 return;
10660
Paulo Zanoni43694d62014-03-07 20:08:08 -030010661 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010662 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010663 if (INTEL_INFO(dev)->gen >= 6)
10664 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010665 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010666}
10667
10668void intel_mark_idle(struct drm_device *dev)
10669{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010670 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010671
Chris Wilsonf62a0072014-02-21 17:55:39 +000010672 if (!dev_priv->mm.busy)
10673 return;
10674
10675 dev_priv->mm.busy = false;
10676
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010677 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010678 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010679
Paulo Zanoni43694d62014-03-07 20:08:08 -030010680 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010681}
10682
Jesse Barnes79e53942008-11-07 14:24:08 -080010683static void intel_crtc_destroy(struct drm_crtc *crtc)
10684{
10685 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010686 struct drm_device *dev = crtc->dev;
10687 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010688
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010689 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010690 work = intel_crtc->unpin_work;
10691 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010692 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010693
10694 if (work) {
10695 cancel_work_sync(&work->work);
10696 kfree(work);
10697 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010698
10699 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010700
Jesse Barnes79e53942008-11-07 14:24:08 -080010701 kfree(intel_crtc);
10702}
10703
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010704static void intel_unpin_work_fn(struct work_struct *__work)
10705{
10706 struct intel_unpin_work *work =
10707 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010708 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10709 struct drm_device *dev = crtc->base.dev;
10710 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010711
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010712 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010713 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010714 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010715
John Harrisonf06cc1b2014-11-24 18:49:37 +000010716 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010717 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010718 mutex_unlock(&dev->struct_mutex);
10719
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010720 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010721 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010722 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010723
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010724 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10725 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010726
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010727 kfree(work);
10728}
10729
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010730static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010731 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010732{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10734 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010735 unsigned long flags;
10736
10737 /* Ignore early vblank irqs */
10738 if (intel_crtc == NULL)
10739 return;
10740
Daniel Vetterf3260382014-09-15 14:55:23 +020010741 /*
10742 * This is called both by irq handlers and the reset code (to complete
10743 * lost pageflips) so needs the full irqsave spinlocks.
10744 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010745 spin_lock_irqsave(&dev->event_lock, flags);
10746 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010747
10748 /* Ensure we don't miss a work->pending update ... */
10749 smp_rmb();
10750
10751 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010752 spin_unlock_irqrestore(&dev->event_lock, flags);
10753 return;
10754 }
10755
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010756 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010757
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010758 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010759}
10760
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010761void intel_finish_page_flip(struct drm_device *dev, int pipe)
10762{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010763 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010764 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10765
Mario Kleiner49b14a52010-12-09 07:00:07 +010010766 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010767}
10768
10769void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10770{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010771 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010772 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10773
Mario Kleiner49b14a52010-12-09 07:00:07 +010010774 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010775}
10776
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010777/* Is 'a' after or equal to 'b'? */
10778static bool g4x_flip_count_after_eq(u32 a, u32 b)
10779{
10780 return !((a - b) & 0x80000000);
10781}
10782
10783static bool page_flip_finished(struct intel_crtc *crtc)
10784{
10785 struct drm_device *dev = crtc->base.dev;
10786 struct drm_i915_private *dev_priv = dev->dev_private;
10787
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010788 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10789 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10790 return true;
10791
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010792 /*
10793 * The relevant registers doen't exist on pre-ctg.
10794 * As the flip done interrupt doesn't trigger for mmio
10795 * flips on gmch platforms, a flip count check isn't
10796 * really needed there. But since ctg has the registers,
10797 * include it in the check anyway.
10798 */
10799 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10800 return true;
10801
10802 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010803 * BDW signals flip done immediately if the plane
10804 * is disabled, even if the plane enable is already
10805 * armed to occur at the next vblank :(
10806 */
10807
10808 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010809 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10810 * used the same base address. In that case the mmio flip might
10811 * have completed, but the CS hasn't even executed the flip yet.
10812 *
10813 * A flip count check isn't enough as the CS might have updated
10814 * the base address just after start of vblank, but before we
10815 * managed to process the interrupt. This means we'd complete the
10816 * CS flip too soon.
10817 *
10818 * Combining both checks should get us a good enough result. It may
10819 * still happen that the CS flip has been executed, but has not
10820 * yet actually completed. But in case the base address is the same
10821 * anyway, we don't really care.
10822 */
10823 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10824 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010825 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010826 crtc->unpin_work->flip_count);
10827}
10828
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010829void intel_prepare_page_flip(struct drm_device *dev, int plane)
10830{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010831 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010832 struct intel_crtc *intel_crtc =
10833 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10834 unsigned long flags;
10835
Daniel Vetterf3260382014-09-15 14:55:23 +020010836
10837 /*
10838 * This is called both by irq handlers and the reset code (to complete
10839 * lost pageflips) so needs the full irqsave spinlocks.
10840 *
10841 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010842 * generate a page-flip completion irq, i.e. every modeset
10843 * is also accompanied by a spurious intel_prepare_page_flip().
10844 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010846 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010847 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010848 spin_unlock_irqrestore(&dev->event_lock, flags);
10849}
10850
Chris Wilson60426392015-10-10 10:44:32 +010010851static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010852{
10853 /* Ensure that the work item is consistent when activating it ... */
10854 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010855 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010856 /* and that it is marked active as soon as the irq could fire. */
10857 smp_wmb();
10858}
10859
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010860static int intel_gen2_queue_flip(struct drm_device *dev,
10861 struct drm_crtc *crtc,
10862 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010863 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010864 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010865 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010866{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010867 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010869 u32 flip_mask;
10870 int ret;
10871
John Harrison5fb9de12015-05-29 17:44:07 +010010872 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010873 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010874 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010875
10876 /* Can't queue multiple flips, so wait for the previous
10877 * one to finish before executing the next.
10878 */
10879 if (intel_crtc->plane)
10880 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10881 else
10882 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010883 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10884 intel_ring_emit(engine, MI_NOOP);
10885 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010886 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010887 intel_ring_emit(engine, fb->pitches[0]);
10888 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10889 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010890
Chris Wilson60426392015-10-10 10:44:32 +010010891 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010892 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010893}
10894
10895static int intel_gen3_queue_flip(struct drm_device *dev,
10896 struct drm_crtc *crtc,
10897 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010898 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010899 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010900 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010901{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010902 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010904 u32 flip_mask;
10905 int ret;
10906
John Harrison5fb9de12015-05-29 17:44:07 +010010907 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010908 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010909 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010910
10911 if (intel_crtc->plane)
10912 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10913 else
10914 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010915 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10916 intel_ring_emit(engine, MI_NOOP);
10917 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010918 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010919 intel_ring_emit(engine, fb->pitches[0]);
10920 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10921 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010922
Chris Wilson60426392015-10-10 10:44:32 +010010923 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010924 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010925}
10926
10927static int intel_gen4_queue_flip(struct drm_device *dev,
10928 struct drm_crtc *crtc,
10929 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010930 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010931 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010932 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010933{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010934 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010935 struct drm_i915_private *dev_priv = dev->dev_private;
10936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10937 uint32_t pf, pipesrc;
10938 int ret;
10939
John Harrison5fb9de12015-05-29 17:44:07 +010010940 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010941 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010942 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010943
10944 /* i965+ uses the linear or tiled offsets from the
10945 * Display Registers (which do not change across a page-flip)
10946 * so we need only reprogram the base address.
10947 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010948 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010949 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010950 intel_ring_emit(engine, fb->pitches[0]);
10951 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010952 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010953
10954 /* XXX Enabling the panel-fitter across page-flip is so far
10955 * untested on non-native modes, so ignore it for now.
10956 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10957 */
10958 pf = 0;
10959 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010960 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010961
Chris Wilson60426392015-10-10 10:44:32 +010010962 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010963 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010964}
10965
10966static int intel_gen6_queue_flip(struct drm_device *dev,
10967 struct drm_crtc *crtc,
10968 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010969 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010970 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010971 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010972{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010973 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010974 struct drm_i915_private *dev_priv = dev->dev_private;
10975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10976 uint32_t pf, pipesrc;
10977 int ret;
10978
John Harrison5fb9de12015-05-29 17:44:07 +010010979 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010980 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010981 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010982
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010983 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010984 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010985 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
10986 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010987
Chris Wilson99d9acd2012-04-17 20:37:00 +010010988 /* Contrary to the suggestions in the documentation,
10989 * "Enable Panel Fitter" does not seem to be required when page
10990 * flipping with a non-native mode, and worse causes a normal
10991 * modeset to fail.
10992 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10993 */
10994 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010995 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010996 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010997
Chris Wilson60426392015-10-10 10:44:32 +010010998 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010999 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011000}
11001
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011002static int intel_gen7_queue_flip(struct drm_device *dev,
11003 struct drm_crtc *crtc,
11004 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011005 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011006 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011007 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011008{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011009 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011011 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011012 int len, ret;
11013
Robin Schroereba905b2014-05-18 02:24:50 +020011014 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011015 case PLANE_A:
11016 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11017 break;
11018 case PLANE_B:
11019 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11020 break;
11021 case PLANE_C:
11022 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11023 break;
11024 default:
11025 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011026 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011027 }
11028
Chris Wilsonffe74d72013-08-26 20:58:12 +010011029 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011030 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011031 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011032 /*
11033 * On Gen 8, SRM is now taking an extra dword to accommodate
11034 * 48bits addresses, and we need a NOOP for the batch size to
11035 * stay even.
11036 */
11037 if (IS_GEN8(dev))
11038 len += 2;
11039 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011040
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011041 /*
11042 * BSpec MI_DISPLAY_FLIP for IVB:
11043 * "The full packet must be contained within the same cache line."
11044 *
11045 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11046 * cacheline, if we ever start emitting more commands before
11047 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11048 * then do the cacheline alignment, and finally emit the
11049 * MI_DISPLAY_FLIP.
11050 */
John Harrisonbba09b12015-05-29 17:44:06 +010011051 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011052 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011053 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011054
John Harrison5fb9de12015-05-29 17:44:07 +010011055 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011056 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011057 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011058
Chris Wilsonffe74d72013-08-26 20:58:12 +010011059 /* Unmask the flip-done completion message. Note that the bspec says that
11060 * we should do this for both the BCS and RCS, and that we must not unmask
11061 * more than one flip event at any time (or ensure that one flip message
11062 * can be sent by waiting for flip-done prior to queueing new flips).
11063 * Experimentation says that BCS works despite DERRMR masking all
11064 * flip-done completion events and that unmasking all planes at once
11065 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11066 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11067 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011068 if (engine->id == RCS) {
11069 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11070 intel_ring_emit_reg(engine, DERRMR);
11071 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11072 DERRMR_PIPEB_PRI_FLIP_DONE |
11073 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011074 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011075 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011076 MI_SRM_LRM_GLOBAL_GTT);
11077 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011078 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011079 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011080 intel_ring_emit_reg(engine, DERRMR);
11081 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011082 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011083 intel_ring_emit(engine, 0);
11084 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011085 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011086 }
11087
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011088 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11089 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11090 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11091 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011092
Chris Wilson60426392015-10-10 10:44:32 +010011093 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011094 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011095}
11096
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011097static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011098 struct drm_i915_gem_object *obj)
11099{
11100 /*
11101 * This is not being used for older platforms, because
11102 * non-availability of flip done interrupt forces us to use
11103 * CS flips. Older platforms derive flip done using some clever
11104 * tricks involving the flip_pending status bits and vblank irqs.
11105 * So using MMIO flips there would disrupt this mechanism.
11106 */
11107
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011108 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011109 return true;
11110
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011111 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011112 return false;
11113
11114 if (i915.use_mmio_flip < 0)
11115 return false;
11116 else if (i915.use_mmio_flip > 0)
11117 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011118 else if (i915.enable_execlists)
11119 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011120 else if (obj->base.dma_buf &&
11121 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11122 false))
11123 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011124 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011125 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011126}
11127
Chris Wilson60426392015-10-10 10:44:32 +010011128static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011129 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011130 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011131{
11132 struct drm_device *dev = intel_crtc->base.dev;
11133 struct drm_i915_private *dev_priv = dev->dev_private;
11134 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011135 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011136 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011137
11138 ctl = I915_READ(PLANE_CTL(pipe, 0));
11139 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011140 switch (fb->modifier[0]) {
11141 case DRM_FORMAT_MOD_NONE:
11142 break;
11143 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011144 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011145 break;
11146 case I915_FORMAT_MOD_Y_TILED:
11147 ctl |= PLANE_CTL_TILED_Y;
11148 break;
11149 case I915_FORMAT_MOD_Yf_TILED:
11150 ctl |= PLANE_CTL_TILED_YF;
11151 break;
11152 default:
11153 MISSING_CASE(fb->modifier[0]);
11154 }
Damien Lespiauff944562014-11-20 14:58:16 +000011155
11156 /*
11157 * The stride is either expressed as a multiple of 64 bytes chunks for
11158 * linear buffers or in number of tiles for tiled buffers.
11159 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011160 if (intel_rotation_90_or_270(rotation)) {
11161 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011162 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011163 stride = DIV_ROUND_UP(fb->height, tile_height);
11164 } else {
11165 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011166 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11167 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011168 }
Damien Lespiauff944562014-11-20 14:58:16 +000011169
11170 /*
11171 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11172 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11173 */
11174 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11175 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11176
Chris Wilson60426392015-10-10 10:44:32 +010011177 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011178 POSTING_READ(PLANE_SURF(pipe, 0));
11179}
11180
Chris Wilson60426392015-10-10 10:44:32 +010011181static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11182 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011183{
11184 struct drm_device *dev = intel_crtc->base.dev;
11185 struct drm_i915_private *dev_priv = dev->dev_private;
11186 struct intel_framebuffer *intel_fb =
11187 to_intel_framebuffer(intel_crtc->base.primary->fb);
11188 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011189 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011190 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011191
Sourab Gupta84c33a62014-06-02 16:47:17 +053011192 dspcntr = I915_READ(reg);
11193
Damien Lespiauc5d97472014-10-25 00:11:11 +010011194 if (obj->tiling_mode != I915_TILING_NONE)
11195 dspcntr |= DISPPLANE_TILED;
11196 else
11197 dspcntr &= ~DISPPLANE_TILED;
11198
Sourab Gupta84c33a62014-06-02 16:47:17 +053011199 I915_WRITE(reg, dspcntr);
11200
Chris Wilson60426392015-10-10 10:44:32 +010011201 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011202 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011203}
11204
11205/*
11206 * XXX: This is the temporary way to update the plane registers until we get
11207 * around to using the usual plane update functions for MMIO flips
11208 */
Chris Wilson60426392015-10-10 10:44:32 +010011209static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011210{
Chris Wilson60426392015-10-10 10:44:32 +010011211 struct intel_crtc *crtc = mmio_flip->crtc;
11212 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011213
Chris Wilson60426392015-10-10 10:44:32 +010011214 spin_lock_irq(&crtc->base.dev->event_lock);
11215 work = crtc->unpin_work;
11216 spin_unlock_irq(&crtc->base.dev->event_lock);
11217 if (work == NULL)
11218 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011219
Chris Wilson60426392015-10-10 10:44:32 +010011220 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011221
Chris Wilson60426392015-10-10 10:44:32 +010011222 intel_pipe_update_start(crtc);
11223
11224 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011225 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011226 else
11227 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011228 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011229
Chris Wilson60426392015-10-10 10:44:32 +010011230 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011231}
11232
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011233static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011234{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011235 struct intel_mmio_flip *mmio_flip =
11236 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011237 struct intel_framebuffer *intel_fb =
11238 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11239 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011240
Chris Wilson60426392015-10-10 10:44:32 +010011241 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011242 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011243 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011244 false, NULL,
11245 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011246 i915_gem_request_unreference__unlocked(mmio_flip->req);
11247 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011248
Alex Goinsfd8e0582015-11-25 18:43:38 -080011249 /* For framebuffer backed by dmabuf, wait for fence */
11250 if (obj->base.dma_buf)
11251 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11252 false, false,
11253 MAX_SCHEDULE_TIMEOUT) < 0);
11254
Chris Wilson60426392015-10-10 10:44:32 +010011255 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011256 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011257}
11258
11259static int intel_queue_mmio_flip(struct drm_device *dev,
11260 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011261 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011262{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011263 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011264
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011265 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11266 if (mmio_flip == NULL)
11267 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011268
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011269 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011270 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011271 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011272 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011273
11274 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11275 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011276
Sourab Gupta84c33a62014-06-02 16:47:17 +053011277 return 0;
11278}
11279
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011280static int intel_default_queue_flip(struct drm_device *dev,
11281 struct drm_crtc *crtc,
11282 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011283 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011284 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011285 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011286{
11287 return -ENODEV;
11288}
11289
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011290static bool __intel_pageflip_stall_check(struct drm_device *dev,
11291 struct drm_crtc *crtc)
11292{
11293 struct drm_i915_private *dev_priv = dev->dev_private;
11294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11295 struct intel_unpin_work *work = intel_crtc->unpin_work;
11296 u32 addr;
11297
11298 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11299 return true;
11300
Chris Wilson908565c2015-08-12 13:08:22 +010011301 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11302 return false;
11303
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011304 if (!work->enable_stall_check)
11305 return false;
11306
11307 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011308 if (work->flip_queued_req &&
11309 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011310 return false;
11311
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011312 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011313 }
11314
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011315 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011316 return false;
11317
11318 /* Potential stall - if we see that the flip has happened,
11319 * assume a missed interrupt. */
11320 if (INTEL_INFO(dev)->gen >= 4)
11321 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11322 else
11323 addr = I915_READ(DSPADDR(intel_crtc->plane));
11324
11325 /* There is a potential issue here with a false positive after a flip
11326 * to the same address. We could address this by checking for a
11327 * non-incrementing frame counter.
11328 */
11329 return addr == work->gtt_offset;
11330}
11331
11332void intel_check_page_flip(struct drm_device *dev, int pipe)
11333{
11334 struct drm_i915_private *dev_priv = dev->dev_private;
11335 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011337 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011338
Dave Gordon6c51d462015-03-06 15:34:26 +000011339 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011340
11341 if (crtc == NULL)
11342 return;
11343
Daniel Vetterf3260382014-09-15 14:55:23 +020011344 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011345 work = intel_crtc->unpin_work;
11346 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011347 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011348 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011349 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011350 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011351 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011352 if (work != NULL &&
11353 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11354 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011355 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011356}
11357
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011358static int intel_crtc_page_flip(struct drm_crtc *crtc,
11359 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011360 struct drm_pending_vblank_event *event,
11361 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011362{
11363 struct drm_device *dev = crtc->dev;
11364 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011365 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011366 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011368 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011369 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011370 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011371 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011372 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011373 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011374 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011375
Matt Roper2ff8fde2014-07-08 07:50:07 -070011376 /*
11377 * drm_mode_page_flip_ioctl() should already catch this, but double
11378 * check to be safe. In the future we may enable pageflipping from
11379 * a disabled primary plane.
11380 */
11381 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11382 return -EBUSY;
11383
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011384 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011385 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011386 return -EINVAL;
11387
11388 /*
11389 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11390 * Note that pitch changes could also affect these register.
11391 */
11392 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011393 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11394 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011395 return -EINVAL;
11396
Chris Wilsonf900db42014-02-20 09:26:13 +000011397 if (i915_terminally_wedged(&dev_priv->gpu_error))
11398 goto out_hang;
11399
Daniel Vetterb14c5672013-09-19 12:18:32 +020011400 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011401 if (work == NULL)
11402 return -ENOMEM;
11403
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011404 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011405 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011406 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011407 INIT_WORK(&work->work, intel_unpin_work_fn);
11408
Daniel Vetter87b6b102014-05-15 15:33:46 +020011409 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011410 if (ret)
11411 goto free_work;
11412
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011413 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011414 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011415 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011416 /* Before declaring the flip queue wedged, check if
11417 * the hardware completed the operation behind our backs.
11418 */
11419 if (__intel_pageflip_stall_check(dev, crtc)) {
11420 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11421 page_flip_completed(intel_crtc);
11422 } else {
11423 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011424 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011425
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011426 drm_crtc_vblank_put(crtc);
11427 kfree(work);
11428 return -EBUSY;
11429 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011430 }
11431 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011432 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011433
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011434 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11435 flush_workqueue(dev_priv->wq);
11436
Jesse Barnes75dfca82010-02-10 15:09:44 -080011437 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011438 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011439 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011440
Matt Roperf4510a22014-04-01 15:22:40 -070011441 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011442 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011443 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011444
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011445 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011446
Chris Wilson89ed88b2015-02-16 14:31:49 +000011447 ret = i915_mutex_lock_interruptible(dev);
11448 if (ret)
11449 goto cleanup;
11450
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011451 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011452 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011453
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011454 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011455 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011456
Wayne Boyer666a4532015-12-09 12:29:35 -080011457 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011458 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011459 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011460 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011461 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011462 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011463 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011464 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011465 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011466 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011467 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011468 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011469 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011470 }
11471
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011472 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011473
11474 /* When using CS flips, we want to emit semaphores between rings.
11475 * However, when using mmio flips we will create a task to do the
11476 * synchronisation, so all we want here is to pin the framebuffer
11477 * into the display plane and skip any waits.
11478 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011479 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011480 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011481 if (ret)
11482 goto cleanup_pending;
11483 }
11484
Ville Syrjälä3465c582016-02-15 22:54:43 +020011485 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011486 if (ret)
11487 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011488
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011489 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11490 obj, 0);
11491 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011492
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011493 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011494 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011495 if (ret)
11496 goto cleanup_unpin;
11497
John Harrisonf06cc1b2014-11-24 18:49:37 +000011498 i915_gem_request_assign(&work->flip_queued_req,
11499 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011500 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011501 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011502 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011503 if (IS_ERR(request)) {
11504 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011505 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011506 }
John Harrison6258fbe2015-05-29 17:43:48 +010011507 }
11508
11509 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011510 page_flip_flags);
11511 if (ret)
11512 goto cleanup_unpin;
11513
John Harrison6258fbe2015-05-29 17:43:48 +010011514 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011515 }
11516
John Harrison91af1272015-06-18 13:14:56 +010011517 if (request)
John Harrison75289872015-05-29 17:43:49 +010011518 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011519
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011520 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011521 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011522
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011523 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011524 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011525 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011526
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011527 intel_frontbuffer_flip_prepare(dev,
11528 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011529
Jesse Barnese5510fa2010-07-01 16:48:37 -070011530 trace_i915_flip_request(intel_crtc->plane, obj);
11531
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011532 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011533
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011534cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011535 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011536cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011537 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011538 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011539 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011540 mutex_unlock(&dev->struct_mutex);
11541cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011542 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011543 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011544
Chris Wilson89ed88b2015-02-16 14:31:49 +000011545 drm_gem_object_unreference_unlocked(&obj->base);
11546 drm_framebuffer_unreference(work->old_fb);
11547
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011548 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011549 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011550 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011551
Daniel Vetter87b6b102014-05-15 15:33:46 +020011552 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011553free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011554 kfree(work);
11555
Chris Wilsonf900db42014-02-20 09:26:13 +000011556 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011557 struct drm_atomic_state *state;
11558 struct drm_plane_state *plane_state;
11559
Chris Wilsonf900db42014-02-20 09:26:13 +000011560out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011561 state = drm_atomic_state_alloc(dev);
11562 if (!state)
11563 return -ENOMEM;
11564 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11565
11566retry:
11567 plane_state = drm_atomic_get_plane_state(state, primary);
11568 ret = PTR_ERR_OR_ZERO(plane_state);
11569 if (!ret) {
11570 drm_atomic_set_fb_for_plane(plane_state, fb);
11571
11572 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11573 if (!ret)
11574 ret = drm_atomic_commit(state);
11575 }
11576
11577 if (ret == -EDEADLK) {
11578 drm_modeset_backoff(state->acquire_ctx);
11579 drm_atomic_state_clear(state);
11580 goto retry;
11581 }
11582
11583 if (ret)
11584 drm_atomic_state_free(state);
11585
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011586 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011587 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011588 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011589 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011590 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011591 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011592 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011593}
11594
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011595
11596/**
11597 * intel_wm_need_update - Check whether watermarks need updating
11598 * @plane: drm plane
11599 * @state: new plane state
11600 *
11601 * Check current plane state versus the new one to determine whether
11602 * watermarks need to be recalculated.
11603 *
11604 * Returns true or false.
11605 */
11606static bool intel_wm_need_update(struct drm_plane *plane,
11607 struct drm_plane_state *state)
11608{
Matt Roperd21fbe82015-09-24 15:53:12 -070011609 struct intel_plane_state *new = to_intel_plane_state(state);
11610 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11611
11612 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011613 if (new->visible != cur->visible)
11614 return true;
11615
11616 if (!cur->base.fb || !new->base.fb)
11617 return false;
11618
11619 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11620 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011621 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11622 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11623 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11624 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011625 return true;
11626
11627 return false;
11628}
11629
Matt Roperd21fbe82015-09-24 15:53:12 -070011630static bool needs_scaling(struct intel_plane_state *state)
11631{
11632 int src_w = drm_rect_width(&state->src) >> 16;
11633 int src_h = drm_rect_height(&state->src) >> 16;
11634 int dst_w = drm_rect_width(&state->dst);
11635 int dst_h = drm_rect_height(&state->dst);
11636
11637 return (src_w != dst_w || src_h != dst_h);
11638}
11639
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011640int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11641 struct drm_plane_state *plane_state)
11642{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011643 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011644 struct drm_crtc *crtc = crtc_state->crtc;
11645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11646 struct drm_plane *plane = plane_state->plane;
11647 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011648 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011649 struct intel_plane_state *old_plane_state =
11650 to_intel_plane_state(plane->state);
11651 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011652 bool mode_changed = needs_modeset(crtc_state);
11653 bool was_crtc_enabled = crtc->state->active;
11654 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011655 bool turn_off, turn_on, visible, was_visible;
11656 struct drm_framebuffer *fb = plane_state->fb;
11657
11658 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11659 plane->type != DRM_PLANE_TYPE_CURSOR) {
11660 ret = skl_update_scaler_plane(
11661 to_intel_crtc_state(crtc_state),
11662 to_intel_plane_state(plane_state));
11663 if (ret)
11664 return ret;
11665 }
11666
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011667 was_visible = old_plane_state->visible;
11668 visible = to_intel_plane_state(plane_state)->visible;
11669
11670 if (!was_crtc_enabled && WARN_ON(was_visible))
11671 was_visible = false;
11672
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011673 /*
11674 * Visibility is calculated as if the crtc was on, but
11675 * after scaler setup everything depends on it being off
11676 * when the crtc isn't active.
11677 */
11678 if (!is_crtc_enabled)
11679 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011680
11681 if (!was_visible && !visible)
11682 return 0;
11683
Maarten Lankhorste8861672016-02-24 11:24:26 +010011684 if (fb != old_plane_state->base.fb)
11685 pipe_config->fb_changed = true;
11686
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011687 turn_off = was_visible && (!visible || mode_changed);
11688 turn_on = visible && (!was_visible || mode_changed);
11689
11690 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11691 plane->base.id, fb ? fb->base.id : -1);
11692
11693 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11694 plane->base.id, was_visible, visible,
11695 turn_off, turn_on, mode_changed);
11696
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011697 if (turn_on) {
11698 pipe_config->update_wm_pre = true;
11699
11700 /* must disable cxsr around plane enable/disable */
11701 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11702 pipe_config->disable_cxsr = true;
11703 } else if (turn_off) {
11704 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011705
Ville Syrjälä852eb002015-06-24 22:00:07 +030011706 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011707 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011708 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011709 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011710 /* FIXME bollocks */
11711 pipe_config->update_wm_pre = true;
11712 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011713 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011714
Matt Ropered4a6a72016-02-23 17:20:13 -080011715 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011716 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11717 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011718 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11719
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011720 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011721 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011722
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011723 /*
11724 * WaCxSRDisabledForSpriteScaling:ivb
11725 *
11726 * cstate->update_wm was already set above, so this flag will
11727 * take effect when we commit and program watermarks.
11728 */
11729 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11730 needs_scaling(to_intel_plane_state(plane_state)) &&
11731 !needs_scaling(old_plane_state))
11732 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011733
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011734 return 0;
11735}
11736
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011737static bool encoders_cloneable(const struct intel_encoder *a,
11738 const struct intel_encoder *b)
11739{
11740 /* masks could be asymmetric, so check both ways */
11741 return a == b || (a->cloneable & (1 << b->type) &&
11742 b->cloneable & (1 << a->type));
11743}
11744
11745static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11746 struct intel_crtc *crtc,
11747 struct intel_encoder *encoder)
11748{
11749 struct intel_encoder *source_encoder;
11750 struct drm_connector *connector;
11751 struct drm_connector_state *connector_state;
11752 int i;
11753
11754 for_each_connector_in_state(state, connector, connector_state, i) {
11755 if (connector_state->crtc != &crtc->base)
11756 continue;
11757
11758 source_encoder =
11759 to_intel_encoder(connector_state->best_encoder);
11760 if (!encoders_cloneable(encoder, source_encoder))
11761 return false;
11762 }
11763
11764 return true;
11765}
11766
11767static bool check_encoder_cloning(struct drm_atomic_state *state,
11768 struct intel_crtc *crtc)
11769{
11770 struct intel_encoder *encoder;
11771 struct drm_connector *connector;
11772 struct drm_connector_state *connector_state;
11773 int i;
11774
11775 for_each_connector_in_state(state, connector, connector_state, i) {
11776 if (connector_state->crtc != &crtc->base)
11777 continue;
11778
11779 encoder = to_intel_encoder(connector_state->best_encoder);
11780 if (!check_single_encoder_cloning(state, crtc, encoder))
11781 return false;
11782 }
11783
11784 return true;
11785}
11786
11787static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11788 struct drm_crtc_state *crtc_state)
11789{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011790 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011791 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011793 struct intel_crtc_state *pipe_config =
11794 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011795 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011796 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011797 bool mode_changed = needs_modeset(crtc_state);
11798
11799 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11800 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11801 return -EINVAL;
11802 }
11803
Ville Syrjälä852eb002015-06-24 22:00:07 +030011804 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011805 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011806
Maarten Lankhorstad421372015-06-15 12:33:42 +020011807 if (mode_changed && crtc_state->enable &&
11808 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011809 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011810 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11811 pipe_config);
11812 if (ret)
11813 return ret;
11814 }
11815
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011816 if (crtc_state->color_mgmt_changed) {
11817 ret = intel_color_check(crtc, crtc_state);
11818 if (ret)
11819 return ret;
11820 }
11821
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011822 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011823 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011824 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011825 if (ret) {
11826 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011827 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011828 }
11829 }
11830
11831 if (dev_priv->display.compute_intermediate_wm &&
11832 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11833 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11834 return 0;
11835
11836 /*
11837 * Calculate 'intermediate' watermarks that satisfy both the
11838 * old state and the new state. We can program these
11839 * immediately.
11840 */
11841 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11842 intel_crtc,
11843 pipe_config);
11844 if (ret) {
11845 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11846 return ret;
11847 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011848 }
11849
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011850 if (INTEL_INFO(dev)->gen >= 9) {
11851 if (mode_changed)
11852 ret = skl_update_scaler_crtc(pipe_config);
11853
11854 if (!ret)
11855 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11856 pipe_config);
11857 }
11858
11859 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011860}
11861
Jani Nikula65b38e02015-04-13 11:26:56 +030011862static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011863 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011864 .atomic_begin = intel_begin_crtc_commit,
11865 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011866 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011867};
11868
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011869static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11870{
11871 struct intel_connector *connector;
11872
11873 for_each_intel_connector(dev, connector) {
11874 if (connector->base.encoder) {
11875 connector->base.state->best_encoder =
11876 connector->base.encoder;
11877 connector->base.state->crtc =
11878 connector->base.encoder->crtc;
11879 } else {
11880 connector->base.state->best_encoder = NULL;
11881 connector->base.state->crtc = NULL;
11882 }
11883 }
11884}
11885
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011886static void
Robin Schroereba905b2014-05-18 02:24:50 +020011887connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011888 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011889{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011890 int bpp = pipe_config->pipe_bpp;
11891
11892 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11893 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011894 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011895
11896 /* Don't use an invalid EDID bpc value */
11897 if (connector->base.display_info.bpc &&
11898 connector->base.display_info.bpc * 3 < bpp) {
11899 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11900 bpp, connector->base.display_info.bpc*3);
11901 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11902 }
11903
Jani Nikula013dd9e2016-01-13 16:35:20 +020011904 /* Clamp bpp to default limit on screens without EDID 1.4 */
11905 if (connector->base.display_info.bpc == 0) {
11906 int type = connector->base.connector_type;
11907 int clamp_bpp = 24;
11908
11909 /* Fall back to 18 bpp when DP sink capability is unknown. */
11910 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11911 type == DRM_MODE_CONNECTOR_eDP)
11912 clamp_bpp = 18;
11913
11914 if (bpp > clamp_bpp) {
11915 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11916 bpp, clamp_bpp);
11917 pipe_config->pipe_bpp = clamp_bpp;
11918 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011919 }
11920}
11921
11922static int
11923compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011924 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011925{
11926 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011927 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011928 struct drm_connector *connector;
11929 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011930 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011931
Wayne Boyer666a4532015-12-09 12:29:35 -080011932 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011933 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011934 else if (INTEL_INFO(dev)->gen >= 5)
11935 bpp = 12*3;
11936 else
11937 bpp = 8*3;
11938
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011939
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011940 pipe_config->pipe_bpp = bpp;
11941
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011942 state = pipe_config->base.state;
11943
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011944 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011945 for_each_connector_in_state(state, connector, connector_state, i) {
11946 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011947 continue;
11948
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011949 connected_sink_compute_bpp(to_intel_connector(connector),
11950 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011951 }
11952
11953 return bpp;
11954}
11955
Daniel Vetter644db712013-09-19 14:53:58 +020011956static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11957{
11958 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11959 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011960 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011961 mode->crtc_hdisplay, mode->crtc_hsync_start,
11962 mode->crtc_hsync_end, mode->crtc_htotal,
11963 mode->crtc_vdisplay, mode->crtc_vsync_start,
11964 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11965}
11966
Daniel Vetterc0b03412013-05-28 12:05:54 +020011967static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011968 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011969 const char *context)
11970{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011971 struct drm_device *dev = crtc->base.dev;
11972 struct drm_plane *plane;
11973 struct intel_plane *intel_plane;
11974 struct intel_plane_state *state;
11975 struct drm_framebuffer *fb;
11976
11977 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11978 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011979
Jani Nikulada205632016-03-15 21:51:10 +020011980 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011981 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11982 pipe_config->pipe_bpp, pipe_config->dither);
11983 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11984 pipe_config->has_pch_encoder,
11985 pipe_config->fdi_lanes,
11986 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11987 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11988 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011989 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 +030011990 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011991 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011992 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11993 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11994 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011995
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011996 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 -070011997 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011998 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011999 pipe_config->dp_m2_n2.gmch_m,
12000 pipe_config->dp_m2_n2.gmch_n,
12001 pipe_config->dp_m2_n2.link_m,
12002 pipe_config->dp_m2_n2.link_n,
12003 pipe_config->dp_m2_n2.tu);
12004
Daniel Vetter55072d12014-11-20 16:10:28 +010012005 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12006 pipe_config->has_audio,
12007 pipe_config->has_infoframe);
12008
Daniel Vetterc0b03412013-05-28 12:05:54 +020012009 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012010 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012011 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012012 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12013 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012014 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012015 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12016 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012017 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12018 crtc->num_scalers,
12019 pipe_config->scaler_state.scaler_users,
12020 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012021 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12022 pipe_config->gmch_pfit.control,
12023 pipe_config->gmch_pfit.pgm_ratios,
12024 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012025 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012026 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012027 pipe_config->pch_pfit.size,
12028 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012029 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012030 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012031
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012032 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012033 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012034 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012035 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012036 pipe_config->ddi_pll_sel,
12037 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012038 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012039 pipe_config->dpll_hw_state.pll0,
12040 pipe_config->dpll_hw_state.pll1,
12041 pipe_config->dpll_hw_state.pll2,
12042 pipe_config->dpll_hw_state.pll3,
12043 pipe_config->dpll_hw_state.pll6,
12044 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012045 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012046 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012047 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012048 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012049 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12050 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12051 pipe_config->ddi_pll_sel,
12052 pipe_config->dpll_hw_state.ctrl1,
12053 pipe_config->dpll_hw_state.cfgcr1,
12054 pipe_config->dpll_hw_state.cfgcr2);
12055 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012056 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 +010012057 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012058 pipe_config->dpll_hw_state.wrpll,
12059 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012060 } else {
12061 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12062 "fp0: 0x%x, fp1: 0x%x\n",
12063 pipe_config->dpll_hw_state.dpll,
12064 pipe_config->dpll_hw_state.dpll_md,
12065 pipe_config->dpll_hw_state.fp0,
12066 pipe_config->dpll_hw_state.fp1);
12067 }
12068
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012069 DRM_DEBUG_KMS("planes on this crtc\n");
12070 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12071 intel_plane = to_intel_plane(plane);
12072 if (intel_plane->pipe != crtc->pipe)
12073 continue;
12074
12075 state = to_intel_plane_state(plane->state);
12076 fb = state->base.fb;
12077 if (!fb) {
12078 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12079 "disabled, scaler_id = %d\n",
12080 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12081 plane->base.id, intel_plane->pipe,
12082 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12083 drm_plane_index(plane), state->scaler_id);
12084 continue;
12085 }
12086
12087 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12088 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12089 plane->base.id, intel_plane->pipe,
12090 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12091 drm_plane_index(plane));
12092 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12093 fb->base.id, fb->width, fb->height, fb->pixel_format);
12094 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12095 state->scaler_id,
12096 state->src.x1 >> 16, state->src.y1 >> 16,
12097 drm_rect_width(&state->src) >> 16,
12098 drm_rect_height(&state->src) >> 16,
12099 state->dst.x1, state->dst.y1,
12100 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12101 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012102}
12103
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012104static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012105{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012106 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012107 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012108 unsigned int used_ports = 0;
12109
12110 /*
12111 * Walk the connector list instead of the encoder
12112 * list to detect the problem on ddi platforms
12113 * where there's just one encoder per digital port.
12114 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012115 drm_for_each_connector(connector, dev) {
12116 struct drm_connector_state *connector_state;
12117 struct intel_encoder *encoder;
12118
12119 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12120 if (!connector_state)
12121 connector_state = connector->state;
12122
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012123 if (!connector_state->best_encoder)
12124 continue;
12125
12126 encoder = to_intel_encoder(connector_state->best_encoder);
12127
12128 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012129
12130 switch (encoder->type) {
12131 unsigned int port_mask;
12132 case INTEL_OUTPUT_UNKNOWN:
12133 if (WARN_ON(!HAS_DDI(dev)))
12134 break;
12135 case INTEL_OUTPUT_DISPLAYPORT:
12136 case INTEL_OUTPUT_HDMI:
12137 case INTEL_OUTPUT_EDP:
12138 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12139
12140 /* the same port mustn't appear more than once */
12141 if (used_ports & port_mask)
12142 return false;
12143
12144 used_ports |= port_mask;
12145 default:
12146 break;
12147 }
12148 }
12149
12150 return true;
12151}
12152
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012153static void
12154clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12155{
12156 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012157 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012158 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012159 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012160 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012161 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012162
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012163 /* FIXME: before the switch to atomic started, a new pipe_config was
12164 * kzalloc'd. Code that depends on any field being zero should be
12165 * fixed, so that the crtc_state can be safely duplicated. For now,
12166 * only fields that are know to not cause problems are preserved. */
12167
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012168 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012169 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012170 shared_dpll = crtc_state->shared_dpll;
12171 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012172 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012173 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012174
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012175 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012176
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012177 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012178 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012179 crtc_state->shared_dpll = shared_dpll;
12180 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012181 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012182 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012183}
12184
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012185static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012186intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012187 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012188{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012189 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012190 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012191 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012192 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012193 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012194 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012195 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012196
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012197 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012198
Daniel Vettere143a212013-07-04 12:01:15 +020012199 pipe_config->cpu_transcoder =
12200 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012201
Imre Deak2960bc92013-07-30 13:36:32 +030012202 /*
12203 * Sanitize sync polarity flags based on requested ones. If neither
12204 * positive or negative polarity is requested, treat this as meaning
12205 * negative polarity.
12206 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012207 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012208 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012209 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012210
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012211 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012212 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012213 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012214
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012215 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12216 pipe_config);
12217 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012218 goto fail;
12219
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012220 /*
12221 * Determine the real pipe dimensions. Note that stereo modes can
12222 * increase the actual pipe size due to the frame doubling and
12223 * insertion of additional space for blanks between the frame. This
12224 * is stored in the crtc timings. We use the requested mode to do this
12225 * computation to clearly distinguish it from the adjusted mode, which
12226 * can be changed by the connectors in the below retry loop.
12227 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012228 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012229 &pipe_config->pipe_src_w,
12230 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012231
Daniel Vettere29c22c2013-02-21 00:00:16 +010012232encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012233 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012234 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012235 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012236
Daniel Vetter135c81b2013-07-21 21:37:09 +020012237 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012238 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12239 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012240
Daniel Vetter7758a112012-07-08 19:40:39 +020012241 /* Pass our mode to the connectors and the CRTC to give them a chance to
12242 * adjust it according to limitations or connector properties, and also
12243 * a chance to reject the mode entirely.
12244 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012245 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012246 if (connector_state->crtc != crtc)
12247 continue;
12248
12249 encoder = to_intel_encoder(connector_state->best_encoder);
12250
Daniel Vetterefea6e82013-07-21 21:36:59 +020012251 if (!(encoder->compute_config(encoder, pipe_config))) {
12252 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012253 goto fail;
12254 }
12255 }
12256
Daniel Vetterff9a6752013-06-01 17:16:21 +020012257 /* Set default port clock if not overwritten by the encoder. Needs to be
12258 * done afterwards in case the encoder adjusts the mode. */
12259 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012260 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012261 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012262
Daniel Vettera43f6e02013-06-07 23:10:32 +020012263 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012264 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012265 DRM_DEBUG_KMS("CRTC fixup failed\n");
12266 goto fail;
12267 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012268
12269 if (ret == RETRY) {
12270 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12271 ret = -EINVAL;
12272 goto fail;
12273 }
12274
12275 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12276 retry = false;
12277 goto encoder_retry;
12278 }
12279
Daniel Vettere8fa4272015-08-12 11:43:34 +020012280 /* Dithering seems to not pass-through bits correctly when it should, so
12281 * only enable it on 6bpc panels. */
12282 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012283 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012284 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012285
Daniel Vetter7758a112012-07-08 19:40:39 +020012286fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012287 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012288}
12289
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012290static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012291intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012292{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012293 struct drm_crtc *crtc;
12294 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012295 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012296
Ville Syrjälä76688512014-01-10 11:28:06 +020012297 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012298 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012299 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012300
12301 /* Update hwmode for vblank functions */
12302 if (crtc->state->active)
12303 crtc->hwmode = crtc->state->adjusted_mode;
12304 else
12305 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012306
12307 /*
12308 * Update legacy state to satisfy fbc code. This can
12309 * be removed when fbc uses the atomic state.
12310 */
12311 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12312 struct drm_plane_state *plane_state = crtc->primary->state;
12313
12314 crtc->primary->fb = plane_state->fb;
12315 crtc->x = plane_state->src_x >> 16;
12316 crtc->y = plane_state->src_y >> 16;
12317 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012318 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012319}
12320
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012321static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012322{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012323 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012324
12325 if (clock1 == clock2)
12326 return true;
12327
12328 if (!clock1 || !clock2)
12329 return false;
12330
12331 diff = abs(clock1 - clock2);
12332
12333 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12334 return true;
12335
12336 return false;
12337}
12338
Daniel Vetter25c5b262012-07-08 22:08:04 +020012339#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12340 list_for_each_entry((intel_crtc), \
12341 &(dev)->mode_config.crtc_list, \
12342 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012343 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012344
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012345static bool
12346intel_compare_m_n(unsigned int m, unsigned int n,
12347 unsigned int m2, unsigned int n2,
12348 bool exact)
12349{
12350 if (m == m2 && n == n2)
12351 return true;
12352
12353 if (exact || !m || !n || !m2 || !n2)
12354 return false;
12355
12356 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12357
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012358 if (n > n2) {
12359 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012360 m2 <<= 1;
12361 n2 <<= 1;
12362 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012363 } else if (n < n2) {
12364 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012365 m <<= 1;
12366 n <<= 1;
12367 }
12368 }
12369
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012370 if (n != n2)
12371 return false;
12372
12373 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012374}
12375
12376static bool
12377intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12378 struct intel_link_m_n *m2_n2,
12379 bool adjust)
12380{
12381 if (m_n->tu == m2_n2->tu &&
12382 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12383 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12384 intel_compare_m_n(m_n->link_m, m_n->link_n,
12385 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12386 if (adjust)
12387 *m2_n2 = *m_n;
12388
12389 return true;
12390 }
12391
12392 return false;
12393}
12394
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012395static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012396intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012397 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012398 struct intel_crtc_state *pipe_config,
12399 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012400{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012401 bool ret = true;
12402
12403#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12404 do { \
12405 if (!adjust) \
12406 DRM_ERROR(fmt, ##__VA_ARGS__); \
12407 else \
12408 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12409 } while (0)
12410
Daniel Vetter66e985c2013-06-05 13:34:20 +020012411#define PIPE_CONF_CHECK_X(name) \
12412 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012413 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012414 "(expected 0x%08x, found 0x%08x)\n", \
12415 current_config->name, \
12416 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012417 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012418 }
12419
Daniel Vetter08a24032013-04-19 11:25:34 +020012420#define PIPE_CONF_CHECK_I(name) \
12421 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012422 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012423 "(expected %i, found %i)\n", \
12424 current_config->name, \
12425 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012426 ret = false; \
12427 }
12428
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012429#define PIPE_CONF_CHECK_P(name) \
12430 if (current_config->name != pipe_config->name) { \
12431 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12432 "(expected %p, found %p)\n", \
12433 current_config->name, \
12434 pipe_config->name); \
12435 ret = false; \
12436 }
12437
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012438#define PIPE_CONF_CHECK_M_N(name) \
12439 if (!intel_compare_link_m_n(&current_config->name, \
12440 &pipe_config->name,\
12441 adjust)) { \
12442 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12443 "(expected tu %i gmch %i/%i link %i/%i, " \
12444 "found tu %i, gmch %i/%i link %i/%i)\n", \
12445 current_config->name.tu, \
12446 current_config->name.gmch_m, \
12447 current_config->name.gmch_n, \
12448 current_config->name.link_m, \
12449 current_config->name.link_n, \
12450 pipe_config->name.tu, \
12451 pipe_config->name.gmch_m, \
12452 pipe_config->name.gmch_n, \
12453 pipe_config->name.link_m, \
12454 pipe_config->name.link_n); \
12455 ret = false; \
12456 }
12457
12458#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12459 if (!intel_compare_link_m_n(&current_config->name, \
12460 &pipe_config->name, adjust) && \
12461 !intel_compare_link_m_n(&current_config->alt_name, \
12462 &pipe_config->name, adjust)) { \
12463 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12464 "(expected tu %i gmch %i/%i link %i/%i, " \
12465 "or tu %i gmch %i/%i link %i/%i, " \
12466 "found tu %i, gmch %i/%i link %i/%i)\n", \
12467 current_config->name.tu, \
12468 current_config->name.gmch_m, \
12469 current_config->name.gmch_n, \
12470 current_config->name.link_m, \
12471 current_config->name.link_n, \
12472 current_config->alt_name.tu, \
12473 current_config->alt_name.gmch_m, \
12474 current_config->alt_name.gmch_n, \
12475 current_config->alt_name.link_m, \
12476 current_config->alt_name.link_n, \
12477 pipe_config->name.tu, \
12478 pipe_config->name.gmch_m, \
12479 pipe_config->name.gmch_n, \
12480 pipe_config->name.link_m, \
12481 pipe_config->name.link_n); \
12482 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012483 }
12484
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012485/* This is required for BDW+ where there is only one set of registers for
12486 * switching between high and low RR.
12487 * This macro can be used whenever a comparison has to be made between one
12488 * hw state and multiple sw state variables.
12489 */
12490#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12491 if ((current_config->name != pipe_config->name) && \
12492 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012493 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012494 "(expected %i or %i, found %i)\n", \
12495 current_config->name, \
12496 current_config->alt_name, \
12497 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012498 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012499 }
12500
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012501#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12502 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012503 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012504 "(expected %i, found %i)\n", \
12505 current_config->name & (mask), \
12506 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012507 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012508 }
12509
Ville Syrjälä5e550652013-09-06 23:29:07 +030012510#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12511 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012512 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012513 "(expected %i, found %i)\n", \
12514 current_config->name, \
12515 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012516 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012517 }
12518
Daniel Vetterbb760062013-06-06 14:55:52 +020012519#define PIPE_CONF_QUIRK(quirk) \
12520 ((current_config->quirks | pipe_config->quirks) & (quirk))
12521
Daniel Vettereccb1402013-05-22 00:50:22 +020012522 PIPE_CONF_CHECK_I(cpu_transcoder);
12523
Daniel Vetter08a24032013-04-19 11:25:34 +020012524 PIPE_CONF_CHECK_I(has_pch_encoder);
12525 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012526 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012527
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012528 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012529 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012530
12531 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012533
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012534 if (current_config->has_drrs)
12535 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12536 } else
12537 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012538
Jani Nikulaa65347b2015-11-27 12:21:46 +020012539 PIPE_CONF_CHECK_I(has_dsi_encoder);
12540
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012541 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12542 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12543 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12544 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12545 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12546 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012547
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012548 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12549 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12550 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12551 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12552 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12553 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012554
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012555 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012556 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012557 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012558 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012559 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012560 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012561
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012562 PIPE_CONF_CHECK_I(has_audio);
12563
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012564 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012565 DRM_MODE_FLAG_INTERLACE);
12566
Daniel Vetterbb760062013-06-06 14:55:52 +020012567 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012568 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012569 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012570 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012571 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012572 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012573 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012574 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012575 DRM_MODE_FLAG_NVSYNC);
12576 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012577
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012578 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012579 /* pfit ratios are autocomputed by the hw on gen4+ */
12580 if (INTEL_INFO(dev)->gen < 4)
12581 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012582 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012583
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012584 if (!adjust) {
12585 PIPE_CONF_CHECK_I(pipe_src_w);
12586 PIPE_CONF_CHECK_I(pipe_src_h);
12587
12588 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12589 if (current_config->pch_pfit.enabled) {
12590 PIPE_CONF_CHECK_X(pch_pfit.pos);
12591 PIPE_CONF_CHECK_X(pch_pfit.size);
12592 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012593
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012594 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12595 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012596
Jesse Barnese59150d2014-01-07 13:30:45 -080012597 /* BDW+ don't expose a synchronous way to read the state */
12598 if (IS_HASWELL(dev))
12599 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012600
Ville Syrjälä282740f2013-09-04 18:30:03 +030012601 PIPE_CONF_CHECK_I(double_wide);
12602
Daniel Vetter26804af2014-06-25 22:01:55 +030012603 PIPE_CONF_CHECK_X(ddi_pll_sel);
12604
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012605 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012606 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012607 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012608 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12609 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012610 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012611 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012612 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12613 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12614 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012615
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012616 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12617 PIPE_CONF_CHECK_I(pipe_bpp);
12618
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012619 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012620 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012621
Daniel Vetter66e985c2013-06-05 13:34:20 +020012622#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012623#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012624#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012625#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012626#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012627#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012628#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012630
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012631 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012632}
12633
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012634static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12635 const struct intel_crtc_state *pipe_config)
12636{
12637 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012638 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012639 &pipe_config->fdi_m_n);
12640 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12641
12642 /*
12643 * FDI already provided one idea for the dotclock.
12644 * Yell if the encoder disagrees.
12645 */
12646 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12647 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12648 fdi_dotclock, dotclock);
12649 }
12650}
12651
Damien Lespiau08db6652014-11-04 17:06:52 +000012652static void check_wm_state(struct drm_device *dev)
12653{
12654 struct drm_i915_private *dev_priv = dev->dev_private;
12655 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12656 struct intel_crtc *intel_crtc;
12657 int plane;
12658
12659 if (INTEL_INFO(dev)->gen < 9)
12660 return;
12661
12662 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12663 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12664
12665 for_each_intel_crtc(dev, intel_crtc) {
12666 struct skl_ddb_entry *hw_entry, *sw_entry;
12667 const enum pipe pipe = intel_crtc->pipe;
12668
12669 if (!intel_crtc->active)
12670 continue;
12671
12672 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012673 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012674 hw_entry = &hw_ddb.plane[pipe][plane];
12675 sw_entry = &sw_ddb->plane[pipe][plane];
12676
12677 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12678 continue;
12679
12680 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12681 "(expected (%u,%u), found (%u,%u))\n",
12682 pipe_name(pipe), plane + 1,
12683 sw_entry->start, sw_entry->end,
12684 hw_entry->start, hw_entry->end);
12685 }
12686
12687 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012688 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12689 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012690
12691 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12692 continue;
12693
12694 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12695 "(expected (%u,%u), found (%u,%u))\n",
12696 pipe_name(pipe),
12697 sw_entry->start, sw_entry->end,
12698 hw_entry->start, hw_entry->end);
12699 }
12700}
12701
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012702static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012703check_connector_state(struct drm_device *dev,
12704 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012705{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012706 struct drm_connector_state *old_conn_state;
12707 struct drm_connector *connector;
12708 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012709
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012710 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12711 struct drm_encoder *encoder = connector->encoder;
12712 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012713
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012714 /* This also checks the encoder/connector hw state with the
12715 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012716 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012717
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012718 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012719 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012720 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012721}
12722
12723static void
12724check_encoder_state(struct drm_device *dev)
12725{
12726 struct intel_encoder *encoder;
12727 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012728
Damien Lespiaub2784e12014-08-05 11:29:37 +010012729 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012730 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012731 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012732
12733 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12734 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012735 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012736
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012737 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012738 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012739 continue;
12740 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012741
12742 I915_STATE_WARN(connector->base.state->crtc !=
12743 encoder->base.crtc,
12744 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012745 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012746
Rob Clarke2c719b2014-12-15 13:56:32 -050012747 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012748 "encoder's enabled state mismatch "
12749 "(expected %i, found %i)\n",
12750 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012751
12752 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012753 bool active;
12754
12755 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012756 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012757 "encoder detached but still enabled on pipe %c.\n",
12758 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012759 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012760 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012761}
12762
12763static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012764check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012765{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012766 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012767 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012768 struct drm_crtc_state *old_crtc_state;
12769 struct drm_crtc *crtc;
12770 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012771
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012772 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12774 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012775 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012776
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012777 if (!needs_modeset(crtc->state) &&
12778 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012779 continue;
12780
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012781 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12782 pipe_config = to_intel_crtc_state(old_crtc_state);
12783 memset(pipe_config, 0, sizeof(*pipe_config));
12784 pipe_config->base.crtc = crtc;
12785 pipe_config->base.state = old_state;
12786
12787 DRM_DEBUG_KMS("[CRTC:%d]\n",
12788 crtc->base.id);
12789
12790 active = dev_priv->display.get_pipe_config(intel_crtc,
12791 pipe_config);
12792
12793 /* hw state is inconsistent with the pipe quirk */
12794 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12795 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12796 active = crtc->state->active;
12797
12798 I915_STATE_WARN(crtc->state->active != active,
12799 "crtc active state doesn't match with hw state "
12800 "(expected %i, found %i)\n", crtc->state->active, active);
12801
12802 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12803 "transitional active state does not match atomic hw state "
12804 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12805
12806 for_each_encoder_on_crtc(dev, crtc, encoder) {
12807 enum pipe pipe;
12808
12809 active = encoder->get_hw_state(encoder, &pipe);
12810 I915_STATE_WARN(active != crtc->state->active,
12811 "[ENCODER:%i] active %i with crtc active %i\n",
12812 encoder->base.base.id, active, crtc->state->active);
12813
12814 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12815 "Encoder connected to wrong pipe %c\n",
12816 pipe_name(pipe));
12817
12818 if (active)
12819 encoder->get_config(encoder, pipe_config);
12820 }
12821
12822 if (!crtc->state->active)
12823 continue;
12824
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012825 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12826
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012827 sw_config = to_intel_crtc_state(crtc->state);
12828 if (!intel_pipe_config_compare(dev, sw_config,
12829 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012830 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012831 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012832 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012833 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012834 "[sw state]");
12835 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012836 }
12837}
12838
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012839static void
12840check_shared_dpll_state(struct drm_device *dev)
12841{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012842 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012843 struct intel_crtc *crtc;
12844 struct intel_dpll_hw_state dpll_hw_state;
12845 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012846
12847 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012848 struct intel_shared_dpll *pll =
12849 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012850 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012851 bool active;
12852
12853 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12854
12855 DRM_DEBUG_KMS("%s\n", pll->name);
12856
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012857 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012858
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012859 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12860 "more active pll users than references: %x vs %x\n",
12861 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012862
12863 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012864 I915_STATE_WARN(!pll->on && pll->active_mask,
12865 "pll in active use but not on in sw tracking\n");
12866 I915_STATE_WARN(pll->on && !pll->active_mask,
12867 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012868 I915_STATE_WARN(pll->on != active,
12869 "pll on state mismatch (expected %i, found %i)\n",
12870 pll->on, active);
12871 }
Daniel Vetter53589012013-06-05 13:34:16 +020012872
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012873 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012874 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012875 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12876 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12877 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012878 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012879
12880 I915_STATE_WARN(pll->active_mask != active_crtcs,
12881 "pll active crtcs mismatch (expected %x, found %x)\n",
12882 pll->active_mask, active_crtcs);
12883 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12884 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12885 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012886
Rob Clarke2c719b2014-12-15 13:56:32 -050012887 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012888 sizeof(dpll_hw_state)),
12889 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012890 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012891}
12892
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012893static void
12894intel_modeset_check_state(struct drm_device *dev,
12895 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012896{
Damien Lespiau08db6652014-11-04 17:06:52 +000012897 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012898 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012899 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012900 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012901 check_shared_dpll_state(dev);
12902}
12903
Ville Syrjälä80715b22014-05-15 20:23:23 +030012904static void update_scanline_offset(struct intel_crtc *crtc)
12905{
12906 struct drm_device *dev = crtc->base.dev;
12907
12908 /*
12909 * The scanline counter increments at the leading edge of hsync.
12910 *
12911 * On most platforms it starts counting from vtotal-1 on the
12912 * first active line. That means the scanline counter value is
12913 * always one less than what we would expect. Ie. just after
12914 * start of vblank, which also occurs at start of hsync (on the
12915 * last active line), the scanline counter will read vblank_start-1.
12916 *
12917 * On gen2 the scanline counter starts counting from 1 instead
12918 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12919 * to keep the value positive), instead of adding one.
12920 *
12921 * On HSW+ the behaviour of the scanline counter depends on the output
12922 * type. For DP ports it behaves like most other platforms, but on HDMI
12923 * there's an extra 1 line difference. So we need to add two instead of
12924 * one to the value.
12925 */
12926 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012927 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012928 int vtotal;
12929
Ville Syrjälä124abe02015-09-08 13:40:45 +030012930 vtotal = adjusted_mode->crtc_vtotal;
12931 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012932 vtotal /= 2;
12933
12934 crtc->scanline_offset = vtotal - 1;
12935 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012936 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012937 crtc->scanline_offset = 2;
12938 } else
12939 crtc->scanline_offset = 1;
12940}
12941
Maarten Lankhorstad421372015-06-15 12:33:42 +020012942static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012943{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012944 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012945 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012946 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012947 struct drm_crtc *crtc;
12948 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012949 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012950
12951 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012952 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012953
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012954 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012956 struct intel_shared_dpll *old_dpll =
12957 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012958
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012959 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012960 continue;
12961
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012962 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012963
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012964 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012965 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012966
Maarten Lankhorstad421372015-06-15 12:33:42 +020012967 if (!shared_dpll)
12968 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12969
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012970 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012971 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012972}
12973
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012974/*
12975 * This implements the workaround described in the "notes" section of the mode
12976 * set sequence documentation. When going from no pipes or single pipe to
12977 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12978 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12979 */
12980static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12981{
12982 struct drm_crtc_state *crtc_state;
12983 struct intel_crtc *intel_crtc;
12984 struct drm_crtc *crtc;
12985 struct intel_crtc_state *first_crtc_state = NULL;
12986 struct intel_crtc_state *other_crtc_state = NULL;
12987 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12988 int i;
12989
12990 /* look at all crtc's that are going to be enabled in during modeset */
12991 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12992 intel_crtc = to_intel_crtc(crtc);
12993
12994 if (!crtc_state->active || !needs_modeset(crtc_state))
12995 continue;
12996
12997 if (first_crtc_state) {
12998 other_crtc_state = to_intel_crtc_state(crtc_state);
12999 break;
13000 } else {
13001 first_crtc_state = to_intel_crtc_state(crtc_state);
13002 first_pipe = intel_crtc->pipe;
13003 }
13004 }
13005
13006 /* No workaround needed? */
13007 if (!first_crtc_state)
13008 return 0;
13009
13010 /* w/a possibly needed, check how many crtc's are already enabled. */
13011 for_each_intel_crtc(state->dev, intel_crtc) {
13012 struct intel_crtc_state *pipe_config;
13013
13014 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13015 if (IS_ERR(pipe_config))
13016 return PTR_ERR(pipe_config);
13017
13018 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13019
13020 if (!pipe_config->base.active ||
13021 needs_modeset(&pipe_config->base))
13022 continue;
13023
13024 /* 2 or more enabled crtcs means no need for w/a */
13025 if (enabled_pipe != INVALID_PIPE)
13026 return 0;
13027
13028 enabled_pipe = intel_crtc->pipe;
13029 }
13030
13031 if (enabled_pipe != INVALID_PIPE)
13032 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13033 else if (other_crtc_state)
13034 other_crtc_state->hsw_workaround_pipe = first_pipe;
13035
13036 return 0;
13037}
13038
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013039static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13040{
13041 struct drm_crtc *crtc;
13042 struct drm_crtc_state *crtc_state;
13043 int ret = 0;
13044
13045 /* add all active pipes to the state */
13046 for_each_crtc(state->dev, crtc) {
13047 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13048 if (IS_ERR(crtc_state))
13049 return PTR_ERR(crtc_state);
13050
13051 if (!crtc_state->active || needs_modeset(crtc_state))
13052 continue;
13053
13054 crtc_state->mode_changed = true;
13055
13056 ret = drm_atomic_add_affected_connectors(state, crtc);
13057 if (ret)
13058 break;
13059
13060 ret = drm_atomic_add_affected_planes(state, crtc);
13061 if (ret)
13062 break;
13063 }
13064
13065 return ret;
13066}
13067
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013068static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013069{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013070 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13071 struct drm_i915_private *dev_priv = state->dev->dev_private;
13072 struct drm_crtc *crtc;
13073 struct drm_crtc_state *crtc_state;
13074 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013075
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013076 if (!check_digital_port_conflicts(state)) {
13077 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13078 return -EINVAL;
13079 }
13080
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013081 intel_state->modeset = true;
13082 intel_state->active_crtcs = dev_priv->active_crtcs;
13083
13084 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13085 if (crtc_state->active)
13086 intel_state->active_crtcs |= 1 << i;
13087 else
13088 intel_state->active_crtcs &= ~(1 << i);
13089 }
13090
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013091 /*
13092 * See if the config requires any additional preparation, e.g.
13093 * to adjust global state with pipes off. We need to do this
13094 * here so we can get the modeset_pipe updated config for the new
13095 * mode set on this crtc. For other crtcs we need to use the
13096 * adjusted_mode bits in the crtc directly.
13097 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013098 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013099 ret = dev_priv->display.modeset_calc_cdclk(state);
13100
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013101 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013102 ret = intel_modeset_all_pipes(state);
13103
13104 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013105 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013106
13107 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13108 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013109 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013110 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013111
Maarten Lankhorstad421372015-06-15 12:33:42 +020013112 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013113
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013114 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013115 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013116
Maarten Lankhorstad421372015-06-15 12:33:42 +020013117 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013118}
13119
Matt Roperaa363132015-09-24 15:53:18 -070013120/*
13121 * Handle calculation of various watermark data at the end of the atomic check
13122 * phase. The code here should be run after the per-crtc and per-plane 'check'
13123 * handlers to ensure that all derived state has been updated.
13124 */
13125static void calc_watermark_data(struct drm_atomic_state *state)
13126{
13127 struct drm_device *dev = state->dev;
13128 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13129 struct drm_crtc *crtc;
13130 struct drm_crtc_state *cstate;
13131 struct drm_plane *plane;
13132 struct drm_plane_state *pstate;
13133
13134 /*
13135 * Calculate watermark configuration details now that derived
13136 * plane/crtc state is all properly updated.
13137 */
13138 drm_for_each_crtc(crtc, dev) {
13139 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13140 crtc->state;
13141
13142 if (cstate->active)
13143 intel_state->wm_config.num_pipes_active++;
13144 }
13145 drm_for_each_legacy_plane(plane, dev) {
13146 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13147 plane->state;
13148
13149 if (!to_intel_plane_state(pstate)->visible)
13150 continue;
13151
13152 intel_state->wm_config.sprites_enabled = true;
13153 if (pstate->crtc_w != pstate->src_w >> 16 ||
13154 pstate->crtc_h != pstate->src_h >> 16)
13155 intel_state->wm_config.sprites_scaled = true;
13156 }
13157}
13158
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013159/**
13160 * intel_atomic_check - validate state object
13161 * @dev: drm device
13162 * @state: state to validate
13163 */
13164static int intel_atomic_check(struct drm_device *dev,
13165 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013166{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013167 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013168 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013169 struct drm_crtc *crtc;
13170 struct drm_crtc_state *crtc_state;
13171 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013172 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013173
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013174 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013175 if (ret)
13176 return ret;
13177
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013178 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013179 struct intel_crtc_state *pipe_config =
13180 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013181
13182 /* Catch I915_MODE_FLAG_INHERITED */
13183 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13184 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013185
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013186 if (!crtc_state->enable) {
13187 if (needs_modeset(crtc_state))
13188 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013189 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013190 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013191
Daniel Vetter26495482015-07-15 14:15:52 +020013192 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013193 continue;
13194
Daniel Vetter26495482015-07-15 14:15:52 +020013195 /* FIXME: For only active_changed we shouldn't need to do any
13196 * state recomputation at all. */
13197
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013198 ret = drm_atomic_add_affected_connectors(state, crtc);
13199 if (ret)
13200 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013201
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013202 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013203 if (ret)
13204 return ret;
13205
Jani Nikula73831232015-11-19 10:26:30 +020013206 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013207 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013208 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013209 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013210 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013211 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013212 }
13213
13214 if (needs_modeset(crtc_state)) {
13215 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013216
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013217 ret = drm_atomic_add_affected_planes(state, crtc);
13218 if (ret)
13219 return ret;
13220 }
13221
Daniel Vetter26495482015-07-15 14:15:52 +020013222 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13223 needs_modeset(crtc_state) ?
13224 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013225 }
13226
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013227 if (any_ms) {
13228 ret = intel_modeset_checks(state);
13229
13230 if (ret)
13231 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013232 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013233 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013234
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013235 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013236 if (ret)
13237 return ret;
13238
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013239 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013240 calc_watermark_data(state);
13241
13242 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013243}
13244
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013245static int intel_atomic_prepare_commit(struct drm_device *dev,
13246 struct drm_atomic_state *state,
13247 bool async)
13248{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013249 struct drm_i915_private *dev_priv = dev->dev_private;
13250 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013251 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013252 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013253 struct drm_crtc *crtc;
13254 int i, ret;
13255
13256 if (async) {
13257 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13258 return -EINVAL;
13259 }
13260
13261 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13262 ret = intel_crtc_wait_for_pending_flips(crtc);
13263 if (ret)
13264 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013265
13266 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13267 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013268 }
13269
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013270 ret = mutex_lock_interruptible(&dev->struct_mutex);
13271 if (ret)
13272 return ret;
13273
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013274 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013275 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13276 u32 reset_counter;
13277
13278 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13279 mutex_unlock(&dev->struct_mutex);
13280
13281 for_each_plane_in_state(state, plane, plane_state, i) {
13282 struct intel_plane_state *intel_plane_state =
13283 to_intel_plane_state(plane_state);
13284
13285 if (!intel_plane_state->wait_req)
13286 continue;
13287
13288 ret = __i915_wait_request(intel_plane_state->wait_req,
13289 reset_counter, true,
13290 NULL, NULL);
13291
13292 /* Swallow -EIO errors to allow updates during hw lockup. */
13293 if (ret == -EIO)
13294 ret = 0;
13295
13296 if (ret)
13297 break;
13298 }
13299
13300 if (!ret)
13301 return 0;
13302
13303 mutex_lock(&dev->struct_mutex);
13304 drm_atomic_helper_cleanup_planes(dev, state);
13305 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013306
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013307 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013308 return ret;
13309}
13310
Maarten Lankhorste8861672016-02-24 11:24:26 +010013311static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13312 struct drm_i915_private *dev_priv,
13313 unsigned crtc_mask)
13314{
13315 unsigned last_vblank_count[I915_MAX_PIPES];
13316 enum pipe pipe;
13317 int ret;
13318
13319 if (!crtc_mask)
13320 return;
13321
13322 for_each_pipe(dev_priv, pipe) {
13323 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13324
13325 if (!((1 << pipe) & crtc_mask))
13326 continue;
13327
13328 ret = drm_crtc_vblank_get(crtc);
13329 if (WARN_ON(ret != 0)) {
13330 crtc_mask &= ~(1 << pipe);
13331 continue;
13332 }
13333
13334 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13335 }
13336
13337 for_each_pipe(dev_priv, pipe) {
13338 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13339 long lret;
13340
13341 if (!((1 << pipe) & crtc_mask))
13342 continue;
13343
13344 lret = wait_event_timeout(dev->vblank[pipe].queue,
13345 last_vblank_count[pipe] !=
13346 drm_crtc_vblank_count(crtc),
13347 msecs_to_jiffies(50));
13348
13349 WARN_ON(!lret);
13350
13351 drm_crtc_vblank_put(crtc);
13352 }
13353}
13354
13355static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13356{
13357 /* fb updated, need to unpin old fb */
13358 if (crtc_state->fb_changed)
13359 return true;
13360
13361 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013362 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013363 return true;
13364
13365 /*
13366 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013367 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013368 * but added for clarity.
13369 */
13370 if (crtc_state->disable_cxsr)
13371 return true;
13372
13373 return false;
13374}
13375
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013376/**
13377 * intel_atomic_commit - commit validated state object
13378 * @dev: DRM device
13379 * @state: the top-level driver state object
13380 * @async: asynchronous commit
13381 *
13382 * This function commits a top-level state object that has been validated
13383 * with drm_atomic_helper_check().
13384 *
13385 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13386 * we can only handle plane-related operations and do not yet support
13387 * asynchronous commit.
13388 *
13389 * RETURNS
13390 * Zero for success or -errno.
13391 */
13392static int intel_atomic_commit(struct drm_device *dev,
13393 struct drm_atomic_state *state,
13394 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013395{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013396 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013397 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013398 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013399 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013400 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013401 int ret = 0, i;
13402 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013403 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013404 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013405
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013406 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013407 if (ret) {
13408 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013409 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013410 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013411
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013412 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013413 dev_priv->wm.config = intel_state->wm_config;
13414 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013415
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013416 if (intel_state->modeset) {
13417 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13418 sizeof(intel_state->min_pixclk));
13419 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013420 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013421
13422 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013423 }
13424
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013425 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13427
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013428 if (needs_modeset(crtc->state) ||
13429 to_intel_crtc_state(crtc->state)->update_pipe) {
13430 hw_check = true;
13431
13432 put_domains[to_intel_crtc(crtc)->pipe] =
13433 modeset_get_crtc_power_domains(crtc,
13434 to_intel_crtc_state(crtc->state));
13435 }
13436
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013437 if (!needs_modeset(crtc->state))
13438 continue;
13439
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013440 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013441
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013442 if (old_crtc_state->active) {
13443 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013444 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013445 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013446 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013447 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013448
13449 /*
13450 * Underruns don't always raise
13451 * interrupts, so check manually.
13452 */
13453 intel_check_cpu_fifo_underruns(dev_priv);
13454 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013455
13456 if (!crtc->state->active)
13457 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013458 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013459 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013460
Daniel Vetterea9d7582012-07-10 10:42:52 +020013461 /* Only after disabling all output pipelines that will be changed can we
13462 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013463 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013464
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013465 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013466 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013467
13468 if (dev_priv->display.modeset_commit_cdclk &&
13469 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13470 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013471 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013472
Daniel Vettera6778b32012-07-02 09:56:42 +020013473 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013474 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13476 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013477 struct intel_crtc_state *pipe_config =
13478 to_intel_crtc_state(crtc->state);
13479 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013480
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013481 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013482 update_scanline_offset(to_intel_crtc(crtc));
13483 dev_priv->display.crtc_enable(crtc);
13484 }
13485
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013486 if (!modeset &&
13487 crtc->state->active &&
13488 crtc->state->color_mgmt_changed) {
13489 /*
13490 * Only update color management when not doing
13491 * a modeset as this will be done by
13492 * crtc_enable already.
13493 */
13494 intel_color_set_csc(crtc);
13495 intel_color_load_luts(crtc);
13496 }
13497
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013498 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013499 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013500
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013501 if (crtc->state->active &&
13502 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013503 intel_fbc_enable(intel_crtc);
13504
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013505 if (crtc->state->active &&
13506 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013507 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013508
Maarten Lankhorste8861672016-02-24 11:24:26 +010013509 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13510 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013511 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013512
Daniel Vettera6778b32012-07-02 09:56:42 +020013513 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013514
Maarten Lankhorste8861672016-02-24 11:24:26 +010013515 if (!state->legacy_cursor_update)
13516 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013517
Matt Ropered4a6a72016-02-23 17:20:13 -080013518 /*
13519 * Now that the vblank has passed, we can go ahead and program the
13520 * optimal watermarks on platforms that need two-step watermark
13521 * programming.
13522 *
13523 * TODO: Move this (and other cleanup) to an async worker eventually.
13524 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013525 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013526 intel_cstate = to_intel_crtc_state(crtc->state);
13527
13528 if (dev_priv->display.optimize_watermarks)
13529 dev_priv->display.optimize_watermarks(intel_cstate);
13530 }
13531
Matt Roper177246a2016-03-04 15:59:39 -080013532 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13533 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13534
13535 if (put_domains[i])
13536 modeset_put_power_domains(dev_priv, put_domains[i]);
13537 }
13538
13539 if (intel_state->modeset)
13540 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13541
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013542 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013543 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013544 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013545
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013546 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013547 intel_modeset_check_state(dev, state);
13548
13549 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013550
Mika Kuoppala75714942015-12-16 09:26:48 +020013551 /* As one of the primary mmio accessors, KMS has a high likelihood
13552 * of triggering bugs in unclaimed access. After we finish
13553 * modesetting, see if an error has been flagged, and if so
13554 * enable debugging for the next modeset - and hope we catch
13555 * the culprit.
13556 *
13557 * XXX note that we assume display power is on at this point.
13558 * This might hold true now but we need to add pm helper to check
13559 * unclaimed only when the hardware is on, as atomic commits
13560 * can happen also when the device is completely off.
13561 */
13562 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13563
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013564 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013565}
13566
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013567void intel_crtc_restore_mode(struct drm_crtc *crtc)
13568{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013569 struct drm_device *dev = crtc->dev;
13570 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013571 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013572 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013573
13574 state = drm_atomic_state_alloc(dev);
13575 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013576 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013577 crtc->base.id);
13578 return;
13579 }
13580
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013581 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013582
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013583retry:
13584 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13585 ret = PTR_ERR_OR_ZERO(crtc_state);
13586 if (!ret) {
13587 if (!crtc_state->active)
13588 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013589
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013590 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013591 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013592 }
13593
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013594 if (ret == -EDEADLK) {
13595 drm_atomic_state_clear(state);
13596 drm_modeset_backoff(state->acquire_ctx);
13597 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013598 }
13599
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013600 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013601out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013602 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013603}
13604
Daniel Vetter25c5b262012-07-08 22:08:04 +020013605#undef for_each_intel_crtc_masked
13606
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013607static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013608 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013609 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013610 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013611 .destroy = intel_crtc_destroy,
13612 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013613 .atomic_duplicate_state = intel_crtc_duplicate_state,
13614 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013615};
13616
Matt Roper6beb8c232014-12-01 15:40:14 -080013617/**
13618 * intel_prepare_plane_fb - Prepare fb for usage on plane
13619 * @plane: drm plane to prepare for
13620 * @fb: framebuffer to prepare for presentation
13621 *
13622 * Prepares a framebuffer for usage on a display plane. Generally this
13623 * involves pinning the underlying object and updating the frontbuffer tracking
13624 * bits. Some older platforms need special physical address handling for
13625 * cursor planes.
13626 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013627 * Must be called with struct_mutex held.
13628 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013629 * Returns 0 on success, negative error code on failure.
13630 */
13631int
13632intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013633 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013634{
13635 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013636 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013637 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013638 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013639 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013640 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013641
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013642 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013643 return 0;
13644
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013645 if (old_obj) {
13646 struct drm_crtc_state *crtc_state =
13647 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13648
13649 /* Big Hammer, we also need to ensure that any pending
13650 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13651 * current scanout is retired before unpinning the old
13652 * framebuffer. Note that we rely on userspace rendering
13653 * into the buffer attached to the pipe they are waiting
13654 * on. If not, userspace generates a GPU hang with IPEHR
13655 * point to the MI_WAIT_FOR_EVENT.
13656 *
13657 * This should only fail upon a hung GPU, in which case we
13658 * can safely continue.
13659 */
13660 if (needs_modeset(crtc_state))
13661 ret = i915_gem_object_wait_rendering(old_obj, true);
13662
13663 /* Swallow -EIO errors to allow updates during hw lockup. */
13664 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013665 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013666 }
13667
Alex Goins3c28ff22015-11-25 18:43:39 -080013668 /* For framebuffer backed by dmabuf, wait for fence */
13669 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013670 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013671
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013672 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13673 false, true,
13674 MAX_SCHEDULE_TIMEOUT);
13675 if (lret == -ERESTARTSYS)
13676 return lret;
13677
13678 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013679 }
13680
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013681 if (!obj) {
13682 ret = 0;
13683 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013684 INTEL_INFO(dev)->cursor_needs_physical) {
13685 int align = IS_I830(dev) ? 16 * 1024 : 256;
13686 ret = i915_gem_object_attach_phys(obj, align);
13687 if (ret)
13688 DRM_DEBUG_KMS("failed to attach phys object\n");
13689 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013690 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013691 }
13692
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013693 if (ret == 0) {
13694 if (obj) {
13695 struct intel_plane_state *plane_state =
13696 to_intel_plane_state(new_state);
13697
13698 i915_gem_request_assign(&plane_state->wait_req,
13699 obj->last_write_req);
13700 }
13701
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013702 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013703 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013704
Matt Roper6beb8c232014-12-01 15:40:14 -080013705 return ret;
13706}
13707
Matt Roper38f3ce32014-12-02 07:45:25 -080013708/**
13709 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13710 * @plane: drm plane to clean up for
13711 * @fb: old framebuffer that was on plane
13712 *
13713 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013714 *
13715 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013716 */
13717void
13718intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013719 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013720{
13721 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013722 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013723 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013724 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13725 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013726
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013727 old_intel_state = to_intel_plane_state(old_state);
13728
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013729 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013730 return;
13731
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013732 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13733 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013734 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013735
13736 /* prepare_fb aborted? */
13737 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13738 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13739 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013740
13741 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013742}
13743
Chandra Konduru6156a452015-04-27 13:48:39 -070013744int
13745skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13746{
13747 int max_scale;
13748 struct drm_device *dev;
13749 struct drm_i915_private *dev_priv;
13750 int crtc_clock, cdclk;
13751
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013752 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013753 return DRM_PLANE_HELPER_NO_SCALING;
13754
13755 dev = intel_crtc->base.dev;
13756 dev_priv = dev->dev_private;
13757 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013758 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013759
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013760 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013761 return DRM_PLANE_HELPER_NO_SCALING;
13762
13763 /*
13764 * skl max scale is lower of:
13765 * close to 3 but not 3, -1 is for that purpose
13766 * or
13767 * cdclk/crtc_clock
13768 */
13769 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13770
13771 return max_scale;
13772}
13773
Matt Roper465c1202014-05-29 08:06:54 -070013774static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013775intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013776 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013777 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013778{
Matt Roper2b875c22014-12-01 15:40:13 -080013779 struct drm_crtc *crtc = state->base.crtc;
13780 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013781 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013782 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13783 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013784
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013785 if (INTEL_INFO(plane->dev)->gen >= 9) {
13786 /* use scaler when colorkey is not required */
13787 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13788 min_scale = 1;
13789 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13790 }
Sonika Jindald8106362015-04-10 14:37:28 +053013791 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013792 }
Sonika Jindald8106362015-04-10 14:37:28 +053013793
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013794 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13795 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013796 min_scale, max_scale,
13797 can_position, true,
13798 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013799}
13800
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013801static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13802 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013803{
13804 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013806 struct intel_crtc_state *old_intel_state =
13807 to_intel_crtc_state(old_crtc_state);
13808 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013809
Matt Roperc34c9ee2014-12-23 10:41:50 -080013810 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013811 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013812
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013813 if (modeset)
13814 return;
13815
13816 if (to_intel_crtc_state(crtc->state)->update_pipe)
13817 intel_update_pipe_config(intel_crtc, old_intel_state);
13818 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013819 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013820}
13821
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013822static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13823 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013824{
Matt Roper32b7eee2014-12-24 07:59:06 -080013825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013826
Maarten Lankhorst62852622015-09-23 16:29:38 +020013827 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013828}
13829
Matt Ropercf4c7c12014-12-04 10:27:42 -080013830/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013831 * intel_plane_destroy - destroy a plane
13832 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013833 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013834 * Common destruction function for all types of planes (primary, cursor,
13835 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013836 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013837void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013838{
13839 struct intel_plane *intel_plane = to_intel_plane(plane);
13840 drm_plane_cleanup(plane);
13841 kfree(intel_plane);
13842}
13843
Matt Roper65a3fea2015-01-21 16:35:42 -080013844const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013845 .update_plane = drm_atomic_helper_update_plane,
13846 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013847 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013848 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013849 .atomic_get_property = intel_plane_atomic_get_property,
13850 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013851 .atomic_duplicate_state = intel_plane_duplicate_state,
13852 .atomic_destroy_state = intel_plane_destroy_state,
13853
Matt Roper465c1202014-05-29 08:06:54 -070013854};
13855
13856static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13857 int pipe)
13858{
13859 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013860 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013861 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013862 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013863
13864 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13865 if (primary == NULL)
13866 return NULL;
13867
Matt Roper8e7d6882015-01-21 16:35:41 -080013868 state = intel_create_plane_state(&primary->base);
13869 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013870 kfree(primary);
13871 return NULL;
13872 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013873 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013874
Matt Roper465c1202014-05-29 08:06:54 -070013875 primary->can_scale = false;
13876 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013877 if (INTEL_INFO(dev)->gen >= 9) {
13878 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013879 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013880 }
Matt Roper465c1202014-05-29 08:06:54 -070013881 primary->pipe = pipe;
13882 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013883 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013884 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013885 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13886 primary->plane = !pipe;
13887
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013888 if (INTEL_INFO(dev)->gen >= 9) {
13889 intel_primary_formats = skl_primary_formats;
13890 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013891
13892 primary->update_plane = skylake_update_primary_plane;
13893 primary->disable_plane = skylake_disable_primary_plane;
13894 } else if (HAS_PCH_SPLIT(dev)) {
13895 intel_primary_formats = i965_primary_formats;
13896 num_formats = ARRAY_SIZE(i965_primary_formats);
13897
13898 primary->update_plane = ironlake_update_primary_plane;
13899 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013900 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013901 intel_primary_formats = i965_primary_formats;
13902 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013903
13904 primary->update_plane = i9xx_update_primary_plane;
13905 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013906 } else {
13907 intel_primary_formats = i8xx_primary_formats;
13908 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013909
13910 primary->update_plane = i9xx_update_primary_plane;
13911 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013912 }
13913
13914 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013915 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013916 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020013917 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053013918
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013919 if (INTEL_INFO(dev)->gen >= 4)
13920 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013921
Matt Roperea2c67b2014-12-23 10:41:52 -080013922 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13923
Matt Roper465c1202014-05-29 08:06:54 -070013924 return &primary->base;
13925}
13926
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013927void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13928{
13929 if (!dev->mode_config.rotation_property) {
13930 unsigned long flags = BIT(DRM_ROTATE_0) |
13931 BIT(DRM_ROTATE_180);
13932
13933 if (INTEL_INFO(dev)->gen >= 9)
13934 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13935
13936 dev->mode_config.rotation_property =
13937 drm_mode_create_rotation_property(dev, flags);
13938 }
13939 if (dev->mode_config.rotation_property)
13940 drm_object_attach_property(&plane->base.base,
13941 dev->mode_config.rotation_property,
13942 plane->base.state->rotation);
13943}
13944
Matt Roper3d7d6512014-06-10 08:28:13 -070013945static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013946intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013947 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013948 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013949{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013950 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013951 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013952 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013953 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013954 unsigned stride;
13955 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013956
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013957 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13958 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013959 DRM_PLANE_HELPER_NO_SCALING,
13960 DRM_PLANE_HELPER_NO_SCALING,
13961 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013962 if (ret)
13963 return ret;
13964
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013965 /* if we want to turn off the cursor ignore width and height */
13966 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013967 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013968
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013969 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013970 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013971 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13972 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013973 return -EINVAL;
13974 }
13975
Matt Roperea2c67b2014-12-23 10:41:52 -080013976 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13977 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013978 DRM_DEBUG_KMS("buffer is too small\n");
13979 return -ENOMEM;
13980 }
13981
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013982 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013983 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013984 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013985 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013986
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013987 /*
13988 * There's something wrong with the cursor on CHV pipe C.
13989 * If it straddles the left edge of the screen then
13990 * moving it away from the edge or disabling it often
13991 * results in a pipe underrun, and often that can lead to
13992 * dead pipe (constant underrun reported, and it scans
13993 * out just a solid color). To recover from that, the
13994 * display power well must be turned off and on again.
13995 * Refuse the put the cursor into that compromised position.
13996 */
13997 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
13998 state->visible && state->base.crtc_x < 0) {
13999 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14000 return -EINVAL;
14001 }
14002
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014003 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014004}
14005
Matt Roperf4a2cf22014-12-01 15:40:12 -080014006static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014007intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014008 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014009{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14011
14012 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014013 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014014}
14015
14016static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014017intel_update_cursor_plane(struct drm_plane *plane,
14018 const struct intel_crtc_state *crtc_state,
14019 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014020{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014021 struct drm_crtc *crtc = crtc_state->base.crtc;
14022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014023 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014024 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014025 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014026
Matt Roperf4a2cf22014-12-01 15:40:12 -080014027 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014028 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014029 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014030 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014031 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014032 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014033
Gustavo Padovana912f122014-12-01 15:40:10 -080014034 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014035 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014036}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014037
Matt Roper3d7d6512014-06-10 08:28:13 -070014038static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14039 int pipe)
14040{
14041 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014042 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014043
14044 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14045 if (cursor == NULL)
14046 return NULL;
14047
Matt Roper8e7d6882015-01-21 16:35:41 -080014048 state = intel_create_plane_state(&cursor->base);
14049 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014050 kfree(cursor);
14051 return NULL;
14052 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014053 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014054
Matt Roper3d7d6512014-06-10 08:28:13 -070014055 cursor->can_scale = false;
14056 cursor->max_downscale = 1;
14057 cursor->pipe = pipe;
14058 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014059 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014060 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014061 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014062 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014063
14064 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014065 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014066 intel_cursor_formats,
14067 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014068 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014069
14070 if (INTEL_INFO(dev)->gen >= 4) {
14071 if (!dev->mode_config.rotation_property)
14072 dev->mode_config.rotation_property =
14073 drm_mode_create_rotation_property(dev,
14074 BIT(DRM_ROTATE_0) |
14075 BIT(DRM_ROTATE_180));
14076 if (dev->mode_config.rotation_property)
14077 drm_object_attach_property(&cursor->base.base,
14078 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014079 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014080 }
14081
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014082 if (INTEL_INFO(dev)->gen >=9)
14083 state->scaler_id = -1;
14084
Matt Roperea2c67b2014-12-23 10:41:52 -080014085 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14086
Matt Roper3d7d6512014-06-10 08:28:13 -070014087 return &cursor->base;
14088}
14089
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014090static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14091 struct intel_crtc_state *crtc_state)
14092{
14093 int i;
14094 struct intel_scaler *intel_scaler;
14095 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14096
14097 for (i = 0; i < intel_crtc->num_scalers; i++) {
14098 intel_scaler = &scaler_state->scalers[i];
14099 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014100 intel_scaler->mode = PS_SCALER_MODE_DYN;
14101 }
14102
14103 scaler_state->scaler_id = -1;
14104}
14105
Hannes Ederb358d0a2008-12-18 21:18:47 +010014106static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014107{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014108 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014109 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014110 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014111 struct drm_plane *primary = NULL;
14112 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014113 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014114
Daniel Vetter955382f2013-09-19 14:05:45 +020014115 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014116 if (intel_crtc == NULL)
14117 return;
14118
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014119 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14120 if (!crtc_state)
14121 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014122 intel_crtc->config = crtc_state;
14123 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014124 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014125
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014126 /* initialize shared scalers */
14127 if (INTEL_INFO(dev)->gen >= 9) {
14128 if (pipe == PIPE_C)
14129 intel_crtc->num_scalers = 1;
14130 else
14131 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14132
14133 skl_init_scalers(dev, intel_crtc, crtc_state);
14134 }
14135
Matt Roper465c1202014-05-29 08:06:54 -070014136 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014137 if (!primary)
14138 goto fail;
14139
14140 cursor = intel_cursor_plane_create(dev, pipe);
14141 if (!cursor)
14142 goto fail;
14143
Matt Roper465c1202014-05-29 08:06:54 -070014144 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014145 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014146 if (ret)
14147 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014148
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014149 /*
14150 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014151 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014152 */
Jesse Barnes80824002009-09-10 15:28:06 -070014153 intel_crtc->pipe = pipe;
14154 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014155 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014156 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014157 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014158 }
14159
Chris Wilson4b0e3332014-05-30 16:35:26 +030014160 intel_crtc->cursor_base = ~0;
14161 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014162 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014163
Ville Syrjälä852eb002015-06-24 22:00:07 +030014164 intel_crtc->wm.cxsr_allowed = true;
14165
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014166 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14167 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14168 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14169 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14170
Jesse Barnes79e53942008-11-07 14:24:08 -080014171 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014172
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014173 intel_color_init(&intel_crtc->base);
14174
Daniel Vetter87b6b102014-05-15 15:33:46 +020014175 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014176 return;
14177
14178fail:
14179 if (primary)
14180 drm_plane_cleanup(primary);
14181 if (cursor)
14182 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014183 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014184 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014185}
14186
Jesse Barnes752aa882013-10-31 18:55:49 +020014187enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14188{
14189 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014190 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014191
Rob Clark51fd3712013-11-19 12:10:12 -050014192 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014193
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014194 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014195 return INVALID_PIPE;
14196
14197 return to_intel_crtc(encoder->crtc)->pipe;
14198}
14199
Carl Worth08d7b3d2009-04-29 14:43:54 -070014200int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014201 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014202{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014203 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014204 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014205 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014206
Rob Clark7707e652014-07-17 23:30:04 -040014207 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014208
Rob Clark7707e652014-07-17 23:30:04 -040014209 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014210 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014211 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014212 }
14213
Rob Clark7707e652014-07-17 23:30:04 -040014214 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014215 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014216
Daniel Vetterc05422d2009-08-11 16:05:30 +020014217 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014218}
14219
Daniel Vetter66a92782012-07-12 20:08:18 +020014220static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014221{
Daniel Vetter66a92782012-07-12 20:08:18 +020014222 struct drm_device *dev = encoder->base.dev;
14223 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014224 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014225 int entry = 0;
14226
Damien Lespiaub2784e12014-08-05 11:29:37 +010014227 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014228 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014229 index_mask |= (1 << entry);
14230
Jesse Barnes79e53942008-11-07 14:24:08 -080014231 entry++;
14232 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014233
Jesse Barnes79e53942008-11-07 14:24:08 -080014234 return index_mask;
14235}
14236
Chris Wilson4d302442010-12-14 19:21:29 +000014237static bool has_edp_a(struct drm_device *dev)
14238{
14239 struct drm_i915_private *dev_priv = dev->dev_private;
14240
14241 if (!IS_MOBILE(dev))
14242 return false;
14243
14244 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14245 return false;
14246
Damien Lespiaue3589902014-02-07 19:12:50 +000014247 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014248 return false;
14249
14250 return true;
14251}
14252
Jesse Barnes84b4e042014-06-25 08:24:29 -070014253static bool intel_crt_present(struct drm_device *dev)
14254{
14255 struct drm_i915_private *dev_priv = dev->dev_private;
14256
Damien Lespiau884497e2013-12-03 13:56:23 +000014257 if (INTEL_INFO(dev)->gen >= 9)
14258 return false;
14259
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014260 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014261 return false;
14262
14263 if (IS_CHERRYVIEW(dev))
14264 return false;
14265
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014266 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14267 return false;
14268
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014269 /* DDI E can't be used if DDI A requires 4 lanes */
14270 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14271 return false;
14272
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014273 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014274 return false;
14275
14276 return true;
14277}
14278
Jesse Barnes79e53942008-11-07 14:24:08 -080014279static void intel_setup_outputs(struct drm_device *dev)
14280{
Eric Anholt725e30a2009-01-22 13:01:02 -080014281 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014282 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014283 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014284
Daniel Vetterc9093352013-06-06 22:22:47 +020014285 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014286
Jesse Barnes84b4e042014-06-25 08:24:29 -070014287 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014288 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014289
Vandana Kannanc776eb22014-08-19 12:05:01 +053014290 if (IS_BROXTON(dev)) {
14291 /*
14292 * FIXME: Broxton doesn't support port detection via the
14293 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14294 * detect the ports.
14295 */
14296 intel_ddi_init(dev, PORT_A);
14297 intel_ddi_init(dev, PORT_B);
14298 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014299
14300 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014301 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014302 int found;
14303
Jesse Barnesde31fac2015-03-06 15:53:32 -080014304 /*
14305 * Haswell uses DDI functions to detect digital outputs.
14306 * On SKL pre-D0 the strap isn't connected, so we assume
14307 * it's there.
14308 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014309 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014310 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014311 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014312 intel_ddi_init(dev, PORT_A);
14313
14314 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14315 * register */
14316 found = I915_READ(SFUSE_STRAP);
14317
14318 if (found & SFUSE_STRAP_DDIB_DETECTED)
14319 intel_ddi_init(dev, PORT_B);
14320 if (found & SFUSE_STRAP_DDIC_DETECTED)
14321 intel_ddi_init(dev, PORT_C);
14322 if (found & SFUSE_STRAP_DDID_DETECTED)
14323 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014324 /*
14325 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14326 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014327 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014328 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14329 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14330 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14331 intel_ddi_init(dev, PORT_E);
14332
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014333 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014334 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014335 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014336
14337 if (has_edp_a(dev))
14338 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014339
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014340 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014341 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014342 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014343 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014344 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014345 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014346 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014347 }
14348
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014349 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014350 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014351
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014352 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014353 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014354
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014355 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014356 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014357
Daniel Vetter270b3042012-10-27 15:52:05 +020014358 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014359 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014360 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014361 /*
14362 * The DP_DETECTED bit is the latched state of the DDC
14363 * SDA pin at boot. However since eDP doesn't require DDC
14364 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14365 * eDP ports may have been muxed to an alternate function.
14366 * Thus we can't rely on the DP_DETECTED bit alone to detect
14367 * eDP ports. Consult the VBT as well as DP_DETECTED to
14368 * detect eDP ports.
14369 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014370 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014371 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014372 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14373 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014374 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014375 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014376
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014377 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014378 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014379 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14380 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014381 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014382 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014383
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014384 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014385 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014386 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14387 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14388 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14389 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014390 }
14391
Jani Nikula3cfca972013-08-27 15:12:26 +030014392 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014393 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014394 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014395
Paulo Zanonie2debe92013-02-18 19:00:27 -030014396 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014397 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014398 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014399 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014400 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014401 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014402 }
Ma Ling27185ae2009-08-24 13:50:23 +080014403
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014404 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014405 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014406 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014407
14408 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014409
Paulo Zanonie2debe92013-02-18 19:00:27 -030014410 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014411 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014412 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014413 }
Ma Ling27185ae2009-08-24 13:50:23 +080014414
Paulo Zanonie2debe92013-02-18 19:00:27 -030014415 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014416
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014417 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014418 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014419 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014420 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014421 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014422 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014423 }
Ma Ling27185ae2009-08-24 13:50:23 +080014424
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014425 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014426 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014427 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014428 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014429 intel_dvo_init(dev);
14430
Zhenyu Wang103a1962009-11-27 11:44:36 +080014431 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014432 intel_tv_init(dev);
14433
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014434 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014435
Damien Lespiaub2784e12014-08-05 11:29:37 +010014436 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014437 encoder->base.possible_crtcs = encoder->crtc_mask;
14438 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014439 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014440 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014441
Paulo Zanonidde86e22012-12-01 12:04:25 -020014442 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014443
14444 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014445}
14446
14447static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14448{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014449 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014450 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014451
Daniel Vetteref2d6332014-02-10 18:00:38 +010014452 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014453 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014454 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014455 drm_gem_object_unreference(&intel_fb->obj->base);
14456 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014457 kfree(intel_fb);
14458}
14459
14460static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014461 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014462 unsigned int *handle)
14463{
14464 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014465 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014466
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014467 if (obj->userptr.mm) {
14468 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14469 return -EINVAL;
14470 }
14471
Chris Wilson05394f32010-11-08 19:18:58 +000014472 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014473}
14474
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014475static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14476 struct drm_file *file,
14477 unsigned flags, unsigned color,
14478 struct drm_clip_rect *clips,
14479 unsigned num_clips)
14480{
14481 struct drm_device *dev = fb->dev;
14482 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14483 struct drm_i915_gem_object *obj = intel_fb->obj;
14484
14485 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014486 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014487 mutex_unlock(&dev->struct_mutex);
14488
14489 return 0;
14490}
14491
Jesse Barnes79e53942008-11-07 14:24:08 -080014492static const struct drm_framebuffer_funcs intel_fb_funcs = {
14493 .destroy = intel_user_framebuffer_destroy,
14494 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014495 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014496};
14497
Damien Lespiaub3218032015-02-27 11:15:18 +000014498static
14499u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14500 uint32_t pixel_format)
14501{
14502 u32 gen = INTEL_INFO(dev)->gen;
14503
14504 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014505 int cpp = drm_format_plane_cpp(pixel_format, 0);
14506
Damien Lespiaub3218032015-02-27 11:15:18 +000014507 /* "The stride in bytes must not exceed the of the size of 8K
14508 * pixels and 32K bytes."
14509 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014510 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014511 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014512 return 32*1024;
14513 } else if (gen >= 4) {
14514 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14515 return 16*1024;
14516 else
14517 return 32*1024;
14518 } else if (gen >= 3) {
14519 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14520 return 8*1024;
14521 else
14522 return 16*1024;
14523 } else {
14524 /* XXX DSPC is limited to 4k tiled */
14525 return 8*1024;
14526 }
14527}
14528
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014529static int intel_framebuffer_init(struct drm_device *dev,
14530 struct intel_framebuffer *intel_fb,
14531 struct drm_mode_fb_cmd2 *mode_cmd,
14532 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014533{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014534 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014535 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014536 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014537 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014538
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014539 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14540
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014541 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14542 /* Enforce that fb modifier and tiling mode match, but only for
14543 * X-tiled. This is needed for FBC. */
14544 if (!!(obj->tiling_mode == I915_TILING_X) !=
14545 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14546 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14547 return -EINVAL;
14548 }
14549 } else {
14550 if (obj->tiling_mode == I915_TILING_X)
14551 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14552 else if (obj->tiling_mode == I915_TILING_Y) {
14553 DRM_DEBUG("No Y tiling for legacy addfb\n");
14554 return -EINVAL;
14555 }
14556 }
14557
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014558 /* Passed in modifier sanity checking. */
14559 switch (mode_cmd->modifier[0]) {
14560 case I915_FORMAT_MOD_Y_TILED:
14561 case I915_FORMAT_MOD_Yf_TILED:
14562 if (INTEL_INFO(dev)->gen < 9) {
14563 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14564 mode_cmd->modifier[0]);
14565 return -EINVAL;
14566 }
14567 case DRM_FORMAT_MOD_NONE:
14568 case I915_FORMAT_MOD_X_TILED:
14569 break;
14570 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014571 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14572 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014573 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014574 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014575
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014576 stride_alignment = intel_fb_stride_alignment(dev_priv,
14577 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014578 mode_cmd->pixel_format);
14579 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14580 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14581 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014582 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014583 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014584
Damien Lespiaub3218032015-02-27 11:15:18 +000014585 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14586 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014587 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014588 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14589 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014590 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014591 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014592 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014593 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014594
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014595 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014596 mode_cmd->pitches[0] != obj->stride) {
14597 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14598 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014599 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014600 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014601
Ville Syrjälä57779d02012-10-31 17:50:14 +020014602 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014603 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014604 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014605 case DRM_FORMAT_RGB565:
14606 case DRM_FORMAT_XRGB8888:
14607 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014608 break;
14609 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014610 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014611 DRM_DEBUG("unsupported pixel format: %s\n",
14612 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014613 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014614 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014615 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014616 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014617 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14618 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014619 DRM_DEBUG("unsupported pixel format: %s\n",
14620 drm_get_format_name(mode_cmd->pixel_format));
14621 return -EINVAL;
14622 }
14623 break;
14624 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014625 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014626 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014627 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014628 DRM_DEBUG("unsupported pixel format: %s\n",
14629 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014630 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014631 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014632 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014633 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014634 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014635 DRM_DEBUG("unsupported pixel format: %s\n",
14636 drm_get_format_name(mode_cmd->pixel_format));
14637 return -EINVAL;
14638 }
14639 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014640 case DRM_FORMAT_YUYV:
14641 case DRM_FORMAT_UYVY:
14642 case DRM_FORMAT_YVYU:
14643 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014644 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014645 DRM_DEBUG("unsupported pixel format: %s\n",
14646 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014647 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014648 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014649 break;
14650 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014651 DRM_DEBUG("unsupported pixel format: %s\n",
14652 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014653 return -EINVAL;
14654 }
14655
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014656 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14657 if (mode_cmd->offsets[0] != 0)
14658 return -EINVAL;
14659
Damien Lespiauec2c9812015-01-20 12:51:45 +000014660 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014661 mode_cmd->pixel_format,
14662 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014663 /* FIXME drm helper for size checks (especially planar formats)? */
14664 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14665 return -EINVAL;
14666
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014667 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14668 intel_fb->obj = obj;
14669
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014670 intel_fill_fb_info(dev_priv, &intel_fb->base);
14671
Jesse Barnes79e53942008-11-07 14:24:08 -080014672 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14673 if (ret) {
14674 DRM_ERROR("framebuffer init failed %d\n", ret);
14675 return ret;
14676 }
14677
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014678 intel_fb->obj->framebuffer_references++;
14679
Jesse Barnes79e53942008-11-07 14:24:08 -080014680 return 0;
14681}
14682
Jesse Barnes79e53942008-11-07 14:24:08 -080014683static struct drm_framebuffer *
14684intel_user_framebuffer_create(struct drm_device *dev,
14685 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014686 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014687{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014688 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014689 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014690 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014691
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014692 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014693 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014694 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014695 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014696
Daniel Vetter92907cb2015-11-23 09:04:05 +010014697 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014698 if (IS_ERR(fb))
14699 drm_gem_object_unreference_unlocked(&obj->base);
14700
14701 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014702}
14703
Daniel Vetter06957262015-08-10 13:34:08 +020014704#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014705static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014706{
14707}
14708#endif
14709
Jesse Barnes79e53942008-11-07 14:24:08 -080014710static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014711 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014712 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014713 .atomic_check = intel_atomic_check,
14714 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014715 .atomic_state_alloc = intel_atomic_state_alloc,
14716 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014717};
14718
Imre Deak88212942016-03-16 13:38:53 +020014719/**
14720 * intel_init_display_hooks - initialize the display modesetting hooks
14721 * @dev_priv: device private
14722 */
14723void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014724{
Imre Deak88212942016-03-16 13:38:53 +020014725 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014726 dev_priv->display.find_dpll = g4x_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014727 else if (IS_CHERRYVIEW(dev_priv))
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014728 dev_priv->display.find_dpll = chv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014729 else if (IS_VALLEYVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014730 dev_priv->display.find_dpll = vlv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014731 else if (IS_PINEVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014732 dev_priv->display.find_dpll = pnv_find_best_dpll;
14733 else
14734 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14735
Imre Deak88212942016-03-16 13:38:53 +020014736 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014737 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014738 dev_priv->display.get_initial_plane_config =
14739 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014740 dev_priv->display.crtc_compute_clock =
14741 haswell_crtc_compute_clock;
14742 dev_priv->display.crtc_enable = haswell_crtc_enable;
14743 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014744 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014745 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014746 dev_priv->display.get_initial_plane_config =
14747 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014748 dev_priv->display.crtc_compute_clock =
14749 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014750 dev_priv->display.crtc_enable = haswell_crtc_enable;
14751 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014752 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014753 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014754 dev_priv->display.get_initial_plane_config =
14755 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014756 dev_priv->display.crtc_compute_clock =
14757 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014758 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14759 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014760 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014761 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014762 dev_priv->display.get_initial_plane_config =
14763 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014764 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014765 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14766 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014767 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014768 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014769 dev_priv->display.get_initial_plane_config =
14770 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014771 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014772 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14773 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014774 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014775
Jesse Barnese70236a2009-09-21 10:42:27 -070014776 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014777 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014778 dev_priv->display.get_display_clock_speed =
14779 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014780 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014781 dev_priv->display.get_display_clock_speed =
14782 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014783 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014784 dev_priv->display.get_display_clock_speed =
14785 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014786 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014787 dev_priv->display.get_display_clock_speed =
14788 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014789 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014790 dev_priv->display.get_display_clock_speed =
14791 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014792 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014793 dev_priv->display.get_display_clock_speed =
14794 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014795 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14796 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014797 dev_priv->display.get_display_clock_speed =
14798 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014799 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014800 dev_priv->display.get_display_clock_speed =
14801 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014802 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014803 dev_priv->display.get_display_clock_speed =
14804 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014805 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014806 dev_priv->display.get_display_clock_speed =
14807 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014808 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014809 dev_priv->display.get_display_clock_speed =
14810 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014811 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014812 dev_priv->display.get_display_clock_speed =
14813 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014814 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014815 dev_priv->display.get_display_clock_speed =
14816 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014817 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014818 dev_priv->display.get_display_clock_speed =
14819 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014820 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014821 dev_priv->display.get_display_clock_speed =
14822 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014823 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014824 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014825 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014826 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014827 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014828 dev_priv->display.get_display_clock_speed =
14829 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014830 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014831
Imre Deak88212942016-03-16 13:38:53 +020014832 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014833 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014834 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014835 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014836 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014837 /* FIXME: detect B0+ stepping and use auto training */
14838 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014839 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014840 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014841 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014842 dev_priv->display.modeset_commit_cdclk =
14843 broadwell_modeset_commit_cdclk;
14844 dev_priv->display.modeset_calc_cdclk =
14845 broadwell_modeset_calc_cdclk;
14846 }
Imre Deak88212942016-03-16 13:38:53 +020014847 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014848 dev_priv->display.modeset_commit_cdclk =
14849 valleyview_modeset_commit_cdclk;
14850 dev_priv->display.modeset_calc_cdclk =
14851 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014852 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014853 dev_priv->display.modeset_commit_cdclk =
14854 broxton_modeset_commit_cdclk;
14855 dev_priv->display.modeset_calc_cdclk =
14856 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014857 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014858
Imre Deak88212942016-03-16 13:38:53 +020014859 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014860 case 2:
14861 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14862 break;
14863
14864 case 3:
14865 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14866 break;
14867
14868 case 4:
14869 case 5:
14870 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14871 break;
14872
14873 case 6:
14874 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14875 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014876 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014877 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014878 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14879 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014880 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014881 /* Drop through - unsupported since execlist only. */
14882 default:
14883 /* Default just returns -ENODEV to indicate unsupported */
14884 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014885 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014886}
14887
Jesse Barnesb690e962010-07-19 13:53:12 -070014888/*
14889 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14890 * resume, or other times. This quirk makes sure that's the case for
14891 * affected systems.
14892 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014893static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014894{
14895 struct drm_i915_private *dev_priv = dev->dev_private;
14896
14897 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014898 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014899}
14900
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014901static void quirk_pipeb_force(struct drm_device *dev)
14902{
14903 struct drm_i915_private *dev_priv = dev->dev_private;
14904
14905 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14906 DRM_INFO("applying pipe b force quirk\n");
14907}
14908
Keith Packard435793d2011-07-12 14:56:22 -070014909/*
14910 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14911 */
14912static void quirk_ssc_force_disable(struct drm_device *dev)
14913{
14914 struct drm_i915_private *dev_priv = dev->dev_private;
14915 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014916 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014917}
14918
Carsten Emde4dca20e2012-03-15 15:56:26 +010014919/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014920 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14921 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014922 */
14923static void quirk_invert_brightness(struct drm_device *dev)
14924{
14925 struct drm_i915_private *dev_priv = dev->dev_private;
14926 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014927 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014928}
14929
Scot Doyle9c72cc62014-07-03 23:27:50 +000014930/* Some VBT's incorrectly indicate no backlight is present */
14931static void quirk_backlight_present(struct drm_device *dev)
14932{
14933 struct drm_i915_private *dev_priv = dev->dev_private;
14934 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14935 DRM_INFO("applying backlight present quirk\n");
14936}
14937
Jesse Barnesb690e962010-07-19 13:53:12 -070014938struct intel_quirk {
14939 int device;
14940 int subsystem_vendor;
14941 int subsystem_device;
14942 void (*hook)(struct drm_device *dev);
14943};
14944
Egbert Eich5f85f172012-10-14 15:46:38 +020014945/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14946struct intel_dmi_quirk {
14947 void (*hook)(struct drm_device *dev);
14948 const struct dmi_system_id (*dmi_id_list)[];
14949};
14950
14951static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14952{
14953 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14954 return 1;
14955}
14956
14957static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14958 {
14959 .dmi_id_list = &(const struct dmi_system_id[]) {
14960 {
14961 .callback = intel_dmi_reverse_brightness,
14962 .ident = "NCR Corporation",
14963 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14964 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14965 },
14966 },
14967 { } /* terminating entry */
14968 },
14969 .hook = quirk_invert_brightness,
14970 },
14971};
14972
Ben Widawskyc43b5632012-04-16 14:07:40 -070014973static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014974 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14975 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14976
Jesse Barnesb690e962010-07-19 13:53:12 -070014977 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14978 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14979
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014980 /* 830 needs to leave pipe A & dpll A up */
14981 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14982
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014983 /* 830 needs to leave pipe B & dpll B up */
14984 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14985
Keith Packard435793d2011-07-12 14:56:22 -070014986 /* Lenovo U160 cannot use SSC on LVDS */
14987 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014988
14989 /* Sony Vaio Y cannot use SSC on LVDS */
14990 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014991
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014992 /* Acer Aspire 5734Z must invert backlight brightness */
14993 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14994
14995 /* Acer/eMachines G725 */
14996 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14997
14998 /* Acer/eMachines e725 */
14999 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15000
15001 /* Acer/Packard Bell NCL20 */
15002 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15003
15004 /* Acer Aspire 4736Z */
15005 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015006
15007 /* Acer Aspire 5336 */
15008 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015009
15010 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15011 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015012
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015013 /* Acer C720 Chromebook (Core i3 4005U) */
15014 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15015
jens steinb2a96012014-10-28 20:25:53 +010015016 /* Apple Macbook 2,1 (Core 2 T7400) */
15017 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15018
Jani Nikula1b9448b2015-11-05 11:49:59 +020015019 /* Apple Macbook 4,1 */
15020 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15021
Scot Doyled4967d82014-07-03 23:27:52 +000015022 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15023 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015024
15025 /* HP Chromebook 14 (Celeron 2955U) */
15026 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015027
15028 /* Dell Chromebook 11 */
15029 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015030
15031 /* Dell Chromebook 11 (2015 version) */
15032 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015033};
15034
15035static void intel_init_quirks(struct drm_device *dev)
15036{
15037 struct pci_dev *d = dev->pdev;
15038 int i;
15039
15040 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15041 struct intel_quirk *q = &intel_quirks[i];
15042
15043 if (d->device == q->device &&
15044 (d->subsystem_vendor == q->subsystem_vendor ||
15045 q->subsystem_vendor == PCI_ANY_ID) &&
15046 (d->subsystem_device == q->subsystem_device ||
15047 q->subsystem_device == PCI_ANY_ID))
15048 q->hook(dev);
15049 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015050 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15051 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15052 intel_dmi_quirks[i].hook(dev);
15053 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015054}
15055
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015056/* Disable the VGA plane that we never use */
15057static void i915_disable_vga(struct drm_device *dev)
15058{
15059 struct drm_i915_private *dev_priv = dev->dev_private;
15060 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015061 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015062
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015063 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015064 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015065 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015066 sr1 = inb(VGA_SR_DATA);
15067 outb(sr1 | 1<<5, VGA_SR_DATA);
15068 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15069 udelay(300);
15070
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015071 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015072 POSTING_READ(vga_reg);
15073}
15074
Daniel Vetterf8175862012-04-10 15:50:11 +020015075void intel_modeset_init_hw(struct drm_device *dev)
15076{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015077 struct drm_i915_private *dev_priv = dev->dev_private;
15078
Ville Syrjäläb6283052015-06-03 15:45:07 +030015079 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015080
15081 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15082
Daniel Vetterf8175862012-04-10 15:50:11 +020015083 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015084 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015085}
15086
Matt Roperd93c0372015-12-03 11:37:41 -080015087/*
15088 * Calculate what we think the watermarks should be for the state we've read
15089 * out of the hardware and then immediately program those watermarks so that
15090 * we ensure the hardware settings match our internal state.
15091 *
15092 * We can calculate what we think WM's should be by creating a duplicate of the
15093 * current state (which was constructed during hardware readout) and running it
15094 * through the atomic check code to calculate new watermark values in the
15095 * state object.
15096 */
15097static void sanitize_watermarks(struct drm_device *dev)
15098{
15099 struct drm_i915_private *dev_priv = to_i915(dev);
15100 struct drm_atomic_state *state;
15101 struct drm_crtc *crtc;
15102 struct drm_crtc_state *cstate;
15103 struct drm_modeset_acquire_ctx ctx;
15104 int ret;
15105 int i;
15106
15107 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015108 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015109 return;
15110
15111 /*
15112 * We need to hold connection_mutex before calling duplicate_state so
15113 * that the connector loop is protected.
15114 */
15115 drm_modeset_acquire_init(&ctx, 0);
15116retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015117 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015118 if (ret == -EDEADLK) {
15119 drm_modeset_backoff(&ctx);
15120 goto retry;
15121 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015122 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015123 }
15124
15125 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15126 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015127 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015128
Matt Ropered4a6a72016-02-23 17:20:13 -080015129 /*
15130 * Hardware readout is the only time we don't want to calculate
15131 * intermediate watermarks (since we don't trust the current
15132 * watermarks).
15133 */
15134 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15135
Matt Roperd93c0372015-12-03 11:37:41 -080015136 ret = intel_atomic_check(dev, state);
15137 if (ret) {
15138 /*
15139 * If we fail here, it means that the hardware appears to be
15140 * programmed in a way that shouldn't be possible, given our
15141 * understanding of watermark requirements. This might mean a
15142 * mistake in the hardware readout code or a mistake in the
15143 * watermark calculations for a given platform. Raise a WARN
15144 * so that this is noticeable.
15145 *
15146 * If this actually happens, we'll have to just leave the
15147 * BIOS-programmed watermarks untouched and hope for the best.
15148 */
15149 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015150 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015151 }
15152
15153 /* Write calculated watermark values back */
15154 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15155 for_each_crtc_in_state(state, crtc, cstate, i) {
15156 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15157
Matt Ropered4a6a72016-02-23 17:20:13 -080015158 cs->wm.need_postvbl_update = true;
15159 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015160 }
15161
15162 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015163fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015164 drm_modeset_drop_locks(&ctx);
15165 drm_modeset_acquire_fini(&ctx);
15166}
15167
Jesse Barnes79e53942008-11-07 14:24:08 -080015168void intel_modeset_init(struct drm_device *dev)
15169{
Jesse Barnes652c3932009-08-17 13:31:43 -070015170 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015171 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015172 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015173 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015174
15175 drm_mode_config_init(dev);
15176
15177 dev->mode_config.min_width = 0;
15178 dev->mode_config.min_height = 0;
15179
Dave Airlie019d96c2011-09-29 16:20:42 +010015180 dev->mode_config.preferred_depth = 24;
15181 dev->mode_config.prefer_shadow = 1;
15182
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015183 dev->mode_config.allow_fb_modifiers = true;
15184
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015185 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015186
Jesse Barnesb690e962010-07-19 13:53:12 -070015187 intel_init_quirks(dev);
15188
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015189 intel_init_pm(dev);
15190
Ben Widawskye3c74752013-04-05 13:12:39 -070015191 if (INTEL_INFO(dev)->num_pipes == 0)
15192 return;
15193
Lukas Wunner69f92f62015-07-15 13:57:35 +020015194 /*
15195 * There may be no VBT; and if the BIOS enabled SSC we can
15196 * just keep using it to avoid unnecessary flicker. Whereas if the
15197 * BIOS isn't using it, don't assume it will work even if the VBT
15198 * indicates as much.
15199 */
15200 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15201 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15202 DREF_SSC1_ENABLE);
15203
15204 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15205 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15206 bios_lvds_use_ssc ? "en" : "dis",
15207 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15208 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15209 }
15210 }
15211
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015212 if (IS_GEN2(dev)) {
15213 dev->mode_config.max_width = 2048;
15214 dev->mode_config.max_height = 2048;
15215 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015216 dev->mode_config.max_width = 4096;
15217 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015218 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015219 dev->mode_config.max_width = 8192;
15220 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015221 }
Damien Lespiau068be562014-03-28 14:17:49 +000015222
Ville Syrjälädc41c152014-08-13 11:57:05 +030015223 if (IS_845G(dev) || IS_I865G(dev)) {
15224 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15225 dev->mode_config.cursor_height = 1023;
15226 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015227 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15228 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15229 } else {
15230 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15231 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15232 }
15233
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015234 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015235
Zhao Yakui28c97732009-10-09 11:39:41 +080015236 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015237 INTEL_INFO(dev)->num_pipes,
15238 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015239
Damien Lespiau055e3932014-08-18 13:49:10 +010015240 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015241 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015242 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015243 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015244 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015245 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015246 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015247 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015248 }
15249
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015250 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015251 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015252 intel_update_cdclk(dev);
15253
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015254 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015255
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015256 /* Just disable it once at startup */
15257 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015258 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015259
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015260 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015261 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015262 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015263
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015264 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015265 struct intel_initial_plane_config plane_config = {};
15266
Jesse Barnes46f297f2014-03-07 08:57:48 -080015267 if (!crtc->active)
15268 continue;
15269
Jesse Barnes46f297f2014-03-07 08:57:48 -080015270 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015271 * Note that reserving the BIOS fb up front prevents us
15272 * from stuffing other stolen allocations like the ring
15273 * on top. This prevents some ugliness at boot time, and
15274 * can even allow for smooth boot transitions if the BIOS
15275 * fb is large enough for the active pipe configuration.
15276 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015277 dev_priv->display.get_initial_plane_config(crtc,
15278 &plane_config);
15279
15280 /*
15281 * If the fb is shared between multiple heads, we'll
15282 * just get the first one.
15283 */
15284 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015285 }
Matt Roperd93c0372015-12-03 11:37:41 -080015286
15287 /*
15288 * Make sure hardware watermarks really match the state we read out.
15289 * Note that we need to do this after reconstructing the BIOS fb's
15290 * since the watermark calculation done here will use pstate->fb.
15291 */
15292 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015293}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015294
Daniel Vetter7fad7982012-07-04 17:51:47 +020015295static void intel_enable_pipe_a(struct drm_device *dev)
15296{
15297 struct intel_connector *connector;
15298 struct drm_connector *crt = NULL;
15299 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015300 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015301
15302 /* We can't just switch on the pipe A, we need to set things up with a
15303 * proper mode and output configuration. As a gross hack, enable pipe A
15304 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015305 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015306 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15307 crt = &connector->base;
15308 break;
15309 }
15310 }
15311
15312 if (!crt)
15313 return;
15314
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015315 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015316 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015317}
15318
Daniel Vetterfa555832012-10-10 23:14:00 +020015319static bool
15320intel_check_plane_mapping(struct intel_crtc *crtc)
15321{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015322 struct drm_device *dev = crtc->base.dev;
15323 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015324 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015325
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015326 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015327 return true;
15328
Ville Syrjälä649636e2015-09-22 19:50:01 +030015329 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015330
15331 if ((val & DISPLAY_PLANE_ENABLE) &&
15332 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15333 return false;
15334
15335 return true;
15336}
15337
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015338static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15339{
15340 struct drm_device *dev = crtc->base.dev;
15341 struct intel_encoder *encoder;
15342
15343 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15344 return true;
15345
15346 return false;
15347}
15348
Ville Syrjälädd756192016-02-17 21:28:45 +020015349static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15350{
15351 struct drm_device *dev = encoder->base.dev;
15352 struct intel_connector *connector;
15353
15354 for_each_connector_on_encoder(dev, &encoder->base, connector)
15355 return true;
15356
15357 return false;
15358}
15359
Daniel Vetter24929352012-07-02 20:28:59 +020015360static void intel_sanitize_crtc(struct intel_crtc *crtc)
15361{
15362 struct drm_device *dev = crtc->base.dev;
15363 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015364 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015365
Daniel Vetter24929352012-07-02 20:28:59 +020015366 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015367 if (!transcoder_is_dsi(cpu_transcoder)) {
15368 i915_reg_t reg = PIPECONF(cpu_transcoder);
15369
15370 I915_WRITE(reg,
15371 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15372 }
Daniel Vetter24929352012-07-02 20:28:59 +020015373
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015374 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015375 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015376 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015377 struct intel_plane *plane;
15378
Daniel Vetter96256042015-02-13 21:03:42 +010015379 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015380
15381 /* Disable everything but the primary plane */
15382 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15383 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15384 continue;
15385
15386 plane->disable_plane(&plane->base, &crtc->base);
15387 }
Daniel Vetter96256042015-02-13 21:03:42 +010015388 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015389
Daniel Vetter24929352012-07-02 20:28:59 +020015390 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015391 * disable the crtc (and hence change the state) if it is wrong. Note
15392 * that gen4+ has a fixed plane -> pipe mapping. */
15393 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015394 bool plane;
15395
Daniel Vetter24929352012-07-02 20:28:59 +020015396 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15397 crtc->base.base.id);
15398
15399 /* Pipe has the wrong plane attached and the plane is active.
15400 * Temporarily change the plane mapping and disable everything
15401 * ... */
15402 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015403 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015404 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015405 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015406 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015407 }
Daniel Vetter24929352012-07-02 20:28:59 +020015408
Daniel Vetter7fad7982012-07-04 17:51:47 +020015409 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15410 crtc->pipe == PIPE_A && !crtc->active) {
15411 /* BIOS forgot to enable pipe A, this mostly happens after
15412 * resume. Force-enable the pipe to fix this, the update_dpms
15413 * call below we restore the pipe to the right state, but leave
15414 * the required bits on. */
15415 intel_enable_pipe_a(dev);
15416 }
15417
Daniel Vetter24929352012-07-02 20:28:59 +020015418 /* Adjust the state of the output pipe according to whether we
15419 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015420 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015421 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015422
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015423 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015424 /*
15425 * We start out with underrun reporting disabled to avoid races.
15426 * For correct bookkeeping mark this on active crtcs.
15427 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015428 * Also on gmch platforms we dont have any hardware bits to
15429 * disable the underrun reporting. Which means we need to start
15430 * out with underrun reporting disabled also on inactive pipes,
15431 * since otherwise we'll complain about the garbage we read when
15432 * e.g. coming up after runtime pm.
15433 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015434 * No protection against concurrent access is required - at
15435 * worst a fifo underrun happens which also sets this to false.
15436 */
15437 crtc->cpu_fifo_underrun_disabled = true;
15438 crtc->pch_fifo_underrun_disabled = true;
15439 }
Daniel Vetter24929352012-07-02 20:28:59 +020015440}
15441
15442static void intel_sanitize_encoder(struct intel_encoder *encoder)
15443{
15444 struct intel_connector *connector;
15445 struct drm_device *dev = encoder->base.dev;
15446
15447 /* We need to check both for a crtc link (meaning that the
15448 * encoder is active and trying to read from a pipe) and the
15449 * pipe itself being active. */
15450 bool has_active_crtc = encoder->base.crtc &&
15451 to_intel_crtc(encoder->base.crtc)->active;
15452
Ville Syrjälädd756192016-02-17 21:28:45 +020015453 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015454 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15455 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015456 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015457
15458 /* Connector is active, but has no active pipe. This is
15459 * fallout from our resume register restoring. Disable
15460 * the encoder manually again. */
15461 if (encoder->base.crtc) {
15462 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15463 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015464 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015465 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015466 if (encoder->post_disable)
15467 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015468 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015469 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015470
15471 /* Inconsistent output/port/pipe state happens presumably due to
15472 * a bug in one of the get_hw_state functions. Or someplace else
15473 * in our code, like the register restore mess on resume. Clamp
15474 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015475 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015476 if (connector->encoder != encoder)
15477 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015478 connector->base.dpms = DRM_MODE_DPMS_OFF;
15479 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015480 }
15481 }
15482 /* Enabled encoders without active connectors will be fixed in
15483 * the crtc fixup. */
15484}
15485
Imre Deak04098752014-02-18 00:02:16 +020015486void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015487{
15488 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015489 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015490
Imre Deak04098752014-02-18 00:02:16 +020015491 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15492 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15493 i915_disable_vga(dev);
15494 }
15495}
15496
15497void i915_redisable_vga(struct drm_device *dev)
15498{
15499 struct drm_i915_private *dev_priv = dev->dev_private;
15500
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015501 /* This function can be called both from intel_modeset_setup_hw_state or
15502 * at a very early point in our resume sequence, where the power well
15503 * structures are not yet restored. Since this function is at a very
15504 * paranoid "someone might have enabled VGA while we were not looking"
15505 * level, just check if the power well is enabled instead of trying to
15506 * follow the "don't touch the power well if we don't need it" policy
15507 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015508 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015509 return;
15510
Imre Deak04098752014-02-18 00:02:16 +020015511 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015512
15513 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015514}
15515
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015516static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015517{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015518 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015519
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015520 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015521}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015522
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015523/* FIXME read out full plane state for all planes */
15524static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015525{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015526 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015527 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015528 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015529
Matt Roper19b8d382015-09-24 15:53:17 -070015530 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015531 primary_get_hw_state(to_intel_plane(primary));
15532
15533 if (plane_state->visible)
15534 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015535}
15536
Daniel Vetter30e984d2013-06-05 13:34:17 +020015537static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015538{
15539 struct drm_i915_private *dev_priv = dev->dev_private;
15540 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015541 struct intel_crtc *crtc;
15542 struct intel_encoder *encoder;
15543 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015544 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015545
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015546 dev_priv->active_crtcs = 0;
15547
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015548 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015549 struct intel_crtc_state *crtc_state = crtc->config;
15550 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015551
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015552 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15553 memset(crtc_state, 0, sizeof(*crtc_state));
15554 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015555
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015556 crtc_state->base.active = crtc_state->base.enable =
15557 dev_priv->display.get_pipe_config(crtc, crtc_state);
15558
15559 crtc->base.enabled = crtc_state->base.enable;
15560 crtc->active = crtc_state->base.active;
15561
15562 if (crtc_state->base.active) {
15563 dev_priv->active_crtcs |= 1 << crtc->pipe;
15564
15565 if (IS_BROADWELL(dev_priv)) {
15566 pixclk = ilk_pipe_pixel_rate(crtc_state);
15567
15568 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15569 if (crtc_state->ips_enabled)
15570 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15571 } else if (IS_VALLEYVIEW(dev_priv) ||
15572 IS_CHERRYVIEW(dev_priv) ||
15573 IS_BROXTON(dev_priv))
15574 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15575 else
15576 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15577 }
15578
15579 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015580
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015581 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015582
15583 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15584 crtc->base.base.id,
15585 crtc->active ? "enabled" : "disabled");
15586 }
15587
Daniel Vetter53589012013-06-05 13:34:16 +020015588 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15589 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15590
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015591 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15592 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015593 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015594 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015595 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015596 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015597 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015598 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015599
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015600 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015601 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015602 }
15603
Damien Lespiaub2784e12014-08-05 11:29:37 +010015604 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015605 pipe = 0;
15606
15607 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015608 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15609 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015610 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015611 } else {
15612 encoder->base.crtc = NULL;
15613 }
15614
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015615 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015616 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015617 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015618 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015619 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015620 }
15621
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015622 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015623 if (connector->get_hw_state(connector)) {
15624 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015625
15626 encoder = connector->encoder;
15627 connector->base.encoder = &encoder->base;
15628
15629 if (encoder->base.crtc &&
15630 encoder->base.crtc->state->active) {
15631 /*
15632 * This has to be done during hardware readout
15633 * because anything calling .crtc_disable may
15634 * rely on the connector_mask being accurate.
15635 */
15636 encoder->base.crtc->state->connector_mask |=
15637 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015638 encoder->base.crtc->state->encoder_mask |=
15639 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015640 }
15641
Daniel Vetter24929352012-07-02 20:28:59 +020015642 } else {
15643 connector->base.dpms = DRM_MODE_DPMS_OFF;
15644 connector->base.encoder = NULL;
15645 }
15646 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15647 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015648 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015649 connector->base.encoder ? "enabled" : "disabled");
15650 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015651
15652 for_each_intel_crtc(dev, crtc) {
15653 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15654
15655 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15656 if (crtc->base.state->active) {
15657 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15658 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15659 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15660
15661 /*
15662 * The initial mode needs to be set in order to keep
15663 * the atomic core happy. It wants a valid mode if the
15664 * crtc's enabled, so we do the above call.
15665 *
15666 * At this point some state updated by the connectors
15667 * in their ->detect() callback has not run yet, so
15668 * no recalculation can be done yet.
15669 *
15670 * Even if we could do a recalculation and modeset
15671 * right now it would cause a double modeset if
15672 * fbdev or userspace chooses a different initial mode.
15673 *
15674 * If that happens, someone indicated they wanted a
15675 * mode change, which means it's safe to do a full
15676 * recalculation.
15677 */
15678 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015679
15680 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15681 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015682 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015683
15684 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015685 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015686}
15687
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015688/* Scan out the current hw modeset state,
15689 * and sanitizes it to the current state
15690 */
15691static void
15692intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015693{
15694 struct drm_i915_private *dev_priv = dev->dev_private;
15695 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015696 struct intel_crtc *crtc;
15697 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015698 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015699
15700 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015701
15702 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015703 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015704 intel_sanitize_encoder(encoder);
15705 }
15706
Damien Lespiau055e3932014-08-18 13:49:10 +010015707 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015708 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15709 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015710 intel_dump_pipe_config(crtc, crtc->config,
15711 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015712 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015713
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015714 intel_modeset_update_connector_atomic_state(dev);
15715
Daniel Vetter35c95372013-07-17 06:55:04 +020015716 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15717 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15718
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015719 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015720 continue;
15721
15722 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15723
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015724 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015725 pll->on = false;
15726 }
15727
Wayne Boyer666a4532015-12-09 12:29:35 -080015728 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015729 vlv_wm_get_hw_state(dev);
15730 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015731 skl_wm_get_hw_state(dev);
15732 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015733 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015734
15735 for_each_intel_crtc(dev, crtc) {
15736 unsigned long put_domains;
15737
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015738 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015739 if (WARN_ON(put_domains))
15740 modeset_put_power_domains(dev_priv, put_domains);
15741 }
15742 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015743
15744 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015745}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015746
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015747void intel_display_resume(struct drm_device *dev)
15748{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015749 struct drm_i915_private *dev_priv = to_i915(dev);
15750 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15751 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015752 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015753 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015754
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015755 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015756
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015757 /*
15758 * This is a cludge because with real atomic modeset mode_config.mutex
15759 * won't be taken. Unfortunately some probed state like
15760 * audio_codec_enable is still protected by mode_config.mutex, so lock
15761 * it here for now.
15762 */
15763 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015764 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015765
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015766retry:
15767 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015768
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015769 if (ret == 0 && !setup) {
15770 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015771
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015772 intel_modeset_setup_hw_state(dev);
15773 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015774 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015775
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015776 if (ret == 0 && state) {
15777 struct drm_crtc_state *crtc_state;
15778 struct drm_crtc *crtc;
15779 int i;
15780
15781 state->acquire_ctx = &ctx;
15782
15783 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15784 /*
15785 * Force recalculation even if we restore
15786 * current state. With fast modeset this may not result
15787 * in a modeset when the state is compatible.
15788 */
15789 crtc_state->mode_changed = true;
15790 }
15791
15792 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015793 }
15794
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015795 if (ret == -EDEADLK) {
15796 drm_modeset_backoff(&ctx);
15797 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015798 }
15799
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015800 drm_modeset_drop_locks(&ctx);
15801 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015802 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015803
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015804 if (ret) {
15805 DRM_ERROR("Restoring old state failed with %i\n", ret);
15806 drm_atomic_state_free(state);
15807 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015808}
15809
15810void intel_modeset_gem_init(struct drm_device *dev)
15811{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015812 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015813 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015814 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015815
Imre Deakae484342014-03-31 15:10:44 +030015816 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015817
Chris Wilson1833b132012-05-09 11:56:28 +010015818 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015819
15820 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015821
15822 /*
15823 * Make sure any fbs we allocated at startup are properly
15824 * pinned & fenced. When we do the allocation it's too early
15825 * for this.
15826 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015827 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015828 obj = intel_fb_obj(c->primary->fb);
15829 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015830 continue;
15831
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015832 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015833 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15834 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015835 mutex_unlock(&dev->struct_mutex);
15836 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015837 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15838 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015839 drm_framebuffer_unreference(c->primary->fb);
15840 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015841 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015842 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015843 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015844 }
15845 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015846
15847 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015848}
15849
Imre Deak4932e2c2014-02-11 17:12:48 +020015850void intel_connector_unregister(struct intel_connector *intel_connector)
15851{
15852 struct drm_connector *connector = &intel_connector->base;
15853
15854 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015855 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015856}
15857
Jesse Barnes79e53942008-11-07 14:24:08 -080015858void intel_modeset_cleanup(struct drm_device *dev)
15859{
Jesse Barnes652c3932009-08-17 13:31:43 -070015860 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015861 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015862
Imre Deak2eb52522014-11-19 15:30:05 +020015863 intel_disable_gt_powersave(dev);
15864
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015865 intel_backlight_unregister(dev);
15866
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015867 /*
15868 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015869 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015870 * experience fancy races otherwise.
15871 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015872 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015873
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015874 /*
15875 * Due to the hpd irq storm handling the hotplug work can re-arm the
15876 * poll handlers. Hence disable polling after hpd handling is shut down.
15877 */
Keith Packardf87ea762010-10-03 19:36:26 -070015878 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015879
Jesse Barnes723bfd72010-10-07 16:01:13 -070015880 intel_unregister_dsm_handler();
15881
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015882 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015883
Chris Wilson1630fe72011-07-08 12:22:42 +010015884 /* flush any delayed tasks or pending work */
15885 flush_scheduled_work();
15886
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015887 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020015888 for_each_intel_connector(dev, connector)
15889 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030015890
Jesse Barnes79e53942008-11-07 14:24:08 -080015891 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015892
15893 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015894
Imre Deakae484342014-03-31 15:10:44 +030015895 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010015896
15897 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015898}
15899
Dave Airlie28d52042009-09-21 14:33:58 +100015900/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015901 * Return which encoder is currently attached for connector.
15902 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015903struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015904{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015905 return &intel_attached_encoder(connector)->base;
15906}
Jesse Barnes79e53942008-11-07 14:24:08 -080015907
Chris Wilsondf0e9242010-09-09 16:20:55 +010015908void intel_connector_attach_encoder(struct intel_connector *connector,
15909 struct intel_encoder *encoder)
15910{
15911 connector->encoder = encoder;
15912 drm_mode_connector_attach_encoder(&connector->base,
15913 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015914}
Dave Airlie28d52042009-09-21 14:33:58 +100015915
15916/*
15917 * set vga decode state - true == enable VGA decode
15918 */
15919int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15920{
15921 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015922 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015923 u16 gmch_ctrl;
15924
Chris Wilson75fa0412014-02-07 18:37:02 -020015925 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15926 DRM_ERROR("failed to read control word\n");
15927 return -EIO;
15928 }
15929
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015930 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15931 return 0;
15932
Dave Airlie28d52042009-09-21 14:33:58 +100015933 if (state)
15934 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15935 else
15936 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015937
15938 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15939 DRM_ERROR("failed to write control word\n");
15940 return -EIO;
15941 }
15942
Dave Airlie28d52042009-09-21 14:33:58 +100015943 return 0;
15944}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015945
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015946struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015947
15948 u32 power_well_driver;
15949
Chris Wilson63b66e52013-08-08 15:12:06 +020015950 int num_transcoders;
15951
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015952 struct intel_cursor_error_state {
15953 u32 control;
15954 u32 position;
15955 u32 base;
15956 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015957 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015958
15959 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015960 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015961 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015962 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015963 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015964
15965 struct intel_plane_error_state {
15966 u32 control;
15967 u32 stride;
15968 u32 size;
15969 u32 pos;
15970 u32 addr;
15971 u32 surface;
15972 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015973 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015974
15975 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015976 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015977 enum transcoder cpu_transcoder;
15978
15979 u32 conf;
15980
15981 u32 htotal;
15982 u32 hblank;
15983 u32 hsync;
15984 u32 vtotal;
15985 u32 vblank;
15986 u32 vsync;
15987 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015988};
15989
15990struct intel_display_error_state *
15991intel_display_capture_error_state(struct drm_device *dev)
15992{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015993 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015994 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015995 int transcoders[] = {
15996 TRANSCODER_A,
15997 TRANSCODER_B,
15998 TRANSCODER_C,
15999 TRANSCODER_EDP,
16000 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016001 int i;
16002
Chris Wilson63b66e52013-08-08 15:12:06 +020016003 if (INTEL_INFO(dev)->num_pipes == 0)
16004 return NULL;
16005
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016006 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016007 if (error == NULL)
16008 return NULL;
16009
Imre Deak190be112013-11-25 17:15:31 +020016010 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016011 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16012
Damien Lespiau055e3932014-08-18 13:49:10 +010016013 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016014 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016015 __intel_display_power_is_enabled(dev_priv,
16016 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016017 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016018 continue;
16019
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016020 error->cursor[i].control = I915_READ(CURCNTR(i));
16021 error->cursor[i].position = I915_READ(CURPOS(i));
16022 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016023
16024 error->plane[i].control = I915_READ(DSPCNTR(i));
16025 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016026 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016027 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016028 error->plane[i].pos = I915_READ(DSPPOS(i));
16029 }
Paulo Zanonica291362013-03-06 20:03:14 -030016030 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16031 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016032 if (INTEL_INFO(dev)->gen >= 4) {
16033 error->plane[i].surface = I915_READ(DSPSURF(i));
16034 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16035 }
16036
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016037 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016038
Sonika Jindal3abfce72014-07-21 15:23:43 +053016039 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016040 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016041 }
16042
Jani Nikula4d1de972016-03-18 17:05:42 +020016043 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016044 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16045 if (HAS_DDI(dev_priv->dev))
16046 error->num_transcoders++; /* Account for eDP. */
16047
16048 for (i = 0; i < error->num_transcoders; i++) {
16049 enum transcoder cpu_transcoder = transcoders[i];
16050
Imre Deakddf9c532013-11-27 22:02:02 +020016051 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016052 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016053 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016054 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016055 continue;
16056
Chris Wilson63b66e52013-08-08 15:12:06 +020016057 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16058
16059 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16060 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16061 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16062 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16063 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16064 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16065 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016066 }
16067
16068 return error;
16069}
16070
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016071#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16072
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016073void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016074intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016075 struct drm_device *dev,
16076 struct intel_display_error_state *error)
16077{
Damien Lespiau055e3932014-08-18 13:49:10 +010016078 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016079 int i;
16080
Chris Wilson63b66e52013-08-08 15:12:06 +020016081 if (!error)
16082 return;
16083
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016084 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016085 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016086 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016087 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016088 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016089 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016090 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016091 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016092 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016093 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016094
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016095 err_printf(m, "Plane [%d]:\n", i);
16096 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16097 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016098 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016099 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16100 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016101 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016102 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016103 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016104 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016105 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16106 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016107 }
16108
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016109 err_printf(m, "Cursor [%d]:\n", i);
16110 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16111 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16112 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016113 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016114
16115 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016116 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016117 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016118 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016119 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016120 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16121 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16122 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16123 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16124 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16125 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16126 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16127 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016128}