blob: 0a5e8553b120f2d77a1de4d23ee1c507e3043b45 [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
Ma Lingd4906092009-03-18 20:13:27 +0800849static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200850g4x_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800854{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300855 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800856 intel_clock_t clock;
857 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300858 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400859 /* approximately equals target * 0.00585 */
860 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800861
862 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300863
864 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
865
Ma Lingd4906092009-03-18 20:13:27 +0800866 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200867 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200869 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800870 for (clock.m1 = limit->m1.max;
871 clock.m1 >= limit->m1.min; clock.m1--) {
872 for (clock.m2 = limit->m2.max;
873 clock.m2 >= limit->m2.min; clock.m2--) {
874 for (clock.p1 = limit->p1.max;
875 clock.p1 >= limit->p1.min; clock.p1--) {
876 int this_err;
877
Imre Deakdccbea32015-06-22 23:35:51 +0300878 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000879 if (!intel_PLL_is_valid(dev, limit,
880 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800881 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000882
883 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800884 if (this_err < err_most) {
885 *best_clock = clock;
886 err_most = this_err;
887 max_n = clock.n;
888 found = true;
889 }
890 }
891 }
892 }
893 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800894 return found;
895}
Ma Lingd4906092009-03-18 20:13:27 +0800896
Imre Deakd5dd62b2015-03-17 11:40:03 +0200897/*
898 * Check if the calculated PLL configuration is more optimal compared to the
899 * best configuration and error found so far. Return the calculated error.
900 */
901static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
902 const intel_clock_t *calculated_clock,
903 const intel_clock_t *best_clock,
904 unsigned int best_error_ppm,
905 unsigned int *error_ppm)
906{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200907 /*
908 * For CHV ignore the error and consider only the P value.
909 * Prefer a bigger P value based on HW requirements.
910 */
911 if (IS_CHERRYVIEW(dev)) {
912 *error_ppm = 0;
913
914 return calculated_clock->p > best_clock->p;
915 }
916
Imre Deak24be4e42015-03-17 11:40:04 +0200917 if (WARN_ON_ONCE(!target_freq))
918 return false;
919
Imre Deakd5dd62b2015-03-17 11:40:03 +0200920 *error_ppm = div_u64(1000000ULL *
921 abs(target_freq - calculated_clock->dot),
922 target_freq);
923 /*
924 * Prefer a better P value over a better (smaller) error if the error
925 * is small. Ensure this preference for future configurations too by
926 * setting the error to 0.
927 */
928 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
929 *error_ppm = 0;
930
931 return true;
932 }
933
934 return *error_ppm + 10 < best_error_ppm;
935}
936
Zhenyu Wang2c072452009-06-05 15:38:42 +0800937static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200938vlv_find_best_dpll(const intel_limit_t *limit,
939 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200940 int target, int refclk, intel_clock_t *match_clock,
941 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700942{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300944 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300945 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300946 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300947 /* min update 19.2 MHz */
948 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300949 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700950
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300951 target *= 5; /* fast clock */
952
953 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700954
955 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300956 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300957 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300958 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300959 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300960 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700961 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300962 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200963 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300964
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300965 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
966 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300967
Imre Deakdccbea32015-06-22 23:35:51 +0300968 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300969
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300970 if (!intel_PLL_is_valid(dev, limit,
971 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300972 continue;
973
Imre Deakd5dd62b2015-03-17 11:40:03 +0200974 if (!vlv_PLL_is_optimal(dev, target,
975 &clock,
976 best_clock,
977 bestppm, &ppm))
978 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300979
Imre Deakd5dd62b2015-03-17 11:40:03 +0200980 *best_clock = clock;
981 bestppm = ppm;
982 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700983 }
984 }
985 }
986 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700987
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300988 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700989}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700990
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300991static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200992chv_find_best_dpll(const intel_limit_t *limit,
993 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300994 int target, int refclk, intel_clock_t *match_clock,
995 intel_clock_t *best_clock)
996{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200997 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300998 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200999 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001000 intel_clock_t clock;
1001 uint64_t m2;
1002 int found = false;
1003
1004 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001005 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001006
1007 /*
1008 * Based on hardware doc, the n always set to 1, and m1 always
1009 * set to 2. If requires to support 200Mhz refclk, we need to
1010 * revisit this because n may not 1 anymore.
1011 */
1012 clock.n = 1, clock.m1 = 2;
1013 target *= 5; /* fast clock */
1014
1015 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1016 for (clock.p2 = limit->p2.p2_fast;
1017 clock.p2 >= limit->p2.p2_slow;
1018 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001019 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001020
1021 clock.p = clock.p1 * clock.p2;
1022
1023 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1024 clock.n) << 22, refclk * clock.m1);
1025
1026 if (m2 > INT_MAX/clock.m1)
1027 continue;
1028
1029 clock.m2 = m2;
1030
Imre Deakdccbea32015-06-22 23:35:51 +03001031 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001032
1033 if (!intel_PLL_is_valid(dev, limit, &clock))
1034 continue;
1035
Imre Deak9ca3ba02015-03-17 11:40:05 +02001036 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1037 best_error_ppm, &error_ppm))
1038 continue;
1039
1040 *best_clock = clock;
1041 best_error_ppm = error_ppm;
1042 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001043 }
1044 }
1045
1046 return found;
1047}
1048
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001049bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1050 intel_clock_t *best_clock)
1051{
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02001052 int refclk = i9xx_get_refclk(crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001053
1054 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1055 target_clock, refclk, NULL, best_clock);
1056}
1057
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001058bool intel_crtc_active(struct drm_crtc *crtc)
1059{
1060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1061
1062 /* Be paranoid as we can arrive here with only partial
1063 * state retrieved from the hardware during setup.
1064 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001065 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001066 * as Haswell has gained clock readout/fastboot support.
1067 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001068 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001069 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001070 *
1071 * FIXME: The intel_crtc->active here should be switched to
1072 * crtc->state->active once we have proper CRTC states wired up
1073 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001074 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001075 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001076 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001077}
1078
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001079enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1080 enum pipe pipe)
1081{
1082 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1084
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001085 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001086}
1087
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001088static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1089{
1090 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001091 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001092 u32 line1, line2;
1093 u32 line_mask;
1094
1095 if (IS_GEN2(dev))
1096 line_mask = DSL_LINEMASK_GEN2;
1097 else
1098 line_mask = DSL_LINEMASK_GEN3;
1099
1100 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001101 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001102 line2 = I915_READ(reg) & line_mask;
1103
1104 return line1 == line2;
1105}
1106
Keith Packardab7ad7f2010-10-03 00:33:06 -07001107/*
1108 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001109 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001110 *
1111 * After disabling a pipe, we can't wait for vblank in the usual way,
1112 * spinning on the vblank interrupt status bit, since we won't actually
1113 * see an interrupt when the pipe is disabled.
1114 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001115 * On Gen4 and above:
1116 * wait for the pipe register state bit to turn off
1117 *
1118 * Otherwise:
1119 * wait for the display line value to settle (it usually
1120 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001121 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001122 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001123static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001124{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001125 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001126 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001127 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001128 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001129
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001131 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001132
Keith Packardab7ad7f2010-10-03 00:33:06 -07001133 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001134 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1135 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001136 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001137 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001138 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001139 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001140 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001141 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001142}
1143
Jesse Barnesb24e7172011-01-04 15:09:30 -08001144/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001145void assert_pll(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001147{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148 u32 val;
1149 bool cur_state;
1150
Ville Syrjälä649636e2015-09-22 19:50:01 +03001151 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001152 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001153 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001154 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001155 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001156}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001157
Jani Nikula23538ef2013-08-27 15:12:22 +03001158/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001159void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001160{
1161 u32 val;
1162 bool cur_state;
1163
Ville Syrjäläa5805162015-05-26 20:42:30 +03001164 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001165 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001166 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001167
1168 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001169 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001170 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001171 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001172}
Jani Nikula23538ef2013-08-27 15:12:22 +03001173
Jesse Barnes040484a2011-01-03 12:14:26 -08001174static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1175 enum pipe pipe, bool state)
1176{
Jesse Barnes040484a2011-01-03 12:14:26 -08001177 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001178 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1179 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001180
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001181 if (HAS_DDI(dev_priv->dev)) {
1182 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001183 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001184 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001185 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001186 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001187 cur_state = !!(val & FDI_TX_ENABLE);
1188 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001189 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001190 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001191 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001192}
1193#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1194#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1195
1196static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, bool state)
1198{
Jesse Barnes040484a2011-01-03 12:14:26 -08001199 u32 val;
1200 bool cur_state;
1201
Ville Syrjälä649636e2015-09-22 19:50:01 +03001202 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001203 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001204 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001205 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001206 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001207}
1208#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1209#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1210
1211static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1212 enum pipe pipe)
1213{
Jesse Barnes040484a2011-01-03 12:14:26 -08001214 u32 val;
1215
1216 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001217 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001218 return;
1219
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001220 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001221 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001222 return;
1223
Ville Syrjälä649636e2015-09-22 19:50:01 +03001224 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001225 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 -08001226}
1227
Daniel Vetter55607e82013-06-16 21:42:39 +02001228void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1229 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001230{
Jesse Barnes040484a2011-01-03 12:14:26 -08001231 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001232 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001233
Ville Syrjälä649636e2015-09-22 19:50:01 +03001234 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001235 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001236 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001237 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001238 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001239}
1240
Daniel Vetterb680c372014-09-19 18:27:27 +02001241void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1242 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001243{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001244 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001245 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001246 u32 val;
1247 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001248 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249
Jani Nikulabedd4db2014-08-22 15:04:13 +03001250 if (WARN_ON(HAS_DDI(dev)))
1251 return;
1252
1253 if (HAS_PCH_SPLIT(dev)) {
1254 u32 port_sel;
1255
Jesse Barnesea0760c2011-01-04 15:09:32 -08001256 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1258
1259 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1260 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1261 panel_pipe = PIPE_B;
1262 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001263 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001264 /* presumably write lock depends on pipe, not port select */
1265 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1266 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267 } else {
1268 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001269 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1270 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271 }
1272
1273 val = I915_READ(pp_reg);
1274 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001275 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 locked = false;
1277
Rob Clarke2c719b2014-12-15 13:56:32 -05001278 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001279 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001280 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281}
1282
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001283static void assert_cursor(struct drm_i915_private *dev_priv,
1284 enum pipe pipe, bool state)
1285{
1286 struct drm_device *dev = dev_priv->dev;
1287 bool cur_state;
1288
Paulo Zanonid9d82082014-02-27 16:30:56 -03001289 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001290 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001291 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001292 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001293
Rob Clarke2c719b2014-12-15 13:56:32 -05001294 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001295 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001296 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001297}
1298#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1299#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1300
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001301void assert_pipe(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001303{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001304 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001305 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1306 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001307 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001309 /* if we need the pipe quirk it must be always on */
1310 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1311 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001312 state = true;
1313
Imre Deak4feed0e2016-02-12 18:55:14 +02001314 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1315 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001316 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001317 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001318
1319 intel_display_power_put(dev_priv, power_domain);
1320 } else {
1321 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001322 }
1323
Rob Clarke2c719b2014-12-15 13:56:32 -05001324 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001325 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001326 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327}
1328
Chris Wilson931872f2012-01-16 23:01:13 +00001329static void assert_plane(struct drm_i915_private *dev_priv,
1330 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001331{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001333 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334
Ville Syrjälä649636e2015-09-22 19:50:01 +03001335 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001336 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001337 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001338 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001339 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340}
1341
Chris Wilson931872f2012-01-16 23:01:13 +00001342#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1343#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1344
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1346 enum pipe pipe)
1347{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001348 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001349 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350
Ville Syrjälä653e1022013-06-04 13:49:05 +03001351 /* Primary planes are fixed to pipes on gen4+ */
1352 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001353 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001355 "plane %c assertion failure, should be disabled but not\n",
1356 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001357 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001358 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001359
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001361 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001362 u32 val = I915_READ(DSPCNTR(i));
1363 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001364 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001365 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001366 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1367 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368 }
1369}
1370
Jesse Barnes19332d72013-03-28 09:55:38 -07001371static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe)
1373{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001374 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001375 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001376
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001377 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001378 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001381 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1382 sprite, pipe_name(pipe));
1383 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001384 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001385 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001386 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001389 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001390 }
1391 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001392 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001394 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001395 plane_name(pipe), pipe_name(pipe));
1396 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001397 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001398 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001399 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1400 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001401 }
1402}
1403
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001404static void assert_vblank_disabled(struct drm_crtc *crtc)
1405{
Rob Clarke2c719b2014-12-15 13:56:32 -05001406 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001407 drm_crtc_vblank_put(crtc);
1408}
1409
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001410void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001412{
Jesse Barnes92f25842011-01-04 15:09:34 -08001413 u32 val;
1414 bool enabled;
1415
Ville Syrjälä649636e2015-09-22 19:50:01 +03001416 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001417 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001418 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001419 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1420 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001421}
1422
Keith Packard4e634382011-08-06 10:39:45 -07001423static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001425{
1426 if ((val & DP_PORT_EN) == 0)
1427 return false;
1428
1429 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001430 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001431 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1432 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001433 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1434 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1435 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001436 } else {
1437 if ((val & DP_PIPE_MASK) != (pipe << 30))
1438 return false;
1439 }
1440 return true;
1441}
1442
Keith Packard1519b992011-08-06 10:35:34 -07001443static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1444 enum pipe pipe, u32 val)
1445{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001446 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001447 return false;
1448
1449 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001450 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001451 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001452 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1453 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1454 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001455 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001456 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001457 return false;
1458 }
1459 return true;
1460}
1461
1462static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1463 enum pipe pipe, u32 val)
1464{
1465 if ((val & LVDS_PORT_EN) == 0)
1466 return false;
1467
1468 if (HAS_PCH_CPT(dev_priv->dev)) {
1469 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1470 return false;
1471 } else {
1472 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1473 return false;
1474 }
1475 return true;
1476}
1477
1478static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1479 enum pipe pipe, u32 val)
1480{
1481 if ((val & ADPA_DAC_ENABLE) == 0)
1482 return false;
1483 if (HAS_PCH_CPT(dev_priv->dev)) {
1484 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1485 return false;
1486 } else {
1487 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1488 return false;
1489 }
1490 return true;
1491}
1492
Jesse Barnes291906f2011-02-02 12:28:03 -08001493static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001494 enum pipe pipe, i915_reg_t reg,
1495 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001496{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001497 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001498 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001499 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001500 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001501
Rob Clarke2c719b2014-12-15 13:56:32 -05001502 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001503 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001504 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001505}
1506
1507static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001508 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001509{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001510 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001511 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001512 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001513 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001514
Rob Clarke2c719b2014-12-15 13:56:32 -05001515 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001516 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001517 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001518}
1519
1520static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1521 enum pipe pipe)
1522{
Jesse Barnes291906f2011-02-02 12:28:03 -08001523 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001524
Keith Packardf0575e92011-07-25 22:12:43 -07001525 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1526 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1527 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001528
Ville Syrjälä649636e2015-09-22 19:50:01 +03001529 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001530 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001531 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001532 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001533
Ville Syrjälä649636e2015-09-22 19:50:01 +03001534 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001535 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001536 "PCH LVDS 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
Paulo Zanonie2debe92013-02-18 19:00:27 -03001539 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1540 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1541 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001542}
1543
Ville Syrjäläd288f652014-10-28 13:20:22 +02001544static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001545 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001546{
Daniel Vetter426115c2013-07-11 22:13:42 +02001547 struct drm_device *dev = crtc->base.dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001549 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001550 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001551
Daniel Vetter426115c2013-07-11 22:13:42 +02001552 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001553
Daniel Vetter87442f72013-06-06 00:52:17 +02001554 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001555 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001556 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001557
Daniel Vetter426115c2013-07-11 22:13:42 +02001558 I915_WRITE(reg, dpll);
1559 POSTING_READ(reg);
1560 udelay(150);
1561
1562 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1563 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1564
Ville Syrjäläd288f652014-10-28 13:20:22 +02001565 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001566 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001567
1568 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001569 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001570 POSTING_READ(reg);
1571 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001572 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001573 POSTING_READ(reg);
1574 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001575 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001576 POSTING_READ(reg);
1577 udelay(150); /* wait for warmup */
1578}
1579
Ville Syrjäläd288f652014-10-28 13:20:22 +02001580static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001581 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001582{
1583 struct drm_device *dev = crtc->base.dev;
1584 struct drm_i915_private *dev_priv = dev->dev_private;
1585 int pipe = crtc->pipe;
1586 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001587 u32 tmp;
1588
1589 assert_pipe_disabled(dev_priv, crtc->pipe);
1590
Ville Syrjäläa5805162015-05-26 20:42:30 +03001591 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592
1593 /* Enable back the 10bit clock to display controller */
1594 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1595 tmp |= DPIO_DCLKP_EN;
1596 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1597
Ville Syrjälä54433e92015-05-26 20:42:31 +03001598 mutex_unlock(&dev_priv->sb_lock);
1599
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001600 /*
1601 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1602 */
1603 udelay(1);
1604
1605 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001606 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607
1608 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001609 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001610 DRM_ERROR("PLL %d failed to lock\n", pipe);
1611
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001612 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001613 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001614 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001615}
1616
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001617static int intel_num_dvo_pipes(struct drm_device *dev)
1618{
1619 struct intel_crtc *crtc;
1620 int count = 0;
1621
1622 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001623 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001624 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001625
1626 return count;
1627}
1628
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001629static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001630{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631 struct drm_device *dev = crtc->base.dev;
1632 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001633 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001634 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001635
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001637
1638 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001639 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001640
1641 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001642 if (IS_MOBILE(dev) && !IS_I830(dev))
1643 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001644
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001645 /* Enable DVO 2x clock on both PLLs if necessary */
1646 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1647 /*
1648 * It appears to be important that we don't enable this
1649 * for the current pipe before otherwise configuring the
1650 * PLL. No idea how this should be handled if multiple
1651 * DVO outputs are enabled simultaneosly.
1652 */
1653 dpll |= DPLL_DVO_2X_MODE;
1654 I915_WRITE(DPLL(!crtc->pipe),
1655 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1656 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001657
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001658 /*
1659 * Apparently we need to have VGA mode enabled prior to changing
1660 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1661 * dividers, even though the register value does change.
1662 */
1663 I915_WRITE(reg, 0);
1664
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001665 I915_WRITE(reg, dpll);
1666
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001667 /* Wait for the clocks to stabilize. */
1668 POSTING_READ(reg);
1669 udelay(150);
1670
1671 if (INTEL_INFO(dev)->gen >= 4) {
1672 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001673 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674 } else {
1675 /* The pixel multiplier can only be updated once the
1676 * DPLL is enabled and the clocks are stable.
1677 *
1678 * So write it again.
1679 */
1680 I915_WRITE(reg, dpll);
1681 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001682
1683 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001684 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001685 POSTING_READ(reg);
1686 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001687 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001688 POSTING_READ(reg);
1689 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001690 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001691 POSTING_READ(reg);
1692 udelay(150); /* wait for warmup */
1693}
1694
1695/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001696 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001697 * @dev_priv: i915 private structure
1698 * @pipe: pipe PLL to disable
1699 *
1700 * Disable the PLL for @pipe, making sure the pipe is off first.
1701 *
1702 * Note! This is for pre-ILK only.
1703 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001704static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001705{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001706 struct drm_device *dev = crtc->base.dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 enum pipe pipe = crtc->pipe;
1709
1710 /* Disable DVO 2x clock on both PLLs if necessary */
1711 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001712 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001713 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001714 I915_WRITE(DPLL(PIPE_B),
1715 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1716 I915_WRITE(DPLL(PIPE_A),
1717 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1718 }
1719
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001720 /* Don't disable pipe or pipe PLLs if needed */
1721 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1722 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001723 return;
1724
1725 /* Make sure the pipe isn't still relying on us */
1726 assert_pipe_disabled(dev_priv, pipe);
1727
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001728 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001729 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001730}
1731
Jesse Barnesf6071162013-10-01 10:41:38 -07001732static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1733{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001734 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001735
1736 /* Make sure the pipe isn't still relying on us */
1737 assert_pipe_disabled(dev_priv, pipe);
1738
Imre Deake5cbfbf2014-01-09 17:08:16 +02001739 /*
1740 * Leave integrated clock source and reference clock enabled for pipe B.
1741 * The latter is needed for VGA hotplug / manual detection.
1742 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001743 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001744 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001745 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001746 I915_WRITE(DPLL(pipe), val);
1747 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001748
1749}
1750
1751static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1752{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001753 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001754 u32 val;
1755
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001756 /* Make sure the pipe isn't still relying on us */
1757 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001758
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001759 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001760 val = DPLL_SSC_REF_CLK_CHV |
1761 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001762 if (pipe != PIPE_A)
1763 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1764 I915_WRITE(DPLL(pipe), val);
1765 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001766
Ville Syrjäläa5805162015-05-26 20:42:30 +03001767 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001768
1769 /* Disable 10bit clock to display controller */
1770 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1771 val &= ~DPIO_DCLKP_EN;
1772 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1773
Ville Syrjäläa5805162015-05-26 20:42:30 +03001774 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001775}
1776
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001777void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001778 struct intel_digital_port *dport,
1779 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001780{
1781 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001782 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001784 switch (dport->port) {
1785 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001786 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001787 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001788 break;
1789 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001790 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001791 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001792 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001793 break;
1794 case PORT_D:
1795 port_mask = DPLL_PORTD_READY_MASK;
1796 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001797 break;
1798 default:
1799 BUG();
1800 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001801
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001802 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1803 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1804 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001805}
1806
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001807static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1808 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001809{
Daniel Vetter23670b322012-11-01 09:15:30 +01001810 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001811 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001813 i915_reg_t reg;
1814 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001815
1816 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001817 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001818
1819 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001820 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001821
1822 /* FDI must be feeding us bits for PCH ports */
1823 assert_fdi_tx_enabled(dev_priv, pipe);
1824 assert_fdi_rx_enabled(dev_priv, pipe);
1825
Daniel Vetter23670b322012-11-01 09:15:30 +01001826 if (HAS_PCH_CPT(dev)) {
1827 /* Workaround: Set the timing override bit before enabling the
1828 * pch transcoder. */
1829 reg = TRANS_CHICKEN2(pipe);
1830 val = I915_READ(reg);
1831 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1832 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001833 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001834
Daniel Vetterab9412b2013-05-03 11:49:46 +02001835 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001836 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001837 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001838
1839 if (HAS_PCH_IBX(dev_priv->dev)) {
1840 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001841 * Make the BPC in transcoder be consistent with
1842 * that in pipeconf reg. For HDMI we must use 8bpc
1843 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001844 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001845 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001846 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1847 val |= PIPECONF_8BPC;
1848 else
1849 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001850 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001851
1852 val &= ~TRANS_INTERLACE_MASK;
1853 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001854 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001855 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001856 val |= TRANS_LEGACY_INTERLACED_ILK;
1857 else
1858 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001859 else
1860 val |= TRANS_PROGRESSIVE;
1861
Jesse Barnes040484a2011-01-03 12:14:26 -08001862 I915_WRITE(reg, val | TRANS_ENABLE);
1863 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001864 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001865}
1866
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001867static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001868 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001869{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001870 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001871
1872 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001873 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001876 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001877 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001879 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001880 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001881 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001882 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001883
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001884 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001885 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001886
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001887 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1888 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001889 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001890 else
1891 val |= TRANS_PROGRESSIVE;
1892
Daniel Vetterab9412b2013-05-03 11:49:46 +02001893 I915_WRITE(LPT_TRANSCONF, val);
1894 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001895 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001896}
1897
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001898static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1899 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001900{
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001902 i915_reg_t reg;
1903 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001904
1905 /* FDI relies on the transcoder */
1906 assert_fdi_tx_disabled(dev_priv, pipe);
1907 assert_fdi_rx_disabled(dev_priv, pipe);
1908
Jesse Barnes291906f2011-02-02 12:28:03 -08001909 /* Ports must be off as well */
1910 assert_pch_ports_disabled(dev_priv, pipe);
1911
Daniel Vetterab9412b2013-05-03 11:49:46 +02001912 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001913 val = I915_READ(reg);
1914 val &= ~TRANS_ENABLE;
1915 I915_WRITE(reg, val);
1916 /* wait for PCH transcoder off, transcoder state */
1917 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001918 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001919
Ville Syrjäläc4656132015-10-29 21:25:56 +02001920 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001921 /* Workaround: Clear the timing override chicken bit again. */
1922 reg = TRANS_CHICKEN2(pipe);
1923 val = I915_READ(reg);
1924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1925 I915_WRITE(reg, val);
1926 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001927}
1928
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001929static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001930{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001931 u32 val;
1932
Daniel Vetterab9412b2013-05-03 11:49:46 +02001933 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001934 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001935 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001937 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001938 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001939
1940 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001941 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001942 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001943 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001944}
1945
1946/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001947 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001948 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001949 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001950 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001951 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001952 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001953static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954{
Paulo Zanoni03722642014-01-17 13:51:09 -02001955 struct drm_device *dev = crtc->base.dev;
1956 struct drm_i915_private *dev_priv = dev->dev_private;
1957 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001958 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001959 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001960 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961 u32 val;
1962
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001963 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1964
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001965 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001966 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001967 assert_sprites_disabled(dev_priv, pipe);
1968
Paulo Zanoni681e5812012-12-06 11:12:38 -02001969 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001970 pch_transcoder = TRANSCODER_A;
1971 else
1972 pch_transcoder = pipe;
1973
Jesse Barnesb24e7172011-01-04 15:09:30 -08001974 /*
1975 * A pipe without a PLL won't actually be able to drive bits from
1976 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1977 * need the check.
1978 */
Imre Deak50360402015-01-16 00:55:16 -08001979 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001980 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001981 assert_dsi_pll_enabled(dev_priv);
1982 else
1983 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001984 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001985 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001986 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001987 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001988 assert_fdi_tx_pll_enabled(dev_priv,
1989 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001990 }
1991 /* FIXME: assert CPU port conditions for SNB+ */
1992 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001993
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001994 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001995 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001996 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001997 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1998 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001999 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002000 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002001
2002 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002003 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002004
2005 /*
2006 * Until the pipe starts DSL will read as 0, which would cause
2007 * an apparent vblank timestamp jump, which messes up also the
2008 * frame count when it's derived from the timestamps. So let's
2009 * wait for the pipe to start properly before we call
2010 * drm_crtc_vblank_on()
2011 */
2012 if (dev->max_vblank_count == 0 &&
2013 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2014 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015}
2016
2017/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002018 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002019 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002021 * Disable the pipe of @crtc, making sure that various hardware
2022 * specific requirements are met, if applicable, e.g. plane
2023 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024 *
2025 * Will wait until the pipe has shut down before returning.
2026 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002027static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002029 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002030 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002031 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002032 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033 u32 val;
2034
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002035 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2036
Jesse Barnesb24e7172011-01-04 15:09:30 -08002037 /*
2038 * Make sure planes won't keep trying to pump pixels to us,
2039 * or we might hang the display.
2040 */
2041 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002042 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002043 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002044
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002045 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002047 if ((val & PIPECONF_ENABLE) == 0)
2048 return;
2049
Ville Syrjälä67adc642014-08-15 01:21:57 +03002050 /*
2051 * Double wide has implications for planes
2052 * so best keep it disabled when not needed.
2053 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002054 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002055 val &= ~PIPECONF_DOUBLE_WIDE;
2056
2057 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002058 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2059 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002060 val &= ~PIPECONF_ENABLE;
2061
2062 I915_WRITE(reg, val);
2063 if ((val & PIPECONF_ENABLE) == 0)
2064 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002065}
2066
Chris Wilson693db182013-03-05 14:52:39 +00002067static bool need_vtd_wa(struct drm_device *dev)
2068{
2069#ifdef CONFIG_INTEL_IOMMU
2070 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2071 return true;
2072#endif
2073 return false;
2074}
2075
Ville Syrjälä832be822016-01-12 21:08:33 +02002076static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2077{
2078 return IS_GEN2(dev_priv) ? 2048 : 4096;
2079}
2080
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002081static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2082 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002083{
2084 switch (fb_modifier) {
2085 case DRM_FORMAT_MOD_NONE:
2086 return cpp;
2087 case I915_FORMAT_MOD_X_TILED:
2088 if (IS_GEN2(dev_priv))
2089 return 128;
2090 else
2091 return 512;
2092 case I915_FORMAT_MOD_Y_TILED:
2093 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2094 return 128;
2095 else
2096 return 512;
2097 case I915_FORMAT_MOD_Yf_TILED:
2098 switch (cpp) {
2099 case 1:
2100 return 64;
2101 case 2:
2102 case 4:
2103 return 128;
2104 case 8:
2105 case 16:
2106 return 256;
2107 default:
2108 MISSING_CASE(cpp);
2109 return cpp;
2110 }
2111 break;
2112 default:
2113 MISSING_CASE(fb_modifier);
2114 return cpp;
2115 }
2116}
2117
Ville Syrjälä832be822016-01-12 21:08:33 +02002118unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2119 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002120{
Ville Syrjälä832be822016-01-12 21:08:33 +02002121 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2122 return 1;
2123 else
2124 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002125 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002126}
2127
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002128/* Return the tile dimensions in pixel units */
2129static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2130 unsigned int *tile_width,
2131 unsigned int *tile_height,
2132 uint64_t fb_modifier,
2133 unsigned int cpp)
2134{
2135 unsigned int tile_width_bytes =
2136 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2137
2138 *tile_width = tile_width_bytes / cpp;
2139 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2140}
2141
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002142unsigned int
2143intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002144 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002145{
Ville Syrjälä832be822016-01-12 21:08:33 +02002146 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2147 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2148
2149 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002150}
2151
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002152unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2153{
2154 unsigned int size = 0;
2155 int i;
2156
2157 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2158 size += rot_info->plane[i].width * rot_info->plane[i].height;
2159
2160 return size;
2161}
2162
Daniel Vetter75c82a52015-10-14 16:51:04 +02002163static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002164intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2165 const struct drm_framebuffer *fb,
2166 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002167{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002168 if (intel_rotation_90_or_270(rotation)) {
2169 *view = i915_ggtt_view_rotated;
2170 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2171 } else {
2172 *view = i915_ggtt_view_normal;
2173 }
2174}
2175
2176static void
2177intel_fill_fb_info(struct drm_i915_private *dev_priv,
2178 struct drm_framebuffer *fb)
2179{
2180 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002181 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002182
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002183 tile_size = intel_tile_size(dev_priv);
2184
2185 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002186 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2187 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002188
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002189 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2190 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002191
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002192 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002193 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002194 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2195 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002196
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002197 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002198 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2199 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002200 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002201}
2202
Ville Syrjälä603525d2016-01-12 21:08:37 +02002203static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002204{
2205 if (INTEL_INFO(dev_priv)->gen >= 9)
2206 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002207 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002208 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002209 return 128 * 1024;
2210 else if (INTEL_INFO(dev_priv)->gen >= 4)
2211 return 4 * 1024;
2212 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002213 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002214}
2215
Ville Syrjälä603525d2016-01-12 21:08:37 +02002216static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2217 uint64_t fb_modifier)
2218{
2219 switch (fb_modifier) {
2220 case DRM_FORMAT_MOD_NONE:
2221 return intel_linear_alignment(dev_priv);
2222 case I915_FORMAT_MOD_X_TILED:
2223 if (INTEL_INFO(dev_priv)->gen >= 9)
2224 return 256 * 1024;
2225 return 0;
2226 case I915_FORMAT_MOD_Y_TILED:
2227 case I915_FORMAT_MOD_Yf_TILED:
2228 return 1 * 1024 * 1024;
2229 default:
2230 MISSING_CASE(fb_modifier);
2231 return 0;
2232 }
2233}
2234
Chris Wilson127bd2a2010-07-23 23:32:05 +01002235int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002236intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2237 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002238{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002239 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002240 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002241 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002242 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002243 u32 alignment;
2244 int ret;
2245
Matt Roperebcdd392014-07-09 16:22:11 -07002246 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2247
Ville Syrjälä603525d2016-01-12 21:08:37 +02002248 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002249
Ville Syrjälä3465c582016-02-15 22:54:43 +02002250 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002251
Chris Wilson693db182013-03-05 14:52:39 +00002252 /* Note that the w/a also requires 64 PTE of padding following the
2253 * bo. We currently fill all unused PTE with the shadow page and so
2254 * we should always have valid PTE following the scanout preventing
2255 * the VT-d warning.
2256 */
2257 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2258 alignment = 256 * 1024;
2259
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002260 /*
2261 * Global gtt pte registers are special registers which actually forward
2262 * writes to a chunk of system memory. Which means that there is no risk
2263 * that the register values disappear as soon as we call
2264 * intel_runtime_pm_put(), so it is correct to wrap only the
2265 * pin/unpin/fence and not more.
2266 */
2267 intel_runtime_pm_get(dev_priv);
2268
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002269 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2270 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002271 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002272 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002273
2274 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2275 * fence, whereas 965+ only requires a fence if using
2276 * framebuffer compression. For simplicity, we always install
2277 * a fence as the cost is not that onerous.
2278 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002279 if (view.type == I915_GGTT_VIEW_NORMAL) {
2280 ret = i915_gem_object_get_fence(obj);
2281 if (ret == -EDEADLK) {
2282 /*
2283 * -EDEADLK means there are no free fences
2284 * no pending flips.
2285 *
2286 * This is propagated to atomic, but it uses
2287 * -EDEADLK to force a locking recovery, so
2288 * change the returned error to -EBUSY.
2289 */
2290 ret = -EBUSY;
2291 goto err_unpin;
2292 } else if (ret)
2293 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002294
Vivek Kasireddy98072162015-10-29 18:54:38 -07002295 i915_gem_object_pin_fence(obj);
2296 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002297
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002298 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002299 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002300
2301err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002302 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002303err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002304 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002305 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002306}
2307
Ville Syrjälä3465c582016-02-15 22:54:43 +02002308static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002309{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002310 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002311 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002312
Matt Roperebcdd392014-07-09 16:22:11 -07002313 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2314
Ville Syrjälä3465c582016-02-15 22:54:43 +02002315 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002316
Vivek Kasireddy98072162015-10-29 18:54:38 -07002317 if (view.type == I915_GGTT_VIEW_NORMAL)
2318 i915_gem_object_unpin_fence(obj);
2319
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002320 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002321}
2322
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002323/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002324 * Adjust the tile offset by moving the difference into
2325 * the x/y offsets.
2326 *
2327 * Input tile dimensions and pitch must already be
2328 * rotated to match x and y, and in pixel units.
2329 */
2330static u32 intel_adjust_tile_offset(int *x, int *y,
2331 unsigned int tile_width,
2332 unsigned int tile_height,
2333 unsigned int tile_size,
2334 unsigned int pitch_tiles,
2335 u32 old_offset,
2336 u32 new_offset)
2337{
2338 unsigned int tiles;
2339
2340 WARN_ON(old_offset & (tile_size - 1));
2341 WARN_ON(new_offset & (tile_size - 1));
2342 WARN_ON(new_offset > old_offset);
2343
2344 tiles = (old_offset - new_offset) / tile_size;
2345
2346 *y += tiles / pitch_tiles * tile_height;
2347 *x += tiles % pitch_tiles * tile_width;
2348
2349 return new_offset;
2350}
2351
2352/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002353 * Computes the linear offset to the base tile and adjusts
2354 * x, y. bytes per pixel is assumed to be a power-of-two.
2355 *
2356 * In the 90/270 rotated case, x and y are assumed
2357 * to be already rotated to match the rotated GTT view, and
2358 * pitch is the tile_height aligned framebuffer height.
2359 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002360u32 intel_compute_tile_offset(int *x, int *y,
2361 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002362 unsigned int pitch,
2363 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002364{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002365 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2366 uint64_t fb_modifier = fb->modifier[plane];
2367 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002368 u32 offset, offset_aligned, alignment;
2369
2370 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2371 if (alignment)
2372 alignment--;
2373
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002374 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002375 unsigned int tile_size, tile_width, tile_height;
2376 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002377
Ville Syrjäläd8433102016-01-12 21:08:35 +02002378 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002379 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2380 fb_modifier, cpp);
2381
2382 if (intel_rotation_90_or_270(rotation)) {
2383 pitch_tiles = pitch / tile_height;
2384 swap(tile_width, tile_height);
2385 } else {
2386 pitch_tiles = pitch / (tile_width * cpp);
2387 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002388
Ville Syrjäläd8433102016-01-12 21:08:35 +02002389 tile_rows = *y / tile_height;
2390 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002391
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002392 tiles = *x / tile_width;
2393 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002394
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002395 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2396 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002397
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002398 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2399 tile_size, pitch_tiles,
2400 offset, offset_aligned);
2401 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002402 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002403 offset_aligned = offset & ~alignment;
2404
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002405 *y = (offset & alignment) / pitch;
2406 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002407 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002408
2409 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002410}
2411
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002412static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002413{
2414 switch (format) {
2415 case DISPPLANE_8BPP:
2416 return DRM_FORMAT_C8;
2417 case DISPPLANE_BGRX555:
2418 return DRM_FORMAT_XRGB1555;
2419 case DISPPLANE_BGRX565:
2420 return DRM_FORMAT_RGB565;
2421 default:
2422 case DISPPLANE_BGRX888:
2423 return DRM_FORMAT_XRGB8888;
2424 case DISPPLANE_RGBX888:
2425 return DRM_FORMAT_XBGR8888;
2426 case DISPPLANE_BGRX101010:
2427 return DRM_FORMAT_XRGB2101010;
2428 case DISPPLANE_RGBX101010:
2429 return DRM_FORMAT_XBGR2101010;
2430 }
2431}
2432
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002433static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2434{
2435 switch (format) {
2436 case PLANE_CTL_FORMAT_RGB_565:
2437 return DRM_FORMAT_RGB565;
2438 default:
2439 case PLANE_CTL_FORMAT_XRGB_8888:
2440 if (rgb_order) {
2441 if (alpha)
2442 return DRM_FORMAT_ABGR8888;
2443 else
2444 return DRM_FORMAT_XBGR8888;
2445 } else {
2446 if (alpha)
2447 return DRM_FORMAT_ARGB8888;
2448 else
2449 return DRM_FORMAT_XRGB8888;
2450 }
2451 case PLANE_CTL_FORMAT_XRGB_2101010:
2452 if (rgb_order)
2453 return DRM_FORMAT_XBGR2101010;
2454 else
2455 return DRM_FORMAT_XRGB2101010;
2456 }
2457}
2458
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002459static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002460intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2461 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002462{
2463 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002464 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002465 struct drm_i915_gem_object *obj = NULL;
2466 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002467 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002468 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2469 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2470 PAGE_SIZE);
2471
2472 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002473
Chris Wilsonff2652e2014-03-10 08:07:02 +00002474 if (plane_config->size == 0)
2475 return false;
2476
Paulo Zanoni3badb492015-09-23 12:52:23 -03002477 /* If the FB is too big, just don't use it since fbdev is not very
2478 * important and we should probably use that space with FBC or other
2479 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002480 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002481 return false;
2482
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002483 mutex_lock(&dev->struct_mutex);
2484
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002485 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2486 base_aligned,
2487 base_aligned,
2488 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002489 if (!obj) {
2490 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002491 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002492 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002493
Damien Lespiau49af4492015-01-20 12:51:44 +00002494 obj->tiling_mode = plane_config->tiling;
2495 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002496 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002497
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002498 mode_cmd.pixel_format = fb->pixel_format;
2499 mode_cmd.width = fb->width;
2500 mode_cmd.height = fb->height;
2501 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002502 mode_cmd.modifier[0] = fb->modifier[0];
2503 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002504
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002505 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002506 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002507 DRM_DEBUG_KMS("intel fb init failed\n");
2508 goto out_unref_obj;
2509 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002510
Jesse Barnes46f297f2014-03-07 08:57:48 -08002511 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002512
Daniel Vetterf6936e22015-03-26 12:17:05 +01002513 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002514 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002515
2516out_unref_obj:
2517 drm_gem_object_unreference(&obj->base);
2518 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519 return false;
2520}
2521
Matt Roperafd65eb2015-02-03 13:10:04 -08002522/* Update plane->state->fb to match plane->fb after driver-internal updates */
2523static void
2524update_state_fb(struct drm_plane *plane)
2525{
2526 if (plane->fb == plane->state->fb)
2527 return;
2528
2529 if (plane->state->fb)
2530 drm_framebuffer_unreference(plane->state->fb);
2531 plane->state->fb = plane->fb;
2532 if (plane->state->fb)
2533 drm_framebuffer_reference(plane->state->fb);
2534}
2535
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002536static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002537intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2538 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539{
2540 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002541 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 struct drm_crtc *c;
2543 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002544 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002545 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002546 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002547 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2548 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002549 struct intel_plane_state *intel_state =
2550 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002551 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552
Damien Lespiau2d140302015-02-05 17:22:18 +00002553 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554 return;
2555
Daniel Vetterf6936e22015-03-26 12:17:05 +01002556 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002557 fb = &plane_config->fb->base;
2558 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002559 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560
Damien Lespiau2d140302015-02-05 17:22:18 +00002561 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562
2563 /*
2564 * Failed to alloc the obj, check to see if we should share
2565 * an fb with another CRTC instead
2566 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002567 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568 i = to_intel_crtc(c);
2569
2570 if (c == &intel_crtc->base)
2571 continue;
2572
Matt Roper2ff8fde2014-07-08 07:50:07 -07002573 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 continue;
2575
Daniel Vetter88595ac2015-03-26 12:42:24 +01002576 fb = c->primary->fb;
2577 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002578 continue;
2579
Daniel Vetter88595ac2015-03-26 12:42:24 +01002580 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002581 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002582 drm_framebuffer_reference(fb);
2583 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584 }
2585 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002586
Matt Roper200757f2015-12-03 11:37:36 -08002587 /*
2588 * We've failed to reconstruct the BIOS FB. Current display state
2589 * indicates that the primary plane is visible, but has a NULL FB,
2590 * which will lead to problems later if we don't fix it up. The
2591 * simplest solution is to just disable the primary plane now and
2592 * pretend the BIOS never had it enabled.
2593 */
2594 to_intel_plane_state(plane_state)->visible = false;
2595 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002596 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002597 intel_plane->disable_plane(primary, &intel_crtc->base);
2598
Daniel Vetter88595ac2015-03-26 12:42:24 +01002599 return;
2600
2601valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002602 plane_state->src_x = 0;
2603 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002604 plane_state->src_w = fb->width << 16;
2605 plane_state->src_h = fb->height << 16;
2606
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002607 plane_state->crtc_x = 0;
2608 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002609 plane_state->crtc_w = fb->width;
2610 plane_state->crtc_h = fb->height;
2611
Matt Roper0a8d8a82015-12-03 11:37:38 -08002612 intel_state->src.x1 = plane_state->src_x;
2613 intel_state->src.y1 = plane_state->src_y;
2614 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2615 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2616 intel_state->dst.x1 = plane_state->crtc_x;
2617 intel_state->dst.y1 = plane_state->crtc_y;
2618 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2619 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2620
Daniel Vetter88595ac2015-03-26 12:42:24 +01002621 obj = intel_fb_obj(fb);
2622 if (obj->tiling_mode != I915_TILING_NONE)
2623 dev_priv->preserve_bios_swizzle = true;
2624
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002625 drm_framebuffer_reference(fb);
2626 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002627 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002628 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002629 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002630}
2631
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002632static void i9xx_update_primary_plane(struct drm_plane *primary,
2633 const struct intel_crtc_state *crtc_state,
2634 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002635{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002636 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002637 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2639 struct drm_framebuffer *fb = plane_state->base.fb;
2640 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002641 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002642 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002643 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002644 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002645 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002646 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002647 int x = plane_state->src.x1 >> 16;
2648 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002649
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002650 dspcntr = DISPPLANE_GAMMA_ENABLE;
2651
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002652 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002653
2654 if (INTEL_INFO(dev)->gen < 4) {
2655 if (intel_crtc->pipe == PIPE_B)
2656 dspcntr |= DISPPLANE_SEL_PIPE_B;
2657
2658 /* pipesrc and dspsize control the size that is scaled from,
2659 * which should always be the user's requested size.
2660 */
2661 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002662 ((crtc_state->pipe_src_h - 1) << 16) |
2663 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002664 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002665 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2666 I915_WRITE(PRIMSIZE(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äc14b0482014-10-16 20:52:34 +03002669 I915_WRITE(PRIMPOS(plane), 0);
2670 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002671 }
2672
Ville Syrjälä57779d02012-10-31 17:50:14 +02002673 switch (fb->pixel_format) {
2674 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002675 dspcntr |= DISPPLANE_8BPP;
2676 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002677 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002679 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 case DRM_FORMAT_RGB565:
2681 dspcntr |= DISPPLANE_BGRX565;
2682 break;
2683 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002684 dspcntr |= DISPPLANE_BGRX888;
2685 break;
2686 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 dspcntr |= DISPPLANE_RGBX888;
2688 break;
2689 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002690 dspcntr |= DISPPLANE_BGRX101010;
2691 break;
2692 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002693 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002694 break;
2695 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002696 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002697 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002699 if (INTEL_INFO(dev)->gen >= 4 &&
2700 obj->tiling_mode != I915_TILING_NONE)
2701 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002702
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002703 if (IS_G4X(dev))
2704 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2705
Ville Syrjäläac484962016-01-20 21:05:26 +02002706 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002707
Daniel Vetterc2c75132012-07-05 12:17:30 +02002708 if (INTEL_INFO(dev)->gen >= 4) {
2709 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002710 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002711 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002712 linear_offset -= intel_crtc->dspaddr_offset;
2713 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002715 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002716
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002717 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302718 dspcntr |= DISPPLANE_ROTATE_180;
2719
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002720 x += (crtc_state->pipe_src_w - 1);
2721 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302722
2723 /* Finding the last pixel of the last line of the display
2724 data and adding to linear_offset*/
2725 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002726 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002727 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302728 }
2729
Paulo Zanoni2db33662015-09-14 15:20:03 -03002730 intel_crtc->adjusted_x = x;
2731 intel_crtc->adjusted_y = y;
2732
Sonika Jindal48404c12014-08-22 14:06:04 +05302733 I915_WRITE(reg, dspcntr);
2734
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002735 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002736 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002737 I915_WRITE(DSPSURF(plane),
2738 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002739 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002740 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002741 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002742 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002743 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002744}
2745
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002746static void i9xx_disable_primary_plane(struct drm_plane *primary,
2747 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002748{
2749 struct drm_device *dev = crtc->dev;
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002752 int plane = intel_crtc->plane;
2753
2754 I915_WRITE(DSPCNTR(plane), 0);
2755 if (INTEL_INFO(dev_priv)->gen >= 4)
2756 I915_WRITE(DSPSURF(plane), 0);
2757 else
2758 I915_WRITE(DSPADDR(plane), 0);
2759 POSTING_READ(DSPCNTR(plane));
2760}
2761
2762static void ironlake_update_primary_plane(struct drm_plane *primary,
2763 const struct intel_crtc_state *crtc_state,
2764 const struct intel_plane_state *plane_state)
2765{
2766 struct drm_device *dev = primary->dev;
2767 struct drm_i915_private *dev_priv = dev->dev_private;
2768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2769 struct drm_framebuffer *fb = plane_state->base.fb;
2770 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002771 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002772 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002774 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002775 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002776 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002777 int x = plane_state->src.x1 >> 16;
2778 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002779
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002780 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002781 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002782
2783 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2784 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2785
Ville Syrjälä57779d02012-10-31 17:50:14 +02002786 switch (fb->pixel_format) {
2787 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 dspcntr |= DISPPLANE_8BPP;
2789 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002790 case DRM_FORMAT_RGB565:
2791 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002794 dspcntr |= DISPPLANE_BGRX888;
2795 break;
2796 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 dspcntr |= DISPPLANE_RGBX888;
2798 break;
2799 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002800 dspcntr |= DISPPLANE_BGRX101010;
2801 break;
2802 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804 break;
2805 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002806 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807 }
2808
2809 if (obj->tiling_mode != I915_TILING_NONE)
2810 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002811
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002812 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002813 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814
Ville Syrjäläac484962016-01-20 21:05:26 +02002815 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002816 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002817 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002818 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002819 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002820 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302821 dspcntr |= DISPPLANE_ROTATE_180;
2822
2823 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002824 x += (crtc_state->pipe_src_w - 1);
2825 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302826
2827 /* Finding the last pixel of the last line of the display
2828 data and adding to linear_offset*/
2829 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002830 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002831 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302832 }
2833 }
2834
Paulo Zanoni2db33662015-09-14 15:20:03 -03002835 intel_crtc->adjusted_x = x;
2836 intel_crtc->adjusted_y = y;
2837
Sonika Jindal48404c12014-08-22 14:06:04 +05302838 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002840 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002841 I915_WRITE(DSPSURF(plane),
2842 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002843 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002844 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2845 } else {
2846 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2847 I915_WRITE(DSPLINOFF(plane), linear_offset);
2848 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002849 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002850}
2851
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002852u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2853 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002854{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002855 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2856 return 64;
2857 } else {
2858 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002859
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002860 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002861 }
2862}
2863
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002864u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2865 struct drm_i915_gem_object *obj,
2866 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002867{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002868 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002869 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002870 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002871
Ville Syrjäläe7941292016-01-19 18:23:17 +02002872 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002873 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002874
Daniel Vetterce7f1722015-10-14 16:51:06 +02002875 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002876 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002877 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002878 return -1;
2879
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002880 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002881
2882 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002883 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002884 PAGE_SIZE;
2885 }
2886
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002887 WARN_ON(upper_32_bits(offset));
2888
2889 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002890}
2891
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002892static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2893{
2894 struct drm_device *dev = intel_crtc->base.dev;
2895 struct drm_i915_private *dev_priv = dev->dev_private;
2896
2897 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2898 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2899 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002900}
2901
Chandra Kondurua1b22782015-04-07 15:28:45 -07002902/*
2903 * This function detaches (aka. unbinds) unused scalers in hardware
2904 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002905static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002907 struct intel_crtc_scaler_state *scaler_state;
2908 int i;
2909
Chandra Kondurua1b22782015-04-07 15:28:45 -07002910 scaler_state = &intel_crtc->config->scaler_state;
2911
2912 /* loop through and disable scalers that aren't in use */
2913 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002914 if (!scaler_state->scalers[i].in_use)
2915 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002916 }
2917}
2918
Chandra Konduru6156a452015-04-27 13:48:39 -07002919u32 skl_plane_ctl_format(uint32_t pixel_format)
2920{
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002922 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002923 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002925 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 /*
2931 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2932 * to be already pre-multiplied. We need to add a knob (or a different
2933 * DRM_FORMAT) for user-space to configure that.
2934 */
2935 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002946 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002954 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002956
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958}
2959
2960u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2961{
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 switch (fb_modifier) {
2963 case DRM_FORMAT_MOD_NONE:
2964 break;
2965 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 default:
2972 MISSING_CASE(fb_modifier);
2973 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002974
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976}
2977
2978u32 skl_plane_ctl_rotation(unsigned int rotation)
2979{
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 switch (rotation) {
2981 case BIT(DRM_ROTATE_0):
2982 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302983 /*
2984 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2985 * while i915 HW rotation is clockwise, thats why this swapping.
2986 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302988 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002990 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002991 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302992 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002993 default:
2994 MISSING_CASE(rotation);
2995 }
2996
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002997 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998}
2999
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003000static void skylake_update_primary_plane(struct drm_plane *plane,
3001 const struct intel_crtc_state *crtc_state,
3002 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003003{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003004 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3007 struct drm_framebuffer *fb = plane_state->base.fb;
3008 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003009 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303010 u32 plane_ctl, stride_div, stride;
3011 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003012 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303013 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003014 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 int scaler_id = plane_state->scaler_id;
3016 int src_x = plane_state->src.x1 >> 16;
3017 int src_y = plane_state->src.y1 >> 16;
3018 int src_w = drm_rect_width(&plane_state->src) >> 16;
3019 int src_h = drm_rect_height(&plane_state->src) >> 16;
3020 int dst_x = plane_state->dst.x1;
3021 int dst_y = plane_state->dst.y1;
3022 int dst_w = drm_rect_width(&plane_state->dst);
3023 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003024
3025 plane_ctl = PLANE_CTL_ENABLE |
3026 PLANE_CTL_PIPE_GAMMA_ENABLE |
3027 PLANE_CTL_PIPE_CSC_ENABLE;
3028
Chandra Konduru6156a452015-04-27 13:48:39 -07003029 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3030 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003031 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003034 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003035 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003036 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303037
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003038 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003039
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303040 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003041 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3042
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303043 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003044 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303045 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003046 x_offset = stride * tile_height - src_y - src_h;
3047 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003048 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 } else {
3050 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003051 x_offset = src_x;
3052 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003053 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054 }
3055 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003056
Paulo Zanoni2db33662015-09-14 15:20:03 -03003057 intel_crtc->adjusted_x = x_offset;
3058 intel_crtc->adjusted_y = y_offset;
3059
Damien Lespiau70d21f02013-07-03 21:06:04 +01003060 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303061 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3062 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3063 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003064
3065 if (scaler_id >= 0) {
3066 uint32_t ps_ctrl = 0;
3067
3068 WARN_ON(!dst_w || !dst_h);
3069 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3070 crtc_state->scaler_state.scalers[scaler_id].mode;
3071 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3072 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3073 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3074 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3075 I915_WRITE(PLANE_POS(pipe, 0), 0);
3076 } else {
3077 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3078 }
3079
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003080 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003081
3082 POSTING_READ(PLANE_SURF(pipe, 0));
3083}
3084
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003085static void skylake_disable_primary_plane(struct drm_plane *primary,
3086 struct drm_crtc *crtc)
3087{
3088 struct drm_device *dev = crtc->dev;
3089 struct drm_i915_private *dev_priv = dev->dev_private;
3090 int pipe = to_intel_crtc(crtc)->pipe;
3091
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003092 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3093 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3094 POSTING_READ(PLANE_SURF(pipe, 0));
3095}
3096
Jesse Barnes17638cd2011-06-24 12:19:23 -07003097/* Assume fb object is pinned & idle & fenced and just update base pointers */
3098static int
3099intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3100 int x, int y, enum mode_set_atomic state)
3101{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003102 /* Support for kgdboc is disabled, this needs a major rework. */
3103 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003104
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003105 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003106}
3107
Ville Syrjälä75147472014-11-24 18:28:11 +02003108static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003109{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003110 struct drm_crtc *crtc;
3111
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003112 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3114 enum plane plane = intel_crtc->plane;
3115
3116 intel_prepare_page_flip(dev, plane);
3117 intel_finish_page_flip_plane(dev, plane);
3118 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003119}
3120
3121static void intel_update_primary_planes(struct drm_device *dev)
3122{
Ville Syrjälä75147472014-11-24 18:28:11 +02003123 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003124
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003125 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003126 struct intel_plane *plane = to_intel_plane(crtc->primary);
3127 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003128
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003129 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003130 plane_state = to_intel_plane_state(plane->base.state);
3131
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003132 if (plane_state->visible)
3133 plane->update_plane(&plane->base,
3134 to_intel_crtc_state(crtc->state),
3135 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003136
3137 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003138 }
3139}
3140
Ville Syrjälä75147472014-11-24 18:28:11 +02003141void intel_prepare_reset(struct drm_device *dev)
3142{
3143 /* no reset support for gen2 */
3144 if (IS_GEN2(dev))
3145 return;
3146
3147 /* reset doesn't touch the display */
3148 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3149 return;
3150
3151 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003152 /*
3153 * Disabling the crtcs gracefully seems nicer. Also the
3154 * g33 docs say we should at least disable all the planes.
3155 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003156 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003157}
3158
3159void intel_finish_reset(struct drm_device *dev)
3160{
3161 struct drm_i915_private *dev_priv = to_i915(dev);
3162
3163 /*
3164 * Flips in the rings will be nuked by the reset,
3165 * so complete all pending flips so that user space
3166 * will get its events and not get stuck.
3167 */
3168 intel_complete_page_flips(dev);
3169
3170 /* no reset support for gen2 */
3171 if (IS_GEN2(dev))
3172 return;
3173
3174 /* reset doesn't touch the display */
3175 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3176 /*
3177 * Flips in the rings have been nuked by the reset,
3178 * so update the base address of all primary
3179 * planes to the the last fb to make sure we're
3180 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003181 *
3182 * FIXME: Atomic will make this obsolete since we won't schedule
3183 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003184 */
3185 intel_update_primary_planes(dev);
3186 return;
3187 }
3188
3189 /*
3190 * The display has been reset as well,
3191 * so need a full re-initialization.
3192 */
3193 intel_runtime_pm_disable_interrupts(dev_priv);
3194 intel_runtime_pm_enable_interrupts(dev_priv);
3195
3196 intel_modeset_init_hw(dev);
3197
3198 spin_lock_irq(&dev_priv->irq_lock);
3199 if (dev_priv->display.hpd_irq_setup)
3200 dev_priv->display.hpd_irq_setup(dev);
3201 spin_unlock_irq(&dev_priv->irq_lock);
3202
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003203 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003204
3205 intel_hpd_init(dev_priv);
3206
3207 drm_modeset_unlock_all(dev);
3208}
3209
Chris Wilson7d5e3792014-03-04 13:15:08 +00003210static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3211{
3212 struct drm_device *dev = crtc->dev;
3213 struct drm_i915_private *dev_priv = dev->dev_private;
3214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003215 bool pending;
3216
3217 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3218 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3219 return false;
3220
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003221 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003222 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003223 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003224
3225 return pending;
3226}
3227
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003228static void intel_update_pipe_config(struct intel_crtc *crtc,
3229 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003230{
3231 struct drm_device *dev = crtc->base.dev;
3232 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003233 struct intel_crtc_state *pipe_config =
3234 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003236 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3237 crtc->base.mode = crtc->base.state->mode;
3238
3239 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3240 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3241 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003242
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003243 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003244 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003245
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003246 /*
3247 * Update pipe size and adjust fitter if needed: the reason for this is
3248 * that in compute_mode_changes we check the native mode (not the pfit
3249 * mode) to see if we can flip rather than do a full mode set. In the
3250 * fastboot case, we'll flip, but if we don't update the pipesrc and
3251 * pfit state, we'll end up with a big fb scanned out into the wrong
3252 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003253 */
3254
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003256 ((pipe_config->pipe_src_w - 1) << 16) |
3257 (pipe_config->pipe_src_h - 1));
3258
3259 /* on skylake this is done by detaching scalers */
3260 if (INTEL_INFO(dev)->gen >= 9) {
3261 skl_detach_scalers(crtc);
3262
3263 if (pipe_config->pch_pfit.enabled)
3264 skylake_pfit_enable(crtc);
3265 } else if (HAS_PCH_SPLIT(dev)) {
3266 if (pipe_config->pch_pfit.enabled)
3267 ironlake_pfit_enable(crtc);
3268 else if (old_crtc_state->pch_pfit.enabled)
3269 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003270 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003271}
3272
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003273static void intel_fdi_normal_train(struct drm_crtc *crtc)
3274{
3275 struct drm_device *dev = crtc->dev;
3276 struct drm_i915_private *dev_priv = dev->dev_private;
3277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3278 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003279 i915_reg_t reg;
3280 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003281
3282 /* enable normal train */
3283 reg = FDI_TX_CTL(pipe);
3284 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003285 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003286 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3287 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003288 } else {
3289 temp &= ~FDI_LINK_TRAIN_NONE;
3290 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003291 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003292 I915_WRITE(reg, temp);
3293
3294 reg = FDI_RX_CTL(pipe);
3295 temp = I915_READ(reg);
3296 if (HAS_PCH_CPT(dev)) {
3297 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3298 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3299 } else {
3300 temp &= ~FDI_LINK_TRAIN_NONE;
3301 temp |= FDI_LINK_TRAIN_NONE;
3302 }
3303 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3304
3305 /* wait one idle pattern time */
3306 POSTING_READ(reg);
3307 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003308
3309 /* IVB wants error correction enabled */
3310 if (IS_IVYBRIDGE(dev))
3311 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3312 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003313}
3314
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003315/* The FDI link training functions for ILK/Ibexpeak. */
3316static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3317{
3318 struct drm_device *dev = crtc->dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3321 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003322 i915_reg_t reg;
3323 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003324
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003325 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003326 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003327
Adam Jacksone1a44742010-06-25 15:32:14 -04003328 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3329 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003330 reg = FDI_RX_IMR(pipe);
3331 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003332 temp &= ~FDI_RX_SYMBOL_LOCK;
3333 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 I915_WRITE(reg, temp);
3335 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003336 udelay(150);
3337
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003338 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003339 reg = FDI_TX_CTL(pipe);
3340 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003341 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003342 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003343 temp &= ~FDI_LINK_TRAIN_NONE;
3344 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003345 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003346
Chris Wilson5eddb702010-09-11 13:48:45 +01003347 reg = FDI_RX_CTL(pipe);
3348 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349 temp &= ~FDI_LINK_TRAIN_NONE;
3350 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003351 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3352
3353 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354 udelay(150);
3355
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003356 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3358 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3359 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003360
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003362 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3365
3366 if ((temp & FDI_RX_BIT_LOCK)) {
3367 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 break;
3370 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003372 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374
3375 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 reg = FDI_TX_CTL(pipe);
3377 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 reg = FDI_RX_CTL(pipe);
3383 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003384 temp &= ~FDI_LINK_TRAIN_NONE;
3385 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 I915_WRITE(reg, temp);
3387
3388 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 udelay(150);
3390
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003392 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3395
3396 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398 DRM_DEBUG_KMS("FDI train 2 done.\n");
3399 break;
3400 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003402 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003403 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404
3405 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003406
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407}
3408
Akshay Joshi0206e352011-08-16 15:34:10 -04003409static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3411 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3412 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3413 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3414};
3415
3416/* The FDI link training functions for SNB/Cougarpoint. */
3417static void gen6_fdi_link_train(struct drm_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003423 i915_reg_t reg;
3424 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425
Adam Jacksone1a44742010-06-25 15:32:14 -04003426 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3427 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 reg = FDI_RX_IMR(pipe);
3429 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003430 temp &= ~FDI_RX_SYMBOL_LOCK;
3431 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 I915_WRITE(reg, temp);
3433
3434 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003435 udelay(150);
3436
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 reg = FDI_TX_CTL(pipe);
3439 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003440 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003441 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 temp &= ~FDI_LINK_TRAIN_NONE;
3443 temp |= FDI_LINK_TRAIN_PATTERN_1;
3444 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3445 /* SNB-B */
3446 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
Daniel Vetterd74cf322012-10-26 10:58:13 +02003449 I915_WRITE(FDI_RX_MISC(pipe),
3450 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3451
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 if (HAS_PCH_CPT(dev)) {
3455 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3457 } else {
3458 temp &= ~FDI_LINK_TRAIN_NONE;
3459 temp |= FDI_LINK_TRAIN_PATTERN_1;
3460 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3462
3463 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 udelay(150);
3465
Akshay Joshi0206e352011-08-16 15:34:10 -04003466 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3470 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 I915_WRITE(reg, temp);
3472
3473 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 udelay(500);
3475
Sean Paulfa37d392012-03-02 12:53:39 -05003476 for (retry = 0; retry < 5; retry++) {
3477 reg = FDI_RX_IIR(pipe);
3478 temp = I915_READ(reg);
3479 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3480 if (temp & FDI_RX_BIT_LOCK) {
3481 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3482 DRM_DEBUG_KMS("FDI train 1 done.\n");
3483 break;
3484 }
3485 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 }
Sean Paulfa37d392012-03-02 12:53:39 -05003487 if (retry < 5)
3488 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 }
3490 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492
3493 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2;
3498 if (IS_GEN6(dev)) {
3499 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3500 /* SNB-B */
3501 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3502 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_RX_CTL(pipe);
3506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 if (HAS_PCH_CPT(dev)) {
3508 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3509 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3510 } else {
3511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_2;
3513 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 udelay(150);
3518
Akshay Joshi0206e352011-08-16 15:34:10 -04003519 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3523 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 I915_WRITE(reg, temp);
3525
3526 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 udelay(500);
3528
Sean Paulfa37d392012-03-02 12:53:39 -05003529 for (retry = 0; retry < 5; retry++) {
3530 reg = FDI_RX_IIR(pipe);
3531 temp = I915_READ(reg);
3532 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3533 if (temp & FDI_RX_SYMBOL_LOCK) {
3534 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3535 DRM_DEBUG_KMS("FDI train 2 done.\n");
3536 break;
3537 }
3538 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 }
Sean Paulfa37d392012-03-02 12:53:39 -05003540 if (retry < 5)
3541 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 }
3543 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003544 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545
3546 DRM_DEBUG_KMS("FDI train done.\n");
3547}
3548
Jesse Barnes357555c2011-04-28 15:09:55 -07003549/* Manual link training for Ivy Bridge A0 parts */
3550static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3551{
3552 struct drm_device *dev = crtc->dev;
3553 struct drm_i915_private *dev_priv = dev->dev_private;
3554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3555 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003556 i915_reg_t reg;
3557 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003558
3559 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3560 for train result */
3561 reg = FDI_RX_IMR(pipe);
3562 temp = I915_READ(reg);
3563 temp &= ~FDI_RX_SYMBOL_LOCK;
3564 temp &= ~FDI_RX_BIT_LOCK;
3565 I915_WRITE(reg, temp);
3566
3567 POSTING_READ(reg);
3568 udelay(150);
3569
Daniel Vetter01a415f2012-10-27 15:58:40 +02003570 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3571 I915_READ(FDI_RX_IIR(pipe)));
3572
Jesse Barnes139ccd32013-08-19 11:04:55 -07003573 /* Try each vswing and preemphasis setting twice before moving on */
3574 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3575 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003578 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3579 temp &= ~FDI_TX_ENABLE;
3580 I915_WRITE(reg, temp);
3581
3582 reg = FDI_RX_CTL(pipe);
3583 temp = I915_READ(reg);
3584 temp &= ~FDI_LINK_TRAIN_AUTO;
3585 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3586 temp &= ~FDI_RX_ENABLE;
3587 I915_WRITE(reg, temp);
3588
3589 /* enable CPU FDI TX and PCH FDI RX */
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003593 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003594 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003595 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003596 temp |= snb_b_fdi_train_param[j/2];
3597 temp |= FDI_COMPOSITE_SYNC;
3598 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3599
3600 I915_WRITE(FDI_RX_MISC(pipe),
3601 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3602
3603 reg = FDI_RX_CTL(pipe);
3604 temp = I915_READ(reg);
3605 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3606 temp |= FDI_COMPOSITE_SYNC;
3607 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3608
3609 POSTING_READ(reg);
3610 udelay(1); /* should be 0.5us */
3611
3612 for (i = 0; i < 4; i++) {
3613 reg = FDI_RX_IIR(pipe);
3614 temp = I915_READ(reg);
3615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3616
3617 if (temp & FDI_RX_BIT_LOCK ||
3618 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3619 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3620 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3621 i);
3622 break;
3623 }
3624 udelay(1); /* should be 0.5us */
3625 }
3626 if (i == 4) {
3627 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3628 continue;
3629 }
3630
3631 /* Train 2 */
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
3634 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3635 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3636 I915_WRITE(reg, temp);
3637
3638 reg = FDI_RX_CTL(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003642 I915_WRITE(reg, temp);
3643
3644 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003645 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003646
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647 for (i = 0; i < 4; i++) {
3648 reg = FDI_RX_IIR(pipe);
3649 temp = I915_READ(reg);
3650 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003651
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 if (temp & FDI_RX_SYMBOL_LOCK ||
3653 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3656 i);
3657 goto train_done;
3658 }
3659 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003660 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003661 if (i == 4)
3662 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003663 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003664
Jesse Barnes139ccd32013-08-19 11:04:55 -07003665train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
Daniel Vetter88cefb62012-08-12 19:27:14 +02003669static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003670{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003671 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003672 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003673 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003674 i915_reg_t reg;
3675 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003676
Jesse Barnes0e23b992010-09-10 11:10:00 -07003677 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003678 reg = FDI_RX_CTL(pipe);
3679 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003680 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003681 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003682 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003683 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3684
3685 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003686 udelay(200);
3687
3688 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003689 temp = I915_READ(reg);
3690 I915_WRITE(reg, temp | FDI_PCDCLK);
3691
3692 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003693 udelay(200);
3694
Paulo Zanoni20749732012-11-23 15:30:38 -02003695 /* Enable CPU FDI TX PLL, always on for Ironlake */
3696 reg = FDI_TX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3699 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003700
Paulo Zanoni20749732012-11-23 15:30:38 -02003701 POSTING_READ(reg);
3702 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003703 }
3704}
3705
Daniel Vetter88cefb62012-08-12 19:27:14 +02003706static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3707{
3708 struct drm_device *dev = intel_crtc->base.dev;
3709 struct drm_i915_private *dev_priv = dev->dev_private;
3710 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003711 i915_reg_t reg;
3712 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003713
3714 /* Switch from PCDclk to Rawclk */
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3718
3719 /* Disable CPU FDI TX PLL */
3720 reg = FDI_TX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3723
3724 POSTING_READ(reg);
3725 udelay(100);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3730
3731 /* Wait for the clocks to turn off. */
3732 POSTING_READ(reg);
3733 udelay(100);
3734}
3735
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003736static void ironlake_fdi_disable(struct drm_crtc *crtc)
3737{
3738 struct drm_device *dev = crtc->dev;
3739 struct drm_i915_private *dev_priv = dev->dev_private;
3740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3741 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003742 i915_reg_t reg;
3743 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003744
3745 /* disable CPU FDI tx and PCH FDI rx */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3749 POSTING_READ(reg);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003754 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003755 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3756
3757 POSTING_READ(reg);
3758 udelay(100);
3759
3760 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003761 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003763
3764 /* still set train pattern 1 */
3765 reg = FDI_TX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~FDI_LINK_TRAIN_NONE;
3768 temp |= FDI_LINK_TRAIN_PATTERN_1;
3769 I915_WRITE(reg, temp);
3770
3771 reg = FDI_RX_CTL(pipe);
3772 temp = I915_READ(reg);
3773 if (HAS_PCH_CPT(dev)) {
3774 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3775 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3776 } else {
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_1;
3779 }
3780 /* BPC in FDI rx is consistent with that in PIPECONF */
3781 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003782 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003783 I915_WRITE(reg, temp);
3784
3785 POSTING_READ(reg);
3786 udelay(100);
3787}
3788
Chris Wilson5dce5b932014-01-20 10:17:36 +00003789bool intel_has_pending_fb_unpin(struct drm_device *dev)
3790{
3791 struct intel_crtc *crtc;
3792
3793 /* Note that we don't need to be called with mode_config.lock here
3794 * as our list of CRTC objects is static for the lifetime of the
3795 * device and so cannot disappear as we iterate. Similarly, we can
3796 * happily treat the predicates as racy, atomic checks as userspace
3797 * cannot claim and pin a new fb without at least acquring the
3798 * struct_mutex and so serialising with us.
3799 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003800 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003801 if (atomic_read(&crtc->unpin_work_count) == 0)
3802 continue;
3803
3804 if (crtc->unpin_work)
3805 intel_wait_for_vblank(dev, crtc->pipe);
3806
3807 return true;
3808 }
3809
3810 return false;
3811}
3812
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003813static void page_flip_completed(struct intel_crtc *intel_crtc)
3814{
3815 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3816 struct intel_unpin_work *work = intel_crtc->unpin_work;
3817
3818 /* ensure that the unpin work is consistent wrt ->pending. */
3819 smp_rmb();
3820 intel_crtc->unpin_work = NULL;
3821
3822 if (work->event)
3823 drm_send_vblank_event(intel_crtc->base.dev,
3824 intel_crtc->pipe,
3825 work->event);
3826
3827 drm_crtc_vblank_put(&intel_crtc->base);
3828
3829 wake_up_all(&dev_priv->pending_flip_queue);
3830 queue_work(dev_priv->wq, &work->work);
3831
3832 trace_i915_flip_complete(intel_crtc->plane,
3833 work->pending_flip_obj);
3834}
3835
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003836static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003837{
Chris Wilson0f911282012-04-17 10:05:38 +01003838 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003839 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003840 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003841
Daniel Vetter2c10d572012-12-20 21:24:07 +01003842 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003843
3844 ret = wait_event_interruptible_timeout(
3845 dev_priv->pending_flip_queue,
3846 !intel_crtc_has_pending_flip(crtc),
3847 60*HZ);
3848
3849 if (ret < 0)
3850 return ret;
3851
3852 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003854
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003855 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003856 if (intel_crtc->unpin_work) {
3857 WARN_ONCE(1, "Removing stuck page flip\n");
3858 page_flip_completed(intel_crtc);
3859 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003860 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003861 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003862
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003863 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003864}
3865
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003866static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3867{
3868 u32 temp;
3869
3870 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3871
3872 mutex_lock(&dev_priv->sb_lock);
3873
3874 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3875 temp |= SBI_SSCCTL_DISABLE;
3876 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3877
3878 mutex_unlock(&dev_priv->sb_lock);
3879}
3880
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003881/* Program iCLKIP clock to the desired frequency */
3882static void lpt_program_iclkip(struct drm_crtc *crtc)
3883{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003884 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003885 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3887 u32 temp;
3888
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003889 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003890
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003891 /* The iCLK virtual clock root frequency is in MHz,
3892 * but the adjusted_mode->crtc_clock in in KHz. To get the
3893 * divisors, it is necessary to divide one by another, so we
3894 * convert the virtual clock precision to KHz here for higher
3895 * precision.
3896 */
3897 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898 u32 iclk_virtual_root_freq = 172800 * 1000;
3899 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003900 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003902 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3903 clock << auxdiv);
3904 divsel = (desired_divisor / iclk_pi_range) - 2;
3905 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003907 /*
3908 * Near 20MHz is a corner case which is
3909 * out of range for the 7-bit divisor
3910 */
3911 if (divsel <= 0x7f)
3912 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 }
3914
3915 /* This should not happen with any sane values */
3916 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3917 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3918 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3919 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3920
3921 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003922 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 auxdiv,
3924 divsel,
3925 phasedir,
3926 phaseinc);
3927
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003928 mutex_lock(&dev_priv->sb_lock);
3929
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003931 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3933 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3934 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3935 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3936 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3937 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939
3940 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003941 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3943 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945
3946 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003947 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003949 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003951 mutex_unlock(&dev_priv->sb_lock);
3952
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 /* Wait for initialization time */
3954 udelay(24);
3955
3956 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3957}
3958
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003959int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3960{
3961 u32 divsel, phaseinc, auxdiv;
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor;
3965 u32 temp;
3966
3967 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3968 return 0;
3969
3970 mutex_lock(&dev_priv->sb_lock);
3971
3972 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3973 if (temp & SBI_SSCCTL_DISABLE) {
3974 mutex_unlock(&dev_priv->sb_lock);
3975 return 0;
3976 }
3977
3978 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3979 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3980 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3981 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3982 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3983
3984 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3985 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3986 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3987
3988 mutex_unlock(&dev_priv->sb_lock);
3989
3990 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3991
3992 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3993 desired_divisor << auxdiv);
3994}
3995
Daniel Vetter275f01b22013-05-03 11:49:47 +02003996static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3997 enum pipe pch_transcoder)
3998{
3999 struct drm_device *dev = crtc->base.dev;
4000 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004001 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004002
4003 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4004 I915_READ(HTOTAL(cpu_transcoder)));
4005 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4006 I915_READ(HBLANK(cpu_transcoder)));
4007 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4008 I915_READ(HSYNC(cpu_transcoder)));
4009
4010 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4011 I915_READ(VTOTAL(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4013 I915_READ(VBLANK(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4015 I915_READ(VSYNC(cpu_transcoder)));
4016 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4017 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4018}
4019
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004020static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004021{
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 uint32_t temp;
4024
4025 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004026 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004027 return;
4028
4029 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4030 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4031
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004032 temp &= ~FDI_BC_BIFURCATION_SELECT;
4033 if (enable)
4034 temp |= FDI_BC_BIFURCATION_SELECT;
4035
4036 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004037 I915_WRITE(SOUTH_CHICKEN1, temp);
4038 POSTING_READ(SOUTH_CHICKEN1);
4039}
4040
4041static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4042{
4043 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044
4045 switch (intel_crtc->pipe) {
4046 case PIPE_A:
4047 break;
4048 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004049 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004050 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004052 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053
4054 break;
4055 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004056 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004057
4058 break;
4059 default:
4060 BUG();
4061 }
4062}
4063
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004064/* Return which DP Port should be selected for Transcoder DP control */
4065static enum port
4066intel_trans_dp_port_sel(struct drm_crtc *crtc)
4067{
4068 struct drm_device *dev = crtc->dev;
4069 struct intel_encoder *encoder;
4070
4071 for_each_encoder_on_crtc(dev, crtc, encoder) {
4072 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4073 encoder->type == INTEL_OUTPUT_EDP)
4074 return enc_to_dig_port(&encoder->base)->port;
4075 }
4076
4077 return -1;
4078}
4079
Jesse Barnesf67a5592011-01-05 10:31:48 -08004080/*
4081 * Enable PCH resources required for PCH ports:
4082 * - PCH PLLs
4083 * - FDI training & RX/TX
4084 * - update transcoder timings
4085 * - DP transcoding bits
4086 * - transcoder
4087 */
4088static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004089{
4090 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4093 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004094 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004095
Daniel Vetterab9412b2013-05-03 11:49:46 +02004096 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004097
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004098 if (IS_IVYBRIDGE(dev))
4099 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4100
Daniel Vettercd986ab2012-10-26 10:58:12 +02004101 /* Write the TU size bits before fdi link training, so that error
4102 * detection works. */
4103 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4104 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4105
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004106 /*
4107 * Sometimes spurious CPU pipe underruns happen during FDI
4108 * training, at least with VGA+HDMI cloning. Suppress them.
4109 */
4110 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4111
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004112 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004113 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004114
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004115 /* We need to program the right clock selection before writing the pixel
4116 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004117 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004119
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004121 temp |= TRANS_DPLL_ENABLE(pipe);
4122 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004123 if (intel_crtc->config->shared_dpll ==
4124 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004125 temp |= sel;
4126 else
4127 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004131 /* XXX: pch pll's can be enabled any time before we enable the PCH
4132 * transcoder, and we actually should do this to not upset any PCH
4133 * transcoder that already use the clock when we share it.
4134 *
4135 * Note that enable_shared_dpll tries to do the right thing, but
4136 * get_shared_dpll unconditionally resets the pll - we need that to have
4137 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004138 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004139
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004140 /* set transcoder timing, panel must allow it */
4141 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004142 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004144 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004145
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004146 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4147
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004148 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004149 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004150 const struct drm_display_mode *adjusted_mode =
4151 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004152 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004153 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004154 temp = I915_READ(reg);
4155 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004156 TRANS_DP_SYNC_MASK |
4157 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004158 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004159 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004161 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004163 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004164 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165
4166 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004167 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004168 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004170 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004173 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004174 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 break;
4176 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004177 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 }
4179
Chris Wilson5eddb702010-09-11 13:48:45 +01004180 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 }
4182
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004183 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004184}
4185
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186static void lpt_pch_enable(struct drm_crtc *crtc)
4187{
4188 struct drm_device *dev = crtc->dev;
4189 struct drm_i915_private *dev_priv = dev->dev_private;
4190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004191 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004192
Daniel Vetterab9412b2013-05-03 11:49:46 +02004193 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004195 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004196
Paulo Zanoni0540e482012-10-31 18:12:40 -02004197 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004198 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Paulo Zanoni937bb612012-10-31 18:12:47 -02004200 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004201}
4202
Daniel Vettera1520312013-05-03 11:49:50 +02004203static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004204{
4205 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004206 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004207 u32 temp;
4208
4209 temp = I915_READ(dslreg);
4210 udelay(500);
4211 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004212 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004213 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004214 }
4215}
4216
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004217static int
4218skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4219 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4220 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004221{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 struct intel_crtc_scaler_state *scaler_state =
4223 &crtc_state->scaler_state;
4224 struct intel_crtc *intel_crtc =
4225 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004227
4228 need_scaling = intel_rotation_90_or_270(rotation) ?
4229 (src_h != dst_w || src_w != dst_h):
4230 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231
4232 /*
4233 * if plane is being disabled or scaler is no more required or force detach
4234 * - free scaler binded to this plane/crtc
4235 * - in order to do this, update crtc->scaler_usage
4236 *
4237 * Here scaler state in crtc_state is set free so that
4238 * scaler can be assigned to other user. Actual register
4239 * update to free the scaler is done in plane/panel-fit programming.
4240 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4241 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004243 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004244 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004245 scaler_state->scalers[*scaler_id].in_use = 0;
4246
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004247 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4248 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4249 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 scaler_state->scaler_users);
4251 *scaler_id = -1;
4252 }
4253 return 0;
4254 }
4255
4256 /* range checks */
4257 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4258 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4259
4260 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4261 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004262 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004263 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004264 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004265 return -EINVAL;
4266 }
4267
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268 /* mark this plane as a scaler user in crtc_state */
4269 scaler_state->scaler_users |= (1 << scaler_user);
4270 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4271 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4273 scaler_state->scaler_users);
4274
4275 return 0;
4276}
4277
4278/**
4279 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4280 *
4281 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004282 *
4283 * Return
4284 * 0 - scaler_usage updated successfully
4285 * error - requested scaling cannot be supported or other error condition
4286 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004287int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004288{
4289 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004290 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291
4292 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4293 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4294
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004295 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004296 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004297 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004298 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004299}
4300
4301/**
4302 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4303 *
4304 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004305 * @plane_state: atomic plane state to update
4306 *
4307 * Return
4308 * 0 - scaler_usage updated successfully
4309 * error - requested scaling cannot be supported or other error condition
4310 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004311static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4312 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004313{
4314
4315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004316 struct intel_plane *intel_plane =
4317 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004318 struct drm_framebuffer *fb = plane_state->base.fb;
4319 int ret;
4320
4321 bool force_detach = !fb || !plane_state->visible;
4322
4323 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4324 intel_plane->base.base.id, intel_crtc->pipe,
4325 drm_plane_index(&intel_plane->base));
4326
4327 ret = skl_update_scaler(crtc_state, force_detach,
4328 drm_plane_index(&intel_plane->base),
4329 &plane_state->scaler_id,
4330 plane_state->base.rotation,
4331 drm_rect_width(&plane_state->src) >> 16,
4332 drm_rect_height(&plane_state->src) >> 16,
4333 drm_rect_width(&plane_state->dst),
4334 drm_rect_height(&plane_state->dst));
4335
4336 if (ret || plane_state->scaler_id < 0)
4337 return ret;
4338
Chandra Kondurua1b22782015-04-07 15:28:45 -07004339 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004340 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004341 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004342 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004343 return -EINVAL;
4344 }
4345
4346 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004347 switch (fb->pixel_format) {
4348 case DRM_FORMAT_RGB565:
4349 case DRM_FORMAT_XBGR8888:
4350 case DRM_FORMAT_XRGB8888:
4351 case DRM_FORMAT_ABGR8888:
4352 case DRM_FORMAT_ARGB8888:
4353 case DRM_FORMAT_XRGB2101010:
4354 case DRM_FORMAT_XBGR2101010:
4355 case DRM_FORMAT_YUYV:
4356 case DRM_FORMAT_YVYU:
4357 case DRM_FORMAT_UYVY:
4358 case DRM_FORMAT_VYUY:
4359 break;
4360 default:
4361 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4362 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4363 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004364 }
4365
Chandra Kondurua1b22782015-04-07 15:28:45 -07004366 return 0;
4367}
4368
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004369static void skylake_scaler_disable(struct intel_crtc *crtc)
4370{
4371 int i;
4372
4373 for (i = 0; i < crtc->num_scalers; i++)
4374 skl_detach_scaler(crtc, i);
4375}
4376
4377static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004378{
4379 struct drm_device *dev = crtc->base.dev;
4380 struct drm_i915_private *dev_priv = dev->dev_private;
4381 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004382 struct intel_crtc_scaler_state *scaler_state =
4383 &crtc->config->scaler_state;
4384
4385 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4386
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004387 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004388 int id;
4389
4390 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4391 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4392 return;
4393 }
4394
4395 id = scaler_state->scaler_id;
4396 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4397 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4398 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4399 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4400
4401 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004402 }
4403}
4404
Jesse Barnesb074cec2013-04-25 12:55:02 -07004405static void ironlake_pfit_enable(struct intel_crtc *crtc)
4406{
4407 struct drm_device *dev = crtc->base.dev;
4408 struct drm_i915_private *dev_priv = dev->dev_private;
4409 int pipe = crtc->pipe;
4410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004411 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004412 /* Force use of hard-coded filter coefficients
4413 * as some pre-programmed values are broken,
4414 * e.g. x201.
4415 */
4416 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4417 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4418 PF_PIPE_SEL_IVB(pipe));
4419 else
4420 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004421 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4422 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004423 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004424}
4425
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004426void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004427{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004428 struct drm_device *dev = crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004430
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004431 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004432 return;
4433
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004434 /* We can only enable IPS after we enable a plane and wait for a vblank */
4435 intel_wait_for_vblank(dev, crtc->pipe);
4436
Paulo Zanonid77e4532013-09-24 13:52:55 -03004437 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004438 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004439 mutex_lock(&dev_priv->rps.hw_lock);
4440 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4441 mutex_unlock(&dev_priv->rps.hw_lock);
4442 /* Quoting Art Runyan: "its not safe to expect any particular
4443 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004444 * mailbox." Moreover, the mailbox may return a bogus state,
4445 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004446 */
4447 } else {
4448 I915_WRITE(IPS_CTL, IPS_ENABLE);
4449 /* The bit only becomes 1 in the next vblank, so this wait here
4450 * is essentially intel_wait_for_vblank. If we don't have this
4451 * and don't wait for vblanks until the end of crtc_enable, then
4452 * the HW state readout code will complain that the expected
4453 * IPS_CTL value is not the one we read. */
4454 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4455 DRM_ERROR("Timed out waiting for IPS enable\n");
4456 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004457}
4458
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004459void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004460{
4461 struct drm_device *dev = crtc->base.dev;
4462 struct drm_i915_private *dev_priv = dev->dev_private;
4463
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004464 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004465 return;
4466
4467 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004468 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004469 mutex_lock(&dev_priv->rps.hw_lock);
4470 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4471 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004472 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4473 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4474 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004475 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004476 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004477 POSTING_READ(IPS_CTL);
4478 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004479
4480 /* We need to wait for a vblank before we can disable the plane. */
4481 intel_wait_for_vblank(dev, crtc->pipe);
4482}
4483
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004484static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004485{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004486 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004487 struct drm_device *dev = intel_crtc->base.dev;
4488 struct drm_i915_private *dev_priv = dev->dev_private;
4489
4490 mutex_lock(&dev->struct_mutex);
4491 dev_priv->mm.interruptible = false;
4492 (void) intel_overlay_switch_off(intel_crtc->overlay);
4493 dev_priv->mm.interruptible = true;
4494 mutex_unlock(&dev->struct_mutex);
4495 }
4496
4497 /* Let userspace switch the overlay on again. In most cases userspace
4498 * has to recompute where to put it anyway.
4499 */
4500}
4501
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004502/**
4503 * intel_post_enable_primary - Perform operations after enabling primary plane
4504 * @crtc: the CRTC whose primary plane was just enabled
4505 *
4506 * Performs potentially sleeping operations that must be done after the primary
4507 * plane is enabled, such as updating FBC and IPS. Note that this may be
4508 * called due to an explicit primary plane update, or due to an implicit
4509 * re-enable that is caused when a sprite plane is updated to no longer
4510 * completely hide the primary plane.
4511 */
4512static void
4513intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004514{
4515 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004516 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4518 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004519
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004520 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004521 * FIXME IPS should be fine as long as one plane is
4522 * enabled, but in practice it seems to have problems
4523 * when going from primary only to sprite only and vice
4524 * versa.
4525 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004526 hsw_enable_ips(intel_crtc);
4527
Daniel Vetterf99d7062014-06-19 16:01:59 +02004528 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004529 * Gen2 reports pipe underruns whenever all planes are disabled.
4530 * So don't enable underrun reporting before at least some planes
4531 * are enabled.
4532 * FIXME: Need to fix the logic to work when we turn off all planes
4533 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004534 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004535 if (IS_GEN2(dev))
4536 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4537
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004538 /* Underruns don't always raise interrupts, so check manually. */
4539 intel_check_cpu_fifo_underruns(dev_priv);
4540 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004541}
4542
Ville Syrjälä2622a082016-03-09 19:07:26 +02004543/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004544static void
4545intel_pre_disable_primary(struct drm_crtc *crtc)
4546{
4547 struct drm_device *dev = crtc->dev;
4548 struct drm_i915_private *dev_priv = dev->dev_private;
4549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 int pipe = intel_crtc->pipe;
4551
4552 /*
4553 * Gen2 reports pipe underruns whenever all planes are disabled.
4554 * So diasble underrun reporting before all the planes get disabled.
4555 * FIXME: Need to fix the logic to work when we turn off all planes
4556 * but leave the pipe running.
4557 */
4558 if (IS_GEN2(dev))
4559 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4560
4561 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004562 * FIXME IPS should be fine as long as one plane is
4563 * enabled, but in practice it seems to have problems
4564 * when going from primary only to sprite only and vice
4565 * versa.
4566 */
4567 hsw_disable_ips(intel_crtc);
4568}
4569
4570/* FIXME get rid of this and use pre_plane_update */
4571static void
4572intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4573{
4574 struct drm_device *dev = crtc->dev;
4575 struct drm_i915_private *dev_priv = dev->dev_private;
4576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4577 int pipe = intel_crtc->pipe;
4578
4579 intel_pre_disable_primary(crtc);
4580
4581 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004582 * Vblank time updates from the shadow to live plane control register
4583 * are blocked if the memory self-refresh mode is active at that
4584 * moment. So to make sure the plane gets truly disabled, disable
4585 * first the self-refresh mode. The self-refresh enable bit in turn
4586 * will be checked/applied by the HW only at the next frame start
4587 * event which is after the vblank start event, so we need to have a
4588 * wait-for-vblank between disabling the plane and the pipe.
4589 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004590 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004591 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004592 dev_priv->wm.vlv.cxsr = false;
4593 intel_wait_for_vblank(dev, pipe);
4594 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004595}
4596
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004597static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004598{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004599 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4600 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004601 struct intel_crtc_state *pipe_config =
4602 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004603 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004604 struct drm_plane *primary = crtc->base.primary;
4605 struct drm_plane_state *old_pri_state =
4606 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004607
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004608 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004609
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004610 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004611
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004612 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004613 intel_update_watermarks(&crtc->base);
4614
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004615 if (old_pri_state) {
4616 struct intel_plane_state *primary_state =
4617 to_intel_plane_state(primary->state);
4618 struct intel_plane_state *old_primary_state =
4619 to_intel_plane_state(old_pri_state);
4620
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004621 intel_fbc_post_update(crtc);
4622
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004623 if (primary_state->visible &&
4624 (needs_modeset(&pipe_config->base) ||
4625 !old_primary_state->visible))
4626 intel_post_enable_primary(&crtc->base);
4627 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004628}
4629
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004630static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004631{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004632 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004633 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004634 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004635 struct intel_crtc_state *pipe_config =
4636 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004637 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4638 struct drm_plane *primary = crtc->base.primary;
4639 struct drm_plane_state *old_pri_state =
4640 drm_atomic_get_existing_plane_state(old_state, primary);
4641 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004642
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004643 if (old_pri_state) {
4644 struct intel_plane_state *primary_state =
4645 to_intel_plane_state(primary->state);
4646 struct intel_plane_state *old_primary_state =
4647 to_intel_plane_state(old_pri_state);
4648
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004649 intel_fbc_pre_update(crtc);
4650
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004651 if (old_primary_state->visible &&
4652 (modeset || !primary_state->visible))
4653 intel_pre_disable_primary(&crtc->base);
4654 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004655
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004656 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004657 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004658
Ville Syrjälä2622a082016-03-09 19:07:26 +02004659 /*
4660 * Vblank time updates from the shadow to live plane control register
4661 * are blocked if the memory self-refresh mode is active at that
4662 * moment. So to make sure the plane gets truly disabled, disable
4663 * first the self-refresh mode. The self-refresh enable bit in turn
4664 * will be checked/applied by the HW only at the next frame start
4665 * event which is after the vblank start event, so we need to have a
4666 * wait-for-vblank between disabling the plane and the pipe.
4667 */
4668 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004669 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004670 dev_priv->wm.vlv.cxsr = false;
4671 intel_wait_for_vblank(dev, crtc->pipe);
4672 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004673 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004674
Matt Ropered4a6a72016-02-23 17:20:13 -08004675 /*
4676 * IVB workaround: must disable low power watermarks for at least
4677 * one frame before enabling scaling. LP watermarks can be re-enabled
4678 * when scaling is disabled.
4679 *
4680 * WaCxSRDisabledForSpriteScaling:ivb
4681 */
4682 if (pipe_config->disable_lp_wm) {
4683 ilk_disable_lp_wm(dev);
4684 intel_wait_for_vblank(dev, crtc->pipe);
4685 }
4686
4687 /*
4688 * If we're doing a modeset, we're done. No need to do any pre-vblank
4689 * watermark programming here.
4690 */
4691 if (needs_modeset(&pipe_config->base))
4692 return;
4693
4694 /*
4695 * For platforms that support atomic watermarks, program the
4696 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4697 * will be the intermediate values that are safe for both pre- and
4698 * post- vblank; when vblank happens, the 'active' values will be set
4699 * to the final 'target' values and we'll do this again to get the
4700 * optimal watermarks. For gen9+ platforms, the values we program here
4701 * will be the final target values which will get automatically latched
4702 * at vblank time; no further programming will be necessary.
4703 *
4704 * If a platform hasn't been transitioned to atomic watermarks yet,
4705 * we'll continue to update watermarks the old way, if flags tell
4706 * us to.
4707 */
4708 if (dev_priv->display.initial_watermarks != NULL)
4709 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004710 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004711 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004712}
4713
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004714static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004715{
4716 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004718 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004719 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004720
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004721 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004722
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004723 drm_for_each_plane_mask(p, dev, plane_mask)
4724 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004725
Daniel Vetterf99d7062014-06-19 16:01:59 +02004726 /*
4727 * FIXME: Once we grow proper nuclear flip support out of this we need
4728 * to compute the mask of flip planes precisely. For the time being
4729 * consider this a flip to a NULL plane.
4730 */
4731 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004732}
4733
Jesse Barnesf67a5592011-01-05 10:31:48 -08004734static void ironlake_crtc_enable(struct drm_crtc *crtc)
4735{
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004739 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004740 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004741
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004742 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004743 return;
4744
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004745 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004746 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4747
4748 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004749 intel_prepare_shared_dpll(intel_crtc);
4750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304752 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004753
4754 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004755 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004757 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004758 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004759 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004760 }
4761
4762 ironlake_set_pipeconf(crtc);
4763
Jesse Barnesf67a5592011-01-05 10:31:48 -08004764 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004765
Daniel Vettera72e4c92014-09-30 10:56:47 +02004766 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004767
Daniel Vetterf6736a12013-06-05 13:34:30 +02004768 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004769 if (encoder->pre_enable)
4770 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004771
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004772 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004773 /* Note: FDI PLL enabling _must_ be done before we enable the
4774 * cpu pipes, hence this is separate from all the other fdi/pch
4775 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004776 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004777 } else {
4778 assert_fdi_tx_disabled(dev_priv, pipe);
4779 assert_fdi_rx_disabled(dev_priv, pipe);
4780 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004781
Jesse Barnesb074cec2013-04-25 12:55:02 -07004782 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004783
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004784 /*
4785 * On ILK+ LUT must be loaded before the pipe is running but with
4786 * clocks enabled
4787 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004788 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004789
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004790 if (dev_priv->display.initial_watermarks != NULL)
4791 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004792 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004793
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004794 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004795 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004796
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004797 assert_vblank_disabled(crtc);
4798 drm_crtc_vblank_on(crtc);
4799
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004800 for_each_encoder_on_crtc(dev, crtc, encoder)
4801 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004802
4803 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004804 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004805
4806 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4807 if (intel_crtc->config->has_pch_encoder)
4808 intel_wait_for_vblank(dev, pipe);
4809 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004810}
4811
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004812/* IPS only exists on ULT machines and is tied to pipe A. */
4813static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4814{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004815 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004816}
4817
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004818static void haswell_crtc_enable(struct drm_crtc *crtc)
4819{
4820 struct drm_device *dev = crtc->dev;
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4823 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004824 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004825 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004826 struct intel_crtc_state *pipe_config =
4827 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004828
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004829 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004830 return;
4831
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004832 if (intel_crtc->config->has_pch_encoder)
4833 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4834 false);
4835
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004836 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004837 intel_enable_shared_dpll(intel_crtc);
4838
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004839 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304840 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004841
Jani Nikula4d1de972016-03-18 17:05:42 +02004842 if (!intel_crtc->config->has_dsi_encoder)
4843 intel_set_pipe_timings(intel_crtc);
4844
Jani Nikulabc58be62016-03-18 17:05:39 +02004845 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004846
Jani Nikula4d1de972016-03-18 17:05:42 +02004847 if (cpu_transcoder != TRANSCODER_EDP &&
4848 !transcoder_is_dsi(cpu_transcoder)) {
4849 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004850 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004851 }
4852
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004853 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004854 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004855 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004856 }
4857
Jani Nikula4d1de972016-03-18 17:05:42 +02004858 if (!intel_crtc->config->has_dsi_encoder)
4859 haswell_set_pipeconf(crtc);
4860
Jani Nikula391bf042016-03-18 17:05:40 +02004861 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004862
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004863 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004864
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004865 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004866
Daniel Vetter6b698512015-11-28 11:05:39 +01004867 if (intel_crtc->config->has_pch_encoder)
4868 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4869 else
4870 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4871
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304872 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873 if (encoder->pre_enable)
4874 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304875 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004876
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004877 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004878 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004879
Jani Nikulaa65347b2015-11-27 12:21:46 +02004880 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304881 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004883 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004884 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004885 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004886 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004887
4888 /*
4889 * On ILK+ LUT must be loaded before the pipe is running but with
4890 * clocks enabled
4891 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004892 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004893
Paulo Zanoni1f544382012-10-24 11:32:00 -02004894 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004895 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304896 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004898 if (dev_priv->display.initial_watermarks != NULL)
4899 dev_priv->display.initial_watermarks(pipe_config);
4900 else
4901 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004902
4903 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4904 if (!intel_crtc->config->has_dsi_encoder)
4905 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004906
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004907 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004908 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909
Jani Nikulaa65347b2015-11-27 12:21:46 +02004910 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004911 intel_ddi_set_vc_payload_alloc(crtc, true);
4912
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004913 assert_vblank_disabled(crtc);
4914 drm_crtc_vblank_on(crtc);
4915
Jani Nikula8807e552013-08-30 19:40:32 +03004916 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004918 intel_opregion_notify_encoder(encoder, true);
4919 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920
Daniel Vetter6b698512015-11-28 11:05:39 +01004921 if (intel_crtc->config->has_pch_encoder) {
4922 intel_wait_for_vblank(dev, pipe);
4923 intel_wait_for_vblank(dev, pipe);
4924 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004925 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4926 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004927 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004928
Paulo Zanonie4916942013-09-20 16:21:19 -03004929 /* If we change the relative order between pipe/planes enabling, we need
4930 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004931 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4932 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4933 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4934 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4935 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004936}
4937
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004938static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004939{
4940 struct drm_device *dev = crtc->base.dev;
4941 struct drm_i915_private *dev_priv = dev->dev_private;
4942 int pipe = crtc->pipe;
4943
4944 /* To avoid upsetting the power well on haswell only disable the pfit if
4945 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004946 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004947 I915_WRITE(PF_CTL(pipe), 0);
4948 I915_WRITE(PF_WIN_POS(pipe), 0);
4949 I915_WRITE(PF_WIN_SZ(pipe), 0);
4950 }
4951}
4952
Jesse Barnes6be4a602010-09-10 10:26:01 -07004953static void ironlake_crtc_disable(struct drm_crtc *crtc)
4954{
4955 struct drm_device *dev = crtc->dev;
4956 struct drm_i915_private *dev_priv = dev->dev_private;
4957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004958 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004959 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004960
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004961 if (intel_crtc->config->has_pch_encoder)
4962 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4963
Daniel Vetterea9d7582012-07-10 10:42:52 +02004964 for_each_encoder_on_crtc(dev, crtc, encoder)
4965 encoder->disable(encoder);
4966
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004967 drm_crtc_vblank_off(crtc);
4968 assert_vblank_disabled(crtc);
4969
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004970 /*
4971 * Sometimes spurious CPU pipe underruns happen when the
4972 * pipe is already disabled, but FDI RX/TX is still enabled.
4973 * Happens at least with VGA+HDMI cloning. Suppress them.
4974 */
4975 if (intel_crtc->config->has_pch_encoder)
4976 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4977
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004978 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004979
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004980 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004981
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004982 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004983 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004984 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4985 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004986
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004987 for_each_encoder_on_crtc(dev, crtc, encoder)
4988 if (encoder->post_disable)
4989 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004992 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004993
Daniel Vetterd925c592013-06-05 13:34:04 +02004994 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004995 i915_reg_t reg;
4996 u32 temp;
4997
Daniel Vetterd925c592013-06-05 13:34:04 +02004998 /* disable TRANS_DP_CTL */
4999 reg = TRANS_DP_CTL(pipe);
5000 temp = I915_READ(reg);
5001 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5002 TRANS_DP_PORT_SEL_MASK);
5003 temp |= TRANS_DP_PORT_SEL_NONE;
5004 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005005
Daniel Vetterd925c592013-06-05 13:34:04 +02005006 /* disable DPLL_SEL */
5007 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005008 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005009 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005010 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005011
Daniel Vetterd925c592013-06-05 13:34:04 +02005012 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005014
5015 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016}
5017
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018static void haswell_crtc_disable(struct drm_crtc *crtc)
5019{
5020 struct drm_device *dev = crtc->dev;
5021 struct drm_i915_private *dev_priv = dev->dev_private;
5022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5023 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005024 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005026 if (intel_crtc->config->has_pch_encoder)
5027 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5028 false);
5029
Jani Nikula8807e552013-08-30 19:40:32 +03005030 for_each_encoder_on_crtc(dev, crtc, encoder) {
5031 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005032 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005033 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005035 drm_crtc_vblank_off(crtc);
5036 assert_vblank_disabled(crtc);
5037
Jani Nikula4d1de972016-03-18 17:05:42 +02005038 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5039 if (!intel_crtc->config->has_dsi_encoder)
5040 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005042 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005043 intel_ddi_set_vc_payload_alloc(crtc, false);
5044
Jani Nikulaa65347b2015-11-27 12:21:46 +02005045 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305046 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005048 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005049 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005050 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005051 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052
Jani Nikulaa65347b2015-11-27 12:21:46 +02005053 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305054 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
Imre Deak97b040a2014-06-25 22:01:50 +03005056 for_each_encoder_on_crtc(dev, crtc, encoder)
5057 if (encoder->post_disable)
5058 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005059
Ville Syrjälä92966a32015-12-08 16:05:48 +02005060 if (intel_crtc->config->has_pch_encoder) {
5061 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005062 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005063 intel_ddi_fdi_disable(crtc);
5064
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005065 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5066 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005067 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068}
5069
Jesse Barnes2dd24552013-04-25 12:55:01 -07005070static void i9xx_pfit_enable(struct intel_crtc *crtc)
5071{
5072 struct drm_device *dev = crtc->base.dev;
5073 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005074 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005075
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005076 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077 return;
5078
Daniel Vetterc0b03412013-05-28 12:05:54 +02005079 /*
5080 * The panel fitter should only be adjusted whilst the pipe is disabled,
5081 * according to register description and PRM.
5082 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005083 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5084 assert_pipe_disabled(dev_priv, crtc->pipe);
5085
Jesse Barnesb074cec2013-04-25 12:55:02 -07005086 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5087 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005088
5089 /* Border color in case we don't scale up to the full screen. Black by
5090 * default, change to something else for debugging. */
5091 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005092}
5093
Dave Airlied05410f2014-06-05 13:22:59 +10005094static enum intel_display_power_domain port_to_power_domain(enum port port)
5095{
5096 switch (port) {
5097 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005098 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005099 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005100 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005101 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005102 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005103 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005104 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005105 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005106 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005107 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005108 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005109 return POWER_DOMAIN_PORT_OTHER;
5110 }
5111}
5112
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005113static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5114{
5115 switch (port) {
5116 case PORT_A:
5117 return POWER_DOMAIN_AUX_A;
5118 case PORT_B:
5119 return POWER_DOMAIN_AUX_B;
5120 case PORT_C:
5121 return POWER_DOMAIN_AUX_C;
5122 case PORT_D:
5123 return POWER_DOMAIN_AUX_D;
5124 case PORT_E:
5125 /* FIXME: Check VBT for actual wiring of PORT E */
5126 return POWER_DOMAIN_AUX_D;
5127 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005128 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005129 return POWER_DOMAIN_AUX_A;
5130 }
5131}
5132
Imre Deak319be8a2014-03-04 19:22:57 +02005133enum intel_display_power_domain
5134intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005135{
Imre Deak319be8a2014-03-04 19:22:57 +02005136 struct drm_device *dev = intel_encoder->base.dev;
5137 struct intel_digital_port *intel_dig_port;
5138
5139 switch (intel_encoder->type) {
5140 case INTEL_OUTPUT_UNKNOWN:
5141 /* Only DDI platforms should ever use this output type */
5142 WARN_ON_ONCE(!HAS_DDI(dev));
5143 case INTEL_OUTPUT_DISPLAYPORT:
5144 case INTEL_OUTPUT_HDMI:
5145 case INTEL_OUTPUT_EDP:
5146 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005147 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005148 case INTEL_OUTPUT_DP_MST:
5149 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5150 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005151 case INTEL_OUTPUT_ANALOG:
5152 return POWER_DOMAIN_PORT_CRT;
5153 case INTEL_OUTPUT_DSI:
5154 return POWER_DOMAIN_PORT_DSI;
5155 default:
5156 return POWER_DOMAIN_PORT_OTHER;
5157 }
5158}
5159
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005160enum intel_display_power_domain
5161intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5162{
5163 struct drm_device *dev = intel_encoder->base.dev;
5164 struct intel_digital_port *intel_dig_port;
5165
5166 switch (intel_encoder->type) {
5167 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005168 case INTEL_OUTPUT_HDMI:
5169 /*
5170 * Only DDI platforms should ever use these output types.
5171 * We can get here after the HDMI detect code has already set
5172 * the type of the shared encoder. Since we can't be sure
5173 * what's the status of the given connectors, play safe and
5174 * run the DP detection too.
5175 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005176 WARN_ON_ONCE(!HAS_DDI(dev));
5177 case INTEL_OUTPUT_DISPLAYPORT:
5178 case INTEL_OUTPUT_EDP:
5179 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5180 return port_to_aux_power_domain(intel_dig_port->port);
5181 case INTEL_OUTPUT_DP_MST:
5182 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5183 return port_to_aux_power_domain(intel_dig_port->port);
5184 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005185 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005186 return POWER_DOMAIN_AUX_A;
5187 }
5188}
5189
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005190static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5191 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005192{
5193 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005194 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5196 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005197 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005198 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005199
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005201 return 0;
5202
Imre Deak77d22dc2014-03-05 16:20:52 +02005203 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5204 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005205 if (crtc_state->pch_pfit.enabled ||
5206 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5208
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005209 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5210 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5211
Imre Deak319be8a2014-03-04 19:22:57 +02005212 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005213 }
Imre Deak319be8a2014-03-04 19:22:57 +02005214
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005215 if (crtc_state->shared_dpll)
5216 mask |= BIT(POWER_DOMAIN_PLLS);
5217
Imre Deak77d22dc2014-03-05 16:20:52 +02005218 return mask;
5219}
5220
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005221static unsigned long
5222modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5223 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005224{
5225 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5227 enum intel_display_power_domain domain;
5228 unsigned long domains, new_domains, old_domains;
5229
5230 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005231 intel_crtc->enabled_power_domains = new_domains =
5232 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005233
5234 domains = new_domains & ~old_domains;
5235
5236 for_each_power_domain(domain, domains)
5237 intel_display_power_get(dev_priv, domain);
5238
5239 return old_domains & ~new_domains;
5240}
5241
5242static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5243 unsigned long domains)
5244{
5245 enum intel_display_power_domain domain;
5246
5247 for_each_power_domain(domain, domains)
5248 intel_display_power_put(dev_priv, domain);
5249}
5250
Mika Kaholaadafdc62015-08-18 14:36:59 +03005251static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5252{
5253 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5254
5255 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5256 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5257 return max_cdclk_freq;
5258 else if (IS_CHERRYVIEW(dev_priv))
5259 return max_cdclk_freq*95/100;
5260 else if (INTEL_INFO(dev_priv)->gen < 4)
5261 return 2*max_cdclk_freq*90/100;
5262 else
5263 return max_cdclk_freq*90/100;
5264}
5265
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005266static void intel_update_max_cdclk(struct drm_device *dev)
5267{
5268 struct drm_i915_private *dev_priv = dev->dev_private;
5269
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005270 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005271 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5272
5273 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5274 dev_priv->max_cdclk_freq = 675000;
5275 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5276 dev_priv->max_cdclk_freq = 540000;
5277 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5278 dev_priv->max_cdclk_freq = 450000;
5279 else
5280 dev_priv->max_cdclk_freq = 337500;
5281 } else if (IS_BROADWELL(dev)) {
5282 /*
5283 * FIXME with extra cooling we can allow
5284 * 540 MHz for ULX and 675 Mhz for ULT.
5285 * How can we know if extra cooling is
5286 * available? PCI ID, VTB, something else?
5287 */
5288 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5289 dev_priv->max_cdclk_freq = 450000;
5290 else if (IS_BDW_ULX(dev))
5291 dev_priv->max_cdclk_freq = 450000;
5292 else if (IS_BDW_ULT(dev))
5293 dev_priv->max_cdclk_freq = 540000;
5294 else
5295 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005296 } else if (IS_CHERRYVIEW(dev)) {
5297 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005298 } else if (IS_VALLEYVIEW(dev)) {
5299 dev_priv->max_cdclk_freq = 400000;
5300 } else {
5301 /* otherwise assume cdclk is fixed */
5302 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5303 }
5304
Mika Kaholaadafdc62015-08-18 14:36:59 +03005305 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5306
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005307 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5308 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005309
5310 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5311 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005312}
5313
5314static void intel_update_cdclk(struct drm_device *dev)
5315{
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
5321
5322 /*
5323 * Program the gmbus_freq based on the cdclk frequency.
5324 * BSpec erroneously claims we should aim for 4MHz, but
5325 * in fact 1MHz is the correct frequency.
5326 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005327 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005328 /*
5329 * Program the gmbus_freq based on the cdclk frequency.
5330 * BSpec erroneously claims we should aim for 4MHz, but
5331 * in fact 1MHz is the correct frequency.
5332 */
5333 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5334 }
5335
5336 if (dev_priv->max_cdclk_freq == 0)
5337 intel_update_max_cdclk(dev);
5338}
5339
Damien Lespiau70d0c572015-06-04 18:21:29 +01005340static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343 uint32_t divider;
5344 uint32_t ratio;
5345 uint32_t current_freq;
5346 int ret;
5347
5348 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5349 switch (frequency) {
5350 case 144000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 288000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 384000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5360 ratio = BXT_DE_PLL_RATIO(60);
5361 break;
5362 case 576000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 624000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5368 ratio = BXT_DE_PLL_RATIO(65);
5369 break;
5370 case 19200:
5371 /*
5372 * Bypass frequency with DE PLL disabled. Init ratio, divider
5373 * to suppress GCC warning.
5374 */
5375 ratio = 0;
5376 divider = 0;
5377 break;
5378 default:
5379 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5380
5381 return;
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 /* Inform power controller of upcoming frequency change */
5386 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5387 0x80000000);
5388 mutex_unlock(&dev_priv->rps.hw_lock);
5389
5390 if (ret) {
5391 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5392 ret, frequency);
5393 return;
5394 }
5395
5396 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5397 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5398 current_freq = current_freq * 500 + 1000;
5399
5400 /*
5401 * DE PLL has to be disabled when
5402 * - setting to 19.2MHz (bypass, PLL isn't used)
5403 * - before setting to 624MHz (PLL needs toggling)
5404 * - before setting to any frequency from 624MHz (PLL needs toggling)
5405 */
5406 if (frequency == 19200 || frequency == 624000 ||
5407 current_freq == 624000) {
5408 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5409 /* Timeout 200us */
5410 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5411 1))
5412 DRM_ERROR("timout waiting for DE PLL unlock\n");
5413 }
5414
5415 if (frequency != 19200) {
5416 uint32_t val;
5417
5418 val = I915_READ(BXT_DE_PLL_CTL);
5419 val &= ~BXT_DE_PLL_RATIO_MASK;
5420 val |= ratio;
5421 I915_WRITE(BXT_DE_PLL_CTL, val);
5422
5423 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5424 /* Timeout 200us */
5425 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5426 DRM_ERROR("timeout waiting for DE PLL lock\n");
5427
5428 val = I915_READ(CDCLK_CTL);
5429 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5430 val |= divider;
5431 /*
5432 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5433 * enable otherwise.
5434 */
5435 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5436 if (frequency >= 500000)
5437 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5438
5439 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5440 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5441 val |= (frequency - 1000) / 500;
5442 I915_WRITE(CDCLK_CTL, val);
5443 }
5444
5445 mutex_lock(&dev_priv->rps.hw_lock);
5446 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5447 DIV_ROUND_UP(frequency, 25000));
5448 mutex_unlock(&dev_priv->rps.hw_lock);
5449
5450 if (ret) {
5451 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5452 ret, frequency);
5453 return;
5454 }
5455
Damien Lespiaua47871b2015-06-04 18:21:34 +01005456 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305457}
5458
5459void broxton_init_cdclk(struct drm_device *dev)
5460{
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462 uint32_t val;
5463
5464 /*
5465 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5466 * or else the reset will hang because there is no PCH to respond.
5467 * Move the handshake programming to initialization sequence.
5468 * Previously was left up to BIOS.
5469 */
5470 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5471 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5472 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5473
5474 /* Enable PG1 for cdclk */
5475 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5476
5477 /* check if cd clock is enabled */
5478 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5479 DRM_DEBUG_KMS("Display already initialized\n");
5480 return;
5481 }
5482
5483 /*
5484 * FIXME:
5485 * - The initial CDCLK needs to be read from VBT.
5486 * Need to make this change after VBT has changes for BXT.
5487 * - check if setting the max (or any) cdclk freq is really necessary
5488 * here, it belongs to modeset time
5489 */
5490 broxton_set_cdclk(dev, 624000);
5491
5492 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005493 POSTING_READ(DBUF_CTL);
5494
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 udelay(10);
5496
5497 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5498 DRM_ERROR("DBuf power enable timeout!\n");
5499}
5500
5501void broxton_uninit_cdclk(struct drm_device *dev)
5502{
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5504
5505 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005506 POSTING_READ(DBUF_CTL);
5507
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305508 udelay(10);
5509
5510 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5511 DRM_ERROR("DBuf power disable timeout!\n");
5512
5513 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5514 broxton_set_cdclk(dev, 19200);
5515
5516 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5517}
5518
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005519static const struct skl_cdclk_entry {
5520 unsigned int freq;
5521 unsigned int vco;
5522} skl_cdclk_frequencies[] = {
5523 { .freq = 308570, .vco = 8640 },
5524 { .freq = 337500, .vco = 8100 },
5525 { .freq = 432000, .vco = 8640 },
5526 { .freq = 450000, .vco = 8100 },
5527 { .freq = 540000, .vco = 8100 },
5528 { .freq = 617140, .vco = 8640 },
5529 { .freq = 675000, .vco = 8100 },
5530};
5531
5532static unsigned int skl_cdclk_decimal(unsigned int freq)
5533{
5534 return (freq - 1000) / 500;
5535}
5536
5537static unsigned int skl_cdclk_get_vco(unsigned int freq)
5538{
5539 unsigned int i;
5540
5541 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5542 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5543
5544 if (e->freq == freq)
5545 return e->vco;
5546 }
5547
5548 return 8100;
5549}
5550
5551static void
5552skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5553{
5554 unsigned int min_freq;
5555 u32 val;
5556
5557 /* select the minimum CDCLK before enabling DPLL 0 */
5558 val = I915_READ(CDCLK_CTL);
5559 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5560 val |= CDCLK_FREQ_337_308;
5561
5562 if (required_vco == 8640)
5563 min_freq = 308570;
5564 else
5565 min_freq = 337500;
5566
5567 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5568
5569 I915_WRITE(CDCLK_CTL, val);
5570 POSTING_READ(CDCLK_CTL);
5571
5572 /*
5573 * We always enable DPLL0 with the lowest link rate possible, but still
5574 * taking into account the VCO required to operate the eDP panel at the
5575 * desired frequency. The usual DP link rates operate with a VCO of
5576 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5577 * The modeset code is responsible for the selection of the exact link
5578 * rate later on, with the constraint of choosing a frequency that
5579 * works with required_vco.
5580 */
5581 val = I915_READ(DPLL_CTRL1);
5582
5583 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5585 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5586 if (required_vco == 8640)
5587 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5588 SKL_DPLL0);
5589 else
5590 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5591 SKL_DPLL0);
5592
5593 I915_WRITE(DPLL_CTRL1, val);
5594 POSTING_READ(DPLL_CTRL1);
5595
5596 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5597
5598 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5599 DRM_ERROR("DPLL0 not locked\n");
5600}
5601
5602static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5603{
5604 int ret;
5605 u32 val;
5606
5607 /* inform PCU we want to change CDCLK */
5608 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5609 mutex_lock(&dev_priv->rps.hw_lock);
5610 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5611 mutex_unlock(&dev_priv->rps.hw_lock);
5612
5613 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5614}
5615
5616static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < 15; i++) {
5621 if (skl_cdclk_pcu_ready(dev_priv))
5622 return true;
5623 udelay(10);
5624 }
5625
5626 return false;
5627}
5628
5629static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5630{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005631 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005632 u32 freq_select, pcu_ack;
5633
5634 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5635
5636 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5637 DRM_ERROR("failed to inform PCU about cdclk change\n");
5638 return;
5639 }
5640
5641 /* set CDCLK_CTL */
5642 switch(freq) {
5643 case 450000:
5644 case 432000:
5645 freq_select = CDCLK_FREQ_450_432;
5646 pcu_ack = 1;
5647 break;
5648 case 540000:
5649 freq_select = CDCLK_FREQ_540;
5650 pcu_ack = 2;
5651 break;
5652 case 308570:
5653 case 337500:
5654 default:
5655 freq_select = CDCLK_FREQ_337_308;
5656 pcu_ack = 0;
5657 break;
5658 case 617140:
5659 case 675000:
5660 freq_select = CDCLK_FREQ_675_617;
5661 pcu_ack = 3;
5662 break;
5663 }
5664
5665 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5666 POSTING_READ(CDCLK_CTL);
5667
5668 /* inform PCU of the change */
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5671 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005672
5673 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005674}
5675
5676void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5677{
5678 /* disable DBUF power */
5679 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5680 POSTING_READ(DBUF_CTL);
5681
5682 udelay(10);
5683
5684 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5685 DRM_ERROR("DBuf power disable timeout\n");
5686
Imre Deakab96c1ee2015-11-04 19:24:18 +02005687 /* disable DPLL0 */
5688 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5689 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5690 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005691}
5692
5693void skl_init_cdclk(struct drm_i915_private *dev_priv)
5694{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695 unsigned int required_vco;
5696
Gary Wang39d9b852015-08-28 16:40:34 +08005697 /* DPLL0 not enabled (happens on early BIOS versions) */
5698 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5699 /* enable DPLL0 */
5700 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5701 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005702 }
5703
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005704 /* set CDCLK to the frequency the BIOS chose */
5705 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5706
5707 /* enable DBUF power */
5708 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5709 POSTING_READ(DBUF_CTL);
5710
5711 udelay(10);
5712
5713 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5714 DRM_ERROR("DBuf power enable timeout\n");
5715}
5716
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305717int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5718{
5719 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5720 uint32_t cdctl = I915_READ(CDCLK_CTL);
5721 int freq = dev_priv->skl_boot_cdclk;
5722
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305723 /*
5724 * check if the pre-os intialized the display
5725 * There is SWF18 scratchpad register defined which is set by the
5726 * pre-os which can be used by the OS drivers to check the status
5727 */
5728 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5729 goto sanitize;
5730
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305731 /* Is PLL enabled and locked ? */
5732 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5733 goto sanitize;
5734
5735 /* DPLL okay; verify the cdclock
5736 *
5737 * Noticed in some instances that the freq selection is correct but
5738 * decimal part is programmed wrong from BIOS where pre-os does not
5739 * enable display. Verify the same as well.
5740 */
5741 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5742 /* All well; nothing to sanitize */
5743 return false;
5744sanitize:
5745 /*
5746 * As of now initialize with max cdclk till
5747 * we get dynamic cdclk support
5748 * */
5749 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5750 skl_init_cdclk(dev_priv);
5751
5752 /* we did have to sanitize */
5753 return true;
5754}
5755
Jesse Barnes30a970c2013-11-04 13:48:12 -08005756/* Adjust CDclk dividers to allow high res or save power if possible */
5757static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5758{
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760 u32 val, cmd;
5761
Vandana Kannan164dfd22014-11-24 13:37:41 +05305762 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5763 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005764
Ville Syrjälädfcab172014-06-13 13:37:47 +03005765 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005766 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005767 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005768 cmd = 1;
5769 else
5770 cmd = 0;
5771
5772 mutex_lock(&dev_priv->rps.hw_lock);
5773 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5774 val &= ~DSPFREQGUAR_MASK;
5775 val |= (cmd << DSPFREQGUAR_SHIFT);
5776 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5777 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5778 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5779 50)) {
5780 DRM_ERROR("timed out waiting for CDclk change\n");
5781 }
5782 mutex_unlock(&dev_priv->rps.hw_lock);
5783
Ville Syrjälä54433e92015-05-26 20:42:31 +03005784 mutex_lock(&dev_priv->sb_lock);
5785
Ville Syrjälädfcab172014-06-13 13:37:47 +03005786 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005787 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005788
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005789 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790
Jesse Barnes30a970c2013-11-04 13:48:12 -08005791 /* adjust cdclk divider */
5792 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005793 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005794 val |= divider;
5795 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005796
5797 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005798 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005799 50))
5800 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005801 }
5802
Jesse Barnes30a970c2013-11-04 13:48:12 -08005803 /* adjust self-refresh exit latency value */
5804 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5805 val &= ~0x7f;
5806
5807 /*
5808 * For high bandwidth configs, we set a higher latency in the bunit
5809 * so that the core display fetch happens in time to avoid underruns.
5810 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005811 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812 val |= 4500 / 250; /* 4.5 usec */
5813 else
5814 val |= 3000 / 250; /* 3.0 usec */
5815 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005816
Ville Syrjäläa5805162015-05-26 20:42:30 +03005817 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818
Ville Syrjäläb6283052015-06-03 15:45:07 +03005819 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005820}
5821
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5823{
5824 struct drm_i915_private *dev_priv = dev->dev_private;
5825 u32 val, cmd;
5826
Vandana Kannan164dfd22014-11-24 13:37:41 +05305827 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5828 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005829
5830 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005831 case 333333:
5832 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005834 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005835 break;
5836 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005837 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005838 return;
5839 }
5840
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005841 /*
5842 * Specs are full of misinformation, but testing on actual
5843 * hardware has shown that we just need to write the desired
5844 * CCK divider into the Punit register.
5845 */
5846 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5847
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005848 mutex_lock(&dev_priv->rps.hw_lock);
5849 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5850 val &= ~DSPFREQGUAR_MASK_CHV;
5851 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5852 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5853 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5854 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5855 50)) {
5856 DRM_ERROR("timed out waiting for CDclk change\n");
5857 }
5858 mutex_unlock(&dev_priv->rps.hw_lock);
5859
Ville Syrjäläb6283052015-06-03 15:45:07 +03005860 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005861}
5862
Jesse Barnes30a970c2013-11-04 13:48:12 -08005863static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5864 int max_pixclk)
5865{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005866 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005867 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005868
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869 /*
5870 * Really only a few cases to deal with, as only 4 CDclks are supported:
5871 * 200MHz
5872 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005873 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005874 * 400MHz (VLV only)
5875 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5876 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005877 *
5878 * We seem to get an unstable or solid color picture at 200MHz.
5879 * Not sure what's wrong. For now use 200MHz only when all pipes
5880 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005881 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005882 if (!IS_CHERRYVIEW(dev_priv) &&
5883 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005884 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005885 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005886 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005887 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005888 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005889 else
5890 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005891}
5892
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305893static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5894 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005895{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305896 /*
5897 * FIXME:
5898 * - remove the guardband, it's not needed on BXT
5899 * - set 19.2MHz bypass frequency if there are no active pipes
5900 */
5901 if (max_pixclk > 576000*9/10)
5902 return 624000;
5903 else if (max_pixclk > 384000*9/10)
5904 return 576000;
5905 else if (max_pixclk > 288000*9/10)
5906 return 384000;
5907 else if (max_pixclk > 144000*9/10)
5908 return 288000;
5909 else
5910 return 144000;
5911}
5912
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005913/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005914static int intel_mode_max_pixclk(struct drm_device *dev,
5915 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005917 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5918 struct drm_i915_private *dev_priv = dev->dev_private;
5919 struct drm_crtc *crtc;
5920 struct drm_crtc_state *crtc_state;
5921 unsigned max_pixclk = 0, i;
5922 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005923
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005924 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5925 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005926
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005927 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5928 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005929
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005930 if (crtc_state->enable)
5931 pixclk = crtc_state->adjusted_mode.crtc_clock;
5932
5933 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934 }
5935
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005936 for_each_pipe(dev_priv, pipe)
5937 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5938
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939 return max_pixclk;
5940}
5941
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005942static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005943{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005944 struct drm_device *dev = state->dev;
5945 struct drm_i915_private *dev_priv = dev->dev_private;
5946 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005947 struct intel_atomic_state *intel_state =
5948 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005950 if (max_pixclk < 0)
5951 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005952
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005953 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005954 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305955
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005956 if (!intel_state->active_crtcs)
5957 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5958
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005959 return 0;
5960}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005962static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5963{
5964 struct drm_device *dev = state->dev;
5965 struct drm_i915_private *dev_priv = dev->dev_private;
5966 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005967 struct intel_atomic_state *intel_state =
5968 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005969
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005970 if (max_pixclk < 0)
5971 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005972
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005973 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005974 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005975
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005976 if (!intel_state->active_crtcs)
5977 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5978
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005979 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005980}
5981
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005982static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5983{
5984 unsigned int credits, default_credits;
5985
5986 if (IS_CHERRYVIEW(dev_priv))
5987 default_credits = PFI_CREDIT(12);
5988 else
5989 default_credits = PFI_CREDIT(8);
5990
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005991 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005992 /* CHV suggested value is 31 or 63 */
5993 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005994 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005995 else
5996 credits = PFI_CREDIT(15);
5997 } else {
5998 credits = default_credits;
5999 }
6000
6001 /*
6002 * WA - write default credits before re-programming
6003 * FIXME: should we also set the resend bit here?
6004 */
6005 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6006 default_credits);
6007
6008 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6009 credits | PFI_CREDIT_RESEND);
6010
6011 /*
6012 * FIXME is this guaranteed to clear
6013 * immediately or should we poll for it?
6014 */
6015 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6016}
6017
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006018static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006019{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006020 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006021 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006022 struct intel_atomic_state *old_intel_state =
6023 to_intel_atomic_state(old_state);
6024 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006025
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006026 /*
6027 * FIXME: We can end up here with all power domains off, yet
6028 * with a CDCLK frequency other than the minimum. To account
6029 * for this take the PIPE-A power domain, which covers the HW
6030 * blocks needed for the following programming. This can be
6031 * removed once it's guaranteed that we get here either with
6032 * the minimum CDCLK set, or the required power domains
6033 * enabled.
6034 */
6035 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006036
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006037 if (IS_CHERRYVIEW(dev))
6038 cherryview_set_cdclk(dev, req_cdclk);
6039 else
6040 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006042 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006043
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006044 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006045}
6046
Jesse Barnes89b667f2013-04-18 14:51:36 -07006047static void valleyview_crtc_enable(struct drm_crtc *crtc)
6048{
6049 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006050 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6052 struct intel_encoder *encoder;
6053 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006055 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006056 return;
6057
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006058 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306059 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006060
6061 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006062 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006063
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006064 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6065 struct drm_i915_private *dev_priv = dev->dev_private;
6066
6067 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6068 I915_WRITE(CHV_CANVAS(pipe), 0);
6069 }
6070
Daniel Vetter5b18e572014-04-24 23:55:06 +02006071 i9xx_set_pipeconf(intel_crtc);
6072
Jesse Barnes89b667f2013-04-18 14:51:36 -07006073 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074
Daniel Vettera72e4c92014-09-30 10:56:47 +02006075 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006076
Jesse Barnes89b667f2013-04-18 14:51:36 -07006077 for_each_encoder_on_crtc(dev, crtc, encoder)
6078 if (encoder->pre_pll_enable)
6079 encoder->pre_pll_enable(encoder);
6080
Jani Nikulaa65347b2015-11-27 12:21:46 +02006081 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006082 if (IS_CHERRYVIEW(dev)) {
6083 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006084 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006085 } else {
6086 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006087 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006088 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006089 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006090
6091 for_each_encoder_on_crtc(dev, crtc, encoder)
6092 if (encoder->pre_enable)
6093 encoder->pre_enable(encoder);
6094
Jesse Barnes2dd24552013-04-25 12:55:01 -07006095 i9xx_pfit_enable(intel_crtc);
6096
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006097 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006098
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006099 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006100 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006101
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006102 assert_vblank_disabled(crtc);
6103 drm_crtc_vblank_on(crtc);
6104
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006105 for_each_encoder_on_crtc(dev, crtc, encoder)
6106 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006107}
6108
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006109static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6110{
6111 struct drm_device *dev = crtc->base.dev;
6112 struct drm_i915_private *dev_priv = dev->dev_private;
6113
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006114 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6115 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006116}
6117
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006118static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006119{
6120 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006121 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006123 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006124 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006125
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006126 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006127 return;
6128
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006129 i9xx_set_pll_dividers(intel_crtc);
6130
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006131 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306132 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006133
6134 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006135 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006136
Daniel Vetter5b18e572014-04-24 23:55:06 +02006137 i9xx_set_pipeconf(intel_crtc);
6138
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006139 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006140
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006141 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006142 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006143
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006144 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006145 if (encoder->pre_enable)
6146 encoder->pre_enable(encoder);
6147
Daniel Vetterf6736a12013-06-05 13:34:30 +02006148 i9xx_enable_pll(intel_crtc);
6149
Jesse Barnes2dd24552013-04-25 12:55:01 -07006150 i9xx_pfit_enable(intel_crtc);
6151
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006152 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006153
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006154 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006155 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006156
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006157 assert_vblank_disabled(crtc);
6158 drm_crtc_vblank_on(crtc);
6159
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006160 for_each_encoder_on_crtc(dev, crtc, encoder)
6161 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006162}
6163
Daniel Vetter87476d62013-04-11 16:29:06 +02006164static void i9xx_pfit_disable(struct intel_crtc *crtc)
6165{
6166 struct drm_device *dev = crtc->base.dev;
6167 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006168
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006169 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006170 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006171
6172 assert_pipe_disabled(dev_priv, crtc->pipe);
6173
Daniel Vetter328d8e82013-05-08 10:36:31 +02006174 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6175 I915_READ(PFIT_CONTROL));
6176 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006177}
6178
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006179static void i9xx_crtc_disable(struct drm_crtc *crtc)
6180{
6181 struct drm_device *dev = crtc->dev;
6182 struct drm_i915_private *dev_priv = dev->dev_private;
6183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006184 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006185 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006186
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006187 /*
6188 * On gen2 planes are double buffered but the pipe isn't, so we must
6189 * wait for planes to fully turn off before disabling the pipe.
6190 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006191 if (IS_GEN2(dev))
6192 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006193
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006194 for_each_encoder_on_crtc(dev, crtc, encoder)
6195 encoder->disable(encoder);
6196
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006197 drm_crtc_vblank_off(crtc);
6198 assert_vblank_disabled(crtc);
6199
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006200 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006201
Daniel Vetter87476d62013-04-11 16:29:06 +02006202 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006203
Jesse Barnes89b667f2013-04-18 14:51:36 -07006204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 if (encoder->post_disable)
6206 encoder->post_disable(encoder);
6207
Jani Nikulaa65347b2015-11-27 12:21:46 +02006208 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006209 if (IS_CHERRYVIEW(dev))
6210 chv_disable_pll(dev_priv, pipe);
6211 else if (IS_VALLEYVIEW(dev))
6212 vlv_disable_pll(dev_priv, pipe);
6213 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006214 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006215 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006216
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006217 for_each_encoder_on_crtc(dev, crtc, encoder)
6218 if (encoder->post_pll_disable)
6219 encoder->post_pll_disable(encoder);
6220
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006221 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006222 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006223}
6224
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006225static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006226{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006227 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006229 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006230 enum intel_display_power_domain domain;
6231 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006232
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006233 if (!intel_crtc->active)
6234 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006235
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006236 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006237 WARN_ON(intel_crtc->unpin_work);
6238
Ville Syrjälä2622a082016-03-09 19:07:26 +02006239 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006240
6241 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6242 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006243 }
6244
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006245 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006246
6247 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6248 crtc->base.id);
6249
6250 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6251 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006252 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006253 crtc->enabled = false;
6254 crtc->state->connector_mask = 0;
6255 crtc->state->encoder_mask = 0;
6256
6257 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6258 encoder->base.crtc = NULL;
6259
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006260 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006261 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006262 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006263
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006264 domains = intel_crtc->enabled_power_domains;
6265 for_each_power_domain(domain, domains)
6266 intel_display_power_put(dev_priv, domain);
6267 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006268
6269 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6270 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006271}
6272
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006273/*
6274 * turn all crtc's off, but do not adjust state
6275 * This has to be paired with a call to intel_modeset_setup_hw_state.
6276 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006277int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006278{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006279 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006280 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006281 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006282
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006283 state = drm_atomic_helper_suspend(dev);
6284 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006285 if (ret)
6286 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006287 else
6288 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006289 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006290}
6291
Chris Wilsonea5b2132010-08-04 13:50:23 +01006292void intel_encoder_destroy(struct drm_encoder *encoder)
6293{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006294 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006295
Chris Wilsonea5b2132010-08-04 13:50:23 +01006296 drm_encoder_cleanup(encoder);
6297 kfree(intel_encoder);
6298}
6299
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006300/* Cross check the actual hw state with our own modeset state tracking (and it's
6301 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006302static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006303{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006304 struct drm_crtc *crtc = connector->base.state->crtc;
6305
6306 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6307 connector->base.base.id,
6308 connector->base.name);
6309
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006311 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006312 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006313
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006314 I915_STATE_WARN(!crtc,
6315 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006316
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006317 if (!crtc)
6318 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006319
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006320 I915_STATE_WARN(!crtc->state->active,
6321 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006322
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006323 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006324 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006325
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006326 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006327 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006328
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006329 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006330 "attached encoder crtc differs from connector crtc\n");
6331 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006332 I915_STATE_WARN(crtc && crtc->state->active,
6333 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006334 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6335 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336 }
6337}
6338
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006339int intel_connector_init(struct intel_connector *connector)
6340{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006341 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006342
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006343 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006344 return -ENOMEM;
6345
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006346 return 0;
6347}
6348
6349struct intel_connector *intel_connector_alloc(void)
6350{
6351 struct intel_connector *connector;
6352
6353 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6354 if (!connector)
6355 return NULL;
6356
6357 if (intel_connector_init(connector) < 0) {
6358 kfree(connector);
6359 return NULL;
6360 }
6361
6362 return connector;
6363}
6364
Daniel Vetterf0947c32012-07-02 13:10:34 +02006365/* Simple connector->get_hw_state implementation for encoders that support only
6366 * one connector and no cloning and hence the encoder state determines the state
6367 * of the connector. */
6368bool intel_connector_get_hw_state(struct intel_connector *connector)
6369{
Daniel Vetter24929352012-07-02 20:28:59 +02006370 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006371 struct intel_encoder *encoder = connector->encoder;
6372
6373 return encoder->get_hw_state(encoder, &pipe);
6374}
6375
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006376static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006377{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006378 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6379 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006380
6381 return 0;
6382}
6383
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006384static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006385 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006386{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 struct drm_atomic_state *state = pipe_config->base.state;
6388 struct intel_crtc *other_crtc;
6389 struct intel_crtc_state *other_crtc_state;
6390
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6392 pipe_name(pipe), pipe_config->fdi_lanes);
6393 if (pipe_config->fdi_lanes > 4) {
6394 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6395 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 }
6398
Paulo Zanonibafb6552013-11-02 21:07:44 -07006399 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006400 if (pipe_config->fdi_lanes > 2) {
6401 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6402 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006404 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006405 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006406 }
6407 }
6408
6409 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411
6412 /* Ivybridge 3 pipe is really complicated */
6413 switch (pipe) {
6414 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417 if (pipe_config->fdi_lanes <= 2)
6418 return 0;
6419
6420 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6421 other_crtc_state =
6422 intel_atomic_get_crtc_state(state, other_crtc);
6423 if (IS_ERR(other_crtc_state))
6424 return PTR_ERR(other_crtc_state);
6425
6426 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006427 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6428 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006431 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006433 if (pipe_config->fdi_lanes > 2) {
6434 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6435 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006437 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006438
6439 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6440 other_crtc_state =
6441 intel_atomic_get_crtc_state(state, other_crtc);
6442 if (IS_ERR(other_crtc_state))
6443 return PTR_ERR(other_crtc_state);
6444
6445 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006446 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006449 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450 default:
6451 BUG();
6452 }
6453}
6454
Daniel Vettere29c22c2013-02-21 00:00:16 +01006455#define RETRY 1
6456static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006457 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006458{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006459 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006460 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 int lane, link_bw, fdi_dotclock, ret;
6462 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006463
Daniel Vettere29c22c2013-02-21 00:00:16 +01006464retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006465 /* FDI is a binary signal running at ~2.7GHz, encoding
6466 * each output octet as 10 bits. The actual frequency
6467 * is stored as a divider into a 100MHz clock, and the
6468 * mode pixel clock is stored in units of 1KHz.
6469 * Hence the bw of each lane in terms of the mode signal
6470 * is:
6471 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006472 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006473
Damien Lespiau241bfc32013-09-25 16:45:37 +01006474 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006475
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006476 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006477 pipe_config->pipe_bpp);
6478
6479 pipe_config->fdi_lanes = lane;
6480
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006481 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006482 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006483
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006484 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006486 pipe_config->pipe_bpp -= 2*3;
6487 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6488 pipe_config->pipe_bpp);
6489 needs_recompute = true;
6490 pipe_config->bw_constrained = true;
6491
6492 goto retry;
6493 }
6494
6495 if (needs_recompute)
6496 return RETRY;
6497
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006498 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006499}
6500
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006501static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6502 struct intel_crtc_state *pipe_config)
6503{
6504 if (pipe_config->pipe_bpp > 24)
6505 return false;
6506
6507 /* HSW can handle pixel rate up to cdclk? */
6508 if (IS_HASWELL(dev_priv->dev))
6509 return true;
6510
6511 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006512 * We compare against max which means we must take
6513 * the increased cdclk requirement into account when
6514 * calculating the new cdclk.
6515 *
6516 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006517 */
6518 return ilk_pipe_pixel_rate(pipe_config) <=
6519 dev_priv->max_cdclk_freq * 95 / 100;
6520}
6521
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006522static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006523 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006524{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006525 struct drm_device *dev = crtc->base.dev;
6526 struct drm_i915_private *dev_priv = dev->dev_private;
6527
Jani Nikulad330a952014-01-21 11:24:25 +02006528 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006529 hsw_crtc_supports_ips(crtc) &&
6530 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006531}
6532
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006533static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6534{
6535 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6536
6537 /* GDG double wide on either pipe, otherwise pipe A only */
6538 return INTEL_INFO(dev_priv)->gen < 4 &&
6539 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6540}
6541
Daniel Vettera43f6e02013-06-07 23:10:32 +02006542static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006543 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006544{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006545 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006546 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006547 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006548
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006549 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006550 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006551 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006552
6553 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006554 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006555 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006556 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006557 if (intel_crtc_supports_double_wide(crtc) &&
6558 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006559 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006560 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006561 }
6562
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006563 if (adjusted_mode->crtc_clock > clock_limit) {
6564 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6565 adjusted_mode->crtc_clock, clock_limit,
6566 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006567 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006568 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006569 }
Chris Wilson89749352010-09-12 18:25:19 +01006570
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006571 /*
6572 * Pipe horizontal size must be even in:
6573 * - DVO ganged mode
6574 * - LVDS dual channel mode
6575 * - Double wide pipe
6576 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006577 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006578 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6579 pipe_config->pipe_src_w &= ~1;
6580
Damien Lespiau8693a822013-05-03 18:48:11 +01006581 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6582 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006583 */
6584 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006585 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006586 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006587
Damien Lespiauf5adf942013-06-24 18:29:34 +01006588 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006589 hsw_compute_ips_config(crtc, pipe_config);
6590
Daniel Vetter877d48d2013-04-19 11:24:43 +02006591 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006592 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006593
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006594 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006595}
6596
Ville Syrjälä1652d192015-03-31 14:12:01 +03006597static int skylake_get_display_clock_speed(struct drm_device *dev)
6598{
6599 struct drm_i915_private *dev_priv = to_i915(dev);
6600 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6601 uint32_t cdctl = I915_READ(CDCLK_CTL);
6602 uint32_t linkrate;
6603
Damien Lespiau414355a2015-06-04 18:21:31 +01006604 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006605 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006606
6607 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6608 return 540000;
6609
6610 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006611 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006612
Damien Lespiau71cd8422015-04-30 16:39:17 +01006613 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6614 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006615 /* vco 8640 */
6616 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6617 case CDCLK_FREQ_450_432:
6618 return 432000;
6619 case CDCLK_FREQ_337_308:
6620 return 308570;
6621 case CDCLK_FREQ_675_617:
6622 return 617140;
6623 default:
6624 WARN(1, "Unknown cd freq selection\n");
6625 }
6626 } else {
6627 /* vco 8100 */
6628 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6629 case CDCLK_FREQ_450_432:
6630 return 450000;
6631 case CDCLK_FREQ_337_308:
6632 return 337500;
6633 case CDCLK_FREQ_675_617:
6634 return 675000;
6635 default:
6636 WARN(1, "Unknown cd freq selection\n");
6637 }
6638 }
6639
6640 /* error case, do as if DPLL0 isn't enabled */
6641 return 24000;
6642}
6643
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006644static int broxton_get_display_clock_speed(struct drm_device *dev)
6645{
6646 struct drm_i915_private *dev_priv = to_i915(dev);
6647 uint32_t cdctl = I915_READ(CDCLK_CTL);
6648 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6649 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6650 int cdclk;
6651
6652 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6653 return 19200;
6654
6655 cdclk = 19200 * pll_ratio / 2;
6656
6657 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6658 case BXT_CDCLK_CD2X_DIV_SEL_1:
6659 return cdclk; /* 576MHz or 624MHz */
6660 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6661 return cdclk * 2 / 3; /* 384MHz */
6662 case BXT_CDCLK_CD2X_DIV_SEL_2:
6663 return cdclk / 2; /* 288MHz */
6664 case BXT_CDCLK_CD2X_DIV_SEL_4:
6665 return cdclk / 4; /* 144MHz */
6666 }
6667
6668 /* error case, do as if DE PLL isn't enabled */
6669 return 19200;
6670}
6671
Ville Syrjälä1652d192015-03-31 14:12:01 +03006672static int broadwell_get_display_clock_speed(struct drm_device *dev)
6673{
6674 struct drm_i915_private *dev_priv = dev->dev_private;
6675 uint32_t lcpll = I915_READ(LCPLL_CTL);
6676 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6677
6678 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6679 return 800000;
6680 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6681 return 450000;
6682 else if (freq == LCPLL_CLK_FREQ_450)
6683 return 450000;
6684 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6685 return 540000;
6686 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6687 return 337500;
6688 else
6689 return 675000;
6690}
6691
6692static int haswell_get_display_clock_speed(struct drm_device *dev)
6693{
6694 struct drm_i915_private *dev_priv = dev->dev_private;
6695 uint32_t lcpll = I915_READ(LCPLL_CTL);
6696 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6697
6698 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6699 return 800000;
6700 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6701 return 450000;
6702 else if (freq == LCPLL_CLK_FREQ_450)
6703 return 450000;
6704 else if (IS_HSW_ULT(dev))
6705 return 337500;
6706 else
6707 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006708}
6709
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006710static int valleyview_get_display_clock_speed(struct drm_device *dev)
6711{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006712 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6713 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006714}
6715
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006716static int ilk_get_display_clock_speed(struct drm_device *dev)
6717{
6718 return 450000;
6719}
6720
Jesse Barnese70236a2009-09-21 10:42:27 -07006721static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006722{
Jesse Barnese70236a2009-09-21 10:42:27 -07006723 return 400000;
6724}
Jesse Barnes79e53942008-11-07 14:24:08 -08006725
Jesse Barnese70236a2009-09-21 10:42:27 -07006726static int i915_get_display_clock_speed(struct drm_device *dev)
6727{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006728 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006729}
Jesse Barnes79e53942008-11-07 14:24:08 -08006730
Jesse Barnese70236a2009-09-21 10:42:27 -07006731static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6732{
6733 return 200000;
6734}
Jesse Barnes79e53942008-11-07 14:24:08 -08006735
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006736static int pnv_get_display_clock_speed(struct drm_device *dev)
6737{
6738 u16 gcfgc = 0;
6739
6740 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6741
6742 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6743 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006744 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006745 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006746 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006747 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006748 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006749 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6750 return 200000;
6751 default:
6752 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6753 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006754 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006755 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006756 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006757 }
6758}
6759
Jesse Barnese70236a2009-09-21 10:42:27 -07006760static int i915gm_get_display_clock_speed(struct drm_device *dev)
6761{
6762 u16 gcfgc = 0;
6763
6764 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6765
6766 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006767 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006768 else {
6769 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6770 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006771 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006772 default:
6773 case GC_DISPLAY_CLOCK_190_200_MHZ:
6774 return 190000;
6775 }
6776 }
6777}
Jesse Barnes79e53942008-11-07 14:24:08 -08006778
Jesse Barnese70236a2009-09-21 10:42:27 -07006779static int i865_get_display_clock_speed(struct drm_device *dev)
6780{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006781 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006782}
6783
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006784static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006785{
6786 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006787
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006788 /*
6789 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6790 * encoding is different :(
6791 * FIXME is this the right way to detect 852GM/852GMV?
6792 */
6793 if (dev->pdev->revision == 0x1)
6794 return 133333;
6795
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006796 pci_bus_read_config_word(dev->pdev->bus,
6797 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6798
Jesse Barnese70236a2009-09-21 10:42:27 -07006799 /* Assume that the hardware is in the high speed state. This
6800 * should be the default.
6801 */
6802 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6803 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006804 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006805 case GC_CLOCK_100_200:
6806 return 200000;
6807 case GC_CLOCK_166_250:
6808 return 250000;
6809 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006810 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006811 case GC_CLOCK_133_266:
6812 case GC_CLOCK_133_266_2:
6813 case GC_CLOCK_166_266:
6814 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006815 }
6816
6817 /* Shouldn't happen */
6818 return 0;
6819}
6820
6821static int i830_get_display_clock_speed(struct drm_device *dev)
6822{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006823 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006824}
6825
Ville Syrjälä34edce22015-05-22 11:22:33 +03006826static unsigned int intel_hpll_vco(struct drm_device *dev)
6827{
6828 struct drm_i915_private *dev_priv = dev->dev_private;
6829 static const unsigned int blb_vco[8] = {
6830 [0] = 3200000,
6831 [1] = 4000000,
6832 [2] = 5333333,
6833 [3] = 4800000,
6834 [4] = 6400000,
6835 };
6836 static const unsigned int pnv_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 [4] = 2666667,
6842 };
6843 static const unsigned int cl_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 6400000,
6848 [4] = 3333333,
6849 [5] = 3566667,
6850 [6] = 4266667,
6851 };
6852 static const unsigned int elk_vco[8] = {
6853 [0] = 3200000,
6854 [1] = 4000000,
6855 [2] = 5333333,
6856 [3] = 4800000,
6857 };
6858 static const unsigned int ctg_vco[8] = {
6859 [0] = 3200000,
6860 [1] = 4000000,
6861 [2] = 5333333,
6862 [3] = 6400000,
6863 [4] = 2666667,
6864 [5] = 4266667,
6865 };
6866 const unsigned int *vco_table;
6867 unsigned int vco;
6868 uint8_t tmp = 0;
6869
6870 /* FIXME other chipsets? */
6871 if (IS_GM45(dev))
6872 vco_table = ctg_vco;
6873 else if (IS_G4X(dev))
6874 vco_table = elk_vco;
6875 else if (IS_CRESTLINE(dev))
6876 vco_table = cl_vco;
6877 else if (IS_PINEVIEW(dev))
6878 vco_table = pnv_vco;
6879 else if (IS_G33(dev))
6880 vco_table = blb_vco;
6881 else
6882 return 0;
6883
6884 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6885
6886 vco = vco_table[tmp & 0x7];
6887 if (vco == 0)
6888 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6889 else
6890 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6891
6892 return vco;
6893}
6894
6895static int gm45_get_display_clock_speed(struct drm_device *dev)
6896{
6897 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6898 uint16_t tmp = 0;
6899
6900 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6901
6902 cdclk_sel = (tmp >> 12) & 0x1;
6903
6904 switch (vco) {
6905 case 2666667:
6906 case 4000000:
6907 case 5333333:
6908 return cdclk_sel ? 333333 : 222222;
6909 case 3200000:
6910 return cdclk_sel ? 320000 : 228571;
6911 default:
6912 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6913 return 222222;
6914 }
6915}
6916
6917static int i965gm_get_display_clock_speed(struct drm_device *dev)
6918{
6919 static const uint8_t div_3200[] = { 16, 10, 8 };
6920 static const uint8_t div_4000[] = { 20, 12, 10 };
6921 static const uint8_t div_5333[] = { 24, 16, 14 };
6922 const uint8_t *div_table;
6923 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6924 uint16_t tmp = 0;
6925
6926 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6927
6928 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6929
6930 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6931 goto fail;
6932
6933 switch (vco) {
6934 case 3200000:
6935 div_table = div_3200;
6936 break;
6937 case 4000000:
6938 div_table = div_4000;
6939 break;
6940 case 5333333:
6941 div_table = div_5333;
6942 break;
6943 default:
6944 goto fail;
6945 }
6946
6947 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6948
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006949fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006950 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6951 return 200000;
6952}
6953
6954static int g33_get_display_clock_speed(struct drm_device *dev)
6955{
6956 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6957 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6958 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6959 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6960 const uint8_t *div_table;
6961 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6962 uint16_t tmp = 0;
6963
6964 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6965
6966 cdclk_sel = (tmp >> 4) & 0x7;
6967
6968 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6969 goto fail;
6970
6971 switch (vco) {
6972 case 3200000:
6973 div_table = div_3200;
6974 break;
6975 case 4000000:
6976 div_table = div_4000;
6977 break;
6978 case 4800000:
6979 div_table = div_4800;
6980 break;
6981 case 5333333:
6982 div_table = div_5333;
6983 break;
6984 default:
6985 goto fail;
6986 }
6987
6988 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6989
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006990fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006991 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6992 return 190476;
6993}
6994
Zhenyu Wang2c072452009-06-05 15:38:42 +08006995static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006996intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006997{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006998 while (*num > DATA_LINK_M_N_MASK ||
6999 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007000 *num >>= 1;
7001 *den >>= 1;
7002 }
7003}
7004
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007005static void compute_m_n(unsigned int m, unsigned int n,
7006 uint32_t *ret_m, uint32_t *ret_n)
7007{
7008 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7009 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7010 intel_reduce_m_n_ratio(ret_m, ret_n);
7011}
7012
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007013void
7014intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7015 int pixel_clock, int link_clock,
7016 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007017{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007018 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007019
7020 compute_m_n(bits_per_pixel * pixel_clock,
7021 link_clock * nlanes * 8,
7022 &m_n->gmch_m, &m_n->gmch_n);
7023
7024 compute_m_n(pixel_clock, link_clock,
7025 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007026}
7027
Chris Wilsona7615032011-01-12 17:04:08 +00007028static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7029{
Jani Nikulad330a952014-01-21 11:24:25 +02007030 if (i915.panel_use_ssc >= 0)
7031 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007032 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007033 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007034}
7035
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007036static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007037{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007038 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007039 struct drm_i915_private *dev_priv = dev->dev_private;
7040 int refclk;
7041
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007042 WARN_ON(!crtc_state->base.state);
7043
Wayne Boyer666a4532015-12-09 12:29:35 -08007044 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007045 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007046 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007047 intel_panel_use_ssc(dev_priv)) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007048 refclk = dev_priv->vbt.lvds_ssc_freq;
7049 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007050 } else if (!IS_GEN2(dev)) {
7051 refclk = 96000;
7052 } else {
7053 refclk = 48000;
7054 }
7055
7056 return refclk;
7057}
7058
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007059static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007060{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007061 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007062}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007063
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007064static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7065{
7066 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007067}
7068
Daniel Vetterf47709a2013-03-28 10:42:02 +01007069static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007070 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007071 intel_clock_t *reduced_clock)
7072{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007073 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007074 u32 fp, fp2 = 0;
7075
7076 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007077 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007078 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007079 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007080 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007081 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007082 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007083 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007084 }
7085
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007086 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007087
Daniel Vetterf47709a2013-03-28 10:42:02 +01007088 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007089 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007090 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007091 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007092 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007093 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007094 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007095 }
7096}
7097
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007098static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7099 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007100{
7101 u32 reg_val;
7102
7103 /*
7104 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7105 * and set it to a reasonable value instead.
7106 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007107 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007108 reg_val &= 0xffffff00;
7109 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007110 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007111
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007112 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113 reg_val &= 0x8cffffff;
7114 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007115 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, 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_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007118 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007119 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007120
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007121 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007122 reg_val &= 0x00ffffff;
7123 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007124 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007125}
7126
Daniel Vetterb5518422013-05-03 11:49:48 +02007127static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7128 struct intel_link_m_n *m_n)
7129{
7130 struct drm_device *dev = crtc->base.dev;
7131 struct drm_i915_private *dev_priv = dev->dev_private;
7132 int pipe = crtc->pipe;
7133
Daniel Vettere3b95f12013-05-03 11:49:49 +02007134 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7135 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7136 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7137 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007138}
7139
7140static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007141 struct intel_link_m_n *m_n,
7142 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007143{
7144 struct drm_device *dev = crtc->base.dev;
7145 struct drm_i915_private *dev_priv = dev->dev_private;
7146 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007147 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007148
7149 if (INTEL_INFO(dev)->gen >= 5) {
7150 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7151 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7152 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7153 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007154 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7155 * for gen < 8) and if DRRS is supported (to make sure the
7156 * registers are not unnecessarily accessed).
7157 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307158 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007159 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007160 I915_WRITE(PIPE_DATA_M2(transcoder),
7161 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7162 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7163 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7164 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7165 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007166 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007167 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7168 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7169 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7170 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007171 }
7172}
7173
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307174void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007175{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307176 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7177
7178 if (m_n == M1_N1) {
7179 dp_m_n = &crtc->config->dp_m_n;
7180 dp_m2_n2 = &crtc->config->dp_m2_n2;
7181 } else if (m_n == M2_N2) {
7182
7183 /*
7184 * M2_N2 registers are not supported. Hence m2_n2 divider value
7185 * needs to be programmed into M1_N1.
7186 */
7187 dp_m_n = &crtc->config->dp_m2_n2;
7188 } else {
7189 DRM_ERROR("Unsupported divider value\n");
7190 return;
7191 }
7192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007193 if (crtc->config->has_pch_encoder)
7194 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007195 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307196 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007197}
7198
Daniel Vetter251ac862015-06-18 10:30:24 +02007199static void vlv_compute_dpll(struct intel_crtc *crtc,
7200 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007201{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007202 u32 dpll, dpll_md;
7203
7204 /*
7205 * Enable DPIO clock input. We should never disable the reference
7206 * clock for pipe B, since VGA hotplug / manual detection depends
7207 * on it.
7208 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007209 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7210 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007211 /* We should never disable this, set it here for state tracking */
7212 if (crtc->pipe == PIPE_B)
7213 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7214 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007215 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007216
Ville Syrjäläd288f652014-10-28 13:20:22 +02007217 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007218 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007219 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007220}
7221
Ville Syrjäläd288f652014-10-28 13:20:22 +02007222static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007223 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007224{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007225 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007226 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007227 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007228 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007229 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007230 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007231
Ville Syrjäläa5805162015-05-26 20:42:30 +03007232 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007233
Ville Syrjäläd288f652014-10-28 13:20:22 +02007234 bestn = pipe_config->dpll.n;
7235 bestm1 = pipe_config->dpll.m1;
7236 bestm2 = pipe_config->dpll.m2;
7237 bestp1 = pipe_config->dpll.p1;
7238 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007239
Jesse Barnes89b667f2013-04-18 14:51:36 -07007240 /* See eDP HDMI DPIO driver vbios notes doc */
7241
7242 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007243 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007244 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245
7246 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007247 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007248
7249 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253
7254 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256
7257 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007258 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7259 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7260 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007261 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007262
7263 /*
7264 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7265 * but we don't support that).
7266 * Note: don't use the DAC post divider as it seems unstable.
7267 */
7268 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007271 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007273
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007275 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007276 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7277 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007278 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007279 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007283
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007284 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007286 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007287 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007288 0x0df40000);
7289 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007291 0x0df70000);
7292 } else { /* HDMI or VGA */
7293 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007294 if (pipe == PIPE_A)
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
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007298 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007299 0x0df40000);
7300 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007301
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007302 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007303 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007304 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7305 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007306 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007308
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007310 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007311}
7312
Daniel Vetter251ac862015-06-18 10:30:24 +02007313static void chv_compute_dpll(struct intel_crtc *crtc,
7314 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007315{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007316 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7317 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007318 DPLL_VCO_ENABLE;
7319 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007320 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007321
Ville Syrjäläd288f652014-10-28 13:20:22 +02007322 pipe_config->dpll_hw_state.dpll_md =
7323 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007324}
7325
Ville Syrjäläd288f652014-10-28 13:20:22 +02007326static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007327 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007328{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007329 struct drm_device *dev = crtc->base.dev;
7330 struct drm_i915_private *dev_priv = dev->dev_private;
7331 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007332 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007333 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307334 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007335 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307336 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307337 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007338
Ville Syrjäläd288f652014-10-28 13:20:22 +02007339 bestn = pipe_config->dpll.n;
7340 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7341 bestm1 = pipe_config->dpll.m1;
7342 bestm2 = pipe_config->dpll.m2 >> 22;
7343 bestp1 = pipe_config->dpll.p1;
7344 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307345 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307346 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307347 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007348
7349 /*
7350 * Enable Refclk and SSC
7351 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007352 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007353 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007354
Ville Syrjäläa5805162015-05-26 20:42:30 +03007355 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007356
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007357 /* p1 and p2 divider */
7358 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7359 5 << DPIO_CHV_S1_DIV_SHIFT |
7360 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7361 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7362 1 << DPIO_CHV_K_DIV_SHIFT);
7363
7364 /* Feedback post-divider - m2 */
7365 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7366
7367 /* Feedback refclk divider - n and m1 */
7368 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7369 DPIO_CHV_M1_DIV_BY_2 |
7370 1 << DPIO_CHV_N_DIV_SHIFT);
7371
7372 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007373 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007374
7375 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307376 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7377 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7378 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7379 if (bestm2_frac)
7380 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007382
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307383 /* Program digital lock detect threshold */
7384 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7385 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7386 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7387 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7388 if (!bestm2_frac)
7389 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7390 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7391
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007392 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307393 if (vco == 5400000) {
7394 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7395 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7396 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7397 tribuf_calcntr = 0x9;
7398 } else if (vco <= 6200000) {
7399 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7400 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7401 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7402 tribuf_calcntr = 0x9;
7403 } else if (vco <= 6480000) {
7404 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7405 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7406 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7407 tribuf_calcntr = 0x8;
7408 } else {
7409 /* Not supported. Apply the same limits as in the max case */
7410 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7411 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7412 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7413 tribuf_calcntr = 0;
7414 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7416
Ville Syrjälä968040b2015-03-11 22:52:08 +02007417 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307418 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7419 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7420 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7421
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007422 /* AFC Recal */
7423 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7424 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7425 DPIO_AFC_RECAL);
7426
Ville Syrjäläa5805162015-05-26 20:42:30 +03007427 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007428}
7429
Ville Syrjäläd288f652014-10-28 13:20:22 +02007430/**
7431 * vlv_force_pll_on - forcibly enable just the PLL
7432 * @dev_priv: i915 private structure
7433 * @pipe: pipe PLL to enable
7434 * @dpll: PLL configuration
7435 *
7436 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7437 * in cases where we need the PLL enabled even when @pipe is not going to
7438 * be enabled.
7439 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007440int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7441 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007442{
7443 struct intel_crtc *crtc =
7444 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007445 struct intel_crtc_state *pipe_config;
7446
7447 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7448 if (!pipe_config)
7449 return -ENOMEM;
7450
7451 pipe_config->base.crtc = &crtc->base;
7452 pipe_config->pixel_multiplier = 1;
7453 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007454
7455 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007456 chv_compute_dpll(crtc, pipe_config);
7457 chv_prepare_pll(crtc, pipe_config);
7458 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007459 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007460 vlv_compute_dpll(crtc, pipe_config);
7461 vlv_prepare_pll(crtc, pipe_config);
7462 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007463 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007464
7465 kfree(pipe_config);
7466
7467 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007468}
7469
7470/**
7471 * vlv_force_pll_off - forcibly disable just the PLL
7472 * @dev_priv: i915 private structure
7473 * @pipe: pipe PLL to disable
7474 *
7475 * Disable the PLL for @pipe. To be used in cases where we need
7476 * the PLL enabled even when @pipe is not going to be enabled.
7477 */
7478void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7479{
7480 if (IS_CHERRYVIEW(dev))
7481 chv_disable_pll(to_i915(dev), pipe);
7482 else
7483 vlv_disable_pll(to_i915(dev), pipe);
7484}
7485
Daniel Vetter251ac862015-06-18 10:30:24 +02007486static void i9xx_compute_dpll(struct intel_crtc *crtc,
7487 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007488 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007489{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007490 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007491 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007492 u32 dpll;
7493 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007494 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007495
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007496 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307497
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007498 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7499 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007500
7501 dpll = DPLL_VGA_MODE_DIS;
7502
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007503 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007504 dpll |= DPLLB_MODE_LVDS;
7505 else
7506 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007507
Daniel Vetteref1b4602013-06-01 17:17:04 +02007508 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007509 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007510 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007511 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007512
7513 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007514 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007515
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007517 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518
7519 /* compute bitmask from p1 value */
7520 if (IS_PINEVIEW(dev))
7521 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7522 else {
7523 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7524 if (IS_G4X(dev) && reduced_clock)
7525 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7526 }
7527 switch (clock->p2) {
7528 case 5:
7529 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7530 break;
7531 case 7:
7532 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7533 break;
7534 case 10:
7535 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7536 break;
7537 case 14:
7538 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7539 break;
7540 }
7541 if (INTEL_INFO(dev)->gen >= 4)
7542 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7543
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007546 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007547 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007548 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7549 else
7550 dpll |= PLL_REF_INPUT_DREFCLK;
7551
7552 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007553 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007554
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007556 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007557 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007558 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559 }
7560}
7561
Daniel Vetter251ac862015-06-18 10:30:24 +02007562static void i8xx_compute_dpll(struct intel_crtc *crtc,
7563 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007564 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007565{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007566 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007567 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007568 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007569 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007571 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307572
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 dpll = DPLL_VGA_MODE_DIS;
7574
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007575 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007576 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7577 } else {
7578 if (clock->p1 == 2)
7579 dpll |= PLL_P1_DIVIDE_BY_TWO;
7580 else
7581 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7582 if (clock->p2 == 4)
7583 dpll |= PLL_P2_DIVIDE_BY_4;
7584 }
7585
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007586 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007587 dpll |= DPLL_DVO_2X_MODE;
7588
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007589 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007590 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007591 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7592 else
7593 dpll |= PLL_REF_INPUT_DREFCLK;
7594
7595 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007596 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597}
7598
Daniel Vetter8a654f32013-06-01 17:16:22 +02007599static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007600{
7601 struct drm_device *dev = intel_crtc->base.dev;
7602 struct drm_i915_private *dev_priv = dev->dev_private;
7603 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007604 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007605 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007606 uint32_t crtc_vtotal, crtc_vblank_end;
7607 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007608
7609 /* We need to be careful not to changed the adjusted mode, for otherwise
7610 * the hw state checker will get angry at the mismatch. */
7611 crtc_vtotal = adjusted_mode->crtc_vtotal;
7612 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007613
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007614 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007615 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007616 crtc_vtotal -= 1;
7617 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007618
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007619 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007620 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7621 else
7622 vsyncshift = adjusted_mode->crtc_hsync_start -
7623 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007624 if (vsyncshift < 0)
7625 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007626 }
7627
7628 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007629 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007630
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007631 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007632 (adjusted_mode->crtc_hdisplay - 1) |
7633 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007634 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007635 (adjusted_mode->crtc_hblank_start - 1) |
7636 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007637 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007638 (adjusted_mode->crtc_hsync_start - 1) |
7639 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7640
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007641 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007642 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007643 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007644 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007645 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007646 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007647 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007648 (adjusted_mode->crtc_vsync_start - 1) |
7649 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7650
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007651 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7652 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7653 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7654 * bits. */
7655 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7656 (pipe == PIPE_B || pipe == PIPE_C))
7657 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7658
Jani Nikulabc58be62016-03-18 17:05:39 +02007659}
7660
7661static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7662{
7663 struct drm_device *dev = intel_crtc->base.dev;
7664 struct drm_i915_private *dev_priv = dev->dev_private;
7665 enum pipe pipe = intel_crtc->pipe;
7666
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007667 /* pipesrc controls the size that is scaled from, which should
7668 * always be the user's requested size.
7669 */
7670 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007671 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7672 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673}
7674
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007675static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007676 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677{
7678 struct drm_device *dev = crtc->base.dev;
7679 struct drm_i915_private *dev_priv = dev->dev_private;
7680 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7681 uint32_t tmp;
7682
7683 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007684 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7685 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007686 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007687 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7688 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007689 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007690 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7691 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007692
7693 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007694 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7695 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007696 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007697 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7698 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007699 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007700 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7701 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007702
7703 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007704 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7705 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7706 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007707 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007708}
7709
7710static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7711 struct intel_crtc_state *pipe_config)
7712{
7713 struct drm_device *dev = crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007716
7717 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007718 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7719 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7720
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007721 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7722 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007723}
7724
Daniel Vetterf6a83282014-02-11 15:28:57 -08007725void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007726 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007727{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007728 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7729 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7730 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7731 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007732
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007733 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7734 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7735 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7736 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007737
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007738 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007739 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007740
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007741 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7742 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007743
7744 mode->hsync = drm_mode_hsync(mode);
7745 mode->vrefresh = drm_mode_vrefresh(mode);
7746 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007747}
7748
Daniel Vetter84b046f2013-02-19 18:48:54 +01007749static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7750{
7751 struct drm_device *dev = intel_crtc->base.dev;
7752 struct drm_i915_private *dev_priv = dev->dev_private;
7753 uint32_t pipeconf;
7754
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007755 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007756
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007757 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7758 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7759 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007760
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007761 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007762 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007763
Daniel Vetterff9ce462013-04-24 14:57:17 +02007764 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007765 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007766 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007767 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007768 pipeconf |= PIPECONF_DITHER_EN |
7769 PIPECONF_DITHER_TYPE_SP;
7770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007771 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007772 case 18:
7773 pipeconf |= PIPECONF_6BPC;
7774 break;
7775 case 24:
7776 pipeconf |= PIPECONF_8BPC;
7777 break;
7778 case 30:
7779 pipeconf |= PIPECONF_10BPC;
7780 break;
7781 default:
7782 /* Case prevented by intel_choose_pipe_bpp_dither. */
7783 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007784 }
7785 }
7786
7787 if (HAS_PIPE_CXSR(dev)) {
7788 if (intel_crtc->lowfreq_avail) {
7789 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7790 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7791 } else {
7792 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007793 }
7794 }
7795
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007796 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007797 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007798 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007799 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7800 else
7801 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7802 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007803 pipeconf |= PIPECONF_PROGRESSIVE;
7804
Wayne Boyer666a4532015-12-09 12:29:35 -08007805 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7806 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007807 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007808
Daniel Vetter84b046f2013-02-19 18:48:54 +01007809 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7810 POSTING_READ(PIPECONF(intel_crtc->pipe));
7811}
7812
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007813static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7814 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007815{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007816 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007817 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007818 int refclk;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007819 intel_clock_t clock;
7820 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007821 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08007822
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007823 memset(&crtc_state->dpll_hw_state, 0,
7824 sizeof(crtc_state->dpll_hw_state));
7825
Jani Nikulaa65347b2015-11-27 12:21:46 +02007826 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007827 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007828
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007829 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007830 refclk = i9xx_get_refclk(crtc_state);
Jani Nikulaf2335332013-09-13 11:03:09 +03007831
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007832 /*
7833 * Returns a set of divisors for the desired target clock with
7834 * the given refclk, or FALSE. The returned values represent
7835 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7836 * 2) / p1 / p2.
7837 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007838 limit = intel_limit(crtc_state, refclk);
7839 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007840 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007841 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007842 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007843 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7844 return -EINVAL;
7845 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007846
Jani Nikulaf2335332013-09-13 11:03:09 +03007847 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007848 crtc_state->dpll.n = clock.n;
7849 crtc_state->dpll.m1 = clock.m1;
7850 crtc_state->dpll.m2 = clock.m2;
7851 crtc_state->dpll.p1 = clock.p1;
7852 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007853 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007854
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007855 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007856 i8xx_compute_dpll(crtc, crtc_state, NULL);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007857 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007858 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007859 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007860 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007861 } else {
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007862 i9xx_compute_dpll(crtc, crtc_state, NULL);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007863 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007864
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007865 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007866}
7867
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007868static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007869 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007870{
7871 struct drm_device *dev = crtc->base.dev;
7872 struct drm_i915_private *dev_priv = dev->dev_private;
7873 uint32_t tmp;
7874
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007875 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7876 return;
7877
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007878 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007879 if (!(tmp & PFIT_ENABLE))
7880 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007881
Daniel Vetter06922822013-07-11 13:35:40 +02007882 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007883 if (INTEL_INFO(dev)->gen < 4) {
7884 if (crtc->pipe != PIPE_B)
7885 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007886 } else {
7887 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7888 return;
7889 }
7890
Daniel Vetter06922822013-07-11 13:35:40 +02007891 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007892 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7893 if (INTEL_INFO(dev)->gen < 5)
7894 pipe_config->gmch_pfit.lvds_border_bits =
7895 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7896}
7897
Jesse Barnesacbec812013-09-20 11:29:32 -07007898static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007899 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007900{
7901 struct drm_device *dev = crtc->base.dev;
7902 struct drm_i915_private *dev_priv = dev->dev_private;
7903 int pipe = pipe_config->cpu_transcoder;
7904 intel_clock_t clock;
7905 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007906 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007907
Shobhit Kumarf573de52014-07-30 20:32:37 +05307908 /* In case of MIPI DPLL will not even be used */
7909 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7910 return;
7911
Ville Syrjäläa5805162015-05-26 20:42:30 +03007912 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007913 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007914 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007915
7916 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7917 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7918 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7919 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7920 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7921
Imre Deakdccbea32015-06-22 23:35:51 +03007922 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007923}
7924
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007925static void
7926i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7927 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007928{
7929 struct drm_device *dev = crtc->base.dev;
7930 struct drm_i915_private *dev_priv = dev->dev_private;
7931 u32 val, base, offset;
7932 int pipe = crtc->pipe, plane = crtc->plane;
7933 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007934 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007935 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007936 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007937
Damien Lespiau42a7b082015-02-05 19:35:13 +00007938 val = I915_READ(DSPCNTR(plane));
7939 if (!(val & DISPLAY_PLANE_ENABLE))
7940 return;
7941
Damien Lespiaud9806c92015-01-21 14:07:19 +00007942 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007943 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007944 DRM_DEBUG_KMS("failed to alloc fb\n");
7945 return;
7946 }
7947
Damien Lespiau1b842c82015-01-21 13:50:54 +00007948 fb = &intel_fb->base;
7949
Daniel Vetter18c52472015-02-10 17:16:09 +00007950 if (INTEL_INFO(dev)->gen >= 4) {
7951 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007952 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007953 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7954 }
7955 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007956
7957 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007958 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007959 fb->pixel_format = fourcc;
7960 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007961
7962 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007963 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007964 offset = I915_READ(DSPTILEOFF(plane));
7965 else
7966 offset = I915_READ(DSPLINOFF(plane));
7967 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7968 } else {
7969 base = I915_READ(DSPADDR(plane));
7970 }
7971 plane_config->base = base;
7972
7973 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007974 fb->width = ((val >> 16) & 0xfff) + 1;
7975 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007976
7977 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007978 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007979
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007980 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007981 fb->pixel_format,
7982 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007983
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007984 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007985
Damien Lespiau2844a922015-01-20 12:51:48 +00007986 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7987 pipe_name(pipe), plane, fb->width, fb->height,
7988 fb->bits_per_pixel, base, fb->pitches[0],
7989 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007990
Damien Lespiau2d140302015-02-05 17:22:18 +00007991 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007992}
7993
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007994static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007995 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007996{
7997 struct drm_device *dev = crtc->base.dev;
7998 struct drm_i915_private *dev_priv = dev->dev_private;
7999 int pipe = pipe_config->cpu_transcoder;
8000 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8001 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008002 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008003 int refclk = 100000;
8004
Ville Syrjäläa5805162015-05-26 20:42:30 +03008005 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008006 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8007 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8008 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8009 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008010 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008011 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008012
8013 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008014 clock.m2 = (pll_dw0 & 0xff) << 22;
8015 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8016 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008017 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8018 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8019 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8020
Imre Deakdccbea32015-06-22 23:35:51 +03008021 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008022}
8023
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008024static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008025 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008026{
8027 struct drm_device *dev = crtc->base.dev;
8028 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008029 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008030 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008031 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008032
Imre Deak17290502016-02-12 18:55:11 +02008033 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8034 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008035 return false;
8036
Daniel Vettere143a212013-07-04 12:01:15 +02008037 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008038 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008039
Imre Deak17290502016-02-12 18:55:11 +02008040 ret = false;
8041
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008042 tmp = I915_READ(PIPECONF(crtc->pipe));
8043 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008044 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008045
Wayne Boyer666a4532015-12-09 12:29:35 -08008046 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008047 switch (tmp & PIPECONF_BPC_MASK) {
8048 case PIPECONF_6BPC:
8049 pipe_config->pipe_bpp = 18;
8050 break;
8051 case PIPECONF_8BPC:
8052 pipe_config->pipe_bpp = 24;
8053 break;
8054 case PIPECONF_10BPC:
8055 pipe_config->pipe_bpp = 30;
8056 break;
8057 default:
8058 break;
8059 }
8060 }
8061
Wayne Boyer666a4532015-12-09 12:29:35 -08008062 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8063 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008064 pipe_config->limited_color_range = true;
8065
Ville Syrjälä282740f2013-09-04 18:30:03 +03008066 if (INTEL_INFO(dev)->gen < 4)
8067 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8068
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008069 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008070 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008071
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008072 i9xx_get_pfit_config(crtc, pipe_config);
8073
Daniel Vetter6c49f242013-06-06 12:45:25 +02008074 if (INTEL_INFO(dev)->gen >= 4) {
8075 tmp = I915_READ(DPLL_MD(crtc->pipe));
8076 pipe_config->pixel_multiplier =
8077 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8078 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008079 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008080 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8081 tmp = I915_READ(DPLL(crtc->pipe));
8082 pipe_config->pixel_multiplier =
8083 ((tmp & SDVO_MULTIPLIER_MASK)
8084 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8085 } else {
8086 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8087 * port and will be fixed up in the encoder->get_config
8088 * function. */
8089 pipe_config->pixel_multiplier = 1;
8090 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008091 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008092 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008093 /*
8094 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8095 * on 830. Filter it out here so that we don't
8096 * report errors due to that.
8097 */
8098 if (IS_I830(dev))
8099 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8100
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008101 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8102 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008103 } else {
8104 /* Mask out read-only status bits. */
8105 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8106 DPLL_PORTC_READY_MASK |
8107 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008108 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008109
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008110 if (IS_CHERRYVIEW(dev))
8111 chv_crtc_clock_get(crtc, pipe_config);
8112 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008113 vlv_crtc_clock_get(crtc, pipe_config);
8114 else
8115 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008116
Ville Syrjälä0f646142015-08-26 19:39:18 +03008117 /*
8118 * Normally the dotclock is filled in by the encoder .get_config()
8119 * but in case the pipe is enabled w/o any ports we need a sane
8120 * default.
8121 */
8122 pipe_config->base.adjusted_mode.crtc_clock =
8123 pipe_config->port_clock / pipe_config->pixel_multiplier;
8124
Imre Deak17290502016-02-12 18:55:11 +02008125 ret = true;
8126
8127out:
8128 intel_display_power_put(dev_priv, power_domain);
8129
8130 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008131}
8132
Paulo Zanonidde86e22012-12-01 12:04:25 -02008133static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008134{
8135 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008136 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008137 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008138 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008139 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008140 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008141 bool has_ck505 = false;
8142 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008143
8144 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008145 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008146 switch (encoder->type) {
8147 case INTEL_OUTPUT_LVDS:
8148 has_panel = true;
8149 has_lvds = true;
8150 break;
8151 case INTEL_OUTPUT_EDP:
8152 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008153 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008154 has_cpu_edp = true;
8155 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008156 default:
8157 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008158 }
8159 }
8160
Keith Packard99eb6a02011-09-26 14:29:12 -07008161 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008162 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008163 can_ssc = has_ck505;
8164 } else {
8165 has_ck505 = false;
8166 can_ssc = true;
8167 }
8168
Imre Deak2de69052013-05-08 13:14:04 +03008169 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8170 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008171
8172 /* Ironlake: try to setup display ref clock before DPLL
8173 * enabling. This is only under driver's control after
8174 * PCH B stepping, previous chipset stepping should be
8175 * ignoring this setting.
8176 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008177 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008178
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008179 /* As we must carefully and slowly disable/enable each source in turn,
8180 * compute the final state we want first and check if we need to
8181 * make any changes at all.
8182 */
8183 final = val;
8184 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008185 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008186 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008187 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008188 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8189
8190 final &= ~DREF_SSC_SOURCE_MASK;
8191 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8192 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008193
Keith Packard199e5d72011-09-22 12:01:57 -07008194 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008195 final |= DREF_SSC_SOURCE_ENABLE;
8196
8197 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8198 final |= DREF_SSC1_ENABLE;
8199
8200 if (has_cpu_edp) {
8201 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8202 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8203 else
8204 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8205 } else
8206 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8207 } else {
8208 final |= DREF_SSC_SOURCE_DISABLE;
8209 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8210 }
8211
8212 if (final == val)
8213 return;
8214
8215 /* Always enable nonspread source */
8216 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8217
8218 if (has_ck505)
8219 val |= DREF_NONSPREAD_CK505_ENABLE;
8220 else
8221 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8222
8223 if (has_panel) {
8224 val &= ~DREF_SSC_SOURCE_MASK;
8225 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008226
Keith Packard199e5d72011-09-22 12:01:57 -07008227 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008228 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008229 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008230 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008231 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008232 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008233
8234 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008235 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008236 POSTING_READ(PCH_DREF_CONTROL);
8237 udelay(200);
8238
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008239 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008240
8241 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008242 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008243 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008244 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008245 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008246 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008247 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008248 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008249 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008250
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008251 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008252 POSTING_READ(PCH_DREF_CONTROL);
8253 udelay(200);
8254 } else {
8255 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8256
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008257 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008258
8259 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008261
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008262 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008263 POSTING_READ(PCH_DREF_CONTROL);
8264 udelay(200);
8265
8266 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008267 val &= ~DREF_SSC_SOURCE_MASK;
8268 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008269
8270 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008272
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008273 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008274 POSTING_READ(PCH_DREF_CONTROL);
8275 udelay(200);
8276 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008277
8278 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008279}
8280
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008281static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008282{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008283 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008284
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008285 tmp = I915_READ(SOUTH_CHICKEN2);
8286 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8287 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008288
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008289 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8290 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8291 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008292
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008293 tmp = I915_READ(SOUTH_CHICKEN2);
8294 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8295 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008296
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008297 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8298 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8299 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008300}
8301
8302/* WaMPhyProgramming:hsw */
8303static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8304{
8305 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008306
8307 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8308 tmp &= ~(0xFF << 24);
8309 tmp |= (0x12 << 24);
8310 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8311
Paulo Zanonidde86e22012-12-01 12:04:25 -02008312 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8313 tmp |= (1 << 11);
8314 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8315
8316 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8317 tmp |= (1 << 11);
8318 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8319
Paulo Zanonidde86e22012-12-01 12:04:25 -02008320 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8321 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8322 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8323
8324 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8325 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8326 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8327
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008328 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8329 tmp &= ~(7 << 13);
8330 tmp |= (5 << 13);
8331 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008332
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008333 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8334 tmp &= ~(7 << 13);
8335 tmp |= (5 << 13);
8336 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008337
8338 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8339 tmp &= ~0xFF;
8340 tmp |= 0x1C;
8341 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8342
8343 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8344 tmp &= ~0xFF;
8345 tmp |= 0x1C;
8346 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8347
8348 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8349 tmp &= ~(0xFF << 16);
8350 tmp |= (0x1C << 16);
8351 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8352
8353 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8354 tmp &= ~(0xFF << 16);
8355 tmp |= (0x1C << 16);
8356 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8357
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008358 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8359 tmp |= (1 << 27);
8360 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008361
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008362 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8363 tmp |= (1 << 27);
8364 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008365
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008366 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8367 tmp &= ~(0xF << 28);
8368 tmp |= (4 << 28);
8369 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008370
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008371 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8372 tmp &= ~(0xF << 28);
8373 tmp |= (4 << 28);
8374 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008375}
8376
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008377/* Implements 3 different sequences from BSpec chapter "Display iCLK
8378 * Programming" based on the parameters passed:
8379 * - Sequence to enable CLKOUT_DP
8380 * - Sequence to enable CLKOUT_DP without spread
8381 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8382 */
8383static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8384 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008385{
8386 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008387 uint32_t reg, tmp;
8388
8389 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8390 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008391 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008392 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008393
Ville Syrjäläa5805162015-05-26 20:42:30 +03008394 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008395
8396 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8397 tmp &= ~SBI_SSCCTL_DISABLE;
8398 tmp |= SBI_SSCCTL_PATHALT;
8399 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8400
8401 udelay(24);
8402
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008403 if (with_spread) {
8404 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8405 tmp &= ~SBI_SSCCTL_PATHALT;
8406 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008407
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008408 if (with_fdi) {
8409 lpt_reset_fdi_mphy(dev_priv);
8410 lpt_program_fdi_mphy(dev_priv);
8411 }
8412 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413
Ville Syrjäläc2699522015-08-27 23:55:59 +03008414 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008415 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8416 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8417 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008418
Ville Syrjäläa5805162015-05-26 20:42:30 +03008419 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008420}
8421
Paulo Zanoni47701c32013-07-23 11:19:25 -03008422/* Sequence to disable CLKOUT_DP */
8423static void lpt_disable_clkout_dp(struct drm_device *dev)
8424{
8425 struct drm_i915_private *dev_priv = dev->dev_private;
8426 uint32_t reg, tmp;
8427
Ville Syrjäläa5805162015-05-26 20:42:30 +03008428 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008429
Ville Syrjäläc2699522015-08-27 23:55:59 +03008430 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008431 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8432 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8433 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8434
8435 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8436 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8437 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8438 tmp |= SBI_SSCCTL_PATHALT;
8439 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8440 udelay(32);
8441 }
8442 tmp |= SBI_SSCCTL_DISABLE;
8443 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8444 }
8445
Ville Syrjäläa5805162015-05-26 20:42:30 +03008446 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008447}
8448
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008449#define BEND_IDX(steps) ((50 + (steps)) / 5)
8450
8451static const uint16_t sscdivintphase[] = {
8452 [BEND_IDX( 50)] = 0x3B23,
8453 [BEND_IDX( 45)] = 0x3B23,
8454 [BEND_IDX( 40)] = 0x3C23,
8455 [BEND_IDX( 35)] = 0x3C23,
8456 [BEND_IDX( 30)] = 0x3D23,
8457 [BEND_IDX( 25)] = 0x3D23,
8458 [BEND_IDX( 20)] = 0x3E23,
8459 [BEND_IDX( 15)] = 0x3E23,
8460 [BEND_IDX( 10)] = 0x3F23,
8461 [BEND_IDX( 5)] = 0x3F23,
8462 [BEND_IDX( 0)] = 0x0025,
8463 [BEND_IDX( -5)] = 0x0025,
8464 [BEND_IDX(-10)] = 0x0125,
8465 [BEND_IDX(-15)] = 0x0125,
8466 [BEND_IDX(-20)] = 0x0225,
8467 [BEND_IDX(-25)] = 0x0225,
8468 [BEND_IDX(-30)] = 0x0325,
8469 [BEND_IDX(-35)] = 0x0325,
8470 [BEND_IDX(-40)] = 0x0425,
8471 [BEND_IDX(-45)] = 0x0425,
8472 [BEND_IDX(-50)] = 0x0525,
8473};
8474
8475/*
8476 * Bend CLKOUT_DP
8477 * steps -50 to 50 inclusive, in steps of 5
8478 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8479 * change in clock period = -(steps / 10) * 5.787 ps
8480 */
8481static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8482{
8483 uint32_t tmp;
8484 int idx = BEND_IDX(steps);
8485
8486 if (WARN_ON(steps % 5 != 0))
8487 return;
8488
8489 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8490 return;
8491
8492 mutex_lock(&dev_priv->sb_lock);
8493
8494 if (steps % 10 != 0)
8495 tmp = 0xAAAAAAAB;
8496 else
8497 tmp = 0x00000000;
8498 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8499
8500 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8501 tmp &= 0xffff0000;
8502 tmp |= sscdivintphase[idx];
8503 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8504
8505 mutex_unlock(&dev_priv->sb_lock);
8506}
8507
8508#undef BEND_IDX
8509
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008510static void lpt_init_pch_refclk(struct drm_device *dev)
8511{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008512 struct intel_encoder *encoder;
8513 bool has_vga = false;
8514
Damien Lespiaub2784e12014-08-05 11:29:37 +01008515 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008516 switch (encoder->type) {
8517 case INTEL_OUTPUT_ANALOG:
8518 has_vga = true;
8519 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008520 default:
8521 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008522 }
8523 }
8524
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008525 if (has_vga) {
8526 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008527 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008528 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008529 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008530 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008531}
8532
Paulo Zanonidde86e22012-12-01 12:04:25 -02008533/*
8534 * Initialize reference clocks when the driver loads
8535 */
8536void intel_init_pch_refclk(struct drm_device *dev)
8537{
8538 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8539 ironlake_init_pch_refclk(dev);
8540 else if (HAS_PCH_LPT(dev))
8541 lpt_init_pch_refclk(dev);
8542}
8543
Daniel Vetter6ff93602013-04-19 11:24:36 +02008544static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008545{
8546 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8548 int pipe = intel_crtc->pipe;
8549 uint32_t val;
8550
Daniel Vetter78114072013-06-13 00:54:57 +02008551 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008552
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008553 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008554 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008555 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008556 break;
8557 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008558 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008559 break;
8560 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008561 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008562 break;
8563 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008564 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008565 break;
8566 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008567 /* Case prevented by intel_choose_pipe_bpp_dither. */
8568 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008569 }
8570
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008571 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008572 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8573
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008574 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008575 val |= PIPECONF_INTERLACED_ILK;
8576 else
8577 val |= PIPECONF_PROGRESSIVE;
8578
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008579 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008580 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008581
Paulo Zanonic8203562012-09-12 10:06:29 -03008582 I915_WRITE(PIPECONF(pipe), val);
8583 POSTING_READ(PIPECONF(pipe));
8584}
8585
Daniel Vetter6ff93602013-04-19 11:24:36 +02008586static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008587{
Jani Nikula391bf042016-03-18 17:05:40 +02008588 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008590 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008591 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008592
Jani Nikula391bf042016-03-18 17:05:40 +02008593 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008594 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8595
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008596 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008597 val |= PIPECONF_INTERLACED_ILK;
8598 else
8599 val |= PIPECONF_PROGRESSIVE;
8600
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008601 I915_WRITE(PIPECONF(cpu_transcoder), val);
8602 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008603}
8604
Jani Nikula391bf042016-03-18 17:05:40 +02008605static void haswell_set_pipemisc(struct drm_crtc *crtc)
8606{
8607 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8609
8610 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8611 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008612
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008613 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008614 case 18:
8615 val |= PIPEMISC_DITHER_6_BPC;
8616 break;
8617 case 24:
8618 val |= PIPEMISC_DITHER_8_BPC;
8619 break;
8620 case 30:
8621 val |= PIPEMISC_DITHER_10_BPC;
8622 break;
8623 case 36:
8624 val |= PIPEMISC_DITHER_12_BPC;
8625 break;
8626 default:
8627 /* Case prevented by pipe_config_set_bpp. */
8628 BUG();
8629 }
8630
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008631 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008632 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8633
Jani Nikula391bf042016-03-18 17:05:40 +02008634 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008635 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008636}
8637
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008638static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008639 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008640 intel_clock_t *clock,
8641 bool *has_reduced_clock,
8642 intel_clock_t *reduced_clock)
8643{
8644 struct drm_device *dev = crtc->dev;
8645 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008646 int refclk;
8647 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008648 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008649
Ander Conselvan de Oliveira8f0d5b92016-03-21 18:00:04 +02008650 refclk = 120000;
8651
8652 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8653 if (intel_panel_use_ssc(dev_priv)) {
8654 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8655 dev_priv->vbt.lvds_ssc_freq);
8656 refclk = dev_priv->vbt.lvds_ssc_freq;
8657 }
8658
8659 if (intel_is_dual_link_lvds(dev)) {
8660 if (refclk == 100000)
8661 limit = &intel_limits_ironlake_dual_lvds_100m;
8662 else
8663 limit = &intel_limits_ironlake_dual_lvds;
8664 } else {
8665 if (refclk == 100000)
8666 limit = &intel_limits_ironlake_single_lvds_100m;
8667 else
8668 limit = &intel_limits_ironlake_single_lvds;
8669 }
Ander Conselvan de Oliveira26ce6d52016-03-21 18:00:03 +02008670 } else {
Ander Conselvan de Oliveira8f0d5b92016-03-21 18:00:04 +02008671 limit = &intel_limits_ironlake_dac;
Ander Conselvan de Oliveira26ce6d52016-03-21 18:00:03 +02008672 }
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008673
8674 /*
8675 * Returns a set of divisors for the desired target clock with the given
8676 * refclk, or FALSE. The returned values represent the clock equation:
8677 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8678 */
Ander Conselvan de Oliveira2d7feac2016-03-21 18:00:05 +02008679 ret = g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8680 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008681 if (!ret)
8682 return false;
8683
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008684 return true;
8685}
8686
Paulo Zanonid4b19312012-11-29 11:29:32 -02008687int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8688{
8689 /*
8690 * Account for spread spectrum to avoid
8691 * oversubscribing the link. Max center spread
8692 * is 2.5%; use 5% for safety's sake.
8693 */
8694 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008695 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008696}
8697
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008698static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008699{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008700 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008701}
8702
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008703static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008704 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008705 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008706 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008707{
8708 struct drm_crtc *crtc = &intel_crtc->base;
8709 struct drm_device *dev = crtc->dev;
8710 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008711 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008712 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008713 struct drm_connector_state *connector_state;
8714 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008715 uint32_t dpll;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008716 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008717 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008718
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008719 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008720 if (connector_state->crtc != crtc_state->base.crtc)
8721 continue;
8722
8723 encoder = to_intel_encoder(connector_state->best_encoder);
8724
8725 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008726 case INTEL_OUTPUT_LVDS:
8727 is_lvds = true;
8728 break;
8729 case INTEL_OUTPUT_SDVO:
8730 case INTEL_OUTPUT_HDMI:
8731 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008732 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008733 default:
8734 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008735 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008736 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008737
Chris Wilsonc1858122010-12-03 21:35:48 +00008738 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008739 factor = 21;
8740 if (is_lvds) {
8741 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008742 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008743 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008744 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008745 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008746 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008747
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008748 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008749 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008750
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008751 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8752 *fp2 |= FP_CB_TUNE;
8753
Chris Wilson5eddb702010-09-11 13:48:45 +01008754 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008755
Eric Anholta07d6782011-03-30 13:01:08 -07008756 if (is_lvds)
8757 dpll |= DPLLB_MODE_LVDS;
8758 else
8759 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008760
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008761 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008762 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008763
8764 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008765 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008766 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008767 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008768
Eric Anholta07d6782011-03-30 13:01:08 -07008769 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008770 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008771 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008772 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008773
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008774 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008775 case 5:
8776 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8777 break;
8778 case 7:
8779 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8780 break;
8781 case 10:
8782 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8783 break;
8784 case 14:
8785 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8786 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008787 }
8788
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008789 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008790 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008791 else
8792 dpll |= PLL_REF_INPUT_DREFCLK;
8793
Daniel Vetter959e16d2013-06-05 13:34:21 +02008794 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008795}
8796
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008797static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8798 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008799{
Jesse Barnes79e53942008-11-07 14:24:08 -08008800 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008801 u32 dpll = 0, fp = 0, fp2 = 0;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008802 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008803 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008804
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008805 memset(&crtc_state->dpll_hw_state, 0,
8806 sizeof(crtc_state->dpll_hw_state));
8807
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008808 crtc->lowfreq_avail = false;
8809
8810 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8811 if (!crtc_state->has_pch_encoder)
8812 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008813
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008814 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008815 if (!ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
8816 &has_reduced_clock,
8817 &reduced_clock)) {
8818 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8819 return -EINVAL;
8820 }
8821
8822 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008823 crtc_state->dpll.n = clock.n;
8824 crtc_state->dpll.m1 = clock.m1;
8825 crtc_state->dpll.m2 = clock.m2;
8826 crtc_state->dpll.p1 = clock.p1;
8827 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008828 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008829
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008830 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8831 if (has_reduced_clock)
8832 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
8833 else
8834 fp2 = fp;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008835
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008836 dpll = ironlake_compute_dpll(crtc, crtc_state,
8837 &fp, &reduced_clock,
8838 has_reduced_clock ? &fp2 : NULL);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008839
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008840 crtc_state->dpll_hw_state.dpll = dpll;
8841 crtc_state->dpll_hw_state.fp0 = fp;
8842 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008843
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008844 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8845 if (pll == NULL) {
8846 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8847 pipe_name(crtc->pipe));
8848 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008849 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008850
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008851 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8852 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008853 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008854
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008855 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856}
8857
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008858static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8859 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008860{
8861 struct drm_device *dev = crtc->base.dev;
8862 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008863 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008864
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008865 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8866 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8867 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8868 & ~TU_SIZE_MASK;
8869 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8870 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8871 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8872}
8873
8874static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8875 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008876 struct intel_link_m_n *m_n,
8877 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008878{
8879 struct drm_device *dev = crtc->base.dev;
8880 struct drm_i915_private *dev_priv = dev->dev_private;
8881 enum pipe pipe = crtc->pipe;
8882
8883 if (INTEL_INFO(dev)->gen >= 5) {
8884 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8885 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8886 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8887 & ~TU_SIZE_MASK;
8888 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8889 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8890 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008891 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8892 * gen < 8) and if DRRS is supported (to make sure the
8893 * registers are not unnecessarily read).
8894 */
8895 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008896 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008897 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8898 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8899 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8900 & ~TU_SIZE_MASK;
8901 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8902 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8903 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8904 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008905 } else {
8906 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8907 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8908 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8909 & ~TU_SIZE_MASK;
8910 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8911 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8912 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8913 }
8914}
8915
8916void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008917 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008918{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008919 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008920 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8921 else
8922 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008923 &pipe_config->dp_m_n,
8924 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008925}
8926
Daniel Vetter72419202013-04-04 13:28:53 +02008927static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008928 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008929{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008930 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008931 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008932}
8933
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008934static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008935 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008936{
8937 struct drm_device *dev = crtc->base.dev;
8938 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008939 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8940 uint32_t ps_ctrl = 0;
8941 int id = -1;
8942 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008943
Chandra Kondurua1b22782015-04-07 15:28:45 -07008944 /* find scaler attached to this pipe */
8945 for (i = 0; i < crtc->num_scalers; i++) {
8946 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8947 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8948 id = i;
8949 pipe_config->pch_pfit.enabled = true;
8950 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8951 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8952 break;
8953 }
8954 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008955
Chandra Kondurua1b22782015-04-07 15:28:45 -07008956 scaler_state->scaler_id = id;
8957 if (id >= 0) {
8958 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8959 } else {
8960 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008961 }
8962}
8963
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008964static void
8965skylake_get_initial_plane_config(struct intel_crtc *crtc,
8966 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008967{
8968 struct drm_device *dev = crtc->base.dev;
8969 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008970 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008971 int pipe = crtc->pipe;
8972 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008973 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008974 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008975 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008976
Damien Lespiaud9806c92015-01-21 14:07:19 +00008977 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008978 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008979 DRM_DEBUG_KMS("failed to alloc fb\n");
8980 return;
8981 }
8982
Damien Lespiau1b842c82015-01-21 13:50:54 +00008983 fb = &intel_fb->base;
8984
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008985 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008986 if (!(val & PLANE_CTL_ENABLE))
8987 goto error;
8988
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008989 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8990 fourcc = skl_format_to_fourcc(pixel_format,
8991 val & PLANE_CTL_ORDER_RGBX,
8992 val & PLANE_CTL_ALPHA_MASK);
8993 fb->pixel_format = fourcc;
8994 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8995
Damien Lespiau40f46282015-02-27 11:15:21 +00008996 tiling = val & PLANE_CTL_TILED_MASK;
8997 switch (tiling) {
8998 case PLANE_CTL_TILED_LINEAR:
8999 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9000 break;
9001 case PLANE_CTL_TILED_X:
9002 plane_config->tiling = I915_TILING_X;
9003 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9004 break;
9005 case PLANE_CTL_TILED_Y:
9006 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9007 break;
9008 case PLANE_CTL_TILED_YF:
9009 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9010 break;
9011 default:
9012 MISSING_CASE(tiling);
9013 goto error;
9014 }
9015
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009016 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9017 plane_config->base = base;
9018
9019 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9020
9021 val = I915_READ(PLANE_SIZE(pipe, 0));
9022 fb->height = ((val >> 16) & 0xfff) + 1;
9023 fb->width = ((val >> 0) & 0x1fff) + 1;
9024
9025 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009026 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009027 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009028 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9029
9030 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009031 fb->pixel_format,
9032 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009033
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009034 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009035
9036 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9037 pipe_name(pipe), fb->width, fb->height,
9038 fb->bits_per_pixel, base, fb->pitches[0],
9039 plane_config->size);
9040
Damien Lespiau2d140302015-02-05 17:22:18 +00009041 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009042 return;
9043
9044error:
9045 kfree(fb);
9046}
9047
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009048static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009049 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009050{
9051 struct drm_device *dev = crtc->base.dev;
9052 struct drm_i915_private *dev_priv = dev->dev_private;
9053 uint32_t tmp;
9054
9055 tmp = I915_READ(PF_CTL(crtc->pipe));
9056
9057 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009058 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009059 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9060 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009061
9062 /* We currently do not free assignements of panel fitters on
9063 * ivb/hsw (since we don't use the higher upscaling modes which
9064 * differentiates them) so just WARN about this case for now. */
9065 if (IS_GEN7(dev)) {
9066 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9067 PF_PIPE_SEL_IVB(crtc->pipe));
9068 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009069 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009070}
9071
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009072static void
9073ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9074 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009075{
9076 struct drm_device *dev = crtc->base.dev;
9077 struct drm_i915_private *dev_priv = dev->dev_private;
9078 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009079 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009080 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009081 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009082 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009083 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009084
Damien Lespiau42a7b082015-02-05 19:35:13 +00009085 val = I915_READ(DSPCNTR(pipe));
9086 if (!(val & DISPLAY_PLANE_ENABLE))
9087 return;
9088
Damien Lespiaud9806c92015-01-21 14:07:19 +00009089 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009090 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009091 DRM_DEBUG_KMS("failed to alloc fb\n");
9092 return;
9093 }
9094
Damien Lespiau1b842c82015-01-21 13:50:54 +00009095 fb = &intel_fb->base;
9096
Daniel Vetter18c52472015-02-10 17:16:09 +00009097 if (INTEL_INFO(dev)->gen >= 4) {
9098 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009099 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009100 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9101 }
9102 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009103
9104 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009105 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009106 fb->pixel_format = fourcc;
9107 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009108
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009109 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009110 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009111 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009112 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009113 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009114 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009115 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009116 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009117 }
9118 plane_config->base = base;
9119
9120 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009121 fb->width = ((val >> 16) & 0xfff) + 1;
9122 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009123
9124 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009125 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009126
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009127 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009128 fb->pixel_format,
9129 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009130
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009131 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009132
Damien Lespiau2844a922015-01-20 12:51:48 +00009133 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9134 pipe_name(pipe), fb->width, fb->height,
9135 fb->bits_per_pixel, base, fb->pitches[0],
9136 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009137
Damien Lespiau2d140302015-02-05 17:22:18 +00009138 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009139}
9140
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009141static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009142 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009143{
9144 struct drm_device *dev = crtc->base.dev;
9145 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009146 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009147 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009148 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009149
Imre Deak17290502016-02-12 18:55:11 +02009150 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9151 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009152 return false;
9153
Daniel Vettere143a212013-07-04 12:01:15 +02009154 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009155 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009156
Imre Deak17290502016-02-12 18:55:11 +02009157 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009158 tmp = I915_READ(PIPECONF(crtc->pipe));
9159 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009160 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009161
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009162 switch (tmp & PIPECONF_BPC_MASK) {
9163 case PIPECONF_6BPC:
9164 pipe_config->pipe_bpp = 18;
9165 break;
9166 case PIPECONF_8BPC:
9167 pipe_config->pipe_bpp = 24;
9168 break;
9169 case PIPECONF_10BPC:
9170 pipe_config->pipe_bpp = 30;
9171 break;
9172 case PIPECONF_12BPC:
9173 pipe_config->pipe_bpp = 36;
9174 break;
9175 default:
9176 break;
9177 }
9178
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009179 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9180 pipe_config->limited_color_range = true;
9181
Daniel Vetterab9412b2013-05-03 11:49:46 +02009182 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009183 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009184 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009185
Daniel Vetter88adfff2013-03-28 10:42:01 +01009186 pipe_config->has_pch_encoder = true;
9187
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009188 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9189 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9190 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009191
9192 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009193
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009194 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009195 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009196 } else {
9197 tmp = I915_READ(PCH_DPLL_SEL);
9198 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009199 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009200 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009201 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009202 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009203
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009204 pipe_config->shared_dpll =
9205 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9206 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009207
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009208 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9209 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009210
9211 tmp = pipe_config->dpll_hw_state.dpll;
9212 pipe_config->pixel_multiplier =
9213 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9214 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009215
9216 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009217 } else {
9218 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009219 }
9220
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009221 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009222 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009223
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009224 ironlake_get_pfit_config(crtc, pipe_config);
9225
Imre Deak17290502016-02-12 18:55:11 +02009226 ret = true;
9227
9228out:
9229 intel_display_power_put(dev_priv, power_domain);
9230
9231 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009232}
9233
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009234static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9235{
9236 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009237 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009238
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009239 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009240 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009241 pipe_name(crtc->pipe));
9242
Rob Clarke2c719b2014-12-15 13:56:32 -05009243 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9244 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009245 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9246 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009247 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9248 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009249 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009250 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009251 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009252 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009253 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009254 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009255 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009256 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009257 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009258
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009259 /*
9260 * In theory we can still leave IRQs enabled, as long as only the HPD
9261 * interrupts remain enabled. We used to check for that, but since it's
9262 * gen-specific and since we only disable LCPLL after we fully disable
9263 * the interrupts, the check below should be enough.
9264 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009265 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009266}
9267
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009268static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9269{
9270 struct drm_device *dev = dev_priv->dev;
9271
9272 if (IS_HASWELL(dev))
9273 return I915_READ(D_COMP_HSW);
9274 else
9275 return I915_READ(D_COMP_BDW);
9276}
9277
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009278static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9279{
9280 struct drm_device *dev = dev_priv->dev;
9281
9282 if (IS_HASWELL(dev)) {
9283 mutex_lock(&dev_priv->rps.hw_lock);
9284 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9285 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009286 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009287 mutex_unlock(&dev_priv->rps.hw_lock);
9288 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009289 I915_WRITE(D_COMP_BDW, val);
9290 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009291 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009292}
9293
9294/*
9295 * This function implements pieces of two sequences from BSpec:
9296 * - Sequence for display software to disable LCPLL
9297 * - Sequence for display software to allow package C8+
9298 * The steps implemented here are just the steps that actually touch the LCPLL
9299 * register. Callers should take care of disabling all the display engine
9300 * functions, doing the mode unset, fixing interrupts, etc.
9301 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009302static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9303 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009304{
9305 uint32_t val;
9306
9307 assert_can_disable_lcpll(dev_priv);
9308
9309 val = I915_READ(LCPLL_CTL);
9310
9311 if (switch_to_fclk) {
9312 val |= LCPLL_CD_SOURCE_FCLK;
9313 I915_WRITE(LCPLL_CTL, val);
9314
9315 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9316 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9317 DRM_ERROR("Switching to FCLK failed\n");
9318
9319 val = I915_READ(LCPLL_CTL);
9320 }
9321
9322 val |= LCPLL_PLL_DISABLE;
9323 I915_WRITE(LCPLL_CTL, val);
9324 POSTING_READ(LCPLL_CTL);
9325
9326 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9327 DRM_ERROR("LCPLL still locked\n");
9328
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009329 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009331 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009332 ndelay(100);
9333
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009334 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9335 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009336 DRM_ERROR("D_COMP RCOMP still in progress\n");
9337
9338 if (allow_power_down) {
9339 val = I915_READ(LCPLL_CTL);
9340 val |= LCPLL_POWER_DOWN_ALLOW;
9341 I915_WRITE(LCPLL_CTL, val);
9342 POSTING_READ(LCPLL_CTL);
9343 }
9344}
9345
9346/*
9347 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9348 * source.
9349 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009350static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009351{
9352 uint32_t val;
9353
9354 val = I915_READ(LCPLL_CTL);
9355
9356 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9357 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9358 return;
9359
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009360 /*
9361 * Make sure we're not on PC8 state before disabling PC8, otherwise
9362 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009363 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009364 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009365
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009366 if (val & LCPLL_POWER_DOWN_ALLOW) {
9367 val &= ~LCPLL_POWER_DOWN_ALLOW;
9368 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009369 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009370 }
9371
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009372 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009373 val |= D_COMP_COMP_FORCE;
9374 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009375 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009376
9377 val = I915_READ(LCPLL_CTL);
9378 val &= ~LCPLL_PLL_DISABLE;
9379 I915_WRITE(LCPLL_CTL, val);
9380
9381 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9382 DRM_ERROR("LCPLL not locked yet\n");
9383
9384 if (val & LCPLL_CD_SOURCE_FCLK) {
9385 val = I915_READ(LCPLL_CTL);
9386 val &= ~LCPLL_CD_SOURCE_FCLK;
9387 I915_WRITE(LCPLL_CTL, val);
9388
9389 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9390 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9391 DRM_ERROR("Switching back to LCPLL failed\n");
9392 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009393
Mika Kuoppala59bad942015-01-16 11:34:40 +02009394 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009395 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009396}
9397
Paulo Zanoni765dab672014-03-07 20:08:18 -03009398/*
9399 * Package states C8 and deeper are really deep PC states that can only be
9400 * reached when all the devices on the system allow it, so even if the graphics
9401 * device allows PC8+, it doesn't mean the system will actually get to these
9402 * states. Our driver only allows PC8+ when going into runtime PM.
9403 *
9404 * The requirements for PC8+ are that all the outputs are disabled, the power
9405 * well is disabled and most interrupts are disabled, and these are also
9406 * requirements for runtime PM. When these conditions are met, we manually do
9407 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9408 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9409 * hang the machine.
9410 *
9411 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9412 * the state of some registers, so when we come back from PC8+ we need to
9413 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9414 * need to take care of the registers kept by RC6. Notice that this happens even
9415 * if we don't put the device in PCI D3 state (which is what currently happens
9416 * because of the runtime PM support).
9417 *
9418 * For more, read "Display Sequences for Package C8" on the hardware
9419 * documentation.
9420 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009421void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009422{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009423 struct drm_device *dev = dev_priv->dev;
9424 uint32_t val;
9425
Paulo Zanonic67a4702013-08-19 13:18:09 -03009426 DRM_DEBUG_KMS("Enabling package C8+\n");
9427
Ville Syrjäläc2699522015-08-27 23:55:59 +03009428 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009429 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9430 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9431 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9432 }
9433
9434 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009435 hsw_disable_lcpll(dev_priv, true, true);
9436}
9437
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009438void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009439{
9440 struct drm_device *dev = dev_priv->dev;
9441 uint32_t val;
9442
Paulo Zanonic67a4702013-08-19 13:18:09 -03009443 DRM_DEBUG_KMS("Disabling package C8+\n");
9444
9445 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009446 lpt_init_pch_refclk(dev);
9447
Ville Syrjäläc2699522015-08-27 23:55:59 +03009448 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009449 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9450 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9451 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9452 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009453}
9454
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009455static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309456{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009457 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009458 struct intel_atomic_state *old_intel_state =
9459 to_intel_atomic_state(old_state);
9460 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309461
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009462 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309463}
9464
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009465/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009466static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009467{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009468 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9469 struct drm_i915_private *dev_priv = state->dev->dev_private;
9470 struct drm_crtc *crtc;
9471 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009472 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009473 unsigned max_pixel_rate = 0, i;
9474 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009475
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009476 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9477 sizeof(intel_state->min_pixclk));
9478
9479 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009480 int pixel_rate;
9481
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009482 crtc_state = to_intel_crtc_state(cstate);
9483 if (!crtc_state->base.enable) {
9484 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009485 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009486 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009487
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009488 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009489
9490 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009491 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009492 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9493
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009494 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009495 }
9496
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009497 for_each_pipe(dev_priv, pipe)
9498 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9499
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009500 return max_pixel_rate;
9501}
9502
9503static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9504{
9505 struct drm_i915_private *dev_priv = dev->dev_private;
9506 uint32_t val, data;
9507 int ret;
9508
9509 if (WARN((I915_READ(LCPLL_CTL) &
9510 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9511 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9512 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9513 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9514 "trying to change cdclk frequency with cdclk not enabled\n"))
9515 return;
9516
9517 mutex_lock(&dev_priv->rps.hw_lock);
9518 ret = sandybridge_pcode_write(dev_priv,
9519 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9520 mutex_unlock(&dev_priv->rps.hw_lock);
9521 if (ret) {
9522 DRM_ERROR("failed to inform pcode about cdclk change\n");
9523 return;
9524 }
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, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009532 DRM_ERROR("Switching to FCLK failed\n");
9533
9534 val = I915_READ(LCPLL_CTL);
9535 val &= ~LCPLL_CLK_FREQ_MASK;
9536
9537 switch (cdclk) {
9538 case 450000:
9539 val |= LCPLL_CLK_FREQ_450;
9540 data = 0;
9541 break;
9542 case 540000:
9543 val |= LCPLL_CLK_FREQ_54O_BDW;
9544 data = 1;
9545 break;
9546 case 337500:
9547 val |= LCPLL_CLK_FREQ_337_5_BDW;
9548 data = 2;
9549 break;
9550 case 675000:
9551 val |= LCPLL_CLK_FREQ_675_BDW;
9552 data = 3;
9553 break;
9554 default:
9555 WARN(1, "invalid cdclk frequency\n");
9556 return;
9557 }
9558
9559 I915_WRITE(LCPLL_CTL, val);
9560
9561 val = I915_READ(LCPLL_CTL);
9562 val &= ~LCPLL_CD_SOURCE_FCLK;
9563 I915_WRITE(LCPLL_CTL, val);
9564
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009565 if (wait_for_us((I915_READ(LCPLL_CTL) &
9566 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009567 DRM_ERROR("Switching back to LCPLL failed\n");
9568
9569 mutex_lock(&dev_priv->rps.hw_lock);
9570 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9571 mutex_unlock(&dev_priv->rps.hw_lock);
9572
9573 intel_update_cdclk(dev);
9574
9575 WARN(cdclk != dev_priv->cdclk_freq,
9576 "cdclk requested %d kHz but got %d kHz\n",
9577 cdclk, dev_priv->cdclk_freq);
9578}
9579
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009580static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009581{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009582 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009583 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009584 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009585 int cdclk;
9586
9587 /*
9588 * FIXME should also account for plane ratio
9589 * once 64bpp pixel formats are supported.
9590 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009591 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009592 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009593 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009594 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009595 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009596 cdclk = 450000;
9597 else
9598 cdclk = 337500;
9599
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009600 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009601 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9602 cdclk, dev_priv->max_cdclk_freq);
9603 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009604 }
9605
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009606 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9607 if (!intel_state->active_crtcs)
9608 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009609
9610 return 0;
9611}
9612
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009613static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009614{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009615 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009616 struct intel_atomic_state *old_intel_state =
9617 to_intel_atomic_state(old_state);
9618 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009619
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009620 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009621}
9622
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009623static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9624 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009625{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009626 struct intel_encoder *intel_encoder =
9627 intel_ddi_get_crtc_new_encoder(crtc_state);
9628
9629 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9630 if (!intel_ddi_pll_select(crtc, crtc_state))
9631 return -EINVAL;
9632 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009633
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009634 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009635
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009636 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009637}
9638
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309639static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9640 enum port port,
9641 struct intel_crtc_state *pipe_config)
9642{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009643 enum intel_dpll_id id;
9644
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309645 switch (port) {
9646 case PORT_A:
9647 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009648 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309649 break;
9650 case PORT_B:
9651 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009652 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309653 break;
9654 case PORT_C:
9655 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009656 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309657 break;
9658 default:
9659 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009660 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309661 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009662
9663 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309664}
9665
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009666static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9667 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009668 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009669{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009670 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009671 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009672
9673 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9674 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9675
9676 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009677 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009678 id = DPLL_ID_SKL_DPLL0;
9679 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009680 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009681 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009682 break;
9683 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009684 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009685 break;
9686 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009687 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009688 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009689 default:
9690 MISSING_CASE(pipe_config->ddi_pll_sel);
9691 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009692 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009693
9694 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009695}
9696
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009697static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9698 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009699 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009700{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009701 enum intel_dpll_id id;
9702
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009703 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9704
9705 switch (pipe_config->ddi_pll_sel) {
9706 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009707 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009708 break;
9709 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009710 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009711 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009712 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009713 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009714 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009715 case PORT_CLK_SEL_LCPLL_810:
9716 id = DPLL_ID_LCPLL_810;
9717 break;
9718 case PORT_CLK_SEL_LCPLL_1350:
9719 id = DPLL_ID_LCPLL_1350;
9720 break;
9721 case PORT_CLK_SEL_LCPLL_2700:
9722 id = DPLL_ID_LCPLL_2700;
9723 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009724 default:
9725 MISSING_CASE(pipe_config->ddi_pll_sel);
9726 /* fall through */
9727 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009728 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009729 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009730
9731 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009732}
9733
Jani Nikulacf304292016-03-18 17:05:41 +02009734static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9735 struct intel_crtc_state *pipe_config,
9736 unsigned long *power_domain_mask)
9737{
9738 struct drm_device *dev = crtc->base.dev;
9739 struct drm_i915_private *dev_priv = dev->dev_private;
9740 enum intel_display_power_domain power_domain;
9741 u32 tmp;
9742
9743 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9744
9745 /*
9746 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9747 * consistency and less surprising code; it's in always on power).
9748 */
9749 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9750 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9751 enum pipe trans_edp_pipe;
9752 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9753 default:
9754 WARN(1, "unknown pipe linked to edp transcoder\n");
9755 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9756 case TRANS_DDI_EDP_INPUT_A_ON:
9757 trans_edp_pipe = PIPE_A;
9758 break;
9759 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9760 trans_edp_pipe = PIPE_B;
9761 break;
9762 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9763 trans_edp_pipe = PIPE_C;
9764 break;
9765 }
9766
9767 if (trans_edp_pipe == crtc->pipe)
9768 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9769 }
9770
9771 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9772 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9773 return false;
9774 *power_domain_mask |= BIT(power_domain);
9775
9776 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9777
9778 return tmp & PIPECONF_ENABLE;
9779}
9780
Jani Nikula4d1de972016-03-18 17:05:42 +02009781static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9782 struct intel_crtc_state *pipe_config,
9783 unsigned long *power_domain_mask)
9784{
9785 struct drm_device *dev = crtc->base.dev;
9786 struct drm_i915_private *dev_priv = dev->dev_private;
9787 enum intel_display_power_domain power_domain;
9788 enum port port;
9789 enum transcoder cpu_transcoder;
9790 u32 tmp;
9791
9792 pipe_config->has_dsi_encoder = false;
9793
9794 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9795 if (port == PORT_A)
9796 cpu_transcoder = TRANSCODER_DSI_A;
9797 else
9798 cpu_transcoder = TRANSCODER_DSI_C;
9799
9800 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9801 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9802 continue;
9803 *power_domain_mask |= BIT(power_domain);
9804
9805 /* XXX: this works for video mode only */
9806 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9807 if (!(tmp & DPI_ENABLE))
9808 continue;
9809
9810 tmp = I915_READ(MIPI_CTRL(port));
9811 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9812 continue;
9813
9814 pipe_config->cpu_transcoder = cpu_transcoder;
9815 pipe_config->has_dsi_encoder = true;
9816 break;
9817 }
9818
9819 return pipe_config->has_dsi_encoder;
9820}
9821
Daniel Vetter26804af2014-06-25 22:01:55 +03009822static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009823 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009824{
9825 struct drm_device *dev = crtc->base.dev;
9826 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009827 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009828 enum port port;
9829 uint32_t tmp;
9830
9831 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9832
9833 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9834
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009835 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009836 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309837 else if (IS_BROXTON(dev))
9838 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009839 else
9840 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009841
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009842 pll = pipe_config->shared_dpll;
9843 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009844 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9845 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009846 }
9847
Daniel Vetter26804af2014-06-25 22:01:55 +03009848 /*
9849 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9850 * DDI E. So just check whether this pipe is wired to DDI E and whether
9851 * the PCH transcoder is on.
9852 */
Damien Lespiauca370452013-12-03 13:56:24 +00009853 if (INTEL_INFO(dev)->gen < 9 &&
9854 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009855 pipe_config->has_pch_encoder = true;
9856
9857 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9858 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9859 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9860
9861 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9862 }
9863}
9864
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009865static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009866 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009867{
9868 struct drm_device *dev = crtc->base.dev;
9869 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009870 enum intel_display_power_domain power_domain;
9871 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009872 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009873
Imre Deak17290502016-02-12 18:55:11 +02009874 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9875 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009876 return false;
Imre Deak17290502016-02-12 18:55:11 +02009877 power_domain_mask = BIT(power_domain);
9878
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009879 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009880
Jani Nikulacf304292016-03-18 17:05:41 +02009881 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009882
Jani Nikula4d1de972016-03-18 17:05:42 +02009883 if (IS_BROXTON(dev_priv)) {
9884 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9885 &power_domain_mask);
9886 WARN_ON(active && pipe_config->has_dsi_encoder);
9887 if (pipe_config->has_dsi_encoder)
9888 active = true;
9889 }
9890
Jani Nikulacf304292016-03-18 17:05:41 +02009891 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009892 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009893
Jani Nikula4d1de972016-03-18 17:05:42 +02009894 if (!pipe_config->has_dsi_encoder) {
9895 haswell_get_ddi_port_state(crtc, pipe_config);
9896 intel_get_pipe_timings(crtc, pipe_config);
9897 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009898
Jani Nikulabc58be62016-03-18 17:05:39 +02009899 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009900
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009901 pipe_config->gamma_mode =
9902 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9903
Chandra Kondurua1b22782015-04-07 15:28:45 -07009904 if (INTEL_INFO(dev)->gen >= 9) {
9905 skl_init_scalers(dev, crtc, pipe_config);
9906 }
9907
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009908 if (INTEL_INFO(dev)->gen >= 9) {
9909 pipe_config->scaler_state.scaler_id = -1;
9910 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9911 }
9912
Imre Deak17290502016-02-12 18:55:11 +02009913 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9914 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9915 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009916 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009917 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009918 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009919 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009920 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009921
Jesse Barnese59150d2014-01-07 13:30:45 -08009922 if (IS_HASWELL(dev))
9923 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9924 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009925
Jani Nikula4d1de972016-03-18 17:05:42 +02009926 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9927 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009928 pipe_config->pixel_multiplier =
9929 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9930 } else {
9931 pipe_config->pixel_multiplier = 1;
9932 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009933
Imre Deak17290502016-02-12 18:55:11 +02009934out:
9935 for_each_power_domain(power_domain, power_domain_mask)
9936 intel_display_power_put(dev_priv, power_domain);
9937
Jani Nikulacf304292016-03-18 17:05:41 +02009938 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009939}
9940
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009941static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9942 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009943{
9944 struct drm_device *dev = crtc->dev;
9945 struct drm_i915_private *dev_priv = dev->dev_private;
9946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009947 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009948
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009949 if (plane_state && plane_state->visible) {
9950 unsigned int width = plane_state->base.crtc_w;
9951 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009952 unsigned int stride = roundup_pow_of_two(width) * 4;
9953
9954 switch (stride) {
9955 default:
9956 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9957 width, stride);
9958 stride = 256;
9959 /* fallthrough */
9960 case 256:
9961 case 512:
9962 case 1024:
9963 case 2048:
9964 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009965 }
9966
Ville Syrjälädc41c152014-08-13 11:57:05 +03009967 cntl |= CURSOR_ENABLE |
9968 CURSOR_GAMMA_ENABLE |
9969 CURSOR_FORMAT_ARGB |
9970 CURSOR_STRIDE(stride);
9971
9972 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009973 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009974
Ville Syrjälädc41c152014-08-13 11:57:05 +03009975 if (intel_crtc->cursor_cntl != 0 &&
9976 (intel_crtc->cursor_base != base ||
9977 intel_crtc->cursor_size != size ||
9978 intel_crtc->cursor_cntl != cntl)) {
9979 /* On these chipsets we can only modify the base/size/stride
9980 * whilst the cursor is disabled.
9981 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009982 I915_WRITE(CURCNTR(PIPE_A), 0);
9983 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +03009984 intel_crtc->cursor_cntl = 0;
9985 }
9986
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009987 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009988 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009989 intel_crtc->cursor_base = base;
9990 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009991
9992 if (intel_crtc->cursor_size != size) {
9993 I915_WRITE(CURSIZE, size);
9994 intel_crtc->cursor_size = size;
9995 }
9996
Chris Wilson4b0e3332014-05-30 16:35:26 +03009997 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009998 I915_WRITE(CURCNTR(PIPE_A), cntl);
9999 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010000 intel_crtc->cursor_cntl = cntl;
10001 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010002}
10003
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010004static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10005 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010006{
10007 struct drm_device *dev = crtc->dev;
10008 struct drm_i915_private *dev_priv = dev->dev_private;
10009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10010 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010011 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010012
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010013 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010014 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010015 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010016 case 64:
10017 cntl |= CURSOR_MODE_64_ARGB_AX;
10018 break;
10019 case 128:
10020 cntl |= CURSOR_MODE_128_ARGB_AX;
10021 break;
10022 case 256:
10023 cntl |= CURSOR_MODE_256_ARGB_AX;
10024 break;
10025 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010026 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010027 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010028 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010029 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010030
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010031 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010032 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010033
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010034 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10035 cntl |= CURSOR_ROTATE_180;
10036 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010037
Chris Wilson4b0e3332014-05-30 16:35:26 +030010038 if (intel_crtc->cursor_cntl != cntl) {
10039 I915_WRITE(CURCNTR(pipe), cntl);
10040 POSTING_READ(CURCNTR(pipe));
10041 intel_crtc->cursor_cntl = cntl;
10042 }
10043
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010044 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010045 I915_WRITE(CURBASE(pipe), base);
10046 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010047
10048 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010049}
10050
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010051/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010052static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010053 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010054{
10055 struct drm_device *dev = crtc->dev;
10056 struct drm_i915_private *dev_priv = dev->dev_private;
10057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10058 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010059 u32 base = intel_crtc->cursor_addr;
10060 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010061
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010062 if (plane_state) {
10063 int x = plane_state->base.crtc_x;
10064 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010065
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010066 if (x < 0) {
10067 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10068 x = -x;
10069 }
10070 pos |= x << CURSOR_X_SHIFT;
10071
10072 if (y < 0) {
10073 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10074 y = -y;
10075 }
10076 pos |= y << CURSOR_Y_SHIFT;
10077
10078 /* ILK+ do this automagically */
10079 if (HAS_GMCH_DISPLAY(dev) &&
10080 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10081 base += (plane_state->base.crtc_h *
10082 plane_state->base.crtc_w - 1) * 4;
10083 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010084 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010085
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010086 I915_WRITE(CURPOS(pipe), pos);
10087
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010088 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010089 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010090 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010091 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010092}
10093
Ville Syrjälädc41c152014-08-13 11:57:05 +030010094static bool cursor_size_ok(struct drm_device *dev,
10095 uint32_t width, uint32_t height)
10096{
10097 if (width == 0 || height == 0)
10098 return false;
10099
10100 /*
10101 * 845g/865g are special in that they are only limited by
10102 * the width of their cursors, the height is arbitrary up to
10103 * the precision of the register. Everything else requires
10104 * square cursors, limited to a few power-of-two sizes.
10105 */
10106 if (IS_845G(dev) || IS_I865G(dev)) {
10107 if ((width & 63) != 0)
10108 return false;
10109
10110 if (width > (IS_845G(dev) ? 64 : 512))
10111 return false;
10112
10113 if (height > 1023)
10114 return false;
10115 } else {
10116 switch (width | height) {
10117 case 256:
10118 case 128:
10119 if (IS_GEN2(dev))
10120 return false;
10121 case 64:
10122 break;
10123 default:
10124 return false;
10125 }
10126 }
10127
10128 return true;
10129}
10130
Jesse Barnes79e53942008-11-07 14:24:08 -080010131/* VESA 640x480x72Hz mode to set on the pipe */
10132static struct drm_display_mode load_detect_mode = {
10133 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10134 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10135};
10136
Daniel Vettera8bb6812014-02-10 18:00:39 +010010137struct drm_framebuffer *
10138__intel_framebuffer_create(struct drm_device *dev,
10139 struct drm_mode_fb_cmd2 *mode_cmd,
10140 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010141{
10142 struct intel_framebuffer *intel_fb;
10143 int ret;
10144
10145 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010146 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010147 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010148
10149 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010150 if (ret)
10151 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010152
10153 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010154
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010155err:
10156 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010157 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010158}
10159
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010160static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010161intel_framebuffer_create(struct drm_device *dev,
10162 struct drm_mode_fb_cmd2 *mode_cmd,
10163 struct drm_i915_gem_object *obj)
10164{
10165 struct drm_framebuffer *fb;
10166 int ret;
10167
10168 ret = i915_mutex_lock_interruptible(dev);
10169 if (ret)
10170 return ERR_PTR(ret);
10171 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10172 mutex_unlock(&dev->struct_mutex);
10173
10174 return fb;
10175}
10176
Chris Wilsond2dff872011-04-19 08:36:26 +010010177static u32
10178intel_framebuffer_pitch_for_width(int width, int bpp)
10179{
10180 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10181 return ALIGN(pitch, 64);
10182}
10183
10184static u32
10185intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10186{
10187 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010188 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010189}
10190
10191static struct drm_framebuffer *
10192intel_framebuffer_create_for_mode(struct drm_device *dev,
10193 struct drm_display_mode *mode,
10194 int depth, int bpp)
10195{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010196 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010197 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010198 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010199
10200 obj = i915_gem_alloc_object(dev,
10201 intel_framebuffer_size_for_mode(mode, bpp));
10202 if (obj == NULL)
10203 return ERR_PTR(-ENOMEM);
10204
10205 mode_cmd.width = mode->hdisplay;
10206 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010207 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10208 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010209 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010210
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010211 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10212 if (IS_ERR(fb))
10213 drm_gem_object_unreference_unlocked(&obj->base);
10214
10215 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010216}
10217
10218static struct drm_framebuffer *
10219mode_fits_in_fbdev(struct drm_device *dev,
10220 struct drm_display_mode *mode)
10221{
Daniel Vetter06957262015-08-10 13:34:08 +020010222#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010223 struct drm_i915_private *dev_priv = dev->dev_private;
10224 struct drm_i915_gem_object *obj;
10225 struct drm_framebuffer *fb;
10226
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010227 if (!dev_priv->fbdev)
10228 return NULL;
10229
10230 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010231 return NULL;
10232
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010233 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010234 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010235
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010236 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010237 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10238 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010239 return NULL;
10240
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010241 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010242 return NULL;
10243
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010244 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010245 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010246#else
10247 return NULL;
10248#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010249}
10250
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010251static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10252 struct drm_crtc *crtc,
10253 struct drm_display_mode *mode,
10254 struct drm_framebuffer *fb,
10255 int x, int y)
10256{
10257 struct drm_plane_state *plane_state;
10258 int hdisplay, vdisplay;
10259 int ret;
10260
10261 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10262 if (IS_ERR(plane_state))
10263 return PTR_ERR(plane_state);
10264
10265 if (mode)
10266 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10267 else
10268 hdisplay = vdisplay = 0;
10269
10270 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10271 if (ret)
10272 return ret;
10273 drm_atomic_set_fb_for_plane(plane_state, fb);
10274 plane_state->crtc_x = 0;
10275 plane_state->crtc_y = 0;
10276 plane_state->crtc_w = hdisplay;
10277 plane_state->crtc_h = vdisplay;
10278 plane_state->src_x = x << 16;
10279 plane_state->src_y = y << 16;
10280 plane_state->src_w = hdisplay << 16;
10281 plane_state->src_h = vdisplay << 16;
10282
10283 return 0;
10284}
10285
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010286bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010287 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010288 struct intel_load_detect_pipe *old,
10289 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010290{
10291 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010292 struct intel_encoder *intel_encoder =
10293 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010294 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010295 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010296 struct drm_crtc *crtc = NULL;
10297 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010298 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010299 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010300 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010301 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010302 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010303 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010304
Chris Wilsond2dff872011-04-19 08:36:26 +010010305 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010306 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010307 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010308
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010309 old->restore_state = NULL;
10310
Rob Clark51fd3712013-11-19 12:10:12 -050010311retry:
10312 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10313 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010314 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010315
Jesse Barnes79e53942008-11-07 14:24:08 -080010316 /*
10317 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010318 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010319 * - if the connector already has an assigned crtc, use it (but make
10320 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010321 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010322 * - try to find the first unused crtc that can drive this connector,
10323 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010324 */
10325
10326 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010327 if (connector->state->crtc) {
10328 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010329
Rob Clark51fd3712013-11-19 12:10:12 -050010330 ret = drm_modeset_lock(&crtc->mutex, ctx);
10331 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010332 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010333
10334 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010335 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010336 }
10337
10338 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010339 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010340 i++;
10341 if (!(encoder->possible_crtcs & (1 << i)))
10342 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010343
10344 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10345 if (ret)
10346 goto fail;
10347
10348 if (possible_crtc->state->enable) {
10349 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010350 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010351 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010352
10353 crtc = possible_crtc;
10354 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010355 }
10356
10357 /*
10358 * If we didn't find an unused CRTC, don't use any.
10359 */
10360 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010361 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010362 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010363 }
10364
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010365found:
10366 intel_crtc = to_intel_crtc(crtc);
10367
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010368 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10369 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010370 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010371
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010372 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010373 restore_state = drm_atomic_state_alloc(dev);
10374 if (!state || !restore_state) {
10375 ret = -ENOMEM;
10376 goto fail;
10377 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010378
10379 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010380 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010381
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010382 connector_state = drm_atomic_get_connector_state(state, connector);
10383 if (IS_ERR(connector_state)) {
10384 ret = PTR_ERR(connector_state);
10385 goto fail;
10386 }
10387
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010388 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10389 if (ret)
10390 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010391
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010392 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10393 if (IS_ERR(crtc_state)) {
10394 ret = PTR_ERR(crtc_state);
10395 goto fail;
10396 }
10397
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010398 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010399
Chris Wilson64927112011-04-20 07:25:26 +010010400 if (!mode)
10401 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010402
Chris Wilsond2dff872011-04-19 08:36:26 +010010403 /* We need a framebuffer large enough to accommodate all accesses
10404 * that the plane may generate whilst we perform load detection.
10405 * We can not rely on the fbcon either being present (we get called
10406 * during its initialisation to detect all boot displays, or it may
10407 * not even exist) or that it is large enough to satisfy the
10408 * requested mode.
10409 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010410 fb = mode_fits_in_fbdev(dev, mode);
10411 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010412 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010413 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010414 } else
10415 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010416 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010417 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010418 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010419 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010420
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010421 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10422 if (ret)
10423 goto fail;
10424
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010425 drm_framebuffer_unreference(fb);
10426
10427 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10428 if (ret)
10429 goto fail;
10430
10431 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10432 if (!ret)
10433 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10434 if (!ret)
10435 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10436 if (ret) {
10437 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10438 goto fail;
10439 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010440
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010441 ret = drm_atomic_commit(state);
10442 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010443 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010444 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010445 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010446
10447 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010448
Jesse Barnes79e53942008-11-07 14:24:08 -080010449 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010450 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010451 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010452
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010453fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010454 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010455 drm_atomic_state_free(restore_state);
10456 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010457
Rob Clark51fd3712013-11-19 12:10:12 -050010458 if (ret == -EDEADLK) {
10459 drm_modeset_backoff(ctx);
10460 goto retry;
10461 }
10462
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010463 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010464}
10465
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010466void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010467 struct intel_load_detect_pipe *old,
10468 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010469{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010470 struct intel_encoder *intel_encoder =
10471 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010472 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010473 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010474 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010475
Chris Wilsond2dff872011-04-19 08:36:26 +010010476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010477 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010478 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010479
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010480 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010481 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010482
10483 ret = drm_atomic_commit(state);
10484 if (ret) {
10485 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10486 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010488}
10489
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010490static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010491 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010492{
10493 struct drm_i915_private *dev_priv = dev->dev_private;
10494 u32 dpll = pipe_config->dpll_hw_state.dpll;
10495
10496 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010497 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010498 else if (HAS_PCH_SPLIT(dev))
10499 return 120000;
10500 else if (!IS_GEN2(dev))
10501 return 96000;
10502 else
10503 return 48000;
10504}
10505
Jesse Barnes79e53942008-11-07 14:24:08 -080010506/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010507static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010508 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010509{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010510 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010511 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010512 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010513 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010514 u32 fp;
10515 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010516 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010517 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010518
10519 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010520 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010521 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010522 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010523
10524 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010525 if (IS_PINEVIEW(dev)) {
10526 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10527 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010528 } else {
10529 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10530 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10531 }
10532
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010533 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010534 if (IS_PINEVIEW(dev))
10535 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10536 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010537 else
10538 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010539 DPLL_FPA01_P1_POST_DIV_SHIFT);
10540
10541 switch (dpll & DPLL_MODE_MASK) {
10542 case DPLLB_MODE_DAC_SERIAL:
10543 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10544 5 : 10;
10545 break;
10546 case DPLLB_MODE_LVDS:
10547 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10548 7 : 14;
10549 break;
10550 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010551 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010552 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010553 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010554 }
10555
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010556 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010557 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010558 else
Imre Deakdccbea32015-06-22 23:35:51 +030010559 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010560 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010561 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010562 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010563
10564 if (is_lvds) {
10565 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10566 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010567
10568 if (lvds & LVDS_CLKB_POWER_UP)
10569 clock.p2 = 7;
10570 else
10571 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010572 } else {
10573 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10574 clock.p1 = 2;
10575 else {
10576 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10577 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10578 }
10579 if (dpll & PLL_P2_DIVIDE_BY_4)
10580 clock.p2 = 4;
10581 else
10582 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010583 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010584
Imre Deakdccbea32015-06-22 23:35:51 +030010585 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010586 }
10587
Ville Syrjälä18442d02013-09-13 16:00:08 +030010588 /*
10589 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010590 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010591 * encoder's get_config() function.
10592 */
Imre Deakdccbea32015-06-22 23:35:51 +030010593 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010594}
10595
Ville Syrjälä6878da02013-09-13 15:59:11 +030010596int intel_dotclock_calculate(int link_freq,
10597 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010598{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010599 /*
10600 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010601 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010602 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010603 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010604 *
10605 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010606 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010607 */
10608
Ville Syrjälä6878da02013-09-13 15:59:11 +030010609 if (!m_n->link_n)
10610 return 0;
10611
10612 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10613}
10614
Ville Syrjälä18442d02013-09-13 16:00:08 +030010615static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010616 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010617{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010618 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010619
10620 /* read out port_clock from the DPLL */
10621 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010622
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010623 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010624 * In case there is an active pipe without active ports,
10625 * we may need some idea for the dotclock anyway.
10626 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010627 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010628 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010629 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010630 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010631}
10632
10633/** Returns the currently programmed mode of the given pipe. */
10634struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10635 struct drm_crtc *crtc)
10636{
Jesse Barnes548f2452011-02-17 10:40:53 -080010637 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010639 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010640 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010641 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010642 int htot = I915_READ(HTOTAL(cpu_transcoder));
10643 int hsync = I915_READ(HSYNC(cpu_transcoder));
10644 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10645 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010646 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010647
10648 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10649 if (!mode)
10650 return NULL;
10651
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010652 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10653 if (!pipe_config) {
10654 kfree(mode);
10655 return NULL;
10656 }
10657
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010658 /*
10659 * Construct a pipe_config sufficient for getting the clock info
10660 * back out of crtc_clock_get.
10661 *
10662 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10663 * to use a real value here instead.
10664 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010665 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10666 pipe_config->pixel_multiplier = 1;
10667 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10668 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10669 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10670 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010671
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010672 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010673 mode->hdisplay = (htot & 0xffff) + 1;
10674 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10675 mode->hsync_start = (hsync & 0xffff) + 1;
10676 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10677 mode->vdisplay = (vtot & 0xffff) + 1;
10678 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10679 mode->vsync_start = (vsync & 0xffff) + 1;
10680 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10681
10682 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010683
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010684 kfree(pipe_config);
10685
Jesse Barnes79e53942008-11-07 14:24:08 -080010686 return mode;
10687}
10688
Chris Wilsonf047e392012-07-21 12:31:41 +010010689void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010690{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010691 struct drm_i915_private *dev_priv = dev->dev_private;
10692
Chris Wilsonf62a0072014-02-21 17:55:39 +000010693 if (dev_priv->mm.busy)
10694 return;
10695
Paulo Zanoni43694d62014-03-07 20:08:08 -030010696 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010697 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010698 if (INTEL_INFO(dev)->gen >= 6)
10699 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010700 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010701}
10702
10703void intel_mark_idle(struct drm_device *dev)
10704{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010705 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010706
Chris Wilsonf62a0072014-02-21 17:55:39 +000010707 if (!dev_priv->mm.busy)
10708 return;
10709
10710 dev_priv->mm.busy = false;
10711
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010712 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010713 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010714
Paulo Zanoni43694d62014-03-07 20:08:08 -030010715 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010716}
10717
Jesse Barnes79e53942008-11-07 14:24:08 -080010718static void intel_crtc_destroy(struct drm_crtc *crtc)
10719{
10720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010721 struct drm_device *dev = crtc->dev;
10722 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010723
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010724 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010725 work = intel_crtc->unpin_work;
10726 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010727 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010728
10729 if (work) {
10730 cancel_work_sync(&work->work);
10731 kfree(work);
10732 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010733
10734 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010735
Jesse Barnes79e53942008-11-07 14:24:08 -080010736 kfree(intel_crtc);
10737}
10738
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010739static void intel_unpin_work_fn(struct work_struct *__work)
10740{
10741 struct intel_unpin_work *work =
10742 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010743 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10744 struct drm_device *dev = crtc->base.dev;
10745 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010746
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010747 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010748 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010749 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010750
John Harrisonf06cc1b2014-11-24 18:49:37 +000010751 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010752 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010753 mutex_unlock(&dev->struct_mutex);
10754
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010755 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010756 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010757 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010758
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010759 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10760 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010761
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010762 kfree(work);
10763}
10764
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010765static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010766 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010767{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10769 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010770 unsigned long flags;
10771
10772 /* Ignore early vblank irqs */
10773 if (intel_crtc == NULL)
10774 return;
10775
Daniel Vetterf3260382014-09-15 14:55:23 +020010776 /*
10777 * This is called both by irq handlers and the reset code (to complete
10778 * lost pageflips) so needs the full irqsave spinlocks.
10779 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010780 spin_lock_irqsave(&dev->event_lock, flags);
10781 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010782
10783 /* Ensure we don't miss a work->pending update ... */
10784 smp_rmb();
10785
10786 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010787 spin_unlock_irqrestore(&dev->event_lock, flags);
10788 return;
10789 }
10790
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010791 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010792
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010793 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010794}
10795
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010796void intel_finish_page_flip(struct drm_device *dev, int pipe)
10797{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010798 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010799 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10800
Mario Kleiner49b14a52010-12-09 07:00:07 +010010801 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010802}
10803
10804void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10805{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010806 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010807 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10808
Mario Kleiner49b14a52010-12-09 07:00:07 +010010809 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010810}
10811
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010812/* Is 'a' after or equal to 'b'? */
10813static bool g4x_flip_count_after_eq(u32 a, u32 b)
10814{
10815 return !((a - b) & 0x80000000);
10816}
10817
10818static bool page_flip_finished(struct intel_crtc *crtc)
10819{
10820 struct drm_device *dev = crtc->base.dev;
10821 struct drm_i915_private *dev_priv = dev->dev_private;
10822
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010823 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10824 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10825 return true;
10826
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010827 /*
10828 * The relevant registers doen't exist on pre-ctg.
10829 * As the flip done interrupt doesn't trigger for mmio
10830 * flips on gmch platforms, a flip count check isn't
10831 * really needed there. But since ctg has the registers,
10832 * include it in the check anyway.
10833 */
10834 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10835 return true;
10836
10837 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010838 * BDW signals flip done immediately if the plane
10839 * is disabled, even if the plane enable is already
10840 * armed to occur at the next vblank :(
10841 */
10842
10843 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010844 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10845 * used the same base address. In that case the mmio flip might
10846 * have completed, but the CS hasn't even executed the flip yet.
10847 *
10848 * A flip count check isn't enough as the CS might have updated
10849 * the base address just after start of vblank, but before we
10850 * managed to process the interrupt. This means we'd complete the
10851 * CS flip too soon.
10852 *
10853 * Combining both checks should get us a good enough result. It may
10854 * still happen that the CS flip has been executed, but has not
10855 * yet actually completed. But in case the base address is the same
10856 * anyway, we don't really care.
10857 */
10858 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10859 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010860 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010861 crtc->unpin_work->flip_count);
10862}
10863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010864void intel_prepare_page_flip(struct drm_device *dev, int plane)
10865{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010866 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010867 struct intel_crtc *intel_crtc =
10868 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10869 unsigned long flags;
10870
Daniel Vetterf3260382014-09-15 14:55:23 +020010871
10872 /*
10873 * This is called both by irq handlers and the reset code (to complete
10874 * lost pageflips) so needs the full irqsave spinlocks.
10875 *
10876 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010877 * generate a page-flip completion irq, i.e. every modeset
10878 * is also accompanied by a spurious intel_prepare_page_flip().
10879 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010880 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010881 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010882 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010883 spin_unlock_irqrestore(&dev->event_lock, flags);
10884}
10885
Chris Wilson60426392015-10-10 10:44:32 +010010886static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010887{
10888 /* Ensure that the work item is consistent when activating it ... */
10889 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010890 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010891 /* and that it is marked active as soon as the irq could fire. */
10892 smp_wmb();
10893}
10894
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010895static int intel_gen2_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 /* Can't queue multiple flips, so wait for the previous
10912 * one to finish before executing the next.
10913 */
10914 if (intel_crtc->plane)
10915 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10916 else
10917 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010918 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10919 intel_ring_emit(engine, MI_NOOP);
10920 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010921 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010922 intel_ring_emit(engine, fb->pitches[0]);
10923 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10924 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010925
Chris Wilson60426392015-10-10 10:44:32 +010010926 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010927 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010928}
10929
10930static int intel_gen3_queue_flip(struct drm_device *dev,
10931 struct drm_crtc *crtc,
10932 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010933 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010934 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010935 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010936{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010937 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010939 u32 flip_mask;
10940 int ret;
10941
John Harrison5fb9de12015-05-29 17:44:07 +010010942 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010943 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010944 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010945
10946 if (intel_crtc->plane)
10947 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10948 else
10949 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010950 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10951 intel_ring_emit(engine, MI_NOOP);
10952 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010953 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010954 intel_ring_emit(engine, fb->pitches[0]);
10955 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10956 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010957
Chris Wilson60426392015-10-10 10:44:32 +010010958 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010959 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010960}
10961
10962static int intel_gen4_queue_flip(struct drm_device *dev,
10963 struct drm_crtc *crtc,
10964 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010965 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010966 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010967 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010968{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010969 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010970 struct drm_i915_private *dev_priv = dev->dev_private;
10971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10972 uint32_t pf, pipesrc;
10973 int ret;
10974
John Harrison5fb9de12015-05-29 17:44:07 +010010975 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010976 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010977 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010978
10979 /* i965+ uses the linear or tiled offsets from the
10980 * Display Registers (which do not change across a page-flip)
10981 * so we need only reprogram the base address.
10982 */
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]);
10986 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010987 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988
10989 /* XXX Enabling the panel-fitter across page-flip is so far
10990 * untested on non-native modes, so ignore it for now.
10991 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10992 */
10993 pf = 0;
10994 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010995 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010996
Chris Wilson60426392015-10-10 10:44:32 +010010997 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010998 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010999}
11000
11001static int intel_gen6_queue_flip(struct drm_device *dev,
11002 struct drm_crtc *crtc,
11003 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011004 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011005 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011006 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011007{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011008 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011009 struct drm_i915_private *dev_priv = dev->dev_private;
11010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11011 uint32_t pf, pipesrc;
11012 int ret;
11013
John Harrison5fb9de12015-05-29 17:44:07 +010011014 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011015 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011016 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011017
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011018 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011019 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011020 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11021 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022
Chris Wilson99d9acd2012-04-17 20:37:00 +010011023 /* Contrary to the suggestions in the documentation,
11024 * "Enable Panel Fitter" does not seem to be required when page
11025 * flipping with a non-native mode, and worse causes a normal
11026 * modeset to fail.
11027 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11028 */
11029 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011031 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011032
Chris Wilson60426392015-10-10 10:44:32 +010011033 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011034 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011035}
11036
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011037static int intel_gen7_queue_flip(struct drm_device *dev,
11038 struct drm_crtc *crtc,
11039 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011040 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011041 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011042 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011043{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011044 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011046 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011047 int len, ret;
11048
Robin Schroereba905b2014-05-18 02:24:50 +020011049 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011050 case PLANE_A:
11051 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11052 break;
11053 case PLANE_B:
11054 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11055 break;
11056 case PLANE_C:
11057 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11058 break;
11059 default:
11060 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011061 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011062 }
11063
Chris Wilsonffe74d72013-08-26 20:58:12 +010011064 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011065 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011066 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011067 /*
11068 * On Gen 8, SRM is now taking an extra dword to accommodate
11069 * 48bits addresses, and we need a NOOP for the batch size to
11070 * stay even.
11071 */
11072 if (IS_GEN8(dev))
11073 len += 2;
11074 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011075
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011076 /*
11077 * BSpec MI_DISPLAY_FLIP for IVB:
11078 * "The full packet must be contained within the same cache line."
11079 *
11080 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11081 * cacheline, if we ever start emitting more commands before
11082 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11083 * then do the cacheline alignment, and finally emit the
11084 * MI_DISPLAY_FLIP.
11085 */
John Harrisonbba09b12015-05-29 17:44:06 +010011086 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011087 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011088 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011089
John Harrison5fb9de12015-05-29 17:44:07 +010011090 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011091 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011092 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011093
Chris Wilsonffe74d72013-08-26 20:58:12 +010011094 /* Unmask the flip-done completion message. Note that the bspec says that
11095 * we should do this for both the BCS and RCS, and that we must not unmask
11096 * more than one flip event at any time (or ensure that one flip message
11097 * can be sent by waiting for flip-done prior to queueing new flips).
11098 * Experimentation says that BCS works despite DERRMR masking all
11099 * flip-done completion events and that unmasking all planes at once
11100 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11101 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11102 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011103 if (engine->id == RCS) {
11104 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11105 intel_ring_emit_reg(engine, DERRMR);
11106 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11107 DERRMR_PIPEB_PRI_FLIP_DONE |
11108 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011109 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011110 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011111 MI_SRM_LRM_GLOBAL_GTT);
11112 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011113 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011114 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011115 intel_ring_emit_reg(engine, DERRMR);
11116 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011117 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011118 intel_ring_emit(engine, 0);
11119 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011120 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011121 }
11122
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011123 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11124 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11125 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11126 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011127
Chris Wilson60426392015-10-10 10:44:32 +010011128 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011129 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011130}
11131
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011132static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011133 struct drm_i915_gem_object *obj)
11134{
11135 /*
11136 * This is not being used for older platforms, because
11137 * non-availability of flip done interrupt forces us to use
11138 * CS flips. Older platforms derive flip done using some clever
11139 * tricks involving the flip_pending status bits and vblank irqs.
11140 * So using MMIO flips there would disrupt this mechanism.
11141 */
11142
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011143 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011144 return true;
11145
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011146 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011147 return false;
11148
11149 if (i915.use_mmio_flip < 0)
11150 return false;
11151 else if (i915.use_mmio_flip > 0)
11152 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011153 else if (i915.enable_execlists)
11154 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011155 else if (obj->base.dma_buf &&
11156 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11157 false))
11158 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011159 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011160 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011161}
11162
Chris Wilson60426392015-10-10 10:44:32 +010011163static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011164 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011165 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011166{
11167 struct drm_device *dev = intel_crtc->base.dev;
11168 struct drm_i915_private *dev_priv = dev->dev_private;
11169 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011170 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011171 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011172
11173 ctl = I915_READ(PLANE_CTL(pipe, 0));
11174 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011175 switch (fb->modifier[0]) {
11176 case DRM_FORMAT_MOD_NONE:
11177 break;
11178 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011179 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011180 break;
11181 case I915_FORMAT_MOD_Y_TILED:
11182 ctl |= PLANE_CTL_TILED_Y;
11183 break;
11184 case I915_FORMAT_MOD_Yf_TILED:
11185 ctl |= PLANE_CTL_TILED_YF;
11186 break;
11187 default:
11188 MISSING_CASE(fb->modifier[0]);
11189 }
Damien Lespiauff944562014-11-20 14:58:16 +000011190
11191 /*
11192 * The stride is either expressed as a multiple of 64 bytes chunks for
11193 * linear buffers or in number of tiles for tiled buffers.
11194 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011195 if (intel_rotation_90_or_270(rotation)) {
11196 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011197 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011198 stride = DIV_ROUND_UP(fb->height, tile_height);
11199 } else {
11200 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011201 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11202 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011203 }
Damien Lespiauff944562014-11-20 14:58:16 +000011204
11205 /*
11206 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11207 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11208 */
11209 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11210 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11211
Chris Wilson60426392015-10-10 10:44:32 +010011212 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011213 POSTING_READ(PLANE_SURF(pipe, 0));
11214}
11215
Chris Wilson60426392015-10-10 10:44:32 +010011216static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11217 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011218{
11219 struct drm_device *dev = intel_crtc->base.dev;
11220 struct drm_i915_private *dev_priv = dev->dev_private;
11221 struct intel_framebuffer *intel_fb =
11222 to_intel_framebuffer(intel_crtc->base.primary->fb);
11223 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011224 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011225 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011226
Sourab Gupta84c33a62014-06-02 16:47:17 +053011227 dspcntr = I915_READ(reg);
11228
Damien Lespiauc5d97472014-10-25 00:11:11 +010011229 if (obj->tiling_mode != I915_TILING_NONE)
11230 dspcntr |= DISPPLANE_TILED;
11231 else
11232 dspcntr &= ~DISPPLANE_TILED;
11233
Sourab Gupta84c33a62014-06-02 16:47:17 +053011234 I915_WRITE(reg, dspcntr);
11235
Chris Wilson60426392015-10-10 10:44:32 +010011236 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011237 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011238}
11239
11240/*
11241 * XXX: This is the temporary way to update the plane registers until we get
11242 * around to using the usual plane update functions for MMIO flips
11243 */
Chris Wilson60426392015-10-10 10:44:32 +010011244static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011245{
Chris Wilson60426392015-10-10 10:44:32 +010011246 struct intel_crtc *crtc = mmio_flip->crtc;
11247 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011248
Chris Wilson60426392015-10-10 10:44:32 +010011249 spin_lock_irq(&crtc->base.dev->event_lock);
11250 work = crtc->unpin_work;
11251 spin_unlock_irq(&crtc->base.dev->event_lock);
11252 if (work == NULL)
11253 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011254
Chris Wilson60426392015-10-10 10:44:32 +010011255 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011256
Chris Wilson60426392015-10-10 10:44:32 +010011257 intel_pipe_update_start(crtc);
11258
11259 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011260 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011261 else
11262 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011263 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011264
Chris Wilson60426392015-10-10 10:44:32 +010011265 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011266}
11267
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011268static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011269{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011270 struct intel_mmio_flip *mmio_flip =
11271 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011272 struct intel_framebuffer *intel_fb =
11273 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11274 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011275
Chris Wilson60426392015-10-10 10:44:32 +010011276 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011277 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011278 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011279 false, NULL,
11280 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011281 i915_gem_request_unreference__unlocked(mmio_flip->req);
11282 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011283
Alex Goinsfd8e0582015-11-25 18:43:38 -080011284 /* For framebuffer backed by dmabuf, wait for fence */
11285 if (obj->base.dma_buf)
11286 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11287 false, false,
11288 MAX_SCHEDULE_TIMEOUT) < 0);
11289
Chris Wilson60426392015-10-10 10:44:32 +010011290 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011291 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011292}
11293
11294static int intel_queue_mmio_flip(struct drm_device *dev,
11295 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011296 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011297{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011298 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011299
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011300 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11301 if (mmio_flip == NULL)
11302 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011303
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011304 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011305 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011306 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011307 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011308
11309 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11310 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011311
Sourab Gupta84c33a62014-06-02 16:47:17 +053011312 return 0;
11313}
11314
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011315static int intel_default_queue_flip(struct drm_device *dev,
11316 struct drm_crtc *crtc,
11317 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011318 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011319 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011320 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011321{
11322 return -ENODEV;
11323}
11324
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011325static bool __intel_pageflip_stall_check(struct drm_device *dev,
11326 struct drm_crtc *crtc)
11327{
11328 struct drm_i915_private *dev_priv = dev->dev_private;
11329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11330 struct intel_unpin_work *work = intel_crtc->unpin_work;
11331 u32 addr;
11332
11333 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11334 return true;
11335
Chris Wilson908565c2015-08-12 13:08:22 +010011336 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11337 return false;
11338
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011339 if (!work->enable_stall_check)
11340 return false;
11341
11342 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011343 if (work->flip_queued_req &&
11344 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011345 return false;
11346
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011347 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011348 }
11349
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011350 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011351 return false;
11352
11353 /* Potential stall - if we see that the flip has happened,
11354 * assume a missed interrupt. */
11355 if (INTEL_INFO(dev)->gen >= 4)
11356 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11357 else
11358 addr = I915_READ(DSPADDR(intel_crtc->plane));
11359
11360 /* There is a potential issue here with a false positive after a flip
11361 * to the same address. We could address this by checking for a
11362 * non-incrementing frame counter.
11363 */
11364 return addr == work->gtt_offset;
11365}
11366
11367void intel_check_page_flip(struct drm_device *dev, int pipe)
11368{
11369 struct drm_i915_private *dev_priv = dev->dev_private;
11370 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011372 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011373
Dave Gordon6c51d462015-03-06 15:34:26 +000011374 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011375
11376 if (crtc == NULL)
11377 return;
11378
Daniel Vetterf3260382014-09-15 14:55:23 +020011379 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011380 work = intel_crtc->unpin_work;
11381 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011382 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011383 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011384 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011385 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011386 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011387 if (work != NULL &&
11388 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11389 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011390 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011391}
11392
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011393static int intel_crtc_page_flip(struct drm_crtc *crtc,
11394 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011395 struct drm_pending_vblank_event *event,
11396 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011397{
11398 struct drm_device *dev = crtc->dev;
11399 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011400 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011401 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011403 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011404 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011405 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011406 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011407 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011408 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011409 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011410
Matt Roper2ff8fde2014-07-08 07:50:07 -070011411 /*
11412 * drm_mode_page_flip_ioctl() should already catch this, but double
11413 * check to be safe. In the future we may enable pageflipping from
11414 * a disabled primary plane.
11415 */
11416 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11417 return -EBUSY;
11418
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011419 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011420 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011421 return -EINVAL;
11422
11423 /*
11424 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11425 * Note that pitch changes could also affect these register.
11426 */
11427 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011428 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11429 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011430 return -EINVAL;
11431
Chris Wilsonf900db42014-02-20 09:26:13 +000011432 if (i915_terminally_wedged(&dev_priv->gpu_error))
11433 goto out_hang;
11434
Daniel Vetterb14c5672013-09-19 12:18:32 +020011435 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011436 if (work == NULL)
11437 return -ENOMEM;
11438
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011439 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011440 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011441 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011442 INIT_WORK(&work->work, intel_unpin_work_fn);
11443
Daniel Vetter87b6b102014-05-15 15:33:46 +020011444 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011445 if (ret)
11446 goto free_work;
11447
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011448 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011449 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011450 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011451 /* Before declaring the flip queue wedged, check if
11452 * the hardware completed the operation behind our backs.
11453 */
11454 if (__intel_pageflip_stall_check(dev, crtc)) {
11455 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11456 page_flip_completed(intel_crtc);
11457 } else {
11458 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011459 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011460
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011461 drm_crtc_vblank_put(crtc);
11462 kfree(work);
11463 return -EBUSY;
11464 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465 }
11466 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011467 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011468
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011469 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11470 flush_workqueue(dev_priv->wq);
11471
Jesse Barnes75dfca82010-02-10 15:09:44 -080011472 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011473 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011474 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011475
Matt Roperf4510a22014-04-01 15:22:40 -070011476 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011477 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011478 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011479
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011480 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011481
Chris Wilson89ed88b2015-02-16 14:31:49 +000011482 ret = i915_mutex_lock_interruptible(dev);
11483 if (ret)
11484 goto cleanup;
11485
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011486 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011487 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011488
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011489 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011490 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011491
Wayne Boyer666a4532015-12-09 12:29:35 -080011492 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011493 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011494 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011495 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011496 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011497 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011498 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011499 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011500 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011501 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011502 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011503 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011504 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011505 }
11506
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011507 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011508
11509 /* When using CS flips, we want to emit semaphores between rings.
11510 * However, when using mmio flips we will create a task to do the
11511 * synchronisation, so all we want here is to pin the framebuffer
11512 * into the display plane and skip any waits.
11513 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011514 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011515 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011516 if (ret)
11517 goto cleanup_pending;
11518 }
11519
Ville Syrjälä3465c582016-02-15 22:54:43 +020011520 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011521 if (ret)
11522 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011524 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11525 obj, 0);
11526 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011527
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011528 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011529 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011530 if (ret)
11531 goto cleanup_unpin;
11532
John Harrisonf06cc1b2014-11-24 18:49:37 +000011533 i915_gem_request_assign(&work->flip_queued_req,
11534 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011535 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011536 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011537 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011538 if (IS_ERR(request)) {
11539 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011540 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011541 }
John Harrison6258fbe2015-05-29 17:43:48 +010011542 }
11543
11544 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011545 page_flip_flags);
11546 if (ret)
11547 goto cleanup_unpin;
11548
John Harrison6258fbe2015-05-29 17:43:48 +010011549 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011550 }
11551
John Harrison91af1272015-06-18 13:14:56 +010011552 if (request)
John Harrison75289872015-05-29 17:43:49 +010011553 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011554
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011555 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011556 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011557
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011558 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011559 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011560 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011561
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011562 intel_frontbuffer_flip_prepare(dev,
11563 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011564
Jesse Barnese5510fa2010-07-01 16:48:37 -070011565 trace_i915_flip_request(intel_crtc->plane, obj);
11566
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011567 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011568
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011569cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011570 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011571cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011572 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011573 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011574 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011575 mutex_unlock(&dev->struct_mutex);
11576cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011577 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011578 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011579
Chris Wilson89ed88b2015-02-16 14:31:49 +000011580 drm_gem_object_unreference_unlocked(&obj->base);
11581 drm_framebuffer_unreference(work->old_fb);
11582
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011583 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011584 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011585 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011586
Daniel Vetter87b6b102014-05-15 15:33:46 +020011587 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011588free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011589 kfree(work);
11590
Chris Wilsonf900db42014-02-20 09:26:13 +000011591 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011592 struct drm_atomic_state *state;
11593 struct drm_plane_state *plane_state;
11594
Chris Wilsonf900db42014-02-20 09:26:13 +000011595out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011596 state = drm_atomic_state_alloc(dev);
11597 if (!state)
11598 return -ENOMEM;
11599 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11600
11601retry:
11602 plane_state = drm_atomic_get_plane_state(state, primary);
11603 ret = PTR_ERR_OR_ZERO(plane_state);
11604 if (!ret) {
11605 drm_atomic_set_fb_for_plane(plane_state, fb);
11606
11607 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11608 if (!ret)
11609 ret = drm_atomic_commit(state);
11610 }
11611
11612 if (ret == -EDEADLK) {
11613 drm_modeset_backoff(state->acquire_ctx);
11614 drm_atomic_state_clear(state);
11615 goto retry;
11616 }
11617
11618 if (ret)
11619 drm_atomic_state_free(state);
11620
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011621 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011622 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011623 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011624 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011625 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011626 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011627 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011628}
11629
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011630
11631/**
11632 * intel_wm_need_update - Check whether watermarks need updating
11633 * @plane: drm plane
11634 * @state: new plane state
11635 *
11636 * Check current plane state versus the new one to determine whether
11637 * watermarks need to be recalculated.
11638 *
11639 * Returns true or false.
11640 */
11641static bool intel_wm_need_update(struct drm_plane *plane,
11642 struct drm_plane_state *state)
11643{
Matt Roperd21fbe82015-09-24 15:53:12 -070011644 struct intel_plane_state *new = to_intel_plane_state(state);
11645 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11646
11647 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011648 if (new->visible != cur->visible)
11649 return true;
11650
11651 if (!cur->base.fb || !new->base.fb)
11652 return false;
11653
11654 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11655 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011656 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11657 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11658 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11659 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011660 return true;
11661
11662 return false;
11663}
11664
Matt Roperd21fbe82015-09-24 15:53:12 -070011665static bool needs_scaling(struct intel_plane_state *state)
11666{
11667 int src_w = drm_rect_width(&state->src) >> 16;
11668 int src_h = drm_rect_height(&state->src) >> 16;
11669 int dst_w = drm_rect_width(&state->dst);
11670 int dst_h = drm_rect_height(&state->dst);
11671
11672 return (src_w != dst_w || src_h != dst_h);
11673}
11674
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011675int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11676 struct drm_plane_state *plane_state)
11677{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011678 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011679 struct drm_crtc *crtc = crtc_state->crtc;
11680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11681 struct drm_plane *plane = plane_state->plane;
11682 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011683 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011684 struct intel_plane_state *old_plane_state =
11685 to_intel_plane_state(plane->state);
11686 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011687 bool mode_changed = needs_modeset(crtc_state);
11688 bool was_crtc_enabled = crtc->state->active;
11689 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011690 bool turn_off, turn_on, visible, was_visible;
11691 struct drm_framebuffer *fb = plane_state->fb;
11692
11693 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11694 plane->type != DRM_PLANE_TYPE_CURSOR) {
11695 ret = skl_update_scaler_plane(
11696 to_intel_crtc_state(crtc_state),
11697 to_intel_plane_state(plane_state));
11698 if (ret)
11699 return ret;
11700 }
11701
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011702 was_visible = old_plane_state->visible;
11703 visible = to_intel_plane_state(plane_state)->visible;
11704
11705 if (!was_crtc_enabled && WARN_ON(was_visible))
11706 was_visible = false;
11707
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011708 /*
11709 * Visibility is calculated as if the crtc was on, but
11710 * after scaler setup everything depends on it being off
11711 * when the crtc isn't active.
11712 */
11713 if (!is_crtc_enabled)
11714 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011715
11716 if (!was_visible && !visible)
11717 return 0;
11718
Maarten Lankhorste8861672016-02-24 11:24:26 +010011719 if (fb != old_plane_state->base.fb)
11720 pipe_config->fb_changed = true;
11721
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011722 turn_off = was_visible && (!visible || mode_changed);
11723 turn_on = visible && (!was_visible || mode_changed);
11724
11725 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11726 plane->base.id, fb ? fb->base.id : -1);
11727
11728 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11729 plane->base.id, was_visible, visible,
11730 turn_off, turn_on, mode_changed);
11731
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011732 if (turn_on) {
11733 pipe_config->update_wm_pre = true;
11734
11735 /* must disable cxsr around plane enable/disable */
11736 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11737 pipe_config->disable_cxsr = true;
11738 } else if (turn_off) {
11739 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011740
Ville Syrjälä852eb002015-06-24 22:00:07 +030011741 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011742 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011743 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011744 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011745 /* FIXME bollocks */
11746 pipe_config->update_wm_pre = true;
11747 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011748 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011749
Matt Ropered4a6a72016-02-23 17:20:13 -080011750 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011751 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11752 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011753 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11754
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011755 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011756 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011757
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011758 /*
11759 * WaCxSRDisabledForSpriteScaling:ivb
11760 *
11761 * cstate->update_wm was already set above, so this flag will
11762 * take effect when we commit and program watermarks.
11763 */
11764 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11765 needs_scaling(to_intel_plane_state(plane_state)) &&
11766 !needs_scaling(old_plane_state))
11767 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011768
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011769 return 0;
11770}
11771
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011772static bool encoders_cloneable(const struct intel_encoder *a,
11773 const struct intel_encoder *b)
11774{
11775 /* masks could be asymmetric, so check both ways */
11776 return a == b || (a->cloneable & (1 << b->type) &&
11777 b->cloneable & (1 << a->type));
11778}
11779
11780static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11781 struct intel_crtc *crtc,
11782 struct intel_encoder *encoder)
11783{
11784 struct intel_encoder *source_encoder;
11785 struct drm_connector *connector;
11786 struct drm_connector_state *connector_state;
11787 int i;
11788
11789 for_each_connector_in_state(state, connector, connector_state, i) {
11790 if (connector_state->crtc != &crtc->base)
11791 continue;
11792
11793 source_encoder =
11794 to_intel_encoder(connector_state->best_encoder);
11795 if (!encoders_cloneable(encoder, source_encoder))
11796 return false;
11797 }
11798
11799 return true;
11800}
11801
11802static bool check_encoder_cloning(struct drm_atomic_state *state,
11803 struct intel_crtc *crtc)
11804{
11805 struct intel_encoder *encoder;
11806 struct drm_connector *connector;
11807 struct drm_connector_state *connector_state;
11808 int i;
11809
11810 for_each_connector_in_state(state, connector, connector_state, i) {
11811 if (connector_state->crtc != &crtc->base)
11812 continue;
11813
11814 encoder = to_intel_encoder(connector_state->best_encoder);
11815 if (!check_single_encoder_cloning(state, crtc, encoder))
11816 return false;
11817 }
11818
11819 return true;
11820}
11821
11822static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11823 struct drm_crtc_state *crtc_state)
11824{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011825 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011826 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011828 struct intel_crtc_state *pipe_config =
11829 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011830 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011831 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011832 bool mode_changed = needs_modeset(crtc_state);
11833
11834 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11835 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11836 return -EINVAL;
11837 }
11838
Ville Syrjälä852eb002015-06-24 22:00:07 +030011839 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011840 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011841
Maarten Lankhorstad421372015-06-15 12:33:42 +020011842 if (mode_changed && crtc_state->enable &&
11843 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011844 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011845 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11846 pipe_config);
11847 if (ret)
11848 return ret;
11849 }
11850
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011851 if (crtc_state->color_mgmt_changed) {
11852 ret = intel_color_check(crtc, crtc_state);
11853 if (ret)
11854 return ret;
11855 }
11856
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011857 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011858 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011859 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011860 if (ret) {
11861 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011862 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011863 }
11864 }
11865
11866 if (dev_priv->display.compute_intermediate_wm &&
11867 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11868 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11869 return 0;
11870
11871 /*
11872 * Calculate 'intermediate' watermarks that satisfy both the
11873 * old state and the new state. We can program these
11874 * immediately.
11875 */
11876 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11877 intel_crtc,
11878 pipe_config);
11879 if (ret) {
11880 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11881 return ret;
11882 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011883 }
11884
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011885 if (INTEL_INFO(dev)->gen >= 9) {
11886 if (mode_changed)
11887 ret = skl_update_scaler_crtc(pipe_config);
11888
11889 if (!ret)
11890 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11891 pipe_config);
11892 }
11893
11894 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011895}
11896
Jani Nikula65b38e02015-04-13 11:26:56 +030011897static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011898 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011899 .atomic_begin = intel_begin_crtc_commit,
11900 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011901 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011902};
11903
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011904static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11905{
11906 struct intel_connector *connector;
11907
11908 for_each_intel_connector(dev, connector) {
11909 if (connector->base.encoder) {
11910 connector->base.state->best_encoder =
11911 connector->base.encoder;
11912 connector->base.state->crtc =
11913 connector->base.encoder->crtc;
11914 } else {
11915 connector->base.state->best_encoder = NULL;
11916 connector->base.state->crtc = NULL;
11917 }
11918 }
11919}
11920
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011921static void
Robin Schroereba905b2014-05-18 02:24:50 +020011922connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011923 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011924{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011925 int bpp = pipe_config->pipe_bpp;
11926
11927 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11928 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011929 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011930
11931 /* Don't use an invalid EDID bpc value */
11932 if (connector->base.display_info.bpc &&
11933 connector->base.display_info.bpc * 3 < bpp) {
11934 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11935 bpp, connector->base.display_info.bpc*3);
11936 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11937 }
11938
Jani Nikula013dd9e2016-01-13 16:35:20 +020011939 /* Clamp bpp to default limit on screens without EDID 1.4 */
11940 if (connector->base.display_info.bpc == 0) {
11941 int type = connector->base.connector_type;
11942 int clamp_bpp = 24;
11943
11944 /* Fall back to 18 bpp when DP sink capability is unknown. */
11945 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11946 type == DRM_MODE_CONNECTOR_eDP)
11947 clamp_bpp = 18;
11948
11949 if (bpp > clamp_bpp) {
11950 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11951 bpp, clamp_bpp);
11952 pipe_config->pipe_bpp = clamp_bpp;
11953 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011954 }
11955}
11956
11957static int
11958compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011959 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011960{
11961 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011962 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011963 struct drm_connector *connector;
11964 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011965 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011966
Wayne Boyer666a4532015-12-09 12:29:35 -080011967 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011968 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011969 else if (INTEL_INFO(dev)->gen >= 5)
11970 bpp = 12*3;
11971 else
11972 bpp = 8*3;
11973
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011974
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011975 pipe_config->pipe_bpp = bpp;
11976
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011977 state = pipe_config->base.state;
11978
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011979 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011980 for_each_connector_in_state(state, connector, connector_state, i) {
11981 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011982 continue;
11983
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011984 connected_sink_compute_bpp(to_intel_connector(connector),
11985 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011986 }
11987
11988 return bpp;
11989}
11990
Daniel Vetter644db712013-09-19 14:53:58 +020011991static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11992{
11993 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11994 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011995 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011996 mode->crtc_hdisplay, mode->crtc_hsync_start,
11997 mode->crtc_hsync_end, mode->crtc_htotal,
11998 mode->crtc_vdisplay, mode->crtc_vsync_start,
11999 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12000}
12001
Daniel Vetterc0b03412013-05-28 12:05:54 +020012002static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012003 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012004 const char *context)
12005{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012006 struct drm_device *dev = crtc->base.dev;
12007 struct drm_plane *plane;
12008 struct intel_plane *intel_plane;
12009 struct intel_plane_state *state;
12010 struct drm_framebuffer *fb;
12011
12012 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12013 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012014
Jani Nikulada205632016-03-15 21:51:10 +020012015 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012016 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12017 pipe_config->pipe_bpp, pipe_config->dither);
12018 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12019 pipe_config->has_pch_encoder,
12020 pipe_config->fdi_lanes,
12021 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12022 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12023 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012024 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 +030012025 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012026 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012027 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12028 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12029 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012030
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012031 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 -070012032 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012033 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012034 pipe_config->dp_m2_n2.gmch_m,
12035 pipe_config->dp_m2_n2.gmch_n,
12036 pipe_config->dp_m2_n2.link_m,
12037 pipe_config->dp_m2_n2.link_n,
12038 pipe_config->dp_m2_n2.tu);
12039
Daniel Vetter55072d12014-11-20 16:10:28 +010012040 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12041 pipe_config->has_audio,
12042 pipe_config->has_infoframe);
12043
Daniel Vetterc0b03412013-05-28 12:05:54 +020012044 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012045 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012046 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012047 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12048 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012049 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012050 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12051 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012052 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12053 crtc->num_scalers,
12054 pipe_config->scaler_state.scaler_users,
12055 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012056 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12057 pipe_config->gmch_pfit.control,
12058 pipe_config->gmch_pfit.pgm_ratios,
12059 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012060 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012061 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012062 pipe_config->pch_pfit.size,
12063 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012064 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012065 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012066
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012067 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012068 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012069 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012070 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012071 pipe_config->ddi_pll_sel,
12072 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012073 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012074 pipe_config->dpll_hw_state.pll0,
12075 pipe_config->dpll_hw_state.pll1,
12076 pipe_config->dpll_hw_state.pll2,
12077 pipe_config->dpll_hw_state.pll3,
12078 pipe_config->dpll_hw_state.pll6,
12079 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012080 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012081 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012082 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012083 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012084 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12085 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12086 pipe_config->ddi_pll_sel,
12087 pipe_config->dpll_hw_state.ctrl1,
12088 pipe_config->dpll_hw_state.cfgcr1,
12089 pipe_config->dpll_hw_state.cfgcr2);
12090 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012091 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 +010012092 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012093 pipe_config->dpll_hw_state.wrpll,
12094 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012095 } else {
12096 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12097 "fp0: 0x%x, fp1: 0x%x\n",
12098 pipe_config->dpll_hw_state.dpll,
12099 pipe_config->dpll_hw_state.dpll_md,
12100 pipe_config->dpll_hw_state.fp0,
12101 pipe_config->dpll_hw_state.fp1);
12102 }
12103
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012104 DRM_DEBUG_KMS("planes on this crtc\n");
12105 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12106 intel_plane = to_intel_plane(plane);
12107 if (intel_plane->pipe != crtc->pipe)
12108 continue;
12109
12110 state = to_intel_plane_state(plane->state);
12111 fb = state->base.fb;
12112 if (!fb) {
12113 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12114 "disabled, scaler_id = %d\n",
12115 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12116 plane->base.id, intel_plane->pipe,
12117 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12118 drm_plane_index(plane), state->scaler_id);
12119 continue;
12120 }
12121
12122 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12123 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12124 plane->base.id, intel_plane->pipe,
12125 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12126 drm_plane_index(plane));
12127 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12128 fb->base.id, fb->width, fb->height, fb->pixel_format);
12129 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12130 state->scaler_id,
12131 state->src.x1 >> 16, state->src.y1 >> 16,
12132 drm_rect_width(&state->src) >> 16,
12133 drm_rect_height(&state->src) >> 16,
12134 state->dst.x1, state->dst.y1,
12135 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12136 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012137}
12138
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012139static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012140{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012141 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012142 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012143 unsigned int used_ports = 0;
12144
12145 /*
12146 * Walk the connector list instead of the encoder
12147 * list to detect the problem on ddi platforms
12148 * where there's just one encoder per digital port.
12149 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012150 drm_for_each_connector(connector, dev) {
12151 struct drm_connector_state *connector_state;
12152 struct intel_encoder *encoder;
12153
12154 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12155 if (!connector_state)
12156 connector_state = connector->state;
12157
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012158 if (!connector_state->best_encoder)
12159 continue;
12160
12161 encoder = to_intel_encoder(connector_state->best_encoder);
12162
12163 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012164
12165 switch (encoder->type) {
12166 unsigned int port_mask;
12167 case INTEL_OUTPUT_UNKNOWN:
12168 if (WARN_ON(!HAS_DDI(dev)))
12169 break;
12170 case INTEL_OUTPUT_DISPLAYPORT:
12171 case INTEL_OUTPUT_HDMI:
12172 case INTEL_OUTPUT_EDP:
12173 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12174
12175 /* the same port mustn't appear more than once */
12176 if (used_ports & port_mask)
12177 return false;
12178
12179 used_ports |= port_mask;
12180 default:
12181 break;
12182 }
12183 }
12184
12185 return true;
12186}
12187
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012188static void
12189clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12190{
12191 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012192 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012193 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012194 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012195 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012196 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012197
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012198 /* FIXME: before the switch to atomic started, a new pipe_config was
12199 * kzalloc'd. Code that depends on any field being zero should be
12200 * fixed, so that the crtc_state can be safely duplicated. For now,
12201 * only fields that are know to not cause problems are preserved. */
12202
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012203 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012204 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012205 shared_dpll = crtc_state->shared_dpll;
12206 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012207 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012208 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012209
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012210 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012211
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012212 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012213 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012214 crtc_state->shared_dpll = shared_dpll;
12215 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012216 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012217 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012218}
12219
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012220static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012221intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012222 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012223{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012224 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012225 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012226 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012227 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012228 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012229 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012230 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012231
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012232 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012233
Daniel Vettere143a212013-07-04 12:01:15 +020012234 pipe_config->cpu_transcoder =
12235 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012236
Imre Deak2960bc92013-07-30 13:36:32 +030012237 /*
12238 * Sanitize sync polarity flags based on requested ones. If neither
12239 * positive or negative polarity is requested, treat this as meaning
12240 * negative polarity.
12241 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012242 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012243 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012244 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012245
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012246 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012247 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012248 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012249
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012250 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12251 pipe_config);
12252 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012253 goto fail;
12254
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012255 /*
12256 * Determine the real pipe dimensions. Note that stereo modes can
12257 * increase the actual pipe size due to the frame doubling and
12258 * insertion of additional space for blanks between the frame. This
12259 * is stored in the crtc timings. We use the requested mode to do this
12260 * computation to clearly distinguish it from the adjusted mode, which
12261 * can be changed by the connectors in the below retry loop.
12262 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012263 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012264 &pipe_config->pipe_src_w,
12265 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012266
Daniel Vettere29c22c2013-02-21 00:00:16 +010012267encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012268 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012269 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012270 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012271
Daniel Vetter135c81b2013-07-21 21:37:09 +020012272 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012273 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12274 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012275
Daniel Vetter7758a112012-07-08 19:40:39 +020012276 /* Pass our mode to the connectors and the CRTC to give them a chance to
12277 * adjust it according to limitations or connector properties, and also
12278 * a chance to reject the mode entirely.
12279 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012280 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012281 if (connector_state->crtc != crtc)
12282 continue;
12283
12284 encoder = to_intel_encoder(connector_state->best_encoder);
12285
Daniel Vetterefea6e82013-07-21 21:36:59 +020012286 if (!(encoder->compute_config(encoder, pipe_config))) {
12287 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012288 goto fail;
12289 }
12290 }
12291
Daniel Vetterff9a6752013-06-01 17:16:21 +020012292 /* Set default port clock if not overwritten by the encoder. Needs to be
12293 * done afterwards in case the encoder adjusts the mode. */
12294 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012295 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012296 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012297
Daniel Vettera43f6e02013-06-07 23:10:32 +020012298 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012299 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012300 DRM_DEBUG_KMS("CRTC fixup failed\n");
12301 goto fail;
12302 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012303
12304 if (ret == RETRY) {
12305 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12306 ret = -EINVAL;
12307 goto fail;
12308 }
12309
12310 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12311 retry = false;
12312 goto encoder_retry;
12313 }
12314
Daniel Vettere8fa4272015-08-12 11:43:34 +020012315 /* Dithering seems to not pass-through bits correctly when it should, so
12316 * only enable it on 6bpc panels. */
12317 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012318 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012319 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012320
Daniel Vetter7758a112012-07-08 19:40:39 +020012321fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012322 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012323}
12324
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012325static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012326intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012327{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012328 struct drm_crtc *crtc;
12329 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012330 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012331
Ville Syrjälä76688512014-01-10 11:28:06 +020012332 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012333 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012334 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012335
12336 /* Update hwmode for vblank functions */
12337 if (crtc->state->active)
12338 crtc->hwmode = crtc->state->adjusted_mode;
12339 else
12340 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012341
12342 /*
12343 * Update legacy state to satisfy fbc code. This can
12344 * be removed when fbc uses the atomic state.
12345 */
12346 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12347 struct drm_plane_state *plane_state = crtc->primary->state;
12348
12349 crtc->primary->fb = plane_state->fb;
12350 crtc->x = plane_state->src_x >> 16;
12351 crtc->y = plane_state->src_y >> 16;
12352 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012353 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012354}
12355
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012356static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012357{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012358 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012359
12360 if (clock1 == clock2)
12361 return true;
12362
12363 if (!clock1 || !clock2)
12364 return false;
12365
12366 diff = abs(clock1 - clock2);
12367
12368 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12369 return true;
12370
12371 return false;
12372}
12373
Daniel Vetter25c5b262012-07-08 22:08:04 +020012374#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12375 list_for_each_entry((intel_crtc), \
12376 &(dev)->mode_config.crtc_list, \
12377 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012378 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012379
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012380static bool
12381intel_compare_m_n(unsigned int m, unsigned int n,
12382 unsigned int m2, unsigned int n2,
12383 bool exact)
12384{
12385 if (m == m2 && n == n2)
12386 return true;
12387
12388 if (exact || !m || !n || !m2 || !n2)
12389 return false;
12390
12391 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12392
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012393 if (n > n2) {
12394 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012395 m2 <<= 1;
12396 n2 <<= 1;
12397 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012398 } else if (n < n2) {
12399 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012400 m <<= 1;
12401 n <<= 1;
12402 }
12403 }
12404
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012405 if (n != n2)
12406 return false;
12407
12408 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012409}
12410
12411static bool
12412intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12413 struct intel_link_m_n *m2_n2,
12414 bool adjust)
12415{
12416 if (m_n->tu == m2_n2->tu &&
12417 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12418 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12419 intel_compare_m_n(m_n->link_m, m_n->link_n,
12420 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12421 if (adjust)
12422 *m2_n2 = *m_n;
12423
12424 return true;
12425 }
12426
12427 return false;
12428}
12429
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012430static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012431intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012432 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012433 struct intel_crtc_state *pipe_config,
12434 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012435{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012436 bool ret = true;
12437
12438#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12439 do { \
12440 if (!adjust) \
12441 DRM_ERROR(fmt, ##__VA_ARGS__); \
12442 else \
12443 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12444 } while (0)
12445
Daniel Vetter66e985c2013-06-05 13:34:20 +020012446#define PIPE_CONF_CHECK_X(name) \
12447 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012448 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012449 "(expected 0x%08x, found 0x%08x)\n", \
12450 current_config->name, \
12451 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012452 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012453 }
12454
Daniel Vetter08a24032013-04-19 11:25:34 +020012455#define PIPE_CONF_CHECK_I(name) \
12456 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012457 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012458 "(expected %i, found %i)\n", \
12459 current_config->name, \
12460 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012461 ret = false; \
12462 }
12463
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012464#define PIPE_CONF_CHECK_P(name) \
12465 if (current_config->name != pipe_config->name) { \
12466 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12467 "(expected %p, found %p)\n", \
12468 current_config->name, \
12469 pipe_config->name); \
12470 ret = false; \
12471 }
12472
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012473#define PIPE_CONF_CHECK_M_N(name) \
12474 if (!intel_compare_link_m_n(&current_config->name, \
12475 &pipe_config->name,\
12476 adjust)) { \
12477 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12478 "(expected tu %i gmch %i/%i link %i/%i, " \
12479 "found tu %i, gmch %i/%i link %i/%i)\n", \
12480 current_config->name.tu, \
12481 current_config->name.gmch_m, \
12482 current_config->name.gmch_n, \
12483 current_config->name.link_m, \
12484 current_config->name.link_n, \
12485 pipe_config->name.tu, \
12486 pipe_config->name.gmch_m, \
12487 pipe_config->name.gmch_n, \
12488 pipe_config->name.link_m, \
12489 pipe_config->name.link_n); \
12490 ret = false; \
12491 }
12492
12493#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12494 if (!intel_compare_link_m_n(&current_config->name, \
12495 &pipe_config->name, adjust) && \
12496 !intel_compare_link_m_n(&current_config->alt_name, \
12497 &pipe_config->name, adjust)) { \
12498 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12499 "(expected tu %i gmch %i/%i link %i/%i, " \
12500 "or tu %i gmch %i/%i link %i/%i, " \
12501 "found tu %i, gmch %i/%i link %i/%i)\n", \
12502 current_config->name.tu, \
12503 current_config->name.gmch_m, \
12504 current_config->name.gmch_n, \
12505 current_config->name.link_m, \
12506 current_config->name.link_n, \
12507 current_config->alt_name.tu, \
12508 current_config->alt_name.gmch_m, \
12509 current_config->alt_name.gmch_n, \
12510 current_config->alt_name.link_m, \
12511 current_config->alt_name.link_n, \
12512 pipe_config->name.tu, \
12513 pipe_config->name.gmch_m, \
12514 pipe_config->name.gmch_n, \
12515 pipe_config->name.link_m, \
12516 pipe_config->name.link_n); \
12517 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012518 }
12519
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012520/* This is required for BDW+ where there is only one set of registers for
12521 * switching between high and low RR.
12522 * This macro can be used whenever a comparison has to be made between one
12523 * hw state and multiple sw state variables.
12524 */
12525#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12526 if ((current_config->name != pipe_config->name) && \
12527 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012528 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012529 "(expected %i or %i, found %i)\n", \
12530 current_config->name, \
12531 current_config->alt_name, \
12532 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012533 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012534 }
12535
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012536#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12537 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012538 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012539 "(expected %i, found %i)\n", \
12540 current_config->name & (mask), \
12541 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012542 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012543 }
12544
Ville Syrjälä5e550652013-09-06 23:29:07 +030012545#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12546 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012547 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012548 "(expected %i, found %i)\n", \
12549 current_config->name, \
12550 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012551 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012552 }
12553
Daniel Vetterbb760062013-06-06 14:55:52 +020012554#define PIPE_CONF_QUIRK(quirk) \
12555 ((current_config->quirks | pipe_config->quirks) & (quirk))
12556
Daniel Vettereccb1402013-05-22 00:50:22 +020012557 PIPE_CONF_CHECK_I(cpu_transcoder);
12558
Daniel Vetter08a24032013-04-19 11:25:34 +020012559 PIPE_CONF_CHECK_I(has_pch_encoder);
12560 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012561 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012562
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012563 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012564 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012565
12566 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012567 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012568
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012569 if (current_config->has_drrs)
12570 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12571 } else
12572 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012573
Jani Nikulaa65347b2015-11-27 12:21:46 +020012574 PIPE_CONF_CHECK_I(has_dsi_encoder);
12575
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012576 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12577 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12578 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12579 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12580 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12581 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012582
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012583 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12584 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12585 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12586 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12587 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12588 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012589
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012590 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012591 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012592 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012593 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012594 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012595 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012596
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012597 PIPE_CONF_CHECK_I(has_audio);
12598
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012599 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012600 DRM_MODE_FLAG_INTERLACE);
12601
Daniel Vetterbb760062013-06-06 14:55:52 +020012602 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012603 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012604 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012605 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012606 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012607 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012608 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012609 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012610 DRM_MODE_FLAG_NVSYNC);
12611 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012612
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012613 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012614 /* pfit ratios are autocomputed by the hw on gen4+ */
12615 if (INTEL_INFO(dev)->gen < 4)
12616 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012617 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012618
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012619 if (!adjust) {
12620 PIPE_CONF_CHECK_I(pipe_src_w);
12621 PIPE_CONF_CHECK_I(pipe_src_h);
12622
12623 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12624 if (current_config->pch_pfit.enabled) {
12625 PIPE_CONF_CHECK_X(pch_pfit.pos);
12626 PIPE_CONF_CHECK_X(pch_pfit.size);
12627 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012628
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012629 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12630 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012631
Jesse Barnese59150d2014-01-07 13:30:45 -080012632 /* BDW+ don't expose a synchronous way to read the state */
12633 if (IS_HASWELL(dev))
12634 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012635
Ville Syrjälä282740f2013-09-04 18:30:03 +030012636 PIPE_CONF_CHECK_I(double_wide);
12637
Daniel Vetter26804af2014-06-25 22:01:55 +030012638 PIPE_CONF_CHECK_X(ddi_pll_sel);
12639
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012640 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012641 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012642 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012643 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12644 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012645 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012646 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012647 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12648 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12649 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012650
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012651 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12652 PIPE_CONF_CHECK_I(pipe_bpp);
12653
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012654 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012655 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012656
Daniel Vetter66e985c2013-06-05 13:34:20 +020012657#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012658#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012659#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012660#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012661#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012662#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012663#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012664#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012665
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012666 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012667}
12668
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012669static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12670 const struct intel_crtc_state *pipe_config)
12671{
12672 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012673 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012674 &pipe_config->fdi_m_n);
12675 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12676
12677 /*
12678 * FDI already provided one idea for the dotclock.
12679 * Yell if the encoder disagrees.
12680 */
12681 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12682 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12683 fdi_dotclock, dotclock);
12684 }
12685}
12686
Damien Lespiau08db6652014-11-04 17:06:52 +000012687static void check_wm_state(struct drm_device *dev)
12688{
12689 struct drm_i915_private *dev_priv = dev->dev_private;
12690 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12691 struct intel_crtc *intel_crtc;
12692 int plane;
12693
12694 if (INTEL_INFO(dev)->gen < 9)
12695 return;
12696
12697 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12698 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12699
12700 for_each_intel_crtc(dev, intel_crtc) {
12701 struct skl_ddb_entry *hw_entry, *sw_entry;
12702 const enum pipe pipe = intel_crtc->pipe;
12703
12704 if (!intel_crtc->active)
12705 continue;
12706
12707 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012708 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012709 hw_entry = &hw_ddb.plane[pipe][plane];
12710 sw_entry = &sw_ddb->plane[pipe][plane];
12711
12712 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12713 continue;
12714
12715 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12716 "(expected (%u,%u), found (%u,%u))\n",
12717 pipe_name(pipe), plane + 1,
12718 sw_entry->start, sw_entry->end,
12719 hw_entry->start, hw_entry->end);
12720 }
12721
12722 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012723 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12724 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012725
12726 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12727 continue;
12728
12729 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12730 "(expected (%u,%u), found (%u,%u))\n",
12731 pipe_name(pipe),
12732 sw_entry->start, sw_entry->end,
12733 hw_entry->start, hw_entry->end);
12734 }
12735}
12736
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012737static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012738check_connector_state(struct drm_device *dev,
12739 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012740{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012741 struct drm_connector_state *old_conn_state;
12742 struct drm_connector *connector;
12743 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012744
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012745 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12746 struct drm_encoder *encoder = connector->encoder;
12747 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012748
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012749 /* This also checks the encoder/connector hw state with the
12750 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012751 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012752
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012753 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012754 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012755 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012756}
12757
12758static void
12759check_encoder_state(struct drm_device *dev)
12760{
12761 struct intel_encoder *encoder;
12762 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012763
Damien Lespiaub2784e12014-08-05 11:29:37 +010012764 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012765 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012766 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012767
12768 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12769 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012770 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012771
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012772 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012773 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012774 continue;
12775 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012776
12777 I915_STATE_WARN(connector->base.state->crtc !=
12778 encoder->base.crtc,
12779 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012780 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012781
Rob Clarke2c719b2014-12-15 13:56:32 -050012782 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012783 "encoder's enabled state mismatch "
12784 "(expected %i, found %i)\n",
12785 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012786
12787 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012788 bool active;
12789
12790 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012791 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012792 "encoder detached but still enabled on pipe %c.\n",
12793 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012794 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012795 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012796}
12797
12798static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012799check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012800{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012801 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012802 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012803 struct drm_crtc_state *old_crtc_state;
12804 struct drm_crtc *crtc;
12805 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012806
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012807 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12809 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012810 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012811
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012812 if (!needs_modeset(crtc->state) &&
12813 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012814 continue;
12815
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012816 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12817 pipe_config = to_intel_crtc_state(old_crtc_state);
12818 memset(pipe_config, 0, sizeof(*pipe_config));
12819 pipe_config->base.crtc = crtc;
12820 pipe_config->base.state = old_state;
12821
12822 DRM_DEBUG_KMS("[CRTC:%d]\n",
12823 crtc->base.id);
12824
12825 active = dev_priv->display.get_pipe_config(intel_crtc,
12826 pipe_config);
12827
12828 /* hw state is inconsistent with the pipe quirk */
12829 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12830 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12831 active = crtc->state->active;
12832
12833 I915_STATE_WARN(crtc->state->active != active,
12834 "crtc active state doesn't match with hw state "
12835 "(expected %i, found %i)\n", crtc->state->active, active);
12836
12837 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12838 "transitional active state does not match atomic hw state "
12839 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12840
12841 for_each_encoder_on_crtc(dev, crtc, encoder) {
12842 enum pipe pipe;
12843
12844 active = encoder->get_hw_state(encoder, &pipe);
12845 I915_STATE_WARN(active != crtc->state->active,
12846 "[ENCODER:%i] active %i with crtc active %i\n",
12847 encoder->base.base.id, active, crtc->state->active);
12848
12849 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12850 "Encoder connected to wrong pipe %c\n",
12851 pipe_name(pipe));
12852
12853 if (active)
12854 encoder->get_config(encoder, pipe_config);
12855 }
12856
12857 if (!crtc->state->active)
12858 continue;
12859
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012860 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12861
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012862 sw_config = to_intel_crtc_state(crtc->state);
12863 if (!intel_pipe_config_compare(dev, sw_config,
12864 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012865 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012866 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012867 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012868 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012869 "[sw state]");
12870 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012871 }
12872}
12873
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012874static void
12875check_shared_dpll_state(struct drm_device *dev)
12876{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012877 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012878 struct intel_crtc *crtc;
12879 struct intel_dpll_hw_state dpll_hw_state;
12880 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012881
12882 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012883 struct intel_shared_dpll *pll =
12884 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012885 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012886 bool active;
12887
12888 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12889
12890 DRM_DEBUG_KMS("%s\n", pll->name);
12891
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012892 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012893
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012894 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12895 "more active pll users than references: %x vs %x\n",
12896 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012897
12898 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012899 I915_STATE_WARN(!pll->on && pll->active_mask,
12900 "pll in active use but not on in sw tracking\n");
12901 I915_STATE_WARN(pll->on && !pll->active_mask,
12902 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012903 I915_STATE_WARN(pll->on != active,
12904 "pll on state mismatch (expected %i, found %i)\n",
12905 pll->on, active);
12906 }
Daniel Vetter53589012013-06-05 13:34:16 +020012907
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012908 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012909 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012910 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12911 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12912 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012913 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012914
12915 I915_STATE_WARN(pll->active_mask != active_crtcs,
12916 "pll active crtcs mismatch (expected %x, found %x)\n",
12917 pll->active_mask, active_crtcs);
12918 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12919 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12920 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012921
Rob Clarke2c719b2014-12-15 13:56:32 -050012922 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012923 sizeof(dpll_hw_state)),
12924 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012925 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012926}
12927
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012928static void
12929intel_modeset_check_state(struct drm_device *dev,
12930 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012931{
Damien Lespiau08db6652014-11-04 17:06:52 +000012932 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012933 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012934 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012935 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012936 check_shared_dpll_state(dev);
12937}
12938
Ville Syrjälä80715b22014-05-15 20:23:23 +030012939static void update_scanline_offset(struct intel_crtc *crtc)
12940{
12941 struct drm_device *dev = crtc->base.dev;
12942
12943 /*
12944 * The scanline counter increments at the leading edge of hsync.
12945 *
12946 * On most platforms it starts counting from vtotal-1 on the
12947 * first active line. That means the scanline counter value is
12948 * always one less than what we would expect. Ie. just after
12949 * start of vblank, which also occurs at start of hsync (on the
12950 * last active line), the scanline counter will read vblank_start-1.
12951 *
12952 * On gen2 the scanline counter starts counting from 1 instead
12953 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12954 * to keep the value positive), instead of adding one.
12955 *
12956 * On HSW+ the behaviour of the scanline counter depends on the output
12957 * type. For DP ports it behaves like most other platforms, but on HDMI
12958 * there's an extra 1 line difference. So we need to add two instead of
12959 * one to the value.
12960 */
12961 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012962 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012963 int vtotal;
12964
Ville Syrjälä124abe02015-09-08 13:40:45 +030012965 vtotal = adjusted_mode->crtc_vtotal;
12966 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012967 vtotal /= 2;
12968
12969 crtc->scanline_offset = vtotal - 1;
12970 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012971 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012972 crtc->scanline_offset = 2;
12973 } else
12974 crtc->scanline_offset = 1;
12975}
12976
Maarten Lankhorstad421372015-06-15 12:33:42 +020012977static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012978{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012979 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012980 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012981 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012982 struct drm_crtc *crtc;
12983 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012984 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012985
12986 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012987 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012988
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012989 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012991 struct intel_shared_dpll *old_dpll =
12992 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012993
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012994 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012995 continue;
12996
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012997 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012998
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012999 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013000 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013001
Maarten Lankhorstad421372015-06-15 12:33:42 +020013002 if (!shared_dpll)
13003 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13004
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013005 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013006 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013007}
13008
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013009/*
13010 * This implements the workaround described in the "notes" section of the mode
13011 * set sequence documentation. When going from no pipes or single pipe to
13012 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13013 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13014 */
13015static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13016{
13017 struct drm_crtc_state *crtc_state;
13018 struct intel_crtc *intel_crtc;
13019 struct drm_crtc *crtc;
13020 struct intel_crtc_state *first_crtc_state = NULL;
13021 struct intel_crtc_state *other_crtc_state = NULL;
13022 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13023 int i;
13024
13025 /* look at all crtc's that are going to be enabled in during modeset */
13026 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13027 intel_crtc = to_intel_crtc(crtc);
13028
13029 if (!crtc_state->active || !needs_modeset(crtc_state))
13030 continue;
13031
13032 if (first_crtc_state) {
13033 other_crtc_state = to_intel_crtc_state(crtc_state);
13034 break;
13035 } else {
13036 first_crtc_state = to_intel_crtc_state(crtc_state);
13037 first_pipe = intel_crtc->pipe;
13038 }
13039 }
13040
13041 /* No workaround needed? */
13042 if (!first_crtc_state)
13043 return 0;
13044
13045 /* w/a possibly needed, check how many crtc's are already enabled. */
13046 for_each_intel_crtc(state->dev, intel_crtc) {
13047 struct intel_crtc_state *pipe_config;
13048
13049 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13050 if (IS_ERR(pipe_config))
13051 return PTR_ERR(pipe_config);
13052
13053 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13054
13055 if (!pipe_config->base.active ||
13056 needs_modeset(&pipe_config->base))
13057 continue;
13058
13059 /* 2 or more enabled crtcs means no need for w/a */
13060 if (enabled_pipe != INVALID_PIPE)
13061 return 0;
13062
13063 enabled_pipe = intel_crtc->pipe;
13064 }
13065
13066 if (enabled_pipe != INVALID_PIPE)
13067 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13068 else if (other_crtc_state)
13069 other_crtc_state->hsw_workaround_pipe = first_pipe;
13070
13071 return 0;
13072}
13073
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013074static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13075{
13076 struct drm_crtc *crtc;
13077 struct drm_crtc_state *crtc_state;
13078 int ret = 0;
13079
13080 /* add all active pipes to the state */
13081 for_each_crtc(state->dev, crtc) {
13082 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13083 if (IS_ERR(crtc_state))
13084 return PTR_ERR(crtc_state);
13085
13086 if (!crtc_state->active || needs_modeset(crtc_state))
13087 continue;
13088
13089 crtc_state->mode_changed = true;
13090
13091 ret = drm_atomic_add_affected_connectors(state, crtc);
13092 if (ret)
13093 break;
13094
13095 ret = drm_atomic_add_affected_planes(state, crtc);
13096 if (ret)
13097 break;
13098 }
13099
13100 return ret;
13101}
13102
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013103static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013104{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013105 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13106 struct drm_i915_private *dev_priv = state->dev->dev_private;
13107 struct drm_crtc *crtc;
13108 struct drm_crtc_state *crtc_state;
13109 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013110
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013111 if (!check_digital_port_conflicts(state)) {
13112 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13113 return -EINVAL;
13114 }
13115
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013116 intel_state->modeset = true;
13117 intel_state->active_crtcs = dev_priv->active_crtcs;
13118
13119 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13120 if (crtc_state->active)
13121 intel_state->active_crtcs |= 1 << i;
13122 else
13123 intel_state->active_crtcs &= ~(1 << i);
13124 }
13125
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013126 /*
13127 * See if the config requires any additional preparation, e.g.
13128 * to adjust global state with pipes off. We need to do this
13129 * here so we can get the modeset_pipe updated config for the new
13130 * mode set on this crtc. For other crtcs we need to use the
13131 * adjusted_mode bits in the crtc directly.
13132 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013133 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013134 ret = dev_priv->display.modeset_calc_cdclk(state);
13135
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013136 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013137 ret = intel_modeset_all_pipes(state);
13138
13139 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013140 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013141
13142 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13143 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013144 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013145 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013146
Maarten Lankhorstad421372015-06-15 12:33:42 +020013147 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013148
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013149 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013150 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013151
Maarten Lankhorstad421372015-06-15 12:33:42 +020013152 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013153}
13154
Matt Roperaa363132015-09-24 15:53:18 -070013155/*
13156 * Handle calculation of various watermark data at the end of the atomic check
13157 * phase. The code here should be run after the per-crtc and per-plane 'check'
13158 * handlers to ensure that all derived state has been updated.
13159 */
13160static void calc_watermark_data(struct drm_atomic_state *state)
13161{
13162 struct drm_device *dev = state->dev;
13163 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13164 struct drm_crtc *crtc;
13165 struct drm_crtc_state *cstate;
13166 struct drm_plane *plane;
13167 struct drm_plane_state *pstate;
13168
13169 /*
13170 * Calculate watermark configuration details now that derived
13171 * plane/crtc state is all properly updated.
13172 */
13173 drm_for_each_crtc(crtc, dev) {
13174 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13175 crtc->state;
13176
13177 if (cstate->active)
13178 intel_state->wm_config.num_pipes_active++;
13179 }
13180 drm_for_each_legacy_plane(plane, dev) {
13181 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13182 plane->state;
13183
13184 if (!to_intel_plane_state(pstate)->visible)
13185 continue;
13186
13187 intel_state->wm_config.sprites_enabled = true;
13188 if (pstate->crtc_w != pstate->src_w >> 16 ||
13189 pstate->crtc_h != pstate->src_h >> 16)
13190 intel_state->wm_config.sprites_scaled = true;
13191 }
13192}
13193
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013194/**
13195 * intel_atomic_check - validate state object
13196 * @dev: drm device
13197 * @state: state to validate
13198 */
13199static int intel_atomic_check(struct drm_device *dev,
13200 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013201{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013202 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013203 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013204 struct drm_crtc *crtc;
13205 struct drm_crtc_state *crtc_state;
13206 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013207 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013208
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013209 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013210 if (ret)
13211 return ret;
13212
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013213 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013214 struct intel_crtc_state *pipe_config =
13215 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013216
13217 /* Catch I915_MODE_FLAG_INHERITED */
13218 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13219 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013220
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013221 if (!crtc_state->enable) {
13222 if (needs_modeset(crtc_state))
13223 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013224 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013225 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013226
Daniel Vetter26495482015-07-15 14:15:52 +020013227 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013228 continue;
13229
Daniel Vetter26495482015-07-15 14:15:52 +020013230 /* FIXME: For only active_changed we shouldn't need to do any
13231 * state recomputation at all. */
13232
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013233 ret = drm_atomic_add_affected_connectors(state, crtc);
13234 if (ret)
13235 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013236
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013237 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013238 if (ret)
13239 return ret;
13240
Jani Nikula73831232015-11-19 10:26:30 +020013241 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013242 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013243 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013244 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013245 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013246 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013247 }
13248
13249 if (needs_modeset(crtc_state)) {
13250 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013251
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013252 ret = drm_atomic_add_affected_planes(state, crtc);
13253 if (ret)
13254 return ret;
13255 }
13256
Daniel Vetter26495482015-07-15 14:15:52 +020013257 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13258 needs_modeset(crtc_state) ?
13259 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013260 }
13261
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013262 if (any_ms) {
13263 ret = intel_modeset_checks(state);
13264
13265 if (ret)
13266 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013267 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013268 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013269
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013270 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013271 if (ret)
13272 return ret;
13273
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013274 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013275 calc_watermark_data(state);
13276
13277 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013278}
13279
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013280static int intel_atomic_prepare_commit(struct drm_device *dev,
13281 struct drm_atomic_state *state,
13282 bool async)
13283{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013284 struct drm_i915_private *dev_priv = dev->dev_private;
13285 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013286 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013287 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013288 struct drm_crtc *crtc;
13289 int i, ret;
13290
13291 if (async) {
13292 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13293 return -EINVAL;
13294 }
13295
13296 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13297 ret = intel_crtc_wait_for_pending_flips(crtc);
13298 if (ret)
13299 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013300
13301 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13302 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013303 }
13304
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013305 ret = mutex_lock_interruptible(&dev->struct_mutex);
13306 if (ret)
13307 return ret;
13308
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013309 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013310 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13311 u32 reset_counter;
13312
13313 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13314 mutex_unlock(&dev->struct_mutex);
13315
13316 for_each_plane_in_state(state, plane, plane_state, i) {
13317 struct intel_plane_state *intel_plane_state =
13318 to_intel_plane_state(plane_state);
13319
13320 if (!intel_plane_state->wait_req)
13321 continue;
13322
13323 ret = __i915_wait_request(intel_plane_state->wait_req,
13324 reset_counter, true,
13325 NULL, NULL);
13326
13327 /* Swallow -EIO errors to allow updates during hw lockup. */
13328 if (ret == -EIO)
13329 ret = 0;
13330
13331 if (ret)
13332 break;
13333 }
13334
13335 if (!ret)
13336 return 0;
13337
13338 mutex_lock(&dev->struct_mutex);
13339 drm_atomic_helper_cleanup_planes(dev, state);
13340 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013341
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013342 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013343 return ret;
13344}
13345
Maarten Lankhorste8861672016-02-24 11:24:26 +010013346static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13347 struct drm_i915_private *dev_priv,
13348 unsigned crtc_mask)
13349{
13350 unsigned last_vblank_count[I915_MAX_PIPES];
13351 enum pipe pipe;
13352 int ret;
13353
13354 if (!crtc_mask)
13355 return;
13356
13357 for_each_pipe(dev_priv, pipe) {
13358 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13359
13360 if (!((1 << pipe) & crtc_mask))
13361 continue;
13362
13363 ret = drm_crtc_vblank_get(crtc);
13364 if (WARN_ON(ret != 0)) {
13365 crtc_mask &= ~(1 << pipe);
13366 continue;
13367 }
13368
13369 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13370 }
13371
13372 for_each_pipe(dev_priv, pipe) {
13373 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13374 long lret;
13375
13376 if (!((1 << pipe) & crtc_mask))
13377 continue;
13378
13379 lret = wait_event_timeout(dev->vblank[pipe].queue,
13380 last_vblank_count[pipe] !=
13381 drm_crtc_vblank_count(crtc),
13382 msecs_to_jiffies(50));
13383
13384 WARN_ON(!lret);
13385
13386 drm_crtc_vblank_put(crtc);
13387 }
13388}
13389
13390static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13391{
13392 /* fb updated, need to unpin old fb */
13393 if (crtc_state->fb_changed)
13394 return true;
13395
13396 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013397 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013398 return true;
13399
13400 /*
13401 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013402 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013403 * but added for clarity.
13404 */
13405 if (crtc_state->disable_cxsr)
13406 return true;
13407
13408 return false;
13409}
13410
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013411/**
13412 * intel_atomic_commit - commit validated state object
13413 * @dev: DRM device
13414 * @state: the top-level driver state object
13415 * @async: asynchronous commit
13416 *
13417 * This function commits a top-level state object that has been validated
13418 * with drm_atomic_helper_check().
13419 *
13420 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13421 * we can only handle plane-related operations and do not yet support
13422 * asynchronous commit.
13423 *
13424 * RETURNS
13425 * Zero for success or -errno.
13426 */
13427static int intel_atomic_commit(struct drm_device *dev,
13428 struct drm_atomic_state *state,
13429 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013430{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013431 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013432 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013433 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013434 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013435 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013436 int ret = 0, i;
13437 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013438 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013439 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013440
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013441 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013442 if (ret) {
13443 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013444 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013445 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013446
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013447 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013448 dev_priv->wm.config = intel_state->wm_config;
13449 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013450
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013451 if (intel_state->modeset) {
13452 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13453 sizeof(intel_state->min_pixclk));
13454 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013455 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013456
13457 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013458 }
13459
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013460 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13462
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013463 if (needs_modeset(crtc->state) ||
13464 to_intel_crtc_state(crtc->state)->update_pipe) {
13465 hw_check = true;
13466
13467 put_domains[to_intel_crtc(crtc)->pipe] =
13468 modeset_get_crtc_power_domains(crtc,
13469 to_intel_crtc_state(crtc->state));
13470 }
13471
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013472 if (!needs_modeset(crtc->state))
13473 continue;
13474
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013475 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013476
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013477 if (old_crtc_state->active) {
13478 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013479 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013480 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013481 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013482 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013483
13484 /*
13485 * Underruns don't always raise
13486 * interrupts, so check manually.
13487 */
13488 intel_check_cpu_fifo_underruns(dev_priv);
13489 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013490
13491 if (!crtc->state->active)
13492 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013493 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013494 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013495
Daniel Vetterea9d7582012-07-10 10:42:52 +020013496 /* Only after disabling all output pipelines that will be changed can we
13497 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013498 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013499
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013500 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013501 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013502
13503 if (dev_priv->display.modeset_commit_cdclk &&
13504 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13505 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013506 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013507
Daniel Vettera6778b32012-07-02 09:56:42 +020013508 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013509 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13511 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013512 struct intel_crtc_state *pipe_config =
13513 to_intel_crtc_state(crtc->state);
13514 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013515
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013516 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013517 update_scanline_offset(to_intel_crtc(crtc));
13518 dev_priv->display.crtc_enable(crtc);
13519 }
13520
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013521 if (!modeset &&
13522 crtc->state->active &&
13523 crtc->state->color_mgmt_changed) {
13524 /*
13525 * Only update color management when not doing
13526 * a modeset as this will be done by
13527 * crtc_enable already.
13528 */
13529 intel_color_set_csc(crtc);
13530 intel_color_load_luts(crtc);
13531 }
13532
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013533 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013534 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013535
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013536 if (crtc->state->active &&
13537 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013538 intel_fbc_enable(intel_crtc);
13539
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013540 if (crtc->state->active &&
13541 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013542 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013543
Maarten Lankhorste8861672016-02-24 11:24:26 +010013544 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13545 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013546 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013547
Daniel Vettera6778b32012-07-02 09:56:42 +020013548 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013549
Maarten Lankhorste8861672016-02-24 11:24:26 +010013550 if (!state->legacy_cursor_update)
13551 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013552
Matt Ropered4a6a72016-02-23 17:20:13 -080013553 /*
13554 * Now that the vblank has passed, we can go ahead and program the
13555 * optimal watermarks on platforms that need two-step watermark
13556 * programming.
13557 *
13558 * TODO: Move this (and other cleanup) to an async worker eventually.
13559 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013560 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013561 intel_cstate = to_intel_crtc_state(crtc->state);
13562
13563 if (dev_priv->display.optimize_watermarks)
13564 dev_priv->display.optimize_watermarks(intel_cstate);
13565 }
13566
Matt Roper177246a2016-03-04 15:59:39 -080013567 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13568 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13569
13570 if (put_domains[i])
13571 modeset_put_power_domains(dev_priv, put_domains[i]);
13572 }
13573
13574 if (intel_state->modeset)
13575 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13576
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013577 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013578 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013579 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013580
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013581 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013582 intel_modeset_check_state(dev, state);
13583
13584 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013585
Mika Kuoppala75714942015-12-16 09:26:48 +020013586 /* As one of the primary mmio accessors, KMS has a high likelihood
13587 * of triggering bugs in unclaimed access. After we finish
13588 * modesetting, see if an error has been flagged, and if so
13589 * enable debugging for the next modeset - and hope we catch
13590 * the culprit.
13591 *
13592 * XXX note that we assume display power is on at this point.
13593 * This might hold true now but we need to add pm helper to check
13594 * unclaimed only when the hardware is on, as atomic commits
13595 * can happen also when the device is completely off.
13596 */
13597 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13598
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013599 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013600}
13601
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013602void intel_crtc_restore_mode(struct drm_crtc *crtc)
13603{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013604 struct drm_device *dev = crtc->dev;
13605 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013606 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013607 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013608
13609 state = drm_atomic_state_alloc(dev);
13610 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013611 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013612 crtc->base.id);
13613 return;
13614 }
13615
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013616 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013617
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013618retry:
13619 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13620 ret = PTR_ERR_OR_ZERO(crtc_state);
13621 if (!ret) {
13622 if (!crtc_state->active)
13623 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013624
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013625 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013626 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013627 }
13628
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013629 if (ret == -EDEADLK) {
13630 drm_atomic_state_clear(state);
13631 drm_modeset_backoff(state->acquire_ctx);
13632 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013633 }
13634
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013635 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013636out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013637 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013638}
13639
Daniel Vetter25c5b262012-07-08 22:08:04 +020013640#undef for_each_intel_crtc_masked
13641
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013642static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013643 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013644 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013645 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013646 .destroy = intel_crtc_destroy,
13647 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013648 .atomic_duplicate_state = intel_crtc_duplicate_state,
13649 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013650};
13651
Matt Roper6beb8c232014-12-01 15:40:14 -080013652/**
13653 * intel_prepare_plane_fb - Prepare fb for usage on plane
13654 * @plane: drm plane to prepare for
13655 * @fb: framebuffer to prepare for presentation
13656 *
13657 * Prepares a framebuffer for usage on a display plane. Generally this
13658 * involves pinning the underlying object and updating the frontbuffer tracking
13659 * bits. Some older platforms need special physical address handling for
13660 * cursor planes.
13661 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013662 * Must be called with struct_mutex held.
13663 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013664 * Returns 0 on success, negative error code on failure.
13665 */
13666int
13667intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013668 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013669{
13670 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013671 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013672 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013673 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013674 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013675 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013676
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013677 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013678 return 0;
13679
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013680 if (old_obj) {
13681 struct drm_crtc_state *crtc_state =
13682 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13683
13684 /* Big Hammer, we also need to ensure that any pending
13685 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13686 * current scanout is retired before unpinning the old
13687 * framebuffer. Note that we rely on userspace rendering
13688 * into the buffer attached to the pipe they are waiting
13689 * on. If not, userspace generates a GPU hang with IPEHR
13690 * point to the MI_WAIT_FOR_EVENT.
13691 *
13692 * This should only fail upon a hung GPU, in which case we
13693 * can safely continue.
13694 */
13695 if (needs_modeset(crtc_state))
13696 ret = i915_gem_object_wait_rendering(old_obj, true);
13697
13698 /* Swallow -EIO errors to allow updates during hw lockup. */
13699 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013700 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013701 }
13702
Alex Goins3c28ff22015-11-25 18:43:39 -080013703 /* For framebuffer backed by dmabuf, wait for fence */
13704 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013705 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013706
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013707 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13708 false, true,
13709 MAX_SCHEDULE_TIMEOUT);
13710 if (lret == -ERESTARTSYS)
13711 return lret;
13712
13713 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013714 }
13715
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013716 if (!obj) {
13717 ret = 0;
13718 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013719 INTEL_INFO(dev)->cursor_needs_physical) {
13720 int align = IS_I830(dev) ? 16 * 1024 : 256;
13721 ret = i915_gem_object_attach_phys(obj, align);
13722 if (ret)
13723 DRM_DEBUG_KMS("failed to attach phys object\n");
13724 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013725 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013726 }
13727
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013728 if (ret == 0) {
13729 if (obj) {
13730 struct intel_plane_state *plane_state =
13731 to_intel_plane_state(new_state);
13732
13733 i915_gem_request_assign(&plane_state->wait_req,
13734 obj->last_write_req);
13735 }
13736
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013737 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013738 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013739
Matt Roper6beb8c232014-12-01 15:40:14 -080013740 return ret;
13741}
13742
Matt Roper38f3ce32014-12-02 07:45:25 -080013743/**
13744 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13745 * @plane: drm plane to clean up for
13746 * @fb: old framebuffer that was on plane
13747 *
13748 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013749 *
13750 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013751 */
13752void
13753intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013754 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013755{
13756 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013757 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013758 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013759 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13760 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013761
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013762 old_intel_state = to_intel_plane_state(old_state);
13763
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013764 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013765 return;
13766
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013767 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13768 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013769 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013770
13771 /* prepare_fb aborted? */
13772 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13773 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13774 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013775
13776 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013777}
13778
Chandra Konduru6156a452015-04-27 13:48:39 -070013779int
13780skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13781{
13782 int max_scale;
13783 struct drm_device *dev;
13784 struct drm_i915_private *dev_priv;
13785 int crtc_clock, cdclk;
13786
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013787 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013788 return DRM_PLANE_HELPER_NO_SCALING;
13789
13790 dev = intel_crtc->base.dev;
13791 dev_priv = dev->dev_private;
13792 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013793 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013794
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013795 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013796 return DRM_PLANE_HELPER_NO_SCALING;
13797
13798 /*
13799 * skl max scale is lower of:
13800 * close to 3 but not 3, -1 is for that purpose
13801 * or
13802 * cdclk/crtc_clock
13803 */
13804 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13805
13806 return max_scale;
13807}
13808
Matt Roper465c1202014-05-29 08:06:54 -070013809static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013810intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013811 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013812 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013813{
Matt Roper2b875c22014-12-01 15:40:13 -080013814 struct drm_crtc *crtc = state->base.crtc;
13815 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013816 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013817 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13818 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013819
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013820 if (INTEL_INFO(plane->dev)->gen >= 9) {
13821 /* use scaler when colorkey is not required */
13822 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13823 min_scale = 1;
13824 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13825 }
Sonika Jindald8106362015-04-10 14:37:28 +053013826 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013827 }
Sonika Jindald8106362015-04-10 14:37:28 +053013828
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013829 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13830 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013831 min_scale, max_scale,
13832 can_position, true,
13833 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013834}
13835
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013836static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13837 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013838{
13839 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013841 struct intel_crtc_state *old_intel_state =
13842 to_intel_crtc_state(old_crtc_state);
13843 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013844
Matt Roperc34c9ee2014-12-23 10:41:50 -080013845 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013846 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013847
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013848 if (modeset)
13849 return;
13850
13851 if (to_intel_crtc_state(crtc->state)->update_pipe)
13852 intel_update_pipe_config(intel_crtc, old_intel_state);
13853 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013854 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013855}
13856
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013857static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13858 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013859{
Matt Roper32b7eee2014-12-24 07:59:06 -080013860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013861
Maarten Lankhorst62852622015-09-23 16:29:38 +020013862 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013863}
13864
Matt Ropercf4c7c12014-12-04 10:27:42 -080013865/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013866 * intel_plane_destroy - destroy a plane
13867 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013868 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013869 * Common destruction function for all types of planes (primary, cursor,
13870 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013871 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013872void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013873{
13874 struct intel_plane *intel_plane = to_intel_plane(plane);
13875 drm_plane_cleanup(plane);
13876 kfree(intel_plane);
13877}
13878
Matt Roper65a3fea2015-01-21 16:35:42 -080013879const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013880 .update_plane = drm_atomic_helper_update_plane,
13881 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013882 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013883 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013884 .atomic_get_property = intel_plane_atomic_get_property,
13885 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013886 .atomic_duplicate_state = intel_plane_duplicate_state,
13887 .atomic_destroy_state = intel_plane_destroy_state,
13888
Matt Roper465c1202014-05-29 08:06:54 -070013889};
13890
13891static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13892 int pipe)
13893{
13894 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013895 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013896 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013897 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013898
13899 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13900 if (primary == NULL)
13901 return NULL;
13902
Matt Roper8e7d6882015-01-21 16:35:41 -080013903 state = intel_create_plane_state(&primary->base);
13904 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013905 kfree(primary);
13906 return NULL;
13907 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013908 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013909
Matt Roper465c1202014-05-29 08:06:54 -070013910 primary->can_scale = false;
13911 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013912 if (INTEL_INFO(dev)->gen >= 9) {
13913 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013914 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013915 }
Matt Roper465c1202014-05-29 08:06:54 -070013916 primary->pipe = pipe;
13917 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013918 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013919 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013920 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13921 primary->plane = !pipe;
13922
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013923 if (INTEL_INFO(dev)->gen >= 9) {
13924 intel_primary_formats = skl_primary_formats;
13925 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013926
13927 primary->update_plane = skylake_update_primary_plane;
13928 primary->disable_plane = skylake_disable_primary_plane;
13929 } else if (HAS_PCH_SPLIT(dev)) {
13930 intel_primary_formats = i965_primary_formats;
13931 num_formats = ARRAY_SIZE(i965_primary_formats);
13932
13933 primary->update_plane = ironlake_update_primary_plane;
13934 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013935 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013936 intel_primary_formats = i965_primary_formats;
13937 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013938
13939 primary->update_plane = i9xx_update_primary_plane;
13940 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013941 } else {
13942 intel_primary_formats = i8xx_primary_formats;
13943 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013944
13945 primary->update_plane = i9xx_update_primary_plane;
13946 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013947 }
13948
13949 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013950 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013951 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020013952 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053013953
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013954 if (INTEL_INFO(dev)->gen >= 4)
13955 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013956
Matt Roperea2c67b2014-12-23 10:41:52 -080013957 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13958
Matt Roper465c1202014-05-29 08:06:54 -070013959 return &primary->base;
13960}
13961
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013962void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13963{
13964 if (!dev->mode_config.rotation_property) {
13965 unsigned long flags = BIT(DRM_ROTATE_0) |
13966 BIT(DRM_ROTATE_180);
13967
13968 if (INTEL_INFO(dev)->gen >= 9)
13969 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13970
13971 dev->mode_config.rotation_property =
13972 drm_mode_create_rotation_property(dev, flags);
13973 }
13974 if (dev->mode_config.rotation_property)
13975 drm_object_attach_property(&plane->base.base,
13976 dev->mode_config.rotation_property,
13977 plane->base.state->rotation);
13978}
13979
Matt Roper3d7d6512014-06-10 08:28:13 -070013980static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013981intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013982 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013983 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013984{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013985 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013986 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013987 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013988 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013989 unsigned stride;
13990 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013991
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013992 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13993 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013994 DRM_PLANE_HELPER_NO_SCALING,
13995 DRM_PLANE_HELPER_NO_SCALING,
13996 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013997 if (ret)
13998 return ret;
13999
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014000 /* if we want to turn off the cursor ignore width and height */
14001 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014002 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014003
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014004 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014005 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014006 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14007 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014008 return -EINVAL;
14009 }
14010
Matt Roperea2c67b2014-12-23 10:41:52 -080014011 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14012 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014013 DRM_DEBUG_KMS("buffer is too small\n");
14014 return -ENOMEM;
14015 }
14016
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014017 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014018 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014019 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014020 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014021
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014022 /*
14023 * There's something wrong with the cursor on CHV pipe C.
14024 * If it straddles the left edge of the screen then
14025 * moving it away from the edge or disabling it often
14026 * results in a pipe underrun, and often that can lead to
14027 * dead pipe (constant underrun reported, and it scans
14028 * out just a solid color). To recover from that, the
14029 * display power well must be turned off and on again.
14030 * Refuse the put the cursor into that compromised position.
14031 */
14032 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14033 state->visible && state->base.crtc_x < 0) {
14034 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14035 return -EINVAL;
14036 }
14037
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014038 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014039}
14040
Matt Roperf4a2cf22014-12-01 15:40:12 -080014041static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014042intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014043 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014044{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14046
14047 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014048 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014049}
14050
14051static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014052intel_update_cursor_plane(struct drm_plane *plane,
14053 const struct intel_crtc_state *crtc_state,
14054 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014055{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014056 struct drm_crtc *crtc = crtc_state->base.crtc;
14057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014058 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014059 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014060 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014061
Matt Roperf4a2cf22014-12-01 15:40:12 -080014062 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014063 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014064 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014065 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014066 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014067 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014068
Gustavo Padovana912f122014-12-01 15:40:10 -080014069 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014070 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014071}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014072
Matt Roper3d7d6512014-06-10 08:28:13 -070014073static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14074 int pipe)
14075{
14076 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014077 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014078
14079 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14080 if (cursor == NULL)
14081 return NULL;
14082
Matt Roper8e7d6882015-01-21 16:35:41 -080014083 state = intel_create_plane_state(&cursor->base);
14084 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014085 kfree(cursor);
14086 return NULL;
14087 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014088 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014089
Matt Roper3d7d6512014-06-10 08:28:13 -070014090 cursor->can_scale = false;
14091 cursor->max_downscale = 1;
14092 cursor->pipe = pipe;
14093 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014094 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014095 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014096 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014097 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014098
14099 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014100 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014101 intel_cursor_formats,
14102 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014103 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014104
14105 if (INTEL_INFO(dev)->gen >= 4) {
14106 if (!dev->mode_config.rotation_property)
14107 dev->mode_config.rotation_property =
14108 drm_mode_create_rotation_property(dev,
14109 BIT(DRM_ROTATE_0) |
14110 BIT(DRM_ROTATE_180));
14111 if (dev->mode_config.rotation_property)
14112 drm_object_attach_property(&cursor->base.base,
14113 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014114 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014115 }
14116
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014117 if (INTEL_INFO(dev)->gen >=9)
14118 state->scaler_id = -1;
14119
Matt Roperea2c67b2014-12-23 10:41:52 -080014120 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14121
Matt Roper3d7d6512014-06-10 08:28:13 -070014122 return &cursor->base;
14123}
14124
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014125static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14126 struct intel_crtc_state *crtc_state)
14127{
14128 int i;
14129 struct intel_scaler *intel_scaler;
14130 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14131
14132 for (i = 0; i < intel_crtc->num_scalers; i++) {
14133 intel_scaler = &scaler_state->scalers[i];
14134 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014135 intel_scaler->mode = PS_SCALER_MODE_DYN;
14136 }
14137
14138 scaler_state->scaler_id = -1;
14139}
14140
Hannes Ederb358d0a2008-12-18 21:18:47 +010014141static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014142{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014143 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014144 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014145 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014146 struct drm_plane *primary = NULL;
14147 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014148 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014149
Daniel Vetter955382f2013-09-19 14:05:45 +020014150 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014151 if (intel_crtc == NULL)
14152 return;
14153
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014154 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14155 if (!crtc_state)
14156 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014157 intel_crtc->config = crtc_state;
14158 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014159 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014160
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014161 /* initialize shared scalers */
14162 if (INTEL_INFO(dev)->gen >= 9) {
14163 if (pipe == PIPE_C)
14164 intel_crtc->num_scalers = 1;
14165 else
14166 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14167
14168 skl_init_scalers(dev, intel_crtc, crtc_state);
14169 }
14170
Matt Roper465c1202014-05-29 08:06:54 -070014171 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014172 if (!primary)
14173 goto fail;
14174
14175 cursor = intel_cursor_plane_create(dev, pipe);
14176 if (!cursor)
14177 goto fail;
14178
Matt Roper465c1202014-05-29 08:06:54 -070014179 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014180 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014181 if (ret)
14182 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014183
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014184 /*
14185 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014186 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014187 */
Jesse Barnes80824002009-09-10 15:28:06 -070014188 intel_crtc->pipe = pipe;
14189 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014190 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014191 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014192 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014193 }
14194
Chris Wilson4b0e3332014-05-30 16:35:26 +030014195 intel_crtc->cursor_base = ~0;
14196 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014197 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014198
Ville Syrjälä852eb002015-06-24 22:00:07 +030014199 intel_crtc->wm.cxsr_allowed = true;
14200
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014201 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14202 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14203 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14204 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14205
Jesse Barnes79e53942008-11-07 14:24:08 -080014206 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014207
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014208 intel_color_init(&intel_crtc->base);
14209
Daniel Vetter87b6b102014-05-15 15:33:46 +020014210 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014211 return;
14212
14213fail:
14214 if (primary)
14215 drm_plane_cleanup(primary);
14216 if (cursor)
14217 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014218 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014219 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014220}
14221
Jesse Barnes752aa882013-10-31 18:55:49 +020014222enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14223{
14224 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014225 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014226
Rob Clark51fd3712013-11-19 12:10:12 -050014227 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014228
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014229 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014230 return INVALID_PIPE;
14231
14232 return to_intel_crtc(encoder->crtc)->pipe;
14233}
14234
Carl Worth08d7b3d2009-04-29 14:43:54 -070014235int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014236 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014237{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014238 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014239 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014240 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014241
Rob Clark7707e652014-07-17 23:30:04 -040014242 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014243
Rob Clark7707e652014-07-17 23:30:04 -040014244 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014245 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014246 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014247 }
14248
Rob Clark7707e652014-07-17 23:30:04 -040014249 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014250 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014251
Daniel Vetterc05422d2009-08-11 16:05:30 +020014252 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014253}
14254
Daniel Vetter66a92782012-07-12 20:08:18 +020014255static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014256{
Daniel Vetter66a92782012-07-12 20:08:18 +020014257 struct drm_device *dev = encoder->base.dev;
14258 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014259 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014260 int entry = 0;
14261
Damien Lespiaub2784e12014-08-05 11:29:37 +010014262 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014263 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014264 index_mask |= (1 << entry);
14265
Jesse Barnes79e53942008-11-07 14:24:08 -080014266 entry++;
14267 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014268
Jesse Barnes79e53942008-11-07 14:24:08 -080014269 return index_mask;
14270}
14271
Chris Wilson4d302442010-12-14 19:21:29 +000014272static bool has_edp_a(struct drm_device *dev)
14273{
14274 struct drm_i915_private *dev_priv = dev->dev_private;
14275
14276 if (!IS_MOBILE(dev))
14277 return false;
14278
14279 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14280 return false;
14281
Damien Lespiaue3589902014-02-07 19:12:50 +000014282 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014283 return false;
14284
14285 return true;
14286}
14287
Jesse Barnes84b4e042014-06-25 08:24:29 -070014288static bool intel_crt_present(struct drm_device *dev)
14289{
14290 struct drm_i915_private *dev_priv = dev->dev_private;
14291
Damien Lespiau884497e2013-12-03 13:56:23 +000014292 if (INTEL_INFO(dev)->gen >= 9)
14293 return false;
14294
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014295 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014296 return false;
14297
14298 if (IS_CHERRYVIEW(dev))
14299 return false;
14300
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014301 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14302 return false;
14303
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014304 /* DDI E can't be used if DDI A requires 4 lanes */
14305 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14306 return false;
14307
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014308 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014309 return false;
14310
14311 return true;
14312}
14313
Jesse Barnes79e53942008-11-07 14:24:08 -080014314static void intel_setup_outputs(struct drm_device *dev)
14315{
Eric Anholt725e30a2009-01-22 13:01:02 -080014316 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014317 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014318 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014319
Daniel Vetterc9093352013-06-06 22:22:47 +020014320 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014321
Jesse Barnes84b4e042014-06-25 08:24:29 -070014322 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014323 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014324
Vandana Kannanc776eb22014-08-19 12:05:01 +053014325 if (IS_BROXTON(dev)) {
14326 /*
14327 * FIXME: Broxton doesn't support port detection via the
14328 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14329 * detect the ports.
14330 */
14331 intel_ddi_init(dev, PORT_A);
14332 intel_ddi_init(dev, PORT_B);
14333 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014334
14335 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014336 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014337 int found;
14338
Jesse Barnesde31fac2015-03-06 15:53:32 -080014339 /*
14340 * Haswell uses DDI functions to detect digital outputs.
14341 * On SKL pre-D0 the strap isn't connected, so we assume
14342 * it's there.
14343 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014344 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014345 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014346 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014347 intel_ddi_init(dev, PORT_A);
14348
14349 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14350 * register */
14351 found = I915_READ(SFUSE_STRAP);
14352
14353 if (found & SFUSE_STRAP_DDIB_DETECTED)
14354 intel_ddi_init(dev, PORT_B);
14355 if (found & SFUSE_STRAP_DDIC_DETECTED)
14356 intel_ddi_init(dev, PORT_C);
14357 if (found & SFUSE_STRAP_DDID_DETECTED)
14358 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014359 /*
14360 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14361 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014362 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014363 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14364 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14365 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14366 intel_ddi_init(dev, PORT_E);
14367
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014368 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014369 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014370 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014371
14372 if (has_edp_a(dev))
14373 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014374
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014375 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014376 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014377 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014378 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014379 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014380 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014381 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014382 }
14383
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014384 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014385 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014386
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014387 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014388 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014389
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014390 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014391 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014392
Daniel Vetter270b3042012-10-27 15:52:05 +020014393 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014394 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014395 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014396 /*
14397 * The DP_DETECTED bit is the latched state of the DDC
14398 * SDA pin at boot. However since eDP doesn't require DDC
14399 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14400 * eDP ports may have been muxed to an alternate function.
14401 * Thus we can't rely on the DP_DETECTED bit alone to detect
14402 * eDP ports. Consult the VBT as well as DP_DETECTED to
14403 * detect eDP ports.
14404 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014405 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014406 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014407 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14408 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014409 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014410 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014411
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014412 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014413 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014414 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14415 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014416 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014417 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014418
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014419 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014420 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014421 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14422 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14423 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14424 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014425 }
14426
Jani Nikula3cfca972013-08-27 15:12:26 +030014427 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014428 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014429 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014430
Paulo Zanonie2debe92013-02-18 19:00:27 -030014431 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014432 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014433 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014434 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014435 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014436 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014437 }
Ma Ling27185ae2009-08-24 13:50:23 +080014438
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014439 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014440 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014441 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014442
14443 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014444
Paulo Zanonie2debe92013-02-18 19:00:27 -030014445 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014446 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014447 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014448 }
Ma Ling27185ae2009-08-24 13:50:23 +080014449
Paulo Zanonie2debe92013-02-18 19:00:27 -030014450 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014451
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014452 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014453 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014454 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014455 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014456 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014457 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014458 }
Ma Ling27185ae2009-08-24 13:50:23 +080014459
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014460 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014461 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014462 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014463 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014464 intel_dvo_init(dev);
14465
Zhenyu Wang103a1962009-11-27 11:44:36 +080014466 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014467 intel_tv_init(dev);
14468
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014469 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014470
Damien Lespiaub2784e12014-08-05 11:29:37 +010014471 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014472 encoder->base.possible_crtcs = encoder->crtc_mask;
14473 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014474 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014475 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014476
Paulo Zanonidde86e22012-12-01 12:04:25 -020014477 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014478
14479 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014480}
14481
14482static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14483{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014484 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014485 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014486
Daniel Vetteref2d6332014-02-10 18:00:38 +010014487 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014488 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014489 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014490 drm_gem_object_unreference(&intel_fb->obj->base);
14491 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014492 kfree(intel_fb);
14493}
14494
14495static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014496 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014497 unsigned int *handle)
14498{
14499 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014500 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014501
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014502 if (obj->userptr.mm) {
14503 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14504 return -EINVAL;
14505 }
14506
Chris Wilson05394f32010-11-08 19:18:58 +000014507 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014508}
14509
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014510static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14511 struct drm_file *file,
14512 unsigned flags, unsigned color,
14513 struct drm_clip_rect *clips,
14514 unsigned num_clips)
14515{
14516 struct drm_device *dev = fb->dev;
14517 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14518 struct drm_i915_gem_object *obj = intel_fb->obj;
14519
14520 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014521 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014522 mutex_unlock(&dev->struct_mutex);
14523
14524 return 0;
14525}
14526
Jesse Barnes79e53942008-11-07 14:24:08 -080014527static const struct drm_framebuffer_funcs intel_fb_funcs = {
14528 .destroy = intel_user_framebuffer_destroy,
14529 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014530 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014531};
14532
Damien Lespiaub3218032015-02-27 11:15:18 +000014533static
14534u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14535 uint32_t pixel_format)
14536{
14537 u32 gen = INTEL_INFO(dev)->gen;
14538
14539 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014540 int cpp = drm_format_plane_cpp(pixel_format, 0);
14541
Damien Lespiaub3218032015-02-27 11:15:18 +000014542 /* "The stride in bytes must not exceed the of the size of 8K
14543 * pixels and 32K bytes."
14544 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014545 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014546 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014547 return 32*1024;
14548 } else if (gen >= 4) {
14549 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14550 return 16*1024;
14551 else
14552 return 32*1024;
14553 } else if (gen >= 3) {
14554 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14555 return 8*1024;
14556 else
14557 return 16*1024;
14558 } else {
14559 /* XXX DSPC is limited to 4k tiled */
14560 return 8*1024;
14561 }
14562}
14563
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014564static int intel_framebuffer_init(struct drm_device *dev,
14565 struct intel_framebuffer *intel_fb,
14566 struct drm_mode_fb_cmd2 *mode_cmd,
14567 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014568{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014569 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014570 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014571 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014572 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014573
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014574 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14575
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014576 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14577 /* Enforce that fb modifier and tiling mode match, but only for
14578 * X-tiled. This is needed for FBC. */
14579 if (!!(obj->tiling_mode == I915_TILING_X) !=
14580 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14581 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14582 return -EINVAL;
14583 }
14584 } else {
14585 if (obj->tiling_mode == I915_TILING_X)
14586 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14587 else if (obj->tiling_mode == I915_TILING_Y) {
14588 DRM_DEBUG("No Y tiling for legacy addfb\n");
14589 return -EINVAL;
14590 }
14591 }
14592
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014593 /* Passed in modifier sanity checking. */
14594 switch (mode_cmd->modifier[0]) {
14595 case I915_FORMAT_MOD_Y_TILED:
14596 case I915_FORMAT_MOD_Yf_TILED:
14597 if (INTEL_INFO(dev)->gen < 9) {
14598 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14599 mode_cmd->modifier[0]);
14600 return -EINVAL;
14601 }
14602 case DRM_FORMAT_MOD_NONE:
14603 case I915_FORMAT_MOD_X_TILED:
14604 break;
14605 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014606 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14607 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014608 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014609 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014610
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014611 stride_alignment = intel_fb_stride_alignment(dev_priv,
14612 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014613 mode_cmd->pixel_format);
14614 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14615 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14616 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014617 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014618 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014619
Damien Lespiaub3218032015-02-27 11:15:18 +000014620 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14621 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014622 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014623 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14624 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014625 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014626 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014627 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014628 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014629
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014630 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014631 mode_cmd->pitches[0] != obj->stride) {
14632 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14633 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014634 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014635 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014636
Ville Syrjälä57779d02012-10-31 17:50:14 +020014637 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014638 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014639 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014640 case DRM_FORMAT_RGB565:
14641 case DRM_FORMAT_XRGB8888:
14642 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014643 break;
14644 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014645 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014646 DRM_DEBUG("unsupported pixel format: %s\n",
14647 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014648 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014649 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014650 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014651 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014652 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14653 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014654 DRM_DEBUG("unsupported pixel format: %s\n",
14655 drm_get_format_name(mode_cmd->pixel_format));
14656 return -EINVAL;
14657 }
14658 break;
14659 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014660 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014661 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014662 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014663 DRM_DEBUG("unsupported pixel format: %s\n",
14664 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014665 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014666 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014667 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014668 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014669 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014670 DRM_DEBUG("unsupported pixel format: %s\n",
14671 drm_get_format_name(mode_cmd->pixel_format));
14672 return -EINVAL;
14673 }
14674 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014675 case DRM_FORMAT_YUYV:
14676 case DRM_FORMAT_UYVY:
14677 case DRM_FORMAT_YVYU:
14678 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014679 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014680 DRM_DEBUG("unsupported pixel format: %s\n",
14681 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014682 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014683 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014684 break;
14685 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014686 DRM_DEBUG("unsupported pixel format: %s\n",
14687 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014688 return -EINVAL;
14689 }
14690
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014691 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14692 if (mode_cmd->offsets[0] != 0)
14693 return -EINVAL;
14694
Damien Lespiauec2c9812015-01-20 12:51:45 +000014695 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014696 mode_cmd->pixel_format,
14697 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014698 /* FIXME drm helper for size checks (especially planar formats)? */
14699 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14700 return -EINVAL;
14701
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014702 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14703 intel_fb->obj = obj;
14704
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014705 intel_fill_fb_info(dev_priv, &intel_fb->base);
14706
Jesse Barnes79e53942008-11-07 14:24:08 -080014707 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14708 if (ret) {
14709 DRM_ERROR("framebuffer init failed %d\n", ret);
14710 return ret;
14711 }
14712
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014713 intel_fb->obj->framebuffer_references++;
14714
Jesse Barnes79e53942008-11-07 14:24:08 -080014715 return 0;
14716}
14717
Jesse Barnes79e53942008-11-07 14:24:08 -080014718static struct drm_framebuffer *
14719intel_user_framebuffer_create(struct drm_device *dev,
14720 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014721 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014722{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014723 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014724 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014725 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014726
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014727 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014728 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014729 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014730 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014731
Daniel Vetter92907cb2015-11-23 09:04:05 +010014732 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014733 if (IS_ERR(fb))
14734 drm_gem_object_unreference_unlocked(&obj->base);
14735
14736 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014737}
14738
Daniel Vetter06957262015-08-10 13:34:08 +020014739#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014740static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014741{
14742}
14743#endif
14744
Jesse Barnes79e53942008-11-07 14:24:08 -080014745static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014746 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014747 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014748 .atomic_check = intel_atomic_check,
14749 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014750 .atomic_state_alloc = intel_atomic_state_alloc,
14751 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014752};
14753
Imre Deak88212942016-03-16 13:38:53 +020014754/**
14755 * intel_init_display_hooks - initialize the display modesetting hooks
14756 * @dev_priv: device private
14757 */
14758void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014759{
Imre Deak88212942016-03-16 13:38:53 +020014760 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014761 dev_priv->display.find_dpll = g4x_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014762 else if (IS_CHERRYVIEW(dev_priv))
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014763 dev_priv->display.find_dpll = chv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014764 else if (IS_VALLEYVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014765 dev_priv->display.find_dpll = vlv_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014766 else if (IS_PINEVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014767 dev_priv->display.find_dpll = pnv_find_best_dpll;
14768 else
14769 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14770
Imre Deak88212942016-03-16 13:38:53 +020014771 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014772 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014773 dev_priv->display.get_initial_plane_config =
14774 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014775 dev_priv->display.crtc_compute_clock =
14776 haswell_crtc_compute_clock;
14777 dev_priv->display.crtc_enable = haswell_crtc_enable;
14778 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014779 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014780 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014781 dev_priv->display.get_initial_plane_config =
14782 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014783 dev_priv->display.crtc_compute_clock =
14784 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014785 dev_priv->display.crtc_enable = haswell_crtc_enable;
14786 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014787 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014788 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014789 dev_priv->display.get_initial_plane_config =
14790 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014791 dev_priv->display.crtc_compute_clock =
14792 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014793 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14794 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014795 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014796 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014797 dev_priv->display.get_initial_plane_config =
14798 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014799 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014800 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14801 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014802 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014803 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014804 dev_priv->display.get_initial_plane_config =
14805 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014806 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014807 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14808 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014809 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014810
Jesse Barnese70236a2009-09-21 10:42:27 -070014811 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014812 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014813 dev_priv->display.get_display_clock_speed =
14814 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014815 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014816 dev_priv->display.get_display_clock_speed =
14817 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014818 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014819 dev_priv->display.get_display_clock_speed =
14820 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014821 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014822 dev_priv->display.get_display_clock_speed =
14823 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014824 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014825 dev_priv->display.get_display_clock_speed =
14826 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014827 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014828 dev_priv->display.get_display_clock_speed =
14829 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014830 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14831 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014832 dev_priv->display.get_display_clock_speed =
14833 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014834 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014835 dev_priv->display.get_display_clock_speed =
14836 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014837 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014838 dev_priv->display.get_display_clock_speed =
14839 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014840 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014841 dev_priv->display.get_display_clock_speed =
14842 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014843 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014844 dev_priv->display.get_display_clock_speed =
14845 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014846 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014847 dev_priv->display.get_display_clock_speed =
14848 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014849 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014850 dev_priv->display.get_display_clock_speed =
14851 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014852 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014853 dev_priv->display.get_display_clock_speed =
14854 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014855 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014856 dev_priv->display.get_display_clock_speed =
14857 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014858 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014859 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014860 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014861 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014862 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014863 dev_priv->display.get_display_clock_speed =
14864 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014865 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014866
Imre Deak88212942016-03-16 13:38:53 +020014867 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014868 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014869 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014870 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014871 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014872 /* FIXME: detect B0+ stepping and use auto training */
14873 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014874 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014875 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014876 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014877 dev_priv->display.modeset_commit_cdclk =
14878 broadwell_modeset_commit_cdclk;
14879 dev_priv->display.modeset_calc_cdclk =
14880 broadwell_modeset_calc_cdclk;
14881 }
Imre Deak88212942016-03-16 13:38:53 +020014882 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014883 dev_priv->display.modeset_commit_cdclk =
14884 valleyview_modeset_commit_cdclk;
14885 dev_priv->display.modeset_calc_cdclk =
14886 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014887 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014888 dev_priv->display.modeset_commit_cdclk =
14889 broxton_modeset_commit_cdclk;
14890 dev_priv->display.modeset_calc_cdclk =
14891 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014892 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014893
Imre Deak88212942016-03-16 13:38:53 +020014894 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014895 case 2:
14896 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14897 break;
14898
14899 case 3:
14900 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14901 break;
14902
14903 case 4:
14904 case 5:
14905 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14906 break;
14907
14908 case 6:
14909 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14910 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014911 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014912 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014913 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14914 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014915 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014916 /* Drop through - unsupported since execlist only. */
14917 default:
14918 /* Default just returns -ENODEV to indicate unsupported */
14919 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014920 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014921}
14922
Jesse Barnesb690e962010-07-19 13:53:12 -070014923/*
14924 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14925 * resume, or other times. This quirk makes sure that's the case for
14926 * affected systems.
14927 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014928static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014929{
14930 struct drm_i915_private *dev_priv = dev->dev_private;
14931
14932 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014933 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014934}
14935
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014936static void quirk_pipeb_force(struct drm_device *dev)
14937{
14938 struct drm_i915_private *dev_priv = dev->dev_private;
14939
14940 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14941 DRM_INFO("applying pipe b force quirk\n");
14942}
14943
Keith Packard435793d2011-07-12 14:56:22 -070014944/*
14945 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14946 */
14947static void quirk_ssc_force_disable(struct drm_device *dev)
14948{
14949 struct drm_i915_private *dev_priv = dev->dev_private;
14950 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014951 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014952}
14953
Carsten Emde4dca20e2012-03-15 15:56:26 +010014954/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014955 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14956 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014957 */
14958static void quirk_invert_brightness(struct drm_device *dev)
14959{
14960 struct drm_i915_private *dev_priv = dev->dev_private;
14961 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014962 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014963}
14964
Scot Doyle9c72cc62014-07-03 23:27:50 +000014965/* Some VBT's incorrectly indicate no backlight is present */
14966static void quirk_backlight_present(struct drm_device *dev)
14967{
14968 struct drm_i915_private *dev_priv = dev->dev_private;
14969 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14970 DRM_INFO("applying backlight present quirk\n");
14971}
14972
Jesse Barnesb690e962010-07-19 13:53:12 -070014973struct intel_quirk {
14974 int device;
14975 int subsystem_vendor;
14976 int subsystem_device;
14977 void (*hook)(struct drm_device *dev);
14978};
14979
Egbert Eich5f85f172012-10-14 15:46:38 +020014980/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14981struct intel_dmi_quirk {
14982 void (*hook)(struct drm_device *dev);
14983 const struct dmi_system_id (*dmi_id_list)[];
14984};
14985
14986static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14987{
14988 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14989 return 1;
14990}
14991
14992static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14993 {
14994 .dmi_id_list = &(const struct dmi_system_id[]) {
14995 {
14996 .callback = intel_dmi_reverse_brightness,
14997 .ident = "NCR Corporation",
14998 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14999 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15000 },
15001 },
15002 { } /* terminating entry */
15003 },
15004 .hook = quirk_invert_brightness,
15005 },
15006};
15007
Ben Widawskyc43b5632012-04-16 14:07:40 -070015008static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015009 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15010 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15011
Jesse Barnesb690e962010-07-19 13:53:12 -070015012 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15013 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15014
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015015 /* 830 needs to leave pipe A & dpll A up */
15016 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15017
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015018 /* 830 needs to leave pipe B & dpll B up */
15019 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15020
Keith Packard435793d2011-07-12 14:56:22 -070015021 /* Lenovo U160 cannot use SSC on LVDS */
15022 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015023
15024 /* Sony Vaio Y cannot use SSC on LVDS */
15025 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015026
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015027 /* Acer Aspire 5734Z must invert backlight brightness */
15028 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15029
15030 /* Acer/eMachines G725 */
15031 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15032
15033 /* Acer/eMachines e725 */
15034 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15035
15036 /* Acer/Packard Bell NCL20 */
15037 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15038
15039 /* Acer Aspire 4736Z */
15040 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015041
15042 /* Acer Aspire 5336 */
15043 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015044
15045 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15046 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015047
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015048 /* Acer C720 Chromebook (Core i3 4005U) */
15049 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15050
jens steinb2a96012014-10-28 20:25:53 +010015051 /* Apple Macbook 2,1 (Core 2 T7400) */
15052 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15053
Jani Nikula1b9448b2015-11-05 11:49:59 +020015054 /* Apple Macbook 4,1 */
15055 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15056
Scot Doyled4967d82014-07-03 23:27:52 +000015057 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15058 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015059
15060 /* HP Chromebook 14 (Celeron 2955U) */
15061 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015062
15063 /* Dell Chromebook 11 */
15064 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015065
15066 /* Dell Chromebook 11 (2015 version) */
15067 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015068};
15069
15070static void intel_init_quirks(struct drm_device *dev)
15071{
15072 struct pci_dev *d = dev->pdev;
15073 int i;
15074
15075 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15076 struct intel_quirk *q = &intel_quirks[i];
15077
15078 if (d->device == q->device &&
15079 (d->subsystem_vendor == q->subsystem_vendor ||
15080 q->subsystem_vendor == PCI_ANY_ID) &&
15081 (d->subsystem_device == q->subsystem_device ||
15082 q->subsystem_device == PCI_ANY_ID))
15083 q->hook(dev);
15084 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015085 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15086 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15087 intel_dmi_quirks[i].hook(dev);
15088 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015089}
15090
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015091/* Disable the VGA plane that we never use */
15092static void i915_disable_vga(struct drm_device *dev)
15093{
15094 struct drm_i915_private *dev_priv = dev->dev_private;
15095 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015096 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015097
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015098 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015099 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015100 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015101 sr1 = inb(VGA_SR_DATA);
15102 outb(sr1 | 1<<5, VGA_SR_DATA);
15103 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15104 udelay(300);
15105
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015106 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015107 POSTING_READ(vga_reg);
15108}
15109
Daniel Vetterf8175862012-04-10 15:50:11 +020015110void intel_modeset_init_hw(struct drm_device *dev)
15111{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015112 struct drm_i915_private *dev_priv = dev->dev_private;
15113
Ville Syrjäläb6283052015-06-03 15:45:07 +030015114 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015115
15116 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15117
Daniel Vetterf8175862012-04-10 15:50:11 +020015118 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015119 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015120}
15121
Matt Roperd93c0372015-12-03 11:37:41 -080015122/*
15123 * Calculate what we think the watermarks should be for the state we've read
15124 * out of the hardware and then immediately program those watermarks so that
15125 * we ensure the hardware settings match our internal state.
15126 *
15127 * We can calculate what we think WM's should be by creating a duplicate of the
15128 * current state (which was constructed during hardware readout) and running it
15129 * through the atomic check code to calculate new watermark values in the
15130 * state object.
15131 */
15132static void sanitize_watermarks(struct drm_device *dev)
15133{
15134 struct drm_i915_private *dev_priv = to_i915(dev);
15135 struct drm_atomic_state *state;
15136 struct drm_crtc *crtc;
15137 struct drm_crtc_state *cstate;
15138 struct drm_modeset_acquire_ctx ctx;
15139 int ret;
15140 int i;
15141
15142 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015143 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015144 return;
15145
15146 /*
15147 * We need to hold connection_mutex before calling duplicate_state so
15148 * that the connector loop is protected.
15149 */
15150 drm_modeset_acquire_init(&ctx, 0);
15151retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015152 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015153 if (ret == -EDEADLK) {
15154 drm_modeset_backoff(&ctx);
15155 goto retry;
15156 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015157 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015158 }
15159
15160 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15161 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015162 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015163
Matt Ropered4a6a72016-02-23 17:20:13 -080015164 /*
15165 * Hardware readout is the only time we don't want to calculate
15166 * intermediate watermarks (since we don't trust the current
15167 * watermarks).
15168 */
15169 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15170
Matt Roperd93c0372015-12-03 11:37:41 -080015171 ret = intel_atomic_check(dev, state);
15172 if (ret) {
15173 /*
15174 * If we fail here, it means that the hardware appears to be
15175 * programmed in a way that shouldn't be possible, given our
15176 * understanding of watermark requirements. This might mean a
15177 * mistake in the hardware readout code or a mistake in the
15178 * watermark calculations for a given platform. Raise a WARN
15179 * so that this is noticeable.
15180 *
15181 * If this actually happens, we'll have to just leave the
15182 * BIOS-programmed watermarks untouched and hope for the best.
15183 */
15184 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015185 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015186 }
15187
15188 /* Write calculated watermark values back */
15189 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15190 for_each_crtc_in_state(state, crtc, cstate, i) {
15191 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15192
Matt Ropered4a6a72016-02-23 17:20:13 -080015193 cs->wm.need_postvbl_update = true;
15194 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015195 }
15196
15197 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015198fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015199 drm_modeset_drop_locks(&ctx);
15200 drm_modeset_acquire_fini(&ctx);
15201}
15202
Jesse Barnes79e53942008-11-07 14:24:08 -080015203void intel_modeset_init(struct drm_device *dev)
15204{
Jesse Barnes652c3932009-08-17 13:31:43 -070015205 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015206 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015207 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015208 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015209
15210 drm_mode_config_init(dev);
15211
15212 dev->mode_config.min_width = 0;
15213 dev->mode_config.min_height = 0;
15214
Dave Airlie019d96c2011-09-29 16:20:42 +010015215 dev->mode_config.preferred_depth = 24;
15216 dev->mode_config.prefer_shadow = 1;
15217
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015218 dev->mode_config.allow_fb_modifiers = true;
15219
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015220 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015221
Jesse Barnesb690e962010-07-19 13:53:12 -070015222 intel_init_quirks(dev);
15223
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015224 intel_init_pm(dev);
15225
Ben Widawskye3c74752013-04-05 13:12:39 -070015226 if (INTEL_INFO(dev)->num_pipes == 0)
15227 return;
15228
Lukas Wunner69f92f62015-07-15 13:57:35 +020015229 /*
15230 * There may be no VBT; and if the BIOS enabled SSC we can
15231 * just keep using it to avoid unnecessary flicker. Whereas if the
15232 * BIOS isn't using it, don't assume it will work even if the VBT
15233 * indicates as much.
15234 */
15235 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15236 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15237 DREF_SSC1_ENABLE);
15238
15239 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15240 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15241 bios_lvds_use_ssc ? "en" : "dis",
15242 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15243 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15244 }
15245 }
15246
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015247 if (IS_GEN2(dev)) {
15248 dev->mode_config.max_width = 2048;
15249 dev->mode_config.max_height = 2048;
15250 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015251 dev->mode_config.max_width = 4096;
15252 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015253 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015254 dev->mode_config.max_width = 8192;
15255 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015256 }
Damien Lespiau068be562014-03-28 14:17:49 +000015257
Ville Syrjälädc41c152014-08-13 11:57:05 +030015258 if (IS_845G(dev) || IS_I865G(dev)) {
15259 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15260 dev->mode_config.cursor_height = 1023;
15261 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015262 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15263 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15264 } else {
15265 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15266 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15267 }
15268
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015269 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015270
Zhao Yakui28c97732009-10-09 11:39:41 +080015271 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015272 INTEL_INFO(dev)->num_pipes,
15273 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015274
Damien Lespiau055e3932014-08-18 13:49:10 +010015275 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015276 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015277 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015278 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015279 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015280 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015281 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015282 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015283 }
15284
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015285 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015286 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015287 intel_update_cdclk(dev);
15288
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015289 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015290
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015291 /* Just disable it once at startup */
15292 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015293 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015294
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015295 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015296 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015297 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015298
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015299 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015300 struct intel_initial_plane_config plane_config = {};
15301
Jesse Barnes46f297f2014-03-07 08:57:48 -080015302 if (!crtc->active)
15303 continue;
15304
Jesse Barnes46f297f2014-03-07 08:57:48 -080015305 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015306 * Note that reserving the BIOS fb up front prevents us
15307 * from stuffing other stolen allocations like the ring
15308 * on top. This prevents some ugliness at boot time, and
15309 * can even allow for smooth boot transitions if the BIOS
15310 * fb is large enough for the active pipe configuration.
15311 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015312 dev_priv->display.get_initial_plane_config(crtc,
15313 &plane_config);
15314
15315 /*
15316 * If the fb is shared between multiple heads, we'll
15317 * just get the first one.
15318 */
15319 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015320 }
Matt Roperd93c0372015-12-03 11:37:41 -080015321
15322 /*
15323 * Make sure hardware watermarks really match the state we read out.
15324 * Note that we need to do this after reconstructing the BIOS fb's
15325 * since the watermark calculation done here will use pstate->fb.
15326 */
15327 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015328}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015329
Daniel Vetter7fad7982012-07-04 17:51:47 +020015330static void intel_enable_pipe_a(struct drm_device *dev)
15331{
15332 struct intel_connector *connector;
15333 struct drm_connector *crt = NULL;
15334 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015335 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015336
15337 /* We can't just switch on the pipe A, we need to set things up with a
15338 * proper mode and output configuration. As a gross hack, enable pipe A
15339 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015340 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015341 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15342 crt = &connector->base;
15343 break;
15344 }
15345 }
15346
15347 if (!crt)
15348 return;
15349
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015350 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015351 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015352}
15353
Daniel Vetterfa555832012-10-10 23:14:00 +020015354static bool
15355intel_check_plane_mapping(struct intel_crtc *crtc)
15356{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015357 struct drm_device *dev = crtc->base.dev;
15358 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015359 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015360
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015361 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015362 return true;
15363
Ville Syrjälä649636e2015-09-22 19:50:01 +030015364 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015365
15366 if ((val & DISPLAY_PLANE_ENABLE) &&
15367 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15368 return false;
15369
15370 return true;
15371}
15372
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015373static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15374{
15375 struct drm_device *dev = crtc->base.dev;
15376 struct intel_encoder *encoder;
15377
15378 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15379 return true;
15380
15381 return false;
15382}
15383
Ville Syrjälädd756192016-02-17 21:28:45 +020015384static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15385{
15386 struct drm_device *dev = encoder->base.dev;
15387 struct intel_connector *connector;
15388
15389 for_each_connector_on_encoder(dev, &encoder->base, connector)
15390 return true;
15391
15392 return false;
15393}
15394
Daniel Vetter24929352012-07-02 20:28:59 +020015395static void intel_sanitize_crtc(struct intel_crtc *crtc)
15396{
15397 struct drm_device *dev = crtc->base.dev;
15398 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015399 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015400
Daniel Vetter24929352012-07-02 20:28:59 +020015401 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015402 if (!transcoder_is_dsi(cpu_transcoder)) {
15403 i915_reg_t reg = PIPECONF(cpu_transcoder);
15404
15405 I915_WRITE(reg,
15406 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15407 }
Daniel Vetter24929352012-07-02 20:28:59 +020015408
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015409 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015410 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015411 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015412 struct intel_plane *plane;
15413
Daniel Vetter96256042015-02-13 21:03:42 +010015414 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015415
15416 /* Disable everything but the primary plane */
15417 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15418 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15419 continue;
15420
15421 plane->disable_plane(&plane->base, &crtc->base);
15422 }
Daniel Vetter96256042015-02-13 21:03:42 +010015423 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015424
Daniel Vetter24929352012-07-02 20:28:59 +020015425 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015426 * disable the crtc (and hence change the state) if it is wrong. Note
15427 * that gen4+ has a fixed plane -> pipe mapping. */
15428 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015429 bool plane;
15430
Daniel Vetter24929352012-07-02 20:28:59 +020015431 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15432 crtc->base.base.id);
15433
15434 /* Pipe has the wrong plane attached and the plane is active.
15435 * Temporarily change the plane mapping and disable everything
15436 * ... */
15437 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015438 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015439 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015440 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015441 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015442 }
Daniel Vetter24929352012-07-02 20:28:59 +020015443
Daniel Vetter7fad7982012-07-04 17:51:47 +020015444 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15445 crtc->pipe == PIPE_A && !crtc->active) {
15446 /* BIOS forgot to enable pipe A, this mostly happens after
15447 * resume. Force-enable the pipe to fix this, the update_dpms
15448 * call below we restore the pipe to the right state, but leave
15449 * the required bits on. */
15450 intel_enable_pipe_a(dev);
15451 }
15452
Daniel Vetter24929352012-07-02 20:28:59 +020015453 /* Adjust the state of the output pipe according to whether we
15454 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015455 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015456 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015457
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015458 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015459 /*
15460 * We start out with underrun reporting disabled to avoid races.
15461 * For correct bookkeeping mark this on active crtcs.
15462 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015463 * Also on gmch platforms we dont have any hardware bits to
15464 * disable the underrun reporting. Which means we need to start
15465 * out with underrun reporting disabled also on inactive pipes,
15466 * since otherwise we'll complain about the garbage we read when
15467 * e.g. coming up after runtime pm.
15468 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015469 * No protection against concurrent access is required - at
15470 * worst a fifo underrun happens which also sets this to false.
15471 */
15472 crtc->cpu_fifo_underrun_disabled = true;
15473 crtc->pch_fifo_underrun_disabled = true;
15474 }
Daniel Vetter24929352012-07-02 20:28:59 +020015475}
15476
15477static void intel_sanitize_encoder(struct intel_encoder *encoder)
15478{
15479 struct intel_connector *connector;
15480 struct drm_device *dev = encoder->base.dev;
15481
15482 /* We need to check both for a crtc link (meaning that the
15483 * encoder is active and trying to read from a pipe) and the
15484 * pipe itself being active. */
15485 bool has_active_crtc = encoder->base.crtc &&
15486 to_intel_crtc(encoder->base.crtc)->active;
15487
Ville Syrjälädd756192016-02-17 21:28:45 +020015488 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015489 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15490 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015491 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015492
15493 /* Connector is active, but has no active pipe. This is
15494 * fallout from our resume register restoring. Disable
15495 * the encoder manually again. */
15496 if (encoder->base.crtc) {
15497 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15498 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015499 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015500 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015501 if (encoder->post_disable)
15502 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015503 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015504 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015505
15506 /* Inconsistent output/port/pipe state happens presumably due to
15507 * a bug in one of the get_hw_state functions. Or someplace else
15508 * in our code, like the register restore mess on resume. Clamp
15509 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015510 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015511 if (connector->encoder != encoder)
15512 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015513 connector->base.dpms = DRM_MODE_DPMS_OFF;
15514 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015515 }
15516 }
15517 /* Enabled encoders without active connectors will be fixed in
15518 * the crtc fixup. */
15519}
15520
Imre Deak04098752014-02-18 00:02:16 +020015521void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015522{
15523 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015524 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015525
Imre Deak04098752014-02-18 00:02:16 +020015526 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15527 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15528 i915_disable_vga(dev);
15529 }
15530}
15531
15532void i915_redisable_vga(struct drm_device *dev)
15533{
15534 struct drm_i915_private *dev_priv = dev->dev_private;
15535
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015536 /* This function can be called both from intel_modeset_setup_hw_state or
15537 * at a very early point in our resume sequence, where the power well
15538 * structures are not yet restored. Since this function is at a very
15539 * paranoid "someone might have enabled VGA while we were not looking"
15540 * level, just check if the power well is enabled instead of trying to
15541 * follow the "don't touch the power well if we don't need it" policy
15542 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015543 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015544 return;
15545
Imre Deak04098752014-02-18 00:02:16 +020015546 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015547
15548 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015549}
15550
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015551static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015552{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015553 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015554
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015555 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015556}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015557
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015558/* FIXME read out full plane state for all planes */
15559static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015560{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015561 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015562 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015563 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015564
Matt Roper19b8d382015-09-24 15:53:17 -070015565 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015566 primary_get_hw_state(to_intel_plane(primary));
15567
15568 if (plane_state->visible)
15569 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015570}
15571
Daniel Vetter30e984d2013-06-05 13:34:17 +020015572static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015573{
15574 struct drm_i915_private *dev_priv = dev->dev_private;
15575 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015576 struct intel_crtc *crtc;
15577 struct intel_encoder *encoder;
15578 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015579 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015580
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015581 dev_priv->active_crtcs = 0;
15582
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015583 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015584 struct intel_crtc_state *crtc_state = crtc->config;
15585 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015586
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015587 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15588 memset(crtc_state, 0, sizeof(*crtc_state));
15589 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015590
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015591 crtc_state->base.active = crtc_state->base.enable =
15592 dev_priv->display.get_pipe_config(crtc, crtc_state);
15593
15594 crtc->base.enabled = crtc_state->base.enable;
15595 crtc->active = crtc_state->base.active;
15596
15597 if (crtc_state->base.active) {
15598 dev_priv->active_crtcs |= 1 << crtc->pipe;
15599
15600 if (IS_BROADWELL(dev_priv)) {
15601 pixclk = ilk_pipe_pixel_rate(crtc_state);
15602
15603 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15604 if (crtc_state->ips_enabled)
15605 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15606 } else if (IS_VALLEYVIEW(dev_priv) ||
15607 IS_CHERRYVIEW(dev_priv) ||
15608 IS_BROXTON(dev_priv))
15609 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15610 else
15611 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15612 }
15613
15614 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015615
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015616 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015617
15618 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15619 crtc->base.base.id,
15620 crtc->active ? "enabled" : "disabled");
15621 }
15622
Daniel Vetter53589012013-06-05 13:34:16 +020015623 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15624 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15625
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015626 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15627 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015628 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015629 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015630 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015631 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015632 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015633 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015634
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015635 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015636 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015637 }
15638
Damien Lespiaub2784e12014-08-05 11:29:37 +010015639 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015640 pipe = 0;
15641
15642 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015643 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15644 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015645 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015646 } else {
15647 encoder->base.crtc = NULL;
15648 }
15649
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015650 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015651 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015652 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015653 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015654 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015655 }
15656
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015657 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015658 if (connector->get_hw_state(connector)) {
15659 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015660
15661 encoder = connector->encoder;
15662 connector->base.encoder = &encoder->base;
15663
15664 if (encoder->base.crtc &&
15665 encoder->base.crtc->state->active) {
15666 /*
15667 * This has to be done during hardware readout
15668 * because anything calling .crtc_disable may
15669 * rely on the connector_mask being accurate.
15670 */
15671 encoder->base.crtc->state->connector_mask |=
15672 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015673 encoder->base.crtc->state->encoder_mask |=
15674 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015675 }
15676
Daniel Vetter24929352012-07-02 20:28:59 +020015677 } else {
15678 connector->base.dpms = DRM_MODE_DPMS_OFF;
15679 connector->base.encoder = NULL;
15680 }
15681 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15682 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015683 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015684 connector->base.encoder ? "enabled" : "disabled");
15685 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015686
15687 for_each_intel_crtc(dev, crtc) {
15688 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15689
15690 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15691 if (crtc->base.state->active) {
15692 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15693 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15694 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15695
15696 /*
15697 * The initial mode needs to be set in order to keep
15698 * the atomic core happy. It wants a valid mode if the
15699 * crtc's enabled, so we do the above call.
15700 *
15701 * At this point some state updated by the connectors
15702 * in their ->detect() callback has not run yet, so
15703 * no recalculation can be done yet.
15704 *
15705 * Even if we could do a recalculation and modeset
15706 * right now it would cause a double modeset if
15707 * fbdev or userspace chooses a different initial mode.
15708 *
15709 * If that happens, someone indicated they wanted a
15710 * mode change, which means it's safe to do a full
15711 * recalculation.
15712 */
15713 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015714
15715 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15716 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015717 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015718
15719 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015720 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015721}
15722
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015723/* Scan out the current hw modeset state,
15724 * and sanitizes it to the current state
15725 */
15726static void
15727intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015728{
15729 struct drm_i915_private *dev_priv = dev->dev_private;
15730 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015731 struct intel_crtc *crtc;
15732 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015733 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015734
15735 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015736
15737 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015738 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015739 intel_sanitize_encoder(encoder);
15740 }
15741
Damien Lespiau055e3932014-08-18 13:49:10 +010015742 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015743 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15744 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015745 intel_dump_pipe_config(crtc, crtc->config,
15746 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015747 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015748
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015749 intel_modeset_update_connector_atomic_state(dev);
15750
Daniel Vetter35c95372013-07-17 06:55:04 +020015751 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15752 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15753
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015754 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015755 continue;
15756
15757 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15758
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015759 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015760 pll->on = false;
15761 }
15762
Wayne Boyer666a4532015-12-09 12:29:35 -080015763 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015764 vlv_wm_get_hw_state(dev);
15765 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015766 skl_wm_get_hw_state(dev);
15767 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015768 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015769
15770 for_each_intel_crtc(dev, crtc) {
15771 unsigned long put_domains;
15772
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015773 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015774 if (WARN_ON(put_domains))
15775 modeset_put_power_domains(dev_priv, put_domains);
15776 }
15777 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015778
15779 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015780}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015781
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015782void intel_display_resume(struct drm_device *dev)
15783{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015784 struct drm_i915_private *dev_priv = to_i915(dev);
15785 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15786 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015787 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015788 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015789
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015790 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015791
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015792 /*
15793 * This is a cludge because with real atomic modeset mode_config.mutex
15794 * won't be taken. Unfortunately some probed state like
15795 * audio_codec_enable is still protected by mode_config.mutex, so lock
15796 * it here for now.
15797 */
15798 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015799 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015800
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015801retry:
15802 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015803
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015804 if (ret == 0 && !setup) {
15805 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015806
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015807 intel_modeset_setup_hw_state(dev);
15808 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015809 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015810
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015811 if (ret == 0 && state) {
15812 struct drm_crtc_state *crtc_state;
15813 struct drm_crtc *crtc;
15814 int i;
15815
15816 state->acquire_ctx = &ctx;
15817
15818 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15819 /*
15820 * Force recalculation even if we restore
15821 * current state. With fast modeset this may not result
15822 * in a modeset when the state is compatible.
15823 */
15824 crtc_state->mode_changed = true;
15825 }
15826
15827 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015828 }
15829
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015830 if (ret == -EDEADLK) {
15831 drm_modeset_backoff(&ctx);
15832 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015833 }
15834
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015835 drm_modeset_drop_locks(&ctx);
15836 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015837 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015838
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015839 if (ret) {
15840 DRM_ERROR("Restoring old state failed with %i\n", ret);
15841 drm_atomic_state_free(state);
15842 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015843}
15844
15845void intel_modeset_gem_init(struct drm_device *dev)
15846{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015847 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015848 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015849 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015850
Imre Deakae484342014-03-31 15:10:44 +030015851 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015852
Chris Wilson1833b132012-05-09 11:56:28 +010015853 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015854
15855 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015856
15857 /*
15858 * Make sure any fbs we allocated at startup are properly
15859 * pinned & fenced. When we do the allocation it's too early
15860 * for this.
15861 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015862 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015863 obj = intel_fb_obj(c->primary->fb);
15864 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015865 continue;
15866
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015867 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015868 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15869 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015870 mutex_unlock(&dev->struct_mutex);
15871 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015872 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15873 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015874 drm_framebuffer_unreference(c->primary->fb);
15875 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015876 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015877 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015878 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015879 }
15880 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015881
15882 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015883}
15884
Imre Deak4932e2c2014-02-11 17:12:48 +020015885void intel_connector_unregister(struct intel_connector *intel_connector)
15886{
15887 struct drm_connector *connector = &intel_connector->base;
15888
15889 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015890 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015891}
15892
Jesse Barnes79e53942008-11-07 14:24:08 -080015893void intel_modeset_cleanup(struct drm_device *dev)
15894{
Jesse Barnes652c3932009-08-17 13:31:43 -070015895 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015896 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015897
Imre Deak2eb52522014-11-19 15:30:05 +020015898 intel_disable_gt_powersave(dev);
15899
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015900 intel_backlight_unregister(dev);
15901
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015902 /*
15903 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015904 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015905 * experience fancy races otherwise.
15906 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015907 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015908
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015909 /*
15910 * Due to the hpd irq storm handling the hotplug work can re-arm the
15911 * poll handlers. Hence disable polling after hpd handling is shut down.
15912 */
Keith Packardf87ea762010-10-03 19:36:26 -070015913 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015914
Jesse Barnes723bfd72010-10-07 16:01:13 -070015915 intel_unregister_dsm_handler();
15916
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015917 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015918
Chris Wilson1630fe72011-07-08 12:22:42 +010015919 /* flush any delayed tasks or pending work */
15920 flush_scheduled_work();
15921
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015922 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020015923 for_each_intel_connector(dev, connector)
15924 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030015925
Jesse Barnes79e53942008-11-07 14:24:08 -080015926 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015927
15928 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015929
Imre Deakae484342014-03-31 15:10:44 +030015930 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010015931
15932 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015933}
15934
Dave Airlie28d52042009-09-21 14:33:58 +100015935/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015936 * Return which encoder is currently attached for connector.
15937 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015938struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015939{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015940 return &intel_attached_encoder(connector)->base;
15941}
Jesse Barnes79e53942008-11-07 14:24:08 -080015942
Chris Wilsondf0e9242010-09-09 16:20:55 +010015943void intel_connector_attach_encoder(struct intel_connector *connector,
15944 struct intel_encoder *encoder)
15945{
15946 connector->encoder = encoder;
15947 drm_mode_connector_attach_encoder(&connector->base,
15948 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015949}
Dave Airlie28d52042009-09-21 14:33:58 +100015950
15951/*
15952 * set vga decode state - true == enable VGA decode
15953 */
15954int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15955{
15956 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015957 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015958 u16 gmch_ctrl;
15959
Chris Wilson75fa0412014-02-07 18:37:02 -020015960 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15961 DRM_ERROR("failed to read control word\n");
15962 return -EIO;
15963 }
15964
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015965 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15966 return 0;
15967
Dave Airlie28d52042009-09-21 14:33:58 +100015968 if (state)
15969 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15970 else
15971 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015972
15973 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15974 DRM_ERROR("failed to write control word\n");
15975 return -EIO;
15976 }
15977
Dave Airlie28d52042009-09-21 14:33:58 +100015978 return 0;
15979}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015980
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015981struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015982
15983 u32 power_well_driver;
15984
Chris Wilson63b66e52013-08-08 15:12:06 +020015985 int num_transcoders;
15986
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015987 struct intel_cursor_error_state {
15988 u32 control;
15989 u32 position;
15990 u32 base;
15991 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015992 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015993
15994 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015995 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015996 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015997 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015998 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015999
16000 struct intel_plane_error_state {
16001 u32 control;
16002 u32 stride;
16003 u32 size;
16004 u32 pos;
16005 u32 addr;
16006 u32 surface;
16007 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016008 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016009
16010 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016011 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016012 enum transcoder cpu_transcoder;
16013
16014 u32 conf;
16015
16016 u32 htotal;
16017 u32 hblank;
16018 u32 hsync;
16019 u32 vtotal;
16020 u32 vblank;
16021 u32 vsync;
16022 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016023};
16024
16025struct intel_display_error_state *
16026intel_display_capture_error_state(struct drm_device *dev)
16027{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016028 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016029 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016030 int transcoders[] = {
16031 TRANSCODER_A,
16032 TRANSCODER_B,
16033 TRANSCODER_C,
16034 TRANSCODER_EDP,
16035 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016036 int i;
16037
Chris Wilson63b66e52013-08-08 15:12:06 +020016038 if (INTEL_INFO(dev)->num_pipes == 0)
16039 return NULL;
16040
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016041 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016042 if (error == NULL)
16043 return NULL;
16044
Imre Deak190be112013-11-25 17:15:31 +020016045 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016046 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16047
Damien Lespiau055e3932014-08-18 13:49:10 +010016048 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016049 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016050 __intel_display_power_is_enabled(dev_priv,
16051 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016052 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016053 continue;
16054
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016055 error->cursor[i].control = I915_READ(CURCNTR(i));
16056 error->cursor[i].position = I915_READ(CURPOS(i));
16057 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016058
16059 error->plane[i].control = I915_READ(DSPCNTR(i));
16060 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016061 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016062 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016063 error->plane[i].pos = I915_READ(DSPPOS(i));
16064 }
Paulo Zanonica291362013-03-06 20:03:14 -030016065 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16066 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016067 if (INTEL_INFO(dev)->gen >= 4) {
16068 error->plane[i].surface = I915_READ(DSPSURF(i));
16069 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16070 }
16071
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016072 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016073
Sonika Jindal3abfce72014-07-21 15:23:43 +053016074 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016075 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016076 }
16077
Jani Nikula4d1de972016-03-18 17:05:42 +020016078 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016079 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16080 if (HAS_DDI(dev_priv->dev))
16081 error->num_transcoders++; /* Account for eDP. */
16082
16083 for (i = 0; i < error->num_transcoders; i++) {
16084 enum transcoder cpu_transcoder = transcoders[i];
16085
Imre Deakddf9c532013-11-27 22:02:02 +020016086 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016087 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016088 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016089 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016090 continue;
16091
Chris Wilson63b66e52013-08-08 15:12:06 +020016092 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16093
16094 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16095 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16096 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16097 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16098 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16099 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16100 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016101 }
16102
16103 return error;
16104}
16105
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016106#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16107
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016108void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016109intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016110 struct drm_device *dev,
16111 struct intel_display_error_state *error)
16112{
Damien Lespiau055e3932014-08-18 13:49:10 +010016113 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016114 int i;
16115
Chris Wilson63b66e52013-08-08 15:12:06 +020016116 if (!error)
16117 return;
16118
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016119 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016120 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016121 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016122 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016123 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016124 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016125 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016126 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016127 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016128 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016129
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016130 err_printf(m, "Plane [%d]:\n", i);
16131 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16132 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016133 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016134 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16135 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016136 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016137 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016138 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016139 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016140 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16141 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016142 }
16143
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016144 err_printf(m, "Cursor [%d]:\n", i);
16145 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16146 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16147 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016148 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016149
16150 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016151 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016152 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016153 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016154 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016155 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16156 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16157 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16158 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16159 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16160 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16161 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16162 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016163}