blob: 611757a462a7ecd953cbb23d084a65b925f92d1a [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);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200114static void skylake_pfit_enable(struct intel_crtc *crtc);
115static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
116static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200117static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200118static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100119
Jesse Barnes79e53942008-11-07 14:24:08 -0800120typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122} intel_range_t;
123
124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int dot_limit;
126 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127} intel_p2_t;
128
Ma Lingd4906092009-03-18 20:13:27 +0800129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800133};
Jesse Barnes79e53942008-11-07 14:24:08 -0800134
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300135/* returns HPLL frequency in kHz */
136static int valleyview_get_vco(struct drm_i915_private *dev_priv)
137{
138 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
139
140 /* Obtain SKU information */
141 mutex_lock(&dev_priv->sb_lock);
142 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
143 CCK_FUSE_HPLL_FREQ_MASK;
144 mutex_unlock(&dev_priv->sb_lock);
145
146 return vco_freq[hpll_freq] * 1000;
147}
148
149static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
150 const char *name, u32 reg)
151{
152 u32 val;
153 int divider;
154
155 if (dev_priv->hpll_freq == 0)
156 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
157
158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
168 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
169}
170
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200171static int
172intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200173{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200174 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200175}
176
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200177static int
178intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300179{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200180 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
181 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182}
183
184static int
185intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
186{
Jani Nikula79e50a42015-08-26 10:58:20 +0300187 uint32_t clkcfg;
188
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300190 clkcfg = I915_READ(CLKCFG);
191 switch (clkcfg & CLKCFG_FSB_MASK) {
192 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200193 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300194 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300196 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200197 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200199 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 /* these two are just a guess; one of them might be right */
205 case CLKCFG_FSB_1600:
206 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 }
211}
212
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213static void intel_update_rawclk(struct drm_i915_private *dev_priv)
214{
215 if (HAS_PCH_SPLIT(dev_priv))
216 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
217 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
218 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
219 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
220 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
221 else
222 return; /* no rawclk on other platforms, or no need to know it */
223
224 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
225}
226
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300227static void intel_update_czclk(struct drm_i915_private *dev_priv)
228{
Wayne Boyer666a4532015-12-09 12:29:35 -0800229 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300230 return;
231
232 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
233 CCK_CZ_CLOCK_CONTROL);
234
235 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
236}
237
Chris Wilson021357a2010-09-07 20:54:59 +0100238static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200239intel_fdi_link_freq(struct drm_i915_private *dev_priv,
240 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100241{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200242 if (HAS_DDI(dev_priv))
243 return pipe_config->port_clock; /* SPLL */
244 else if (IS_GEN5(dev_priv))
245 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200246 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200247 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100248}
249
Daniel Vetter5d536e22013-07-06 12:52:06 +0200250static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400251 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200252 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200253 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400254 .m = { .min = 96, .max = 140 },
255 .m1 = { .min = 18, .max = 26 },
256 .m2 = { .min = 6, .max = 16 },
257 .p = { .min = 4, .max = 128 },
258 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700259 .p2 = { .dot_limit = 165000,
260 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700261};
262
Daniel Vetter5d536e22013-07-06 12:52:06 +0200263static const intel_limit_t intel_limits_i8xx_dvo = {
264 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200265 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200266 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 4 },
274};
275
Keith Packarde4b36692009-06-05 19:22:17 -0700276static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200278 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200279 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700287};
Eric Anholt273e27c2011-03-30 13:01:10 -0700288
Keith Packarde4b36692009-06-05 19:22:17 -0700289static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 20000, .max = 400000 },
291 .vco = { .min = 1400000, .max = 2800000 },
292 .n = { .min = 1, .max = 6 },
293 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100294 .m1 = { .min = 8, .max = 18 },
295 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400296 .p = { .min = 5, .max = 80 },
297 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 200000,
299 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
301
302static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p = { .min = 7, .max = 98 },
310 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .p2 = { .dot_limit = 112000,
312 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Eric Anholt273e27c2011-03-30 13:01:10 -0700315
Keith Packarde4b36692009-06-05 19:22:17 -0700316static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700317 .dot = { .min = 25000, .max = 270000 },
318 .vco = { .min = 1750000, .max = 3500000},
319 .n = { .min = 1, .max = 4 },
320 .m = { .min = 104, .max = 138 },
321 .m1 = { .min = 17, .max = 23 },
322 .m2 = { .min = 5, .max = 11 },
323 .p = { .min = 10, .max = 30 },
324 .p1 = { .min = 1, .max = 3},
325 .p2 = { .dot_limit = 270000,
326 .p2_slow = 10,
327 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800328 },
Keith Packarde4b36692009-06-05 19:22:17 -0700329};
330
331static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700332 .dot = { .min = 22000, .max = 400000 },
333 .vco = { .min = 1750000, .max = 3500000},
334 .n = { .min = 1, .max = 4 },
335 .m = { .min = 104, .max = 138 },
336 .m1 = { .min = 16, .max = 23 },
337 .m2 = { .min = 5, .max = 11 },
338 .p = { .min = 5, .max = 80 },
339 .p1 = { .min = 1, .max = 8},
340 .p2 = { .dot_limit = 165000,
341 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
344static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 20000, .max = 115000 },
346 .vco = { .min = 1750000, .max = 3500000 },
347 .n = { .min = 1, .max = 3 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 17, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 28, .max = 112 },
352 .p1 = { .min = 2, .max = 8 },
353 .p2 = { .dot_limit = 0,
354 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800355 },
Keith Packarde4b36692009-06-05 19:22:17 -0700356};
357
358static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .dot = { .min = 80000, .max = 224000 },
360 .vco = { .min = 1750000, .max = 3500000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 104, .max = 138 },
363 .m1 = { .min = 17, .max = 23 },
364 .m2 = { .min = 5, .max = 11 },
365 .p = { .min = 14, .max = 42 },
366 .p1 = { .min = 2, .max = 6 },
367 .p2 = { .dot_limit = 0,
368 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800369 },
Keith Packarde4b36692009-06-05 19:22:17 -0700370};
371
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500372static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400373 .dot = { .min = 20000, .max = 400000},
374 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700375 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400376 .n = { .min = 3, .max = 6 },
377 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700378 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400379 .m1 = { .min = 0, .max = 0 },
380 .m2 = { .min = 0, .max = 254 },
381 .p = { .min = 5, .max = 80 },
382 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700383 .p2 = { .dot_limit = 200000,
384 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700385};
386
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500387static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .dot = { .min = 20000, .max = 400000 },
389 .vco = { .min = 1700000, .max = 3500000 },
390 .n = { .min = 3, .max = 6 },
391 .m = { .min = 2, .max = 256 },
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 7, .max = 112 },
395 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700396 .p2 = { .dot_limit = 112000,
397 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700398};
399
Eric Anholt273e27c2011-03-30 13:01:10 -0700400/* Ironlake / Sandybridge
401 *
402 * We calculate clock using (register_value + 2) for N/M1/M2, so here
403 * the range value for them is (actual_value - 2).
404 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800405static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700406 .dot = { .min = 25000, .max = 350000 },
407 .vco = { .min = 1760000, .max = 3510000 },
408 .n = { .min = 1, .max = 5 },
409 .m = { .min = 79, .max = 127 },
410 .m1 = { .min = 12, .max = 22 },
411 .m2 = { .min = 5, .max = 9 },
412 .p = { .min = 5, .max = 80 },
413 .p1 = { .min = 1, .max = 8 },
414 .p2 = { .dot_limit = 225000,
415 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700416};
417
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800418static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 3 },
422 .m = { .min = 79, .max = 118 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 28, .max = 112 },
426 .p1 = { .min = 2, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800429};
430
431static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 127 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 14, .max = 56 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
Eric Anholt273e27c2011-03-30 13:01:10 -0700444/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800445static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700446 .dot = { .min = 25000, .max = 350000 },
447 .vco = { .min = 1760000, .max = 3510000 },
448 .n = { .min = 1, .max = 2 },
449 .m = { .min = 79, .max = 126 },
450 .m1 = { .min = 12, .max = 22 },
451 .m2 = { .min = 5, .max = 9 },
452 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400453 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700454 .p2 = { .dot_limit = 225000,
455 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800456};
457
458static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 3 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800469};
470
Ville Syrjälädc730512013-09-24 21:26:30 +0300471static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300472 /*
473 * These are the data rate limits (measured in fast clocks)
474 * since those are the strictest limits we have. The fast
475 * clock and actual rate limits are more relaxed, so checking
476 * them would make no difference.
477 */
478 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200479 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700480 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700481 .m1 = { .min = 2, .max = 3 },
482 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300483 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300484 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700485};
486
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300487static const intel_limit_t intel_limits_chv = {
488 /*
489 * These are the data rate limits (measured in fast clocks)
490 * since those are the strictest limits we have. The fast
491 * clock and actual rate limits are more relaxed, so checking
492 * them would make no difference.
493 */
494 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200495 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300496 .n = { .min = 1, .max = 1 },
497 .m1 = { .min = 2, .max = 2 },
498 .m2 = { .min = 24 << 22, .max = 175 << 22 },
499 .p1 = { .min = 2, .max = 4 },
500 .p2 = { .p2_slow = 1, .p2_fast = 14 },
501};
502
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200503static const intel_limit_t intel_limits_bxt = {
504 /* FIXME: find real dot limits */
505 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530506 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200507 .n = { .min = 1, .max = 1 },
508 .m1 = { .min = 2, .max = 2 },
509 /* FIXME: find real m2 limits */
510 .m2 = { .min = 2 << 22, .max = 255 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 20 },
513};
514
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200515static bool
516needs_modeset(struct drm_crtc_state *state)
517{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200518 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200519}
520
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300521/**
522 * Returns whether any output on the specified pipe is of the specified type
523 */
Damien Lespiau40935612014-10-29 11:16:59 +0000524bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300525{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300526 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300527 struct intel_encoder *encoder;
528
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300529 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300530 if (encoder->type == type)
531 return true;
532
533 return false;
534}
535
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200536/**
537 * Returns whether any output on the specified pipe will have the specified
538 * type after a staged modeset is complete, i.e., the same as
539 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
540 * encoder->crtc.
541 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200542static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
543 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200544{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200545 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300546 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200547 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200550
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300551 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200552 if (connector_state->crtc != crtc_state->base.crtc)
553 continue;
554
555 num_connectors++;
556
557 encoder = to_intel_encoder(connector_state->best_encoder);
558 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200559 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200560 }
561
562 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200563
564 return false;
565}
566
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200567static const intel_limit_t *
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200568intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800569{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200570 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800571 const intel_limit_t *limit;
572
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200573 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100574 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700575 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800576 else
Keith Packarde4b36692009-06-05 19:22:17 -0700577 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200578 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
579 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700580 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200581 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700582 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800583 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700584 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800585
586 return limit;
587}
588
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200589static const intel_limit_t *
590intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800591{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200592 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800593 const intel_limit_t *limit;
594
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200595 if (IS_BROXTON(dev) || IS_CHERRYVIEW(dev) || IS_VALLEYVIEW(dev) ||
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +0200596 HAS_PCH_SPLIT(dev) || IS_GEN2(dev))
Ander Conselvan de Oliveira8f0d5b92016-03-21 18:00:04 +0200597 limit = NULL;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200598
599 if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200600 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500601 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200602 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500603 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800604 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500605 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100606 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200607 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100608 limit = &intel_limits_i9xx_lvds;
609 else
610 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 }
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200612
613 WARN_ON(limit == NULL);
614
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 return limit;
616}
617
Imre Deakdccbea32015-06-22 23:35:51 +0300618/*
619 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
620 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
621 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
622 * The helpers' return value is the rate of the clock that is fed to the
623 * display engine's pipe which can be the above fast dot clock rate or a
624 * divided-down version of it.
625 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500626/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300627static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800628{
Shaohua Li21778322009-02-23 15:19:16 +0800629 clock->m = clock->m2 + 2;
630 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200631 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300632 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300633 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
634 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300635
636 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800637}
638
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200639static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
640{
641 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
642}
643
Imre Deakdccbea32015-06-22 23:35:51 +0300644static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800645{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200646 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200648 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300649 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300650 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
651 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300652
653 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800654}
655
Imre Deakdccbea32015-06-22 23:35:51 +0300656static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300657{
658 clock->m = clock->m1 * clock->m2;
659 clock->p = clock->p1 * clock->p2;
660 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300661 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300662 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
663 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300664
665 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300666}
667
Imre Deakdccbea32015-06-22 23:35:51 +0300668int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300669{
670 clock->m = clock->m1 * clock->m2;
671 clock->p = clock->p1 * clock->p2;
672 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300673 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300674 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
675 clock->n << 22);
676 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300677
678 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300679}
680
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800681#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800682/**
683 * Returns whether the given set of divisors are valid for a given refclk with
684 * the given connectors.
685 */
686
Chris Wilson1b894b52010-12-14 20:04:54 +0000687static bool intel_PLL_is_valid(struct drm_device *dev,
688 const intel_limit_t *limit,
689 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800690{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300691 if (clock->n < limit->n.min || limit->n.max < clock->n)
692 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400694 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400696 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400698 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300699
Wayne Boyer666a4532015-12-09 12:29:35 -0800700 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
701 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300702 if (clock->m1 <= clock->m2)
703 INTELPllInvalid("m1 <= m2\n");
704
Wayne Boyer666a4532015-12-09 12:29:35 -0800705 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300706 if (clock->p < limit->p.min || limit->p.max < clock->p)
707 INTELPllInvalid("p out of range\n");
708 if (clock->m < limit->m.min || limit->m.max < clock->m)
709 INTELPllInvalid("m out of range\n");
710 }
711
Jesse Barnes79e53942008-11-07 14:24:08 -0800712 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400713 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800714 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
715 * connector, etc., rather than just a single range.
716 */
717 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400718 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800719
720 return true;
721}
722
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300723static int
724i9xx_select_p2_div(const intel_limit_t *limit,
725 const struct intel_crtc_state *crtc_state,
726 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800727{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300728 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800729
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200730 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800731 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100732 * For LVDS just rely on its current settings for dual-channel.
733 * We haven't figured out how to reliably set up different
734 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800735 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100736 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300737 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800738 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800740 } else {
741 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300742 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300744 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300746}
747
748static bool
749i9xx_find_best_dpll(const intel_limit_t *limit,
750 struct intel_crtc_state *crtc_state,
751 int target, int refclk, intel_clock_t *match_clock,
752 intel_clock_t *best_clock)
753{
754 struct drm_device *dev = crtc_state->base.crtc->dev;
755 intel_clock_t clock;
756 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800757
Akshay Joshi0206e352011-08-16 15:34:10 -0400758 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800759
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300760 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
761
Zhao Yakui42158662009-11-20 11:24:18 +0800762 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
763 clock.m1++) {
764 for (clock.m2 = limit->m2.min;
765 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200766 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800767 break;
768 for (clock.n = limit->n.min;
769 clock.n <= limit->n.max; clock.n++) {
770 for (clock.p1 = limit->p1.min;
771 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800772 int this_err;
773
Imre Deakdccbea32015-06-22 23:35:51 +0300774 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000775 if (!intel_PLL_is_valid(dev, limit,
776 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800777 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800778 if (match_clock &&
779 clock.p != match_clock->p)
780 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800781
782 this_err = abs(clock.dot - target);
783 if (this_err < err) {
784 *best_clock = clock;
785 err = this_err;
786 }
787 }
788 }
789 }
790 }
791
792 return (err != target);
793}
794
Ma Lingd4906092009-03-18 20:13:27 +0800795static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200796pnv_find_best_dpll(const intel_limit_t *limit,
797 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200798 int target, int refclk, intel_clock_t *match_clock,
799 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200800{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300801 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200802 intel_clock_t clock;
803 int err = target;
804
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200805 memset(best_clock, 0, sizeof(*best_clock));
806
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300807 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
808
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200809 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
810 clock.m1++) {
811 for (clock.m2 = limit->m2.min;
812 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200813 for (clock.n = limit->n.min;
814 clock.n <= limit->n.max; clock.n++) {
815 for (clock.p1 = limit->p1.min;
816 clock.p1 <= limit->p1.max; clock.p1++) {
817 int this_err;
818
Imre Deakdccbea32015-06-22 23:35:51 +0300819 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800820 if (!intel_PLL_is_valid(dev, limit,
821 &clock))
822 continue;
823 if (match_clock &&
824 clock.p != match_clock->p)
825 continue;
826
827 this_err = abs(clock.dot - target);
828 if (this_err < err) {
829 *best_clock = clock;
830 err = this_err;
831 }
832 }
833 }
834 }
835 }
836
837 return (err != target);
838}
839
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200840/*
841 * Returns a set of divisors for the desired target clock with the given
842 * refclk, or FALSE. The returned values represent the clock equation:
843 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
844 */
Ma Lingd4906092009-03-18 20:13:27 +0800845static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200846g4x_find_best_dpll(const intel_limit_t *limit,
847 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200848 int target, int refclk, intel_clock_t *match_clock,
849 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800850{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300851 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800852 intel_clock_t clock;
853 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300854 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400855 /* approximately equals target * 0.00585 */
856 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800857
858 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300859
860 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
861
Ma Lingd4906092009-03-18 20:13:27 +0800862 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200863 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800864 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200865 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800866 for (clock.m1 = limit->m1.max;
867 clock.m1 >= limit->m1.min; clock.m1--) {
868 for (clock.m2 = limit->m2.max;
869 clock.m2 >= limit->m2.min; clock.m2--) {
870 for (clock.p1 = limit->p1.max;
871 clock.p1 >= limit->p1.min; clock.p1--) {
872 int this_err;
873
Imre Deakdccbea32015-06-22 23:35:51 +0300874 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000875 if (!intel_PLL_is_valid(dev, limit,
876 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800877 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000878
879 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800880 if (this_err < err_most) {
881 *best_clock = clock;
882 err_most = this_err;
883 max_n = clock.n;
884 found = true;
885 }
886 }
887 }
888 }
889 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800890 return found;
891}
Ma Lingd4906092009-03-18 20:13:27 +0800892
Imre Deakd5dd62b2015-03-17 11:40:03 +0200893/*
894 * Check if the calculated PLL configuration is more optimal compared to the
895 * best configuration and error found so far. Return the calculated error.
896 */
897static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
898 const intel_clock_t *calculated_clock,
899 const intel_clock_t *best_clock,
900 unsigned int best_error_ppm,
901 unsigned int *error_ppm)
902{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200903 /*
904 * For CHV ignore the error and consider only the P value.
905 * Prefer a bigger P value based on HW requirements.
906 */
907 if (IS_CHERRYVIEW(dev)) {
908 *error_ppm = 0;
909
910 return calculated_clock->p > best_clock->p;
911 }
912
Imre Deak24be4e42015-03-17 11:40:04 +0200913 if (WARN_ON_ONCE(!target_freq))
914 return false;
915
Imre Deakd5dd62b2015-03-17 11:40:03 +0200916 *error_ppm = div_u64(1000000ULL *
917 abs(target_freq - calculated_clock->dot),
918 target_freq);
919 /*
920 * Prefer a better P value over a better (smaller) error if the error
921 * is small. Ensure this preference for future configurations too by
922 * setting the error to 0.
923 */
924 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
925 *error_ppm = 0;
926
927 return true;
928 }
929
930 return *error_ppm + 10 < best_error_ppm;
931}
932
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800938static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200939vlv_find_best_dpll(const intel_limit_t *limit,
940 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200941 int target, int refclk, intel_clock_t *match_clock,
942 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700943{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300945 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300946 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300947 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300948 /* min update 19.2 MHz */
949 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300950 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700951
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 target *= 5; /* fast clock */
953
954 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700955
956 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300957 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300958 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300959 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300960 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300961 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700962 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300963 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200964 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300965
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300966 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
967 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300968
Imre Deakdccbea32015-06-22 23:35:51 +0300969 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300970
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300971 if (!intel_PLL_is_valid(dev, limit,
972 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300973 continue;
974
Imre Deakd5dd62b2015-03-17 11:40:03 +0200975 if (!vlv_PLL_is_optimal(dev, target,
976 &clock,
977 best_clock,
978 bestppm, &ppm))
979 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300980
Imre Deakd5dd62b2015-03-17 11:40:03 +0200981 *best_clock = clock;
982 bestppm = ppm;
983 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700984 }
985 }
986 }
987 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700988
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300989 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700990}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700991
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200992/*
993 * Returns a set of divisors for the desired target clock with the given
994 * refclk, or FALSE. The returned values represent the clock equation:
995 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
996 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300997static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200998chv_find_best_dpll(const intel_limit_t *limit,
999 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001000 int target, int refclk, intel_clock_t *match_clock,
1001 intel_clock_t *best_clock)
1002{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001003 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001004 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001005 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001006 intel_clock_t clock;
1007 uint64_t m2;
1008 int found = false;
1009
1010 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001011 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 /*
1014 * Based on hardware doc, the n always set to 1, and m1 always
1015 * set to 2. If requires to support 200Mhz refclk, we need to
1016 * revisit this because n may not 1 anymore.
1017 */
1018 clock.n = 1, clock.m1 = 2;
1019 target *= 5; /* fast clock */
1020
1021 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1022 for (clock.p2 = limit->p2.p2_fast;
1023 clock.p2 >= limit->p2.p2_slow;
1024 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001025 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001026
1027 clock.p = clock.p1 * clock.p2;
1028
1029 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1030 clock.n) << 22, refclk * clock.m1);
1031
1032 if (m2 > INT_MAX/clock.m1)
1033 continue;
1034
1035 clock.m2 = m2;
1036
Imre Deakdccbea32015-06-22 23:35:51 +03001037 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001038
1039 if (!intel_PLL_is_valid(dev, limit, &clock))
1040 continue;
1041
Imre Deak9ca3ba02015-03-17 11:40:05 +02001042 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1043 best_error_ppm, &error_ppm))
1044 continue;
1045
1046 *best_clock = clock;
1047 best_error_ppm = error_ppm;
1048 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001049 }
1050 }
1051
1052 return found;
1053}
1054
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001055bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1056 intel_clock_t *best_clock)
1057{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001058 int refclk = 100000;
1059 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001060
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001061 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001062 target_clock, refclk, NULL, best_clock);
1063}
1064
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001065bool intel_crtc_active(struct drm_crtc *crtc)
1066{
1067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1068
1069 /* Be paranoid as we can arrive here with only partial
1070 * state retrieved from the hardware during setup.
1071 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001072 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001073 * as Haswell has gained clock readout/fastboot support.
1074 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001075 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001076 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001077 *
1078 * FIXME: The intel_crtc->active here should be switched to
1079 * crtc->state->active once we have proper CRTC states wired up
1080 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001081 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001082 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001083 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001084}
1085
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001086enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1087 enum pipe pipe)
1088{
1089 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1091
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001092 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001093}
1094
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1096{
1097 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001098 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001099 u32 line1, line2;
1100 u32 line_mask;
1101
1102 if (IS_GEN2(dev))
1103 line_mask = DSL_LINEMASK_GEN2;
1104 else
1105 line_mask = DSL_LINEMASK_GEN3;
1106
1107 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001108 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001109 line2 = I915_READ(reg) & line_mask;
1110
1111 return line1 == line2;
1112}
1113
Keith Packardab7ad7f2010-10-03 00:33:06 -07001114/*
1115 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117 *
1118 * After disabling a pipe, we can't wait for vblank in the usual way,
1119 * spinning on the vblank interrupt status bit, since we won't actually
1120 * see an interrupt when the pipe is disabled.
1121 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 * On Gen4 and above:
1123 * wait for the pipe register state bit to turn off
1124 *
1125 * Otherwise:
1126 * wait for the display line value to settle (it usually
1127 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001128 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001129 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001130static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001131{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001132 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001133 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001134 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001135 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001136
Keith Packardab7ad7f2010-10-03 00:33:06 -07001137 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001138 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001139
Keith Packardab7ad7f2010-10-03 00:33:06 -07001140 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001141 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1142 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001143 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001144 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001145 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001146 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001147 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001148 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001149}
1150
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001152void assert_pll(struct drm_i915_private *dev_priv,
1153 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001154{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001155 u32 val;
1156 bool cur_state;
1157
Ville Syrjälä649636e2015-09-22 19:50:01 +03001158 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001159 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001160 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001162 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001163}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001164
Jani Nikula23538ef2013-08-27 15:12:22 +03001165/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001166void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001167{
1168 u32 val;
1169 bool cur_state;
1170
Ville Syrjäläa5805162015-05-26 20:42:30 +03001171 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001172 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001173 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001174
1175 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001176 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001177 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001178 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001179}
Jani Nikula23538ef2013-08-27 15:12:22 +03001180
Jesse Barnes040484a2011-01-03 12:14:26 -08001181static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
1183{
Jesse Barnes040484a2011-01-03 12:14:26 -08001184 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001185 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1186 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001187
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001188 if (HAS_DDI(dev_priv->dev)) {
1189 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001190 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001191 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001192 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001193 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001194 cur_state = !!(val & FDI_TX_ENABLE);
1195 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001197 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001198 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001199}
1200#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1201#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1202
1203static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, bool state)
1205{
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 u32 val;
1207 bool cur_state;
1208
Ville Syrjälä649636e2015-09-22 19:50:01 +03001209 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001210 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001211 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001212 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001213 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001214}
1215#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1216#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1217
1218static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1219 enum pipe pipe)
1220{
Jesse Barnes040484a2011-01-03 12:14:26 -08001221 u32 val;
1222
1223 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001224 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001225 return;
1226
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001227 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001228 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001229 return;
1230
Ville Syrjälä649636e2015-09-22 19:50:01 +03001231 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001232 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 -08001233}
1234
Daniel Vetter55607e82013-06-16 21:42:39 +02001235void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1236 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001237{
Jesse Barnes040484a2011-01-03 12:14:26 -08001238 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001239 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001240
Ville Syrjälä649636e2015-09-22 19:50:01 +03001241 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001242 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001244 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001245 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001246}
1247
Daniel Vetterb680c372014-09-19 18:27:27 +02001248void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1249 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001250{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001252 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001253 u32 val;
1254 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001255 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001256
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 if (WARN_ON(HAS_DDI(dev)))
1258 return;
1259
1260 if (HAS_PCH_SPLIT(dev)) {
1261 u32 port_sel;
1262
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001264 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1265
1266 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1267 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1268 panel_pipe = PIPE_B;
1269 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001270 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001271 /* presumably write lock depends on pipe, not port select */
1272 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1273 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274 } else {
1275 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001276 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1277 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001278 }
1279
1280 val = I915_READ(pp_reg);
1281 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001282 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001283 locked = false;
1284
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001287 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288}
1289
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001290static void assert_cursor(struct drm_i915_private *dev_priv,
1291 enum pipe pipe, bool state)
1292{
1293 struct drm_device *dev = dev_priv->dev;
1294 bool cur_state;
1295
Paulo Zanonid9d82082014-02-27 16:30:56 -03001296 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001297 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001298 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001299 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300
Rob Clarke2c719b2014-12-15 13:56:32 -05001301 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001302 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001303 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001304}
1305#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1306#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1307
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001308void assert_pipe(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001310{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001311 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001314 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001315
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001316 /* if we need the pipe quirk it must be always on */
1317 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1318 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001319 state = true;
1320
Imre Deak4feed0e2016-02-12 18:55:14 +02001321 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1322 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001324 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001325
1326 intel_display_power_put(dev_priv, power_domain);
1327 } else {
1328 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001329 }
1330
Rob Clarke2c719b2014-12-15 13:56:32 -05001331 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001332 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001333 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334}
1335
Chris Wilson931872f2012-01-16 23:01:13 +00001336static void assert_plane(struct drm_i915_private *dev_priv,
1337 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001338{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001340 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001341
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001345 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001346 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001355 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001356 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357
Ville Syrjälä653e1022013-06-04 13:49:05 +03001358 /* Primary planes are fixed to pipes on gen4+ */
1359 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001360 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001361 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001362 "plane %c assertion failure, should be disabled but not\n",
1363 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001364 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001365 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001366
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001368 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001369 u32 val = I915_READ(DSPCNTR(i));
1370 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001372 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001373 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1374 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001375 }
1376}
1377
Jesse Barnes19332d72013-03-28 09:55:38 -07001378static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1379 enum pipe pipe)
1380{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001381 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001382 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001383
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001384 if (INTEL_INFO(dev)->gen >= 9) {
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(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001388 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1389 sprite, pipe_name(pipe));
1390 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001391 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001392 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001393 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001395 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001396 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001397 }
1398 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001399 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001402 plane_name(pipe), pipe_name(pipe));
1403 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001404 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001406 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1407 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001408 }
1409}
1410
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001411static void assert_vblank_disabled(struct drm_crtc *crtc)
1412{
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001414 drm_crtc_vblank_put(crtc);
1415}
1416
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001417void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1418 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001419{
Jesse Barnes92f25842011-01-04 15:09:34 -08001420 u32 val;
1421 bool enabled;
1422
Ville Syrjälä649636e2015-09-22 19:50:01 +03001423 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001424 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001425 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001426 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1427 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001428}
1429
Keith Packard4e634382011-08-06 10:39:45 -07001430static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1431 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001432{
1433 if ((val & DP_PORT_EN) == 0)
1434 return false;
1435
1436 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001437 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001438 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1439 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001440 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1441 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1442 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001443 } else {
1444 if ((val & DP_PIPE_MASK) != (pipe << 30))
1445 return false;
1446 }
1447 return true;
1448}
1449
Keith Packard1519b992011-08-06 10:35:34 -07001450static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, u32 val)
1452{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001453 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001454 return false;
1455
1456 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001457 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001458 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001459 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1460 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1461 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001462 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001463 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001464 return false;
1465 }
1466 return true;
1467}
1468
1469static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe, u32 val)
1471{
1472 if ((val & LVDS_PORT_EN) == 0)
1473 return false;
1474
1475 if (HAS_PCH_CPT(dev_priv->dev)) {
1476 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1477 return false;
1478 } else {
1479 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1480 return false;
1481 }
1482 return true;
1483}
1484
1485static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1486 enum pipe pipe, u32 val)
1487{
1488 if ((val & ADPA_DAC_ENABLE) == 0)
1489 return false;
1490 if (HAS_PCH_CPT(dev_priv->dev)) {
1491 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1492 return false;
1493 } else {
1494 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1495 return false;
1496 }
1497 return true;
1498}
1499
Jesse Barnes291906f2011-02-02 12:28:03 -08001500static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001501 enum pipe pipe, i915_reg_t reg,
1502 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001503{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001504 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001505 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001506 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001507 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001508
Rob Clarke2c719b2014-12-15 13:56:32 -05001509 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001510 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001511 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001512}
1513
1514static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001515 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001516{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001517 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001518 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001519 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001520 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001521
Rob Clarke2c719b2014-12-15 13:56:32 -05001522 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001523 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001524 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001525}
1526
1527static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1528 enum pipe pipe)
1529{
Jesse Barnes291906f2011-02-02 12:28:03 -08001530 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001531
Keith Packardf0575e92011-07-25 22:12:43 -07001532 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1533 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1534 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001535
Ville Syrjälä649636e2015-09-22 19:50:01 +03001536 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001537 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001538 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001539 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001540
Ville Syrjälä649636e2015-09-22 19:50:01 +03001541 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001542 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001543 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001544 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001545
Paulo Zanonie2debe92013-02-18 19:00:27 -03001546 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1547 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1548 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001549}
1550
Ville Syrjäläd288f652014-10-28 13:20:22 +02001551static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001552 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001553{
Daniel Vetter426115c2013-07-11 22:13:42 +02001554 struct drm_device *dev = crtc->base.dev;
1555 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001556 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001557 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001558
Daniel Vetter426115c2013-07-11 22:13:42 +02001559 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001560
Daniel Vetter87442f72013-06-06 00:52:17 +02001561 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001562 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001563 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001564
Daniel Vetter426115c2013-07-11 22:13:42 +02001565 I915_WRITE(reg, dpll);
1566 POSTING_READ(reg);
1567 udelay(150);
1568
1569 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1570 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1571
Ville Syrjäläd288f652014-10-28 13:20:22 +02001572 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001573 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001574
1575 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001576 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001577 POSTING_READ(reg);
1578 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001579 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001580 POSTING_READ(reg);
1581 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001582 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001583 POSTING_READ(reg);
1584 udelay(150); /* wait for warmup */
1585}
1586
Ville Syrjäläd288f652014-10-28 13:20:22 +02001587static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001588 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001589{
1590 struct drm_device *dev = crtc->base.dev;
1591 struct drm_i915_private *dev_priv = dev->dev_private;
1592 int pipe = crtc->pipe;
1593 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001594 u32 tmp;
1595
1596 assert_pipe_disabled(dev_priv, crtc->pipe);
1597
Ville Syrjäläa5805162015-05-26 20:42:30 +03001598 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001599
1600 /* Enable back the 10bit clock to display controller */
1601 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1602 tmp |= DPIO_DCLKP_EN;
1603 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1604
Ville Syrjälä54433e92015-05-26 20:42:31 +03001605 mutex_unlock(&dev_priv->sb_lock);
1606
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607 /*
1608 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1609 */
1610 udelay(1);
1611
1612 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001613 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001614
1615 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001616 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001617 DRM_ERROR("PLL %d failed to lock\n", pipe);
1618
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001619 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001620 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001621 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001622}
1623
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624static int intel_num_dvo_pipes(struct drm_device *dev)
1625{
1626 struct intel_crtc *crtc;
1627 int count = 0;
1628
1629 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001630 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001631 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001632
1633 return count;
1634}
1635
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001637{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001638 struct drm_device *dev = crtc->base.dev;
1639 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001640 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001641 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001642
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001643 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644
1645 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001646 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001647
1648 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649 if (IS_MOBILE(dev) && !IS_I830(dev))
1650 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001651
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001652 /* Enable DVO 2x clock on both PLLs if necessary */
1653 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1654 /*
1655 * It appears to be important that we don't enable this
1656 * for the current pipe before otherwise configuring the
1657 * PLL. No idea how this should be handled if multiple
1658 * DVO outputs are enabled simultaneosly.
1659 */
1660 dpll |= DPLL_DVO_2X_MODE;
1661 I915_WRITE(DPLL(!crtc->pipe),
1662 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1663 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001664
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001665 /*
1666 * Apparently we need to have VGA mode enabled prior to changing
1667 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1668 * dividers, even though the register value does change.
1669 */
1670 I915_WRITE(reg, 0);
1671
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001672 I915_WRITE(reg, dpll);
1673
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674 /* Wait for the clocks to stabilize. */
1675 POSTING_READ(reg);
1676 udelay(150);
1677
1678 if (INTEL_INFO(dev)->gen >= 4) {
1679 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001680 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001681 } else {
1682 /* The pixel multiplier can only be updated once the
1683 * DPLL is enabled and the clocks are stable.
1684 *
1685 * So write it again.
1686 */
1687 I915_WRITE(reg, dpll);
1688 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001689
1690 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001692 POSTING_READ(reg);
1693 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001694 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001695 POSTING_READ(reg);
1696 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001697 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698 POSTING_READ(reg);
1699 udelay(150); /* wait for warmup */
1700}
1701
1702/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001703 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704 * @dev_priv: i915 private structure
1705 * @pipe: pipe PLL to disable
1706 *
1707 * Disable the PLL for @pipe, making sure the pipe is off first.
1708 *
1709 * Note! This is for pre-ILK only.
1710 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001711static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001713 struct drm_device *dev = crtc->base.dev;
1714 struct drm_i915_private *dev_priv = dev->dev_private;
1715 enum pipe pipe = crtc->pipe;
1716
1717 /* Disable DVO 2x clock on both PLLs if necessary */
1718 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001719 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001720 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001721 I915_WRITE(DPLL(PIPE_B),
1722 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1723 I915_WRITE(DPLL(PIPE_A),
1724 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1725 }
1726
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001727 /* Don't disable pipe or pipe PLLs if needed */
1728 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1729 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001730 return;
1731
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
1734
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001735 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001736 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001737}
1738
Jesse Barnesf6071162013-10-01 10:41:38 -07001739static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1740{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001741 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001742
1743 /* Make sure the pipe isn't still relying on us */
1744 assert_pipe_disabled(dev_priv, pipe);
1745
Imre Deake5cbfbf2014-01-09 17:08:16 +02001746 /*
1747 * Leave integrated clock source and reference clock enabled for pipe B.
1748 * The latter is needed for VGA hotplug / manual detection.
1749 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001750 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001751 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001752 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001753 I915_WRITE(DPLL(pipe), val);
1754 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001755
1756}
1757
1758static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1759{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001760 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001761 u32 val;
1762
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001763 /* Make sure the pipe isn't still relying on us */
1764 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001765
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001766 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001767 val = DPLL_SSC_REF_CLK_CHV |
1768 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001769 if (pipe != PIPE_A)
1770 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1771 I915_WRITE(DPLL(pipe), val);
1772 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001773
Ville Syrjäläa5805162015-05-26 20:42:30 +03001774 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001775
1776 /* Disable 10bit clock to display controller */
1777 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1778 val &= ~DPIO_DCLKP_EN;
1779 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1780
Ville Syrjäläa5805162015-05-26 20:42:30 +03001781 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001782}
1783
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001784void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001785 struct intel_digital_port *dport,
1786 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001787{
1788 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001789 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001790
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001791 switch (dport->port) {
1792 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001793 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001794 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001795 break;
1796 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001797 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001798 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001799 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001800 break;
1801 case PORT_D:
1802 port_mask = DPLL_PORTD_READY_MASK;
1803 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001804 break;
1805 default:
1806 BUG();
1807 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001808
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001809 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1810 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1811 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001812}
1813
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001814static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1815 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001816{
Daniel Vetter23670b322012-11-01 09:15:30 +01001817 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001818 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001820 i915_reg_t reg;
1821 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001822
1823 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001824 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001825
1826 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001827 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001828
1829 /* FDI must be feeding us bits for PCH ports */
1830 assert_fdi_tx_enabled(dev_priv, pipe);
1831 assert_fdi_rx_enabled(dev_priv, pipe);
1832
Daniel Vetter23670b322012-11-01 09:15:30 +01001833 if (HAS_PCH_CPT(dev)) {
1834 /* Workaround: Set the timing override bit before enabling the
1835 * pch transcoder. */
1836 reg = TRANS_CHICKEN2(pipe);
1837 val = I915_READ(reg);
1838 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1839 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001840 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001841
Daniel Vetterab9412b2013-05-03 11:49:46 +02001842 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001843 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001844 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001845
1846 if (HAS_PCH_IBX(dev_priv->dev)) {
1847 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001848 * Make the BPC in transcoder be consistent with
1849 * that in pipeconf reg. For HDMI we must use 8bpc
1850 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001851 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001852 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001853 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1854 val |= PIPECONF_8BPC;
1855 else
1856 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001857 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001858
1859 val &= ~TRANS_INTERLACE_MASK;
1860 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001861 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001862 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001863 val |= TRANS_LEGACY_INTERLACED_ILK;
1864 else
1865 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001866 else
1867 val |= TRANS_PROGRESSIVE;
1868
Jesse Barnes040484a2011-01-03 12:14:26 -08001869 I915_WRITE(reg, val | TRANS_ENABLE);
1870 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001871 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001872}
1873
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001875 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001876{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878
1879 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001880 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001881
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001882 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001883 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001884 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001885
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001886 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001887 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001888 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001889 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001890
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001891 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001892 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001893
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001894 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1895 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001896 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001897 else
1898 val |= TRANS_PROGRESSIVE;
1899
Daniel Vetterab9412b2013-05-03 11:49:46 +02001900 I915_WRITE(LPT_TRANSCONF, val);
1901 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001902 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903}
1904
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001905static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1906 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001907{
Daniel Vetter23670b322012-11-01 09:15:30 +01001908 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001909 i915_reg_t reg;
1910 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001911
1912 /* FDI relies on the transcoder */
1913 assert_fdi_tx_disabled(dev_priv, pipe);
1914 assert_fdi_rx_disabled(dev_priv, pipe);
1915
Jesse Barnes291906f2011-02-02 12:28:03 -08001916 /* Ports must be off as well */
1917 assert_pch_ports_disabled(dev_priv, pipe);
1918
Daniel Vetterab9412b2013-05-03 11:49:46 +02001919 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001920 val = I915_READ(reg);
1921 val &= ~TRANS_ENABLE;
1922 I915_WRITE(reg, val);
1923 /* wait for PCH transcoder off, transcoder state */
1924 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001925 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001926
Ville Syrjäläc4656132015-10-29 21:25:56 +02001927 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001928 /* Workaround: Clear the timing override chicken bit again. */
1929 reg = TRANS_CHICKEN2(pipe);
1930 val = I915_READ(reg);
1931 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1932 I915_WRITE(reg, val);
1933 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001934}
1935
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001936static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938 u32 val;
1939
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001941 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001942 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001943 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001944 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001945 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001946
1947 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001948 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001949 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001950 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001951}
1952
1953/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001954 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001955 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001957 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001958 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001959 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001960static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961{
Paulo Zanoni03722642014-01-17 13:51:09 -02001962 struct drm_device *dev = crtc->base.dev;
1963 struct drm_i915_private *dev_priv = dev->dev_private;
1964 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001965 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001966 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001967 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001968 u32 val;
1969
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001970 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1971
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001972 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001973 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001974 assert_sprites_disabled(dev_priv, pipe);
1975
Paulo Zanoni681e5812012-12-06 11:12:38 -02001976 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001977 pch_transcoder = TRANSCODER_A;
1978 else
1979 pch_transcoder = pipe;
1980
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981 /*
1982 * A pipe without a PLL won't actually be able to drive bits from
1983 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1984 * need the check.
1985 */
Imre Deak50360402015-01-16 00:55:16 -08001986 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001987 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001988 assert_dsi_pll_enabled(dev_priv);
1989 else
1990 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001991 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001992 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001993 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001994 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001995 assert_fdi_tx_pll_enabled(dev_priv,
1996 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001997 }
1998 /* FIXME: assert CPU port conditions for SNB+ */
1999 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002000
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002001 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002003 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002004 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2005 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002006 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002007 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002008
2009 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002010 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002011
2012 /*
2013 * Until the pipe starts DSL will read as 0, which would cause
2014 * an apparent vblank timestamp jump, which messes up also the
2015 * frame count when it's derived from the timestamps. So let's
2016 * wait for the pipe to start properly before we call
2017 * drm_crtc_vblank_on()
2018 */
2019 if (dev->max_vblank_count == 0 &&
2020 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2021 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022}
2023
2024/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002025 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002026 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002028 * Disable the pipe of @crtc, making sure that various hardware
2029 * specific requirements are met, if applicable, e.g. plane
2030 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002031 *
2032 * Will wait until the pipe has shut down before returning.
2033 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002034static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002035{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002036 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002037 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002038 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002039 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002040 u32 val;
2041
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002042 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2043
Jesse Barnesb24e7172011-01-04 15:09:30 -08002044 /*
2045 * Make sure planes won't keep trying to pump pixels to us,
2046 * or we might hang the display.
2047 */
2048 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002049 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002050 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002052 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002053 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002054 if ((val & PIPECONF_ENABLE) == 0)
2055 return;
2056
Ville Syrjälä67adc642014-08-15 01:21:57 +03002057 /*
2058 * Double wide has implications for planes
2059 * so best keep it disabled when not needed.
2060 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002061 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002062 val &= ~PIPECONF_DOUBLE_WIDE;
2063
2064 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002065 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2066 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002067 val &= ~PIPECONF_ENABLE;
2068
2069 I915_WRITE(reg, val);
2070 if ((val & PIPECONF_ENABLE) == 0)
2071 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002072}
2073
Chris Wilson693db182013-03-05 14:52:39 +00002074static bool need_vtd_wa(struct drm_device *dev)
2075{
2076#ifdef CONFIG_INTEL_IOMMU
2077 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2078 return true;
2079#endif
2080 return false;
2081}
2082
Ville Syrjälä832be822016-01-12 21:08:33 +02002083static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2084{
2085 return IS_GEN2(dev_priv) ? 2048 : 4096;
2086}
2087
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002088static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2089 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002090{
2091 switch (fb_modifier) {
2092 case DRM_FORMAT_MOD_NONE:
2093 return cpp;
2094 case I915_FORMAT_MOD_X_TILED:
2095 if (IS_GEN2(dev_priv))
2096 return 128;
2097 else
2098 return 512;
2099 case I915_FORMAT_MOD_Y_TILED:
2100 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2101 return 128;
2102 else
2103 return 512;
2104 case I915_FORMAT_MOD_Yf_TILED:
2105 switch (cpp) {
2106 case 1:
2107 return 64;
2108 case 2:
2109 case 4:
2110 return 128;
2111 case 8:
2112 case 16:
2113 return 256;
2114 default:
2115 MISSING_CASE(cpp);
2116 return cpp;
2117 }
2118 break;
2119 default:
2120 MISSING_CASE(fb_modifier);
2121 return cpp;
2122 }
2123}
2124
Ville Syrjälä832be822016-01-12 21:08:33 +02002125unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2126 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002127{
Ville Syrjälä832be822016-01-12 21:08:33 +02002128 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2129 return 1;
2130 else
2131 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002132 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002133}
2134
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002135/* Return the tile dimensions in pixel units */
2136static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2137 unsigned int *tile_width,
2138 unsigned int *tile_height,
2139 uint64_t fb_modifier,
2140 unsigned int cpp)
2141{
2142 unsigned int tile_width_bytes =
2143 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2144
2145 *tile_width = tile_width_bytes / cpp;
2146 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2147}
2148
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002149unsigned int
2150intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002151 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002152{
Ville Syrjälä832be822016-01-12 21:08:33 +02002153 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2154 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2155
2156 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002157}
2158
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002159unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2160{
2161 unsigned int size = 0;
2162 int i;
2163
2164 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2165 size += rot_info->plane[i].width * rot_info->plane[i].height;
2166
2167 return size;
2168}
2169
Daniel Vetter75c82a52015-10-14 16:51:04 +02002170static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002171intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2172 const struct drm_framebuffer *fb,
2173 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002174{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002175 if (intel_rotation_90_or_270(rotation)) {
2176 *view = i915_ggtt_view_rotated;
2177 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2178 } else {
2179 *view = i915_ggtt_view_normal;
2180 }
2181}
2182
2183static void
2184intel_fill_fb_info(struct drm_i915_private *dev_priv,
2185 struct drm_framebuffer *fb)
2186{
2187 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002188 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002189
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002190 tile_size = intel_tile_size(dev_priv);
2191
2192 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002193 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2194 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002195
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002196 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2197 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002198
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002199 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002200 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002201 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2202 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002203
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002204 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002205 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2206 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002207 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002208}
2209
Ville Syrjälä603525d2016-01-12 21:08:37 +02002210static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002211{
2212 if (INTEL_INFO(dev_priv)->gen >= 9)
2213 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002214 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002215 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002216 return 128 * 1024;
2217 else if (INTEL_INFO(dev_priv)->gen >= 4)
2218 return 4 * 1024;
2219 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002220 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002221}
2222
Ville Syrjälä603525d2016-01-12 21:08:37 +02002223static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2224 uint64_t fb_modifier)
2225{
2226 switch (fb_modifier) {
2227 case DRM_FORMAT_MOD_NONE:
2228 return intel_linear_alignment(dev_priv);
2229 case I915_FORMAT_MOD_X_TILED:
2230 if (INTEL_INFO(dev_priv)->gen >= 9)
2231 return 256 * 1024;
2232 return 0;
2233 case I915_FORMAT_MOD_Y_TILED:
2234 case I915_FORMAT_MOD_Yf_TILED:
2235 return 1 * 1024 * 1024;
2236 default:
2237 MISSING_CASE(fb_modifier);
2238 return 0;
2239 }
2240}
2241
Chris Wilson127bd2a2010-07-23 23:32:05 +01002242int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002243intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2244 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002245{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002246 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002247 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002248 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002249 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002250 u32 alignment;
2251 int ret;
2252
Matt Roperebcdd392014-07-09 16:22:11 -07002253 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2254
Ville Syrjälä603525d2016-01-12 21:08:37 +02002255 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002256
Ville Syrjälä3465c582016-02-15 22:54:43 +02002257 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002258
Chris Wilson693db182013-03-05 14:52:39 +00002259 /* Note that the w/a also requires 64 PTE of padding following the
2260 * bo. We currently fill all unused PTE with the shadow page and so
2261 * we should always have valid PTE following the scanout preventing
2262 * the VT-d warning.
2263 */
2264 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2265 alignment = 256 * 1024;
2266
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002267 /*
2268 * Global gtt pte registers are special registers which actually forward
2269 * writes to a chunk of system memory. Which means that there is no risk
2270 * that the register values disappear as soon as we call
2271 * intel_runtime_pm_put(), so it is correct to wrap only the
2272 * pin/unpin/fence and not more.
2273 */
2274 intel_runtime_pm_get(dev_priv);
2275
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002276 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2277 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002278 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002279 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002280
2281 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2282 * fence, whereas 965+ only requires a fence if using
2283 * framebuffer compression. For simplicity, we always install
2284 * a fence as the cost is not that onerous.
2285 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002286 if (view.type == I915_GGTT_VIEW_NORMAL) {
2287 ret = i915_gem_object_get_fence(obj);
2288 if (ret == -EDEADLK) {
2289 /*
2290 * -EDEADLK means there are no free fences
2291 * no pending flips.
2292 *
2293 * This is propagated to atomic, but it uses
2294 * -EDEADLK to force a locking recovery, so
2295 * change the returned error to -EBUSY.
2296 */
2297 ret = -EBUSY;
2298 goto err_unpin;
2299 } else if (ret)
2300 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002301
Vivek Kasireddy98072162015-10-29 18:54:38 -07002302 i915_gem_object_pin_fence(obj);
2303 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002304
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002305 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002306 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002307
2308err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002309 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002310err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002311 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002312 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002313}
2314
Ville Syrjälä3465c582016-02-15 22:54:43 +02002315static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002316{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002317 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002318 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002319
Matt Roperebcdd392014-07-09 16:22:11 -07002320 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2321
Ville Syrjälä3465c582016-02-15 22:54:43 +02002322 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002323
Vivek Kasireddy98072162015-10-29 18:54:38 -07002324 if (view.type == I915_GGTT_VIEW_NORMAL)
2325 i915_gem_object_unpin_fence(obj);
2326
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002327 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002328}
2329
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002330/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002331 * Adjust the tile offset by moving the difference into
2332 * the x/y offsets.
2333 *
2334 * Input tile dimensions and pitch must already be
2335 * rotated to match x and y, and in pixel units.
2336 */
2337static u32 intel_adjust_tile_offset(int *x, int *y,
2338 unsigned int tile_width,
2339 unsigned int tile_height,
2340 unsigned int tile_size,
2341 unsigned int pitch_tiles,
2342 u32 old_offset,
2343 u32 new_offset)
2344{
2345 unsigned int tiles;
2346
2347 WARN_ON(old_offset & (tile_size - 1));
2348 WARN_ON(new_offset & (tile_size - 1));
2349 WARN_ON(new_offset > old_offset);
2350
2351 tiles = (old_offset - new_offset) / tile_size;
2352
2353 *y += tiles / pitch_tiles * tile_height;
2354 *x += tiles % pitch_tiles * tile_width;
2355
2356 return new_offset;
2357}
2358
2359/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002360 * Computes the linear offset to the base tile and adjusts
2361 * x, y. bytes per pixel is assumed to be a power-of-two.
2362 *
2363 * In the 90/270 rotated case, x and y are assumed
2364 * to be already rotated to match the rotated GTT view, and
2365 * pitch is the tile_height aligned framebuffer height.
2366 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002367u32 intel_compute_tile_offset(int *x, int *y,
2368 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002369 unsigned int pitch,
2370 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002371{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002372 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2373 uint64_t fb_modifier = fb->modifier[plane];
2374 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002375 u32 offset, offset_aligned, alignment;
2376
2377 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2378 if (alignment)
2379 alignment--;
2380
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002381 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002382 unsigned int tile_size, tile_width, tile_height;
2383 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002384
Ville Syrjäläd8433102016-01-12 21:08:35 +02002385 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002386 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2387 fb_modifier, cpp);
2388
2389 if (intel_rotation_90_or_270(rotation)) {
2390 pitch_tiles = pitch / tile_height;
2391 swap(tile_width, tile_height);
2392 } else {
2393 pitch_tiles = pitch / (tile_width * cpp);
2394 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002395
Ville Syrjäläd8433102016-01-12 21:08:35 +02002396 tile_rows = *y / tile_height;
2397 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002398
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002399 tiles = *x / tile_width;
2400 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002401
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002402 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2403 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002404
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002405 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2406 tile_size, pitch_tiles,
2407 offset, offset_aligned);
2408 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002409 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002410 offset_aligned = offset & ~alignment;
2411
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002412 *y = (offset & alignment) / pitch;
2413 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002414 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002415
2416 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002417}
2418
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002419static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002420{
2421 switch (format) {
2422 case DISPPLANE_8BPP:
2423 return DRM_FORMAT_C8;
2424 case DISPPLANE_BGRX555:
2425 return DRM_FORMAT_XRGB1555;
2426 case DISPPLANE_BGRX565:
2427 return DRM_FORMAT_RGB565;
2428 default:
2429 case DISPPLANE_BGRX888:
2430 return DRM_FORMAT_XRGB8888;
2431 case DISPPLANE_RGBX888:
2432 return DRM_FORMAT_XBGR8888;
2433 case DISPPLANE_BGRX101010:
2434 return DRM_FORMAT_XRGB2101010;
2435 case DISPPLANE_RGBX101010:
2436 return DRM_FORMAT_XBGR2101010;
2437 }
2438}
2439
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002440static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2441{
2442 switch (format) {
2443 case PLANE_CTL_FORMAT_RGB_565:
2444 return DRM_FORMAT_RGB565;
2445 default:
2446 case PLANE_CTL_FORMAT_XRGB_8888:
2447 if (rgb_order) {
2448 if (alpha)
2449 return DRM_FORMAT_ABGR8888;
2450 else
2451 return DRM_FORMAT_XBGR8888;
2452 } else {
2453 if (alpha)
2454 return DRM_FORMAT_ARGB8888;
2455 else
2456 return DRM_FORMAT_XRGB8888;
2457 }
2458 case PLANE_CTL_FORMAT_XRGB_2101010:
2459 if (rgb_order)
2460 return DRM_FORMAT_XBGR2101010;
2461 else
2462 return DRM_FORMAT_XRGB2101010;
2463 }
2464}
2465
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002466static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002467intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2468 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002469{
2470 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002471 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002472 struct drm_i915_gem_object *obj = NULL;
2473 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002474 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002475 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2476 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2477 PAGE_SIZE);
2478
2479 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002480
Chris Wilsonff2652e2014-03-10 08:07:02 +00002481 if (plane_config->size == 0)
2482 return false;
2483
Paulo Zanoni3badb492015-09-23 12:52:23 -03002484 /* If the FB is too big, just don't use it since fbdev is not very
2485 * important and we should probably use that space with FBC or other
2486 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002487 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002488 return false;
2489
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002490 mutex_lock(&dev->struct_mutex);
2491
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002492 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2493 base_aligned,
2494 base_aligned,
2495 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002496 if (!obj) {
2497 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002498 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002499 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002500
Damien Lespiau49af4492015-01-20 12:51:44 +00002501 obj->tiling_mode = plane_config->tiling;
2502 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002503 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002504
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002505 mode_cmd.pixel_format = fb->pixel_format;
2506 mode_cmd.width = fb->width;
2507 mode_cmd.height = fb->height;
2508 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002509 mode_cmd.modifier[0] = fb->modifier[0];
2510 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002511
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002512 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002514 DRM_DEBUG_KMS("intel fb init failed\n");
2515 goto out_unref_obj;
2516 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002517
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519
Daniel Vetterf6936e22015-03-26 12:17:05 +01002520 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002521 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522
2523out_unref_obj:
2524 drm_gem_object_unreference(&obj->base);
2525 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526 return false;
2527}
2528
Matt Roperafd65eb2015-02-03 13:10:04 -08002529/* Update plane->state->fb to match plane->fb after driver-internal updates */
2530static void
2531update_state_fb(struct drm_plane *plane)
2532{
2533 if (plane->fb == plane->state->fb)
2534 return;
2535
2536 if (plane->state->fb)
2537 drm_framebuffer_unreference(plane->state->fb);
2538 plane->state->fb = plane->fb;
2539 if (plane->state->fb)
2540 drm_framebuffer_reference(plane->state->fb);
2541}
2542
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002543static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002544intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2545 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002546{
2547 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002548 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002549 struct drm_crtc *c;
2550 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002551 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002552 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002553 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002554 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2555 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002556 struct intel_plane_state *intel_state =
2557 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002558 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559
Damien Lespiau2d140302015-02-05 17:22:18 +00002560 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561 return;
2562
Daniel Vetterf6936e22015-03-26 12:17:05 +01002563 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002564 fb = &plane_config->fb->base;
2565 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002566 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567
Damien Lespiau2d140302015-02-05 17:22:18 +00002568 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569
2570 /*
2571 * Failed to alloc the obj, check to see if we should share
2572 * an fb with another CRTC instead
2573 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002574 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002575 i = to_intel_crtc(c);
2576
2577 if (c == &intel_crtc->base)
2578 continue;
2579
Matt Roper2ff8fde2014-07-08 07:50:07 -07002580 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 continue;
2582
Daniel Vetter88595ac2015-03-26 12:42:24 +01002583 fb = c->primary->fb;
2584 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002585 continue;
2586
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002588 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 drm_framebuffer_reference(fb);
2590 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002591 }
2592 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002593
Matt Roper200757f2015-12-03 11:37:36 -08002594 /*
2595 * We've failed to reconstruct the BIOS FB. Current display state
2596 * indicates that the primary plane is visible, but has a NULL FB,
2597 * which will lead to problems later if we don't fix it up. The
2598 * simplest solution is to just disable the primary plane now and
2599 * pretend the BIOS never had it enabled.
2600 */
2601 to_intel_plane_state(plane_state)->visible = false;
2602 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002603 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002604 intel_plane->disable_plane(primary, &intel_crtc->base);
2605
Daniel Vetter88595ac2015-03-26 12:42:24 +01002606 return;
2607
2608valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002609 plane_state->src_x = 0;
2610 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002611 plane_state->src_w = fb->width << 16;
2612 plane_state->src_h = fb->height << 16;
2613
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002614 plane_state->crtc_x = 0;
2615 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002616 plane_state->crtc_w = fb->width;
2617 plane_state->crtc_h = fb->height;
2618
Matt Roper0a8d8a82015-12-03 11:37:38 -08002619 intel_state->src.x1 = plane_state->src_x;
2620 intel_state->src.y1 = plane_state->src_y;
2621 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2622 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2623 intel_state->dst.x1 = plane_state->crtc_x;
2624 intel_state->dst.y1 = plane_state->crtc_y;
2625 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2626 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2627
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628 obj = intel_fb_obj(fb);
2629 if (obj->tiling_mode != I915_TILING_NONE)
2630 dev_priv->preserve_bios_swizzle = true;
2631
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002632 drm_framebuffer_reference(fb);
2633 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002634 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002635 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002636 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002637}
2638
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002639static void i9xx_update_primary_plane(struct drm_plane *primary,
2640 const struct intel_crtc_state *crtc_state,
2641 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002642{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002643 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002644 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2646 struct drm_framebuffer *fb = plane_state->base.fb;
2647 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002648 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002649 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002650 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002651 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002652 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002653 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002654 int x = plane_state->src.x1 >> 16;
2655 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002656
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002657 dspcntr = DISPPLANE_GAMMA_ENABLE;
2658
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002659 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002660
2661 if (INTEL_INFO(dev)->gen < 4) {
2662 if (intel_crtc->pipe == PIPE_B)
2663 dspcntr |= DISPPLANE_SEL_PIPE_B;
2664
2665 /* pipesrc and dspsize control the size that is scaled from,
2666 * which should always be the user's requested size.
2667 */
2668 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002669 ((crtc_state->pipe_src_h - 1) << 16) |
2670 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002671 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002672 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2673 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002674 ((crtc_state->pipe_src_h - 1) << 16) |
2675 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002676 I915_WRITE(PRIMPOS(plane), 0);
2677 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002678 }
2679
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 switch (fb->pixel_format) {
2681 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002682 dspcntr |= DISPPLANE_8BPP;
2683 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002684 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002686 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 case DRM_FORMAT_RGB565:
2688 dspcntr |= DISPPLANE_BGRX565;
2689 break;
2690 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002691 dspcntr |= DISPPLANE_BGRX888;
2692 break;
2693 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002694 dspcntr |= DISPPLANE_RGBX888;
2695 break;
2696 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 dspcntr |= DISPPLANE_BGRX101010;
2698 break;
2699 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002701 break;
2702 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002703 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002704 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002706 if (INTEL_INFO(dev)->gen >= 4 &&
2707 obj->tiling_mode != I915_TILING_NONE)
2708 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002709
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002710 if (IS_G4X(dev))
2711 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2712
Ville Syrjäläac484962016-01-20 21:05:26 +02002713 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002714
Daniel Vetterc2c75132012-07-05 12:17:30 +02002715 if (INTEL_INFO(dev)->gen >= 4) {
2716 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002717 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002718 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002719 linear_offset -= intel_crtc->dspaddr_offset;
2720 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002722 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002723
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002724 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302725 dspcntr |= DISPPLANE_ROTATE_180;
2726
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002727 x += (crtc_state->pipe_src_w - 1);
2728 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302729
2730 /* Finding the last pixel of the last line of the display
2731 data and adding to linear_offset*/
2732 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002733 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002734 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302735 }
2736
Paulo Zanoni2db33662015-09-14 15:20:03 -03002737 intel_crtc->adjusted_x = x;
2738 intel_crtc->adjusted_y = y;
2739
Sonika Jindal48404c12014-08-22 14:06:04 +05302740 I915_WRITE(reg, dspcntr);
2741
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002742 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002743 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002744 I915_WRITE(DSPSURF(plane),
2745 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002746 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002747 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002749 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002750 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002751}
2752
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002753static void i9xx_disable_primary_plane(struct drm_plane *primary,
2754 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002755{
2756 struct drm_device *dev = crtc->dev;
2757 struct drm_i915_private *dev_priv = dev->dev_private;
2758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002759 int plane = intel_crtc->plane;
2760
2761 I915_WRITE(DSPCNTR(plane), 0);
2762 if (INTEL_INFO(dev_priv)->gen >= 4)
2763 I915_WRITE(DSPSURF(plane), 0);
2764 else
2765 I915_WRITE(DSPADDR(plane), 0);
2766 POSTING_READ(DSPCNTR(plane));
2767}
2768
2769static void ironlake_update_primary_plane(struct drm_plane *primary,
2770 const struct intel_crtc_state *crtc_state,
2771 const struct intel_plane_state *plane_state)
2772{
2773 struct drm_device *dev = primary->dev;
2774 struct drm_i915_private *dev_priv = dev->dev_private;
2775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2776 struct drm_framebuffer *fb = plane_state->base.fb;
2777 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002779 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002781 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002782 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002783 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002784 int x = plane_state->src.x1 >> 16;
2785 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002786
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002787 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002788 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002789
2790 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2791 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2792
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 switch (fb->pixel_format) {
2794 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 dspcntr |= DISPPLANE_8BPP;
2796 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 case DRM_FORMAT_RGB565:
2798 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002800 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 dspcntr |= DISPPLANE_BGRX888;
2802 break;
2803 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002804 dspcntr |= DISPPLANE_RGBX888;
2805 break;
2806 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002807 dspcntr |= DISPPLANE_BGRX101010;
2808 break;
2809 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002810 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002811 break;
2812 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002813 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814 }
2815
2816 if (obj->tiling_mode != I915_TILING_NONE)
2817 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002819 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002820 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821
Ville Syrjäläac484962016-01-20 21:05:26 +02002822 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002823 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002824 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002825 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002826 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002827 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302828 dspcntr |= DISPPLANE_ROTATE_180;
2829
2830 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002831 x += (crtc_state->pipe_src_w - 1);
2832 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302833
2834 /* Finding the last pixel of the last line of the display
2835 data and adding to linear_offset*/
2836 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002837 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002838 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302839 }
2840 }
2841
Paulo Zanoni2db33662015-09-14 15:20:03 -03002842 intel_crtc->adjusted_x = x;
2843 intel_crtc->adjusted_y = y;
2844
Sonika Jindal48404c12014-08-22 14:06:04 +05302845 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002846
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002847 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002848 I915_WRITE(DSPSURF(plane),
2849 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002850 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002851 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2852 } else {
2853 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2854 I915_WRITE(DSPLINOFF(plane), linear_offset);
2855 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002856 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002857}
2858
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002859u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2860 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002861{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002862 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2863 return 64;
2864 } else {
2865 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002866
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002867 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002868 }
2869}
2870
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002871u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2872 struct drm_i915_gem_object *obj,
2873 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002874{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002875 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002876 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002877 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002878
Ville Syrjäläe7941292016-01-19 18:23:17 +02002879 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002880 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002881
Daniel Vetterce7f1722015-10-14 16:51:06 +02002882 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002883 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002884 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002885 return -1;
2886
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002887 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002888
2889 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002890 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002891 PAGE_SIZE;
2892 }
2893
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002894 WARN_ON(upper_32_bits(offset));
2895
2896 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002897}
2898
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002899static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2900{
2901 struct drm_device *dev = intel_crtc->base.dev;
2902 struct drm_i915_private *dev_priv = dev->dev_private;
2903
2904 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2905 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2906 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002907}
2908
Chandra Kondurua1b22782015-04-07 15:28:45 -07002909/*
2910 * This function detaches (aka. unbinds) unused scalers in hardware
2911 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002912static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002913{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002914 struct intel_crtc_scaler_state *scaler_state;
2915 int i;
2916
Chandra Kondurua1b22782015-04-07 15:28:45 -07002917 scaler_state = &intel_crtc->config->scaler_state;
2918
2919 /* loop through and disable scalers that aren't in use */
2920 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002921 if (!scaler_state->scalers[i].in_use)
2922 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002923 }
2924}
2925
Chandra Konduru6156a452015-04-27 13:48:39 -07002926u32 skl_plane_ctl_format(uint32_t pixel_format)
2927{
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002929 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 /*
2938 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2939 * to be already pre-multiplied. We need to add a knob (or a different
2940 * DRM_FORMAT) for user-space to configure that.
2941 */
2942 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002946 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002961 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002963
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965}
2966
2967u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2968{
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 switch (fb_modifier) {
2970 case DRM_FORMAT_MOD_NONE:
2971 break;
2972 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 default:
2979 MISSING_CASE(fb_modifier);
2980 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002981
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983}
2984
2985u32 skl_plane_ctl_rotation(unsigned int rotation)
2986{
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 switch (rotation) {
2988 case BIT(DRM_ROTATE_0):
2989 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302990 /*
2991 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2992 * while i915 HW rotation is clockwise, thats why this swapping.
2993 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302995 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002997 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302999 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 default:
3001 MISSING_CASE(rotation);
3002 }
3003
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003004 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005}
3006
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003007static void skylake_update_primary_plane(struct drm_plane *plane,
3008 const struct intel_crtc_state *crtc_state,
3009 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003010{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003011 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3014 struct drm_framebuffer *fb = plane_state->base.fb;
3015 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003016 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 u32 plane_ctl, stride_div, stride;
3018 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003019 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303020 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003021 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003022 int scaler_id = plane_state->scaler_id;
3023 int src_x = plane_state->src.x1 >> 16;
3024 int src_y = plane_state->src.y1 >> 16;
3025 int src_w = drm_rect_width(&plane_state->src) >> 16;
3026 int src_h = drm_rect_height(&plane_state->src) >> 16;
3027 int dst_x = plane_state->dst.x1;
3028 int dst_y = plane_state->dst.y1;
3029 int dst_w = drm_rect_width(&plane_state->dst);
3030 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003031
3032 plane_ctl = PLANE_CTL_ENABLE |
3033 PLANE_CTL_PIPE_GAMMA_ENABLE |
3034 PLANE_CTL_PIPE_CSC_ENABLE;
3035
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3037 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003038 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003039 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003040
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003041 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003042 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003043 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303044
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003045 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003046
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303047 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003048 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3049
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303050 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003051 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303052 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003053 x_offset = stride * tile_height - src_y - src_h;
3054 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003055 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303056 } else {
3057 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003058 x_offset = src_x;
3059 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003060 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303061 }
3062 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003063
Paulo Zanoni2db33662015-09-14 15:20:03 -03003064 intel_crtc->adjusted_x = x_offset;
3065 intel_crtc->adjusted_y = y_offset;
3066
Damien Lespiau70d21f02013-07-03 21:06:04 +01003067 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3069 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3070 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003071
3072 if (scaler_id >= 0) {
3073 uint32_t ps_ctrl = 0;
3074
3075 WARN_ON(!dst_w || !dst_h);
3076 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3077 crtc_state->scaler_state.scalers[scaler_id].mode;
3078 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3079 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3080 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3081 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3082 I915_WRITE(PLANE_POS(pipe, 0), 0);
3083 } else {
3084 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3085 }
3086
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003087 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003088
3089 POSTING_READ(PLANE_SURF(pipe, 0));
3090}
3091
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003092static void skylake_disable_primary_plane(struct drm_plane *primary,
3093 struct drm_crtc *crtc)
3094{
3095 struct drm_device *dev = crtc->dev;
3096 struct drm_i915_private *dev_priv = dev->dev_private;
3097 int pipe = to_intel_crtc(crtc)->pipe;
3098
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003099 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3100 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3101 POSTING_READ(PLANE_SURF(pipe, 0));
3102}
3103
Jesse Barnes17638cd2011-06-24 12:19:23 -07003104/* Assume fb object is pinned & idle & fenced and just update base pointers */
3105static int
3106intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3107 int x, int y, enum mode_set_atomic state)
3108{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003109 /* Support for kgdboc is disabled, this needs a major rework. */
3110 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003111
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003112 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003113}
3114
Ville Syrjälä75147472014-11-24 18:28:11 +02003115static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003116{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003117 struct drm_crtc *crtc;
3118
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003119 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3121 enum plane plane = intel_crtc->plane;
3122
3123 intel_prepare_page_flip(dev, plane);
3124 intel_finish_page_flip_plane(dev, plane);
3125 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003126}
3127
3128static void intel_update_primary_planes(struct drm_device *dev)
3129{
Ville Syrjälä75147472014-11-24 18:28:11 +02003130 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003131
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003132 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003133 struct intel_plane *plane = to_intel_plane(crtc->primary);
3134 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003135
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003136 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003137 plane_state = to_intel_plane_state(plane->base.state);
3138
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003139 if (plane_state->visible)
3140 plane->update_plane(&plane->base,
3141 to_intel_crtc_state(crtc->state),
3142 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003143
3144 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003145 }
3146}
3147
Ville Syrjälä75147472014-11-24 18:28:11 +02003148void intel_prepare_reset(struct drm_device *dev)
3149{
3150 /* no reset support for gen2 */
3151 if (IS_GEN2(dev))
3152 return;
3153
3154 /* reset doesn't touch the display */
3155 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3156 return;
3157
3158 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003159 /*
3160 * Disabling the crtcs gracefully seems nicer. Also the
3161 * g33 docs say we should at least disable all the planes.
3162 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003163 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003164}
3165
3166void intel_finish_reset(struct drm_device *dev)
3167{
3168 struct drm_i915_private *dev_priv = to_i915(dev);
3169
3170 /*
3171 * Flips in the rings will be nuked by the reset,
3172 * so complete all pending flips so that user space
3173 * will get its events and not get stuck.
3174 */
3175 intel_complete_page_flips(dev);
3176
3177 /* no reset support for gen2 */
3178 if (IS_GEN2(dev))
3179 return;
3180
3181 /* reset doesn't touch the display */
3182 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3183 /*
3184 * Flips in the rings have been nuked by the reset,
3185 * so update the base address of all primary
3186 * planes to the the last fb to make sure we're
3187 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003188 *
3189 * FIXME: Atomic will make this obsolete since we won't schedule
3190 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003191 */
3192 intel_update_primary_planes(dev);
3193 return;
3194 }
3195
3196 /*
3197 * The display has been reset as well,
3198 * so need a full re-initialization.
3199 */
3200 intel_runtime_pm_disable_interrupts(dev_priv);
3201 intel_runtime_pm_enable_interrupts(dev_priv);
3202
3203 intel_modeset_init_hw(dev);
3204
3205 spin_lock_irq(&dev_priv->irq_lock);
3206 if (dev_priv->display.hpd_irq_setup)
3207 dev_priv->display.hpd_irq_setup(dev);
3208 spin_unlock_irq(&dev_priv->irq_lock);
3209
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003210 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003211
3212 intel_hpd_init(dev_priv);
3213
3214 drm_modeset_unlock_all(dev);
3215}
3216
Chris Wilson7d5e3792014-03-04 13:15:08 +00003217static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3218{
3219 struct drm_device *dev = crtc->dev;
3220 struct drm_i915_private *dev_priv = dev->dev_private;
3221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003222 bool pending;
3223
3224 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3225 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3226 return false;
3227
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003228 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003229 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003230 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003231
3232 return pending;
3233}
3234
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003235static void intel_update_pipe_config(struct intel_crtc *crtc,
3236 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003237{
3238 struct drm_device *dev = crtc->base.dev;
3239 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003240 struct intel_crtc_state *pipe_config =
3241 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003242
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003243 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3244 crtc->base.mode = crtc->base.state->mode;
3245
3246 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3247 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3248 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003249
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003250 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003251 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003252
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003253 /*
3254 * Update pipe size and adjust fitter if needed: the reason for this is
3255 * that in compute_mode_changes we check the native mode (not the pfit
3256 * mode) to see if we can flip rather than do a full mode set. In the
3257 * fastboot case, we'll flip, but if we don't update the pipesrc and
3258 * pfit state, we'll end up with a big fb scanned out into the wrong
3259 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003260 */
3261
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003262 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003263 ((pipe_config->pipe_src_w - 1) << 16) |
3264 (pipe_config->pipe_src_h - 1));
3265
3266 /* on skylake this is done by detaching scalers */
3267 if (INTEL_INFO(dev)->gen >= 9) {
3268 skl_detach_scalers(crtc);
3269
3270 if (pipe_config->pch_pfit.enabled)
3271 skylake_pfit_enable(crtc);
3272 } else if (HAS_PCH_SPLIT(dev)) {
3273 if (pipe_config->pch_pfit.enabled)
3274 ironlake_pfit_enable(crtc);
3275 else if (old_crtc_state->pch_pfit.enabled)
3276 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003277 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003278}
3279
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003280static void intel_fdi_normal_train(struct drm_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->dev;
3283 struct drm_i915_private *dev_priv = dev->dev_private;
3284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003286 i915_reg_t reg;
3287 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003288
3289 /* enable normal train */
3290 reg = FDI_TX_CTL(pipe);
3291 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003292 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003293 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3294 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003295 } else {
3296 temp &= ~FDI_LINK_TRAIN_NONE;
3297 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003298 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003299 I915_WRITE(reg, temp);
3300
3301 reg = FDI_RX_CTL(pipe);
3302 temp = I915_READ(reg);
3303 if (HAS_PCH_CPT(dev)) {
3304 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3305 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3306 } else {
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_NONE;
3309 }
3310 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3311
3312 /* wait one idle pattern time */
3313 POSTING_READ(reg);
3314 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003315
3316 /* IVB wants error correction enabled */
3317 if (IS_IVYBRIDGE(dev))
3318 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3319 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003320}
3321
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003322/* The FDI link training functions for ILK/Ibexpeak. */
3323static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3324{
3325 struct drm_device *dev = crtc->dev;
3326 struct drm_i915_private *dev_priv = dev->dev_private;
3327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3328 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003329 i915_reg_t reg;
3330 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003332 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003333 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003334
Adam Jacksone1a44742010-06-25 15:32:14 -04003335 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3336 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003337 reg = FDI_RX_IMR(pipe);
3338 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003339 temp &= ~FDI_RX_SYMBOL_LOCK;
3340 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 I915_WRITE(reg, temp);
3342 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003343 udelay(150);
3344
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_TX_CTL(pipe);
3347 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003348 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003349 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003350 temp &= ~FDI_LINK_TRAIN_NONE;
3351 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003352 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353
Chris Wilson5eddb702010-09-11 13:48:45 +01003354 reg = FDI_RX_CTL(pipe);
3355 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 temp &= ~FDI_LINK_TRAIN_NONE;
3357 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003358 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3359
3360 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361 udelay(150);
3362
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003363 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003364 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3365 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3366 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003367
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003369 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003370 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3372
3373 if ((temp & FDI_RX_BIT_LOCK)) {
3374 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 break;
3377 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003379 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381
3382 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003383 reg = FDI_TX_CTL(pipe);
3384 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 temp &= ~FDI_LINK_TRAIN_NONE;
3386 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 reg = FDI_RX_CTL(pipe);
3390 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391 temp &= ~FDI_LINK_TRAIN_NONE;
3392 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 I915_WRITE(reg, temp);
3394
3395 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 udelay(150);
3397
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3402
3403 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003405 DRM_DEBUG_KMS("FDI train 2 done.\n");
3406 break;
3407 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003409 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411
3412 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003413
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414}
3415
Akshay Joshi0206e352011-08-16 15:34:10 -04003416static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3418 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3419 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3420 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3421};
3422
3423/* The FDI link training functions for SNB/Cougarpoint. */
3424static void gen6_fdi_link_train(struct drm_crtc *crtc)
3425{
3426 struct drm_device *dev = crtc->dev;
3427 struct drm_i915_private *dev_priv = dev->dev_private;
3428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3429 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003430 i915_reg_t reg;
3431 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432
Adam Jacksone1a44742010-06-25 15:32:14 -04003433 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3434 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 reg = FDI_RX_IMR(pipe);
3436 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003437 temp &= ~FDI_RX_SYMBOL_LOCK;
3438 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 I915_WRITE(reg, temp);
3440
3441 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003442 udelay(150);
3443
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = FDI_TX_CTL(pipe);
3446 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003447 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003448 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 temp &= ~FDI_LINK_TRAIN_NONE;
3450 temp |= FDI_LINK_TRAIN_PATTERN_1;
3451 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3452 /* SNB-B */
3453 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455
Daniel Vetterd74cf322012-10-26 10:58:13 +02003456 I915_WRITE(FDI_RX_MISC(pipe),
3457 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3458
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 reg = FDI_RX_CTL(pipe);
3460 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003461 if (HAS_PCH_CPT(dev)) {
3462 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3463 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3464 } else {
3465 temp &= ~FDI_LINK_TRAIN_NONE;
3466 temp |= FDI_LINK_TRAIN_PATTERN_1;
3467 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3469
3470 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 udelay(150);
3472
Akshay Joshi0206e352011-08-16 15:34:10 -04003473 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 reg = FDI_TX_CTL(pipe);
3475 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3477 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp);
3479
3480 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 udelay(500);
3482
Sean Paulfa37d392012-03-02 12:53:39 -05003483 for (retry = 0; retry < 5; retry++) {
3484 reg = FDI_RX_IIR(pipe);
3485 temp = I915_READ(reg);
3486 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3487 if (temp & FDI_RX_BIT_LOCK) {
3488 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3489 DRM_DEBUG_KMS("FDI train 1 done.\n");
3490 break;
3491 }
3492 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 }
Sean Paulfa37d392012-03-02 12:53:39 -05003494 if (retry < 5)
3495 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 }
3497 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499
3500 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 reg = FDI_TX_CTL(pipe);
3502 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 temp &= ~FDI_LINK_TRAIN_NONE;
3504 temp |= FDI_LINK_TRAIN_PATTERN_2;
3505 if (IS_GEN6(dev)) {
3506 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3507 /* SNB-B */
3508 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3509 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_RX_CTL(pipe);
3513 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514 if (HAS_PCH_CPT(dev)) {
3515 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3516 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3517 } else {
3518 temp &= ~FDI_LINK_TRAIN_NONE;
3519 temp |= FDI_LINK_TRAIN_PATTERN_2;
3520 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp);
3522
3523 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 udelay(150);
3525
Akshay Joshi0206e352011-08-16 15:34:10 -04003526 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003527 reg = FDI_TX_CTL(pipe);
3528 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3530 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 I915_WRITE(reg, temp);
3532
3533 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 udelay(500);
3535
Sean Paulfa37d392012-03-02 12:53:39 -05003536 for (retry = 0; retry < 5; retry++) {
3537 reg = FDI_RX_IIR(pipe);
3538 temp = I915_READ(reg);
3539 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3540 if (temp & FDI_RX_SYMBOL_LOCK) {
3541 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3542 DRM_DEBUG_KMS("FDI train 2 done.\n");
3543 break;
3544 }
3545 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546 }
Sean Paulfa37d392012-03-02 12:53:39 -05003547 if (retry < 5)
3548 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 }
3550 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003551 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552
3553 DRM_DEBUG_KMS("FDI train done.\n");
3554}
3555
Jesse Barnes357555c2011-04-28 15:09:55 -07003556/* Manual link training for Ivy Bridge A0 parts */
3557static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3558{
3559 struct drm_device *dev = crtc->dev;
3560 struct drm_i915_private *dev_priv = dev->dev_private;
3561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3562 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003563 i915_reg_t reg;
3564 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003565
3566 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3567 for train result */
3568 reg = FDI_RX_IMR(pipe);
3569 temp = I915_READ(reg);
3570 temp &= ~FDI_RX_SYMBOL_LOCK;
3571 temp &= ~FDI_RX_BIT_LOCK;
3572 I915_WRITE(reg, temp);
3573
3574 POSTING_READ(reg);
3575 udelay(150);
3576
Daniel Vetter01a415f2012-10-27 15:58:40 +02003577 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3578 I915_READ(FDI_RX_IIR(pipe)));
3579
Jesse Barnes139ccd32013-08-19 11:04:55 -07003580 /* Try each vswing and preemphasis setting twice before moving on */
3581 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3582 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003583 reg = FDI_TX_CTL(pipe);
3584 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003585 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3586 temp &= ~FDI_TX_ENABLE;
3587 I915_WRITE(reg, temp);
3588
3589 reg = FDI_RX_CTL(pipe);
3590 temp = I915_READ(reg);
3591 temp &= ~FDI_LINK_TRAIN_AUTO;
3592 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3593 temp &= ~FDI_RX_ENABLE;
3594 I915_WRITE(reg, temp);
3595
3596 /* enable CPU FDI TX and PCH FDI RX */
3597 reg = FDI_TX_CTL(pipe);
3598 temp = I915_READ(reg);
3599 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003600 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003601 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003602 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003603 temp |= snb_b_fdi_train_param[j/2];
3604 temp |= FDI_COMPOSITE_SYNC;
3605 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3606
3607 I915_WRITE(FDI_RX_MISC(pipe),
3608 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3609
3610 reg = FDI_RX_CTL(pipe);
3611 temp = I915_READ(reg);
3612 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3613 temp |= FDI_COMPOSITE_SYNC;
3614 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3615
3616 POSTING_READ(reg);
3617 udelay(1); /* should be 0.5us */
3618
3619 for (i = 0; i < 4; i++) {
3620 reg = FDI_RX_IIR(pipe);
3621 temp = I915_READ(reg);
3622 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3623
3624 if (temp & FDI_RX_BIT_LOCK ||
3625 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3626 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3627 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3628 i);
3629 break;
3630 }
3631 udelay(1); /* should be 0.5us */
3632 }
3633 if (i == 4) {
3634 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3635 continue;
3636 }
3637
3638 /* Train 2 */
3639 reg = FDI_TX_CTL(pipe);
3640 temp = I915_READ(reg);
3641 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3642 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3643 I915_WRITE(reg, temp);
3644
3645 reg = FDI_RX_CTL(pipe);
3646 temp = I915_READ(reg);
3647 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3648 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003649 I915_WRITE(reg, temp);
3650
3651 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003653
Jesse Barnes139ccd32013-08-19 11:04:55 -07003654 for (i = 0; i < 4; i++) {
3655 reg = FDI_RX_IIR(pipe);
3656 temp = I915_READ(reg);
3657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003658
Jesse Barnes139ccd32013-08-19 11:04:55 -07003659 if (temp & FDI_RX_SYMBOL_LOCK ||
3660 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3661 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3662 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3663 i);
3664 goto train_done;
3665 }
3666 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003667 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003668 if (i == 4)
3669 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003670 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003671
Jesse Barnes139ccd32013-08-19 11:04:55 -07003672train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003673 DRM_DEBUG_KMS("FDI train done.\n");
3674}
3675
Daniel Vetter88cefb62012-08-12 19:27:14 +02003676static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003677{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003678 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003679 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003680 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003681 i915_reg_t reg;
3682 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003683
Jesse Barnes0e23b992010-09-10 11:10:00 -07003684 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003685 reg = FDI_RX_CTL(pipe);
3686 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003687 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003688 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003689 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003690 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3691
3692 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003693 udelay(200);
3694
3695 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003696 temp = I915_READ(reg);
3697 I915_WRITE(reg, temp | FDI_PCDCLK);
3698
3699 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003700 udelay(200);
3701
Paulo Zanoni20749732012-11-23 15:30:38 -02003702 /* Enable CPU FDI TX PLL, always on for Ironlake */
3703 reg = FDI_TX_CTL(pipe);
3704 temp = I915_READ(reg);
3705 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3706 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003707
Paulo Zanoni20749732012-11-23 15:30:38 -02003708 POSTING_READ(reg);
3709 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003710 }
3711}
3712
Daniel Vetter88cefb62012-08-12 19:27:14 +02003713static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3714{
3715 struct drm_device *dev = intel_crtc->base.dev;
3716 struct drm_i915_private *dev_priv = dev->dev_private;
3717 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003718 i915_reg_t reg;
3719 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003720
3721 /* Switch from PCDclk to Rawclk */
3722 reg = FDI_RX_CTL(pipe);
3723 temp = I915_READ(reg);
3724 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3725
3726 /* Disable CPU FDI TX PLL */
3727 reg = FDI_TX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3730
3731 POSTING_READ(reg);
3732 udelay(100);
3733
3734 reg = FDI_RX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3737
3738 /* Wait for the clocks to turn off. */
3739 POSTING_READ(reg);
3740 udelay(100);
3741}
3742
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003743static void ironlake_fdi_disable(struct drm_crtc *crtc)
3744{
3745 struct drm_device *dev = crtc->dev;
3746 struct drm_i915_private *dev_priv = dev->dev_private;
3747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3748 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003749 i915_reg_t reg;
3750 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003751
3752 /* disable CPU FDI tx and PCH FDI rx */
3753 reg = FDI_TX_CTL(pipe);
3754 temp = I915_READ(reg);
3755 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3756 POSTING_READ(reg);
3757
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
3760 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003761 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003762 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3763
3764 POSTING_READ(reg);
3765 udelay(100);
3766
3767 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003768 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003769 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003770
3771 /* still set train pattern 1 */
3772 reg = FDI_TX_CTL(pipe);
3773 temp = I915_READ(reg);
3774 temp &= ~FDI_LINK_TRAIN_NONE;
3775 temp |= FDI_LINK_TRAIN_PATTERN_1;
3776 I915_WRITE(reg, temp);
3777
3778 reg = FDI_RX_CTL(pipe);
3779 temp = I915_READ(reg);
3780 if (HAS_PCH_CPT(dev)) {
3781 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3782 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3783 } else {
3784 temp &= ~FDI_LINK_TRAIN_NONE;
3785 temp |= FDI_LINK_TRAIN_PATTERN_1;
3786 }
3787 /* BPC in FDI rx is consistent with that in PIPECONF */
3788 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003789 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003790 I915_WRITE(reg, temp);
3791
3792 POSTING_READ(reg);
3793 udelay(100);
3794}
3795
Chris Wilson5dce5b932014-01-20 10:17:36 +00003796bool intel_has_pending_fb_unpin(struct drm_device *dev)
3797{
3798 struct intel_crtc *crtc;
3799
3800 /* Note that we don't need to be called with mode_config.lock here
3801 * as our list of CRTC objects is static for the lifetime of the
3802 * device and so cannot disappear as we iterate. Similarly, we can
3803 * happily treat the predicates as racy, atomic checks as userspace
3804 * cannot claim and pin a new fb without at least acquring the
3805 * struct_mutex and so serialising with us.
3806 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003807 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003808 if (atomic_read(&crtc->unpin_work_count) == 0)
3809 continue;
3810
3811 if (crtc->unpin_work)
3812 intel_wait_for_vblank(dev, crtc->pipe);
3813
3814 return true;
3815 }
3816
3817 return false;
3818}
3819
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003820static void page_flip_completed(struct intel_crtc *intel_crtc)
3821{
3822 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3823 struct intel_unpin_work *work = intel_crtc->unpin_work;
3824
3825 /* ensure that the unpin work is consistent wrt ->pending. */
3826 smp_rmb();
3827 intel_crtc->unpin_work = NULL;
3828
3829 if (work->event)
3830 drm_send_vblank_event(intel_crtc->base.dev,
3831 intel_crtc->pipe,
3832 work->event);
3833
3834 drm_crtc_vblank_put(&intel_crtc->base);
3835
3836 wake_up_all(&dev_priv->pending_flip_queue);
3837 queue_work(dev_priv->wq, &work->work);
3838
3839 trace_i915_flip_complete(intel_crtc->plane,
3840 work->pending_flip_obj);
3841}
3842
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003843static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003844{
Chris Wilson0f911282012-04-17 10:05:38 +01003845 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003846 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003847 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003848
Daniel Vetter2c10d572012-12-20 21:24:07 +01003849 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003850
3851 ret = wait_event_interruptible_timeout(
3852 dev_priv->pending_flip_queue,
3853 !intel_crtc_has_pending_flip(crtc),
3854 60*HZ);
3855
3856 if (ret < 0)
3857 return ret;
3858
3859 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003861
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003862 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003863 if (intel_crtc->unpin_work) {
3864 WARN_ONCE(1, "Removing stuck page flip\n");
3865 page_flip_completed(intel_crtc);
3866 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003867 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003868 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003869
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003870 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003871}
3872
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003873static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3874{
3875 u32 temp;
3876
3877 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3878
3879 mutex_lock(&dev_priv->sb_lock);
3880
3881 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3882 temp |= SBI_SSCCTL_DISABLE;
3883 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3884
3885 mutex_unlock(&dev_priv->sb_lock);
3886}
3887
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003888/* Program iCLKIP clock to the desired frequency */
3889static void lpt_program_iclkip(struct drm_crtc *crtc)
3890{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003891 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003892 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003893 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3894 u32 temp;
3895
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003896 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003898 /* The iCLK virtual clock root frequency is in MHz,
3899 * but the adjusted_mode->crtc_clock in in KHz. To get the
3900 * divisors, it is necessary to divide one by another, so we
3901 * convert the virtual clock precision to KHz here for higher
3902 * precision.
3903 */
3904 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003905 u32 iclk_virtual_root_freq = 172800 * 1000;
3906 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003907 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003908
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003909 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3910 clock << auxdiv);
3911 divsel = (desired_divisor / iclk_pi_range) - 2;
3912 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003914 /*
3915 * Near 20MHz is a corner case which is
3916 * out of range for the 7-bit divisor
3917 */
3918 if (divsel <= 0x7f)
3919 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920 }
3921
3922 /* This should not happen with any sane values */
3923 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3924 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3925 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3926 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3927
3928 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 +03003929 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 auxdiv,
3931 divsel,
3932 phasedir,
3933 phaseinc);
3934
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003935 mutex_lock(&dev_priv->sb_lock);
3936
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3940 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3941 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3942 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3943 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3944 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003945 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003946
3947 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003948 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003949 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3950 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003951 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952
3953 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003954 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003956 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003958 mutex_unlock(&dev_priv->sb_lock);
3959
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960 /* Wait for initialization time */
3961 udelay(24);
3962
3963 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3964}
3965
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003966int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3967{
3968 u32 divsel, phaseinc, auxdiv;
3969 u32 iclk_virtual_root_freq = 172800 * 1000;
3970 u32 iclk_pi_range = 64;
3971 u32 desired_divisor;
3972 u32 temp;
3973
3974 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3975 return 0;
3976
3977 mutex_lock(&dev_priv->sb_lock);
3978
3979 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3980 if (temp & SBI_SSCCTL_DISABLE) {
3981 mutex_unlock(&dev_priv->sb_lock);
3982 return 0;
3983 }
3984
3985 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3986 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3987 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3988 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3989 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3990
3991 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3992 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3993 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3994
3995 mutex_unlock(&dev_priv->sb_lock);
3996
3997 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3998
3999 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4000 desired_divisor << auxdiv);
4001}
4002
Daniel Vetter275f01b22013-05-03 11:49:47 +02004003static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4004 enum pipe pch_transcoder)
4005{
4006 struct drm_device *dev = crtc->base.dev;
4007 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004008 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004009
4010 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4011 I915_READ(HTOTAL(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4013 I915_READ(HBLANK(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4015 I915_READ(HSYNC(cpu_transcoder)));
4016
4017 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4018 I915_READ(VTOTAL(cpu_transcoder)));
4019 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4020 I915_READ(VBLANK(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4022 I915_READ(VSYNC(cpu_transcoder)));
4023 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4024 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4025}
4026
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004027static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004028{
4029 struct drm_i915_private *dev_priv = dev->dev_private;
4030 uint32_t temp;
4031
4032 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004033 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034 return;
4035
4036 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4038
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039 temp &= ~FDI_BC_BIFURCATION_SELECT;
4040 if (enable)
4041 temp |= FDI_BC_BIFURCATION_SELECT;
4042
4043 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044 I915_WRITE(SOUTH_CHICKEN1, temp);
4045 POSTING_READ(SOUTH_CHICKEN1);
4046}
4047
4048static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4049{
4050 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051
4052 switch (intel_crtc->pipe) {
4053 case PIPE_A:
4054 break;
4055 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004056 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004057 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004058 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004059 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060
4061 break;
4062 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004063 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004064
4065 break;
4066 default:
4067 BUG();
4068 }
4069}
4070
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004071/* Return which DP Port should be selected for Transcoder DP control */
4072static enum port
4073intel_trans_dp_port_sel(struct drm_crtc *crtc)
4074{
4075 struct drm_device *dev = crtc->dev;
4076 struct intel_encoder *encoder;
4077
4078 for_each_encoder_on_crtc(dev, crtc, encoder) {
4079 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4080 encoder->type == INTEL_OUTPUT_EDP)
4081 return enc_to_dig_port(&encoder->base)->port;
4082 }
4083
4084 return -1;
4085}
4086
Jesse Barnesf67a5592011-01-05 10:31:48 -08004087/*
4088 * Enable PCH resources required for PCH ports:
4089 * - PCH PLLs
4090 * - FDI training & RX/TX
4091 * - update transcoder timings
4092 * - DP transcoding bits
4093 * - transcoder
4094 */
4095static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004096{
4097 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004098 struct drm_i915_private *dev_priv = dev->dev_private;
4099 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4100 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004101 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004102
Daniel Vetterab9412b2013-05-03 11:49:46 +02004103 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004104
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004105 if (IS_IVYBRIDGE(dev))
4106 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4107
Daniel Vettercd986ab2012-10-26 10:58:12 +02004108 /* Write the TU size bits before fdi link training, so that error
4109 * detection works. */
4110 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4111 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4112
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004113 /*
4114 * Sometimes spurious CPU pipe underruns happen during FDI
4115 * training, at least with VGA+HDMI cloning. Suppress them.
4116 */
4117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4118
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004120 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004121
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004122 /* We need to program the right clock selection before writing the pixel
4123 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004124 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004125 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004126
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004127 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004128 temp |= TRANS_DPLL_ENABLE(pipe);
4129 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004130 if (intel_crtc->config->shared_dpll ==
4131 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004132 temp |= sel;
4133 else
4134 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004136 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004138 /* XXX: pch pll's can be enabled any time before we enable the PCH
4139 * transcoder, and we actually should do this to not upset any PCH
4140 * transcoder that already use the clock when we share it.
4141 *
4142 * Note that enable_shared_dpll tries to do the right thing, but
4143 * get_shared_dpll unconditionally resets the pll - we need that to have
4144 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004145 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004146
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004147 /* set transcoder timing, panel must allow it */
4148 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004149 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004151 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004152
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004153 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4154
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004156 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004157 const struct drm_display_mode *adjusted_mode =
4158 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004159 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004160 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 temp = I915_READ(reg);
4162 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004163 TRANS_DP_SYNC_MASK |
4164 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004165 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004166 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004168 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004169 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004170 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172
4173 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004174 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004175 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004176 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004177 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004178 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004180 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004181 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182 break;
4183 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004184 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004185 }
4186
Chris Wilson5eddb702010-09-11 13:48:45 +01004187 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004188 }
4189
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004190 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004191}
4192
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004193static void lpt_pch_enable(struct drm_crtc *crtc)
4194{
4195 struct drm_device *dev = crtc->dev;
4196 struct drm_i915_private *dev_priv = dev->dev_private;
4197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004198 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Daniel Vetterab9412b2013-05-03 11:49:46 +02004200 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004201
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004202 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004203
Paulo Zanoni0540e482012-10-31 18:12:40 -02004204 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004205 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004206
Paulo Zanoni937bb612012-10-31 18:12:47 -02004207 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004208}
4209
Daniel Vettera1520312013-05-03 11:49:50 +02004210static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004211{
4212 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004213 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004214 u32 temp;
4215
4216 temp = I915_READ(dslreg);
4217 udelay(500);
4218 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004219 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004220 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004221 }
4222}
4223
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004224static int
4225skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4226 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4227 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004228{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004229 struct intel_crtc_scaler_state *scaler_state =
4230 &crtc_state->scaler_state;
4231 struct intel_crtc *intel_crtc =
4232 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004233 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004234
4235 need_scaling = intel_rotation_90_or_270(rotation) ?
4236 (src_h != dst_w || src_w != dst_h):
4237 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004238
4239 /*
4240 * if plane is being disabled or scaler is no more required or force detach
4241 * - free scaler binded to this plane/crtc
4242 * - in order to do this, update crtc->scaler_usage
4243 *
4244 * Here scaler state in crtc_state is set free so that
4245 * scaler can be assigned to other user. Actual register
4246 * update to free the scaler is done in plane/panel-fit programming.
4247 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4248 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004252 scaler_state->scalers[*scaler_id].in_use = 0;
4253
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004254 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4255 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4256 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004257 scaler_state->scaler_users);
4258 *scaler_id = -1;
4259 }
4260 return 0;
4261 }
4262
4263 /* range checks */
4264 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4265 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4266
4267 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4268 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004270 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004272 return -EINVAL;
4273 }
4274
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004275 /* mark this plane as a scaler user in crtc_state */
4276 scaler_state->scaler_users |= (1 << scaler_user);
4277 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4278 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4279 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4280 scaler_state->scaler_users);
4281
4282 return 0;
4283}
4284
4285/**
4286 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4287 *
4288 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289 *
4290 * Return
4291 * 0 - scaler_usage updated successfully
4292 * error - requested scaling cannot be supported or other error condition
4293 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004294int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004295{
4296 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004297 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004298
4299 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4300 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4301
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004302 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004303 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004304 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004305 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004306}
4307
4308/**
4309 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4310 *
4311 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004312 * @plane_state: atomic plane state to update
4313 *
4314 * Return
4315 * 0 - scaler_usage updated successfully
4316 * error - requested scaling cannot be supported or other error condition
4317 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004318static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4319 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320{
4321
4322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004323 struct intel_plane *intel_plane =
4324 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004325 struct drm_framebuffer *fb = plane_state->base.fb;
4326 int ret;
4327
4328 bool force_detach = !fb || !plane_state->visible;
4329
4330 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4331 intel_plane->base.base.id, intel_crtc->pipe,
4332 drm_plane_index(&intel_plane->base));
4333
4334 ret = skl_update_scaler(crtc_state, force_detach,
4335 drm_plane_index(&intel_plane->base),
4336 &plane_state->scaler_id,
4337 plane_state->base.rotation,
4338 drm_rect_width(&plane_state->src) >> 16,
4339 drm_rect_height(&plane_state->src) >> 16,
4340 drm_rect_width(&plane_state->dst),
4341 drm_rect_height(&plane_state->dst));
4342
4343 if (ret || plane_state->scaler_id < 0)
4344 return ret;
4345
Chandra Kondurua1b22782015-04-07 15:28:45 -07004346 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004347 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004348 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004349 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004350 return -EINVAL;
4351 }
4352
4353 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004354 switch (fb->pixel_format) {
4355 case DRM_FORMAT_RGB565:
4356 case DRM_FORMAT_XBGR8888:
4357 case DRM_FORMAT_XRGB8888:
4358 case DRM_FORMAT_ABGR8888:
4359 case DRM_FORMAT_ARGB8888:
4360 case DRM_FORMAT_XRGB2101010:
4361 case DRM_FORMAT_XBGR2101010:
4362 case DRM_FORMAT_YUYV:
4363 case DRM_FORMAT_YVYU:
4364 case DRM_FORMAT_UYVY:
4365 case DRM_FORMAT_VYUY:
4366 break;
4367 default:
4368 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4369 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4370 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004371 }
4372
Chandra Kondurua1b22782015-04-07 15:28:45 -07004373 return 0;
4374}
4375
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004376static void skylake_scaler_disable(struct intel_crtc *crtc)
4377{
4378 int i;
4379
4380 for (i = 0; i < crtc->num_scalers; i++)
4381 skl_detach_scaler(crtc, i);
4382}
4383
4384static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004385{
4386 struct drm_device *dev = crtc->base.dev;
4387 struct drm_i915_private *dev_priv = dev->dev_private;
4388 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004389 struct intel_crtc_scaler_state *scaler_state =
4390 &crtc->config->scaler_state;
4391
4392 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4393
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004394 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004395 int id;
4396
4397 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4398 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4399 return;
4400 }
4401
4402 id = scaler_state->scaler_id;
4403 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4404 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4405 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4406 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4407
4408 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004409 }
4410}
4411
Jesse Barnesb074cec2013-04-25 12:55:02 -07004412static void ironlake_pfit_enable(struct intel_crtc *crtc)
4413{
4414 struct drm_device *dev = crtc->base.dev;
4415 struct drm_i915_private *dev_priv = dev->dev_private;
4416 int pipe = crtc->pipe;
4417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004418 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004419 /* Force use of hard-coded filter coefficients
4420 * as some pre-programmed values are broken,
4421 * e.g. x201.
4422 */
4423 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4424 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4425 PF_PIPE_SEL_IVB(pipe));
4426 else
4427 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004428 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4429 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004430 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004431}
4432
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004433void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004434{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004435 struct drm_device *dev = crtc->base.dev;
4436 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004437
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004438 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004439 return;
4440
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004441 /* We can only enable IPS after we enable a plane and wait for a vblank */
4442 intel_wait_for_vblank(dev, crtc->pipe);
4443
Paulo Zanonid77e4532013-09-24 13:52:55 -03004444 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004445 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004446 mutex_lock(&dev_priv->rps.hw_lock);
4447 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4448 mutex_unlock(&dev_priv->rps.hw_lock);
4449 /* Quoting Art Runyan: "its not safe to expect any particular
4450 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004451 * mailbox." Moreover, the mailbox may return a bogus state,
4452 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004453 */
4454 } else {
4455 I915_WRITE(IPS_CTL, IPS_ENABLE);
4456 /* The bit only becomes 1 in the next vblank, so this wait here
4457 * is essentially intel_wait_for_vblank. If we don't have this
4458 * and don't wait for vblanks until the end of crtc_enable, then
4459 * the HW state readout code will complain that the expected
4460 * IPS_CTL value is not the one we read. */
4461 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4462 DRM_ERROR("Timed out waiting for IPS enable\n");
4463 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004464}
4465
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004466void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004467{
4468 struct drm_device *dev = crtc->base.dev;
4469 struct drm_i915_private *dev_priv = dev->dev_private;
4470
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004471 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004472 return;
4473
4474 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004475 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004476 mutex_lock(&dev_priv->rps.hw_lock);
4477 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4478 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004479 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4480 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4481 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004482 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004483 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004484 POSTING_READ(IPS_CTL);
4485 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004486
4487 /* We need to wait for a vblank before we can disable the plane. */
4488 intel_wait_for_vblank(dev, crtc->pipe);
4489}
4490
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004491static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004492{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004493 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004494 struct drm_device *dev = intel_crtc->base.dev;
4495 struct drm_i915_private *dev_priv = dev->dev_private;
4496
4497 mutex_lock(&dev->struct_mutex);
4498 dev_priv->mm.interruptible = false;
4499 (void) intel_overlay_switch_off(intel_crtc->overlay);
4500 dev_priv->mm.interruptible = true;
4501 mutex_unlock(&dev->struct_mutex);
4502 }
4503
4504 /* Let userspace switch the overlay on again. In most cases userspace
4505 * has to recompute where to put it anyway.
4506 */
4507}
4508
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004509/**
4510 * intel_post_enable_primary - Perform operations after enabling primary plane
4511 * @crtc: the CRTC whose primary plane was just enabled
4512 *
4513 * Performs potentially sleeping operations that must be done after the primary
4514 * plane is enabled, such as updating FBC and IPS. Note that this may be
4515 * called due to an explicit primary plane update, or due to an implicit
4516 * re-enable that is caused when a sprite plane is updated to no longer
4517 * completely hide the primary plane.
4518 */
4519static void
4520intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004521{
4522 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004523 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4525 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004526
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004527 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004528 * FIXME IPS should be fine as long as one plane is
4529 * enabled, but in practice it seems to have problems
4530 * when going from primary only to sprite only and vice
4531 * versa.
4532 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004533 hsw_enable_ips(intel_crtc);
4534
Daniel Vetterf99d7062014-06-19 16:01:59 +02004535 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004536 * Gen2 reports pipe underruns whenever all planes are disabled.
4537 * So don't enable underrun reporting before at least some planes
4538 * are enabled.
4539 * FIXME: Need to fix the logic to work when we turn off all planes
4540 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004541 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004542 if (IS_GEN2(dev))
4543 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4544
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004545 /* Underruns don't always raise interrupts, so check manually. */
4546 intel_check_cpu_fifo_underruns(dev_priv);
4547 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004548}
4549
Ville Syrjälä2622a082016-03-09 19:07:26 +02004550/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004551static void
4552intel_pre_disable_primary(struct drm_crtc *crtc)
4553{
4554 struct drm_device *dev = crtc->dev;
4555 struct drm_i915_private *dev_priv = dev->dev_private;
4556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4557 int pipe = intel_crtc->pipe;
4558
4559 /*
4560 * Gen2 reports pipe underruns whenever all planes are disabled.
4561 * So diasble underrun reporting before all the planes get disabled.
4562 * FIXME: Need to fix the logic to work when we turn off all planes
4563 * but leave the pipe running.
4564 */
4565 if (IS_GEN2(dev))
4566 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4567
4568 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004569 * FIXME IPS should be fine as long as one plane is
4570 * enabled, but in practice it seems to have problems
4571 * when going from primary only to sprite only and vice
4572 * versa.
4573 */
4574 hsw_disable_ips(intel_crtc);
4575}
4576
4577/* FIXME get rid of this and use pre_plane_update */
4578static void
4579intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4580{
4581 struct drm_device *dev = crtc->dev;
4582 struct drm_i915_private *dev_priv = dev->dev_private;
4583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4584 int pipe = intel_crtc->pipe;
4585
4586 intel_pre_disable_primary(crtc);
4587
4588 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004589 * Vblank time updates from the shadow to live plane control register
4590 * are blocked if the memory self-refresh mode is active at that
4591 * moment. So to make sure the plane gets truly disabled, disable
4592 * first the self-refresh mode. The self-refresh enable bit in turn
4593 * will be checked/applied by the HW only at the next frame start
4594 * event which is after the vblank start event, so we need to have a
4595 * wait-for-vblank between disabling the plane and the pipe.
4596 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004597 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004598 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004599 dev_priv->wm.vlv.cxsr = false;
4600 intel_wait_for_vblank(dev, pipe);
4601 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004602}
4603
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004604static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004605{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004606 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4607 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004608 struct intel_crtc_state *pipe_config =
4609 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004610 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004611 struct drm_plane *primary = crtc->base.primary;
4612 struct drm_plane_state *old_pri_state =
4613 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004614
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004615 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004616
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004617 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004618
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004619 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004620 intel_update_watermarks(&crtc->base);
4621
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004622 if (old_pri_state) {
4623 struct intel_plane_state *primary_state =
4624 to_intel_plane_state(primary->state);
4625 struct intel_plane_state *old_primary_state =
4626 to_intel_plane_state(old_pri_state);
4627
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004628 intel_fbc_post_update(crtc);
4629
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004630 if (primary_state->visible &&
4631 (needs_modeset(&pipe_config->base) ||
4632 !old_primary_state->visible))
4633 intel_post_enable_primary(&crtc->base);
4634 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004635}
4636
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004637static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004638{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004639 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004640 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004641 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004642 struct intel_crtc_state *pipe_config =
4643 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004644 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4645 struct drm_plane *primary = crtc->base.primary;
4646 struct drm_plane_state *old_pri_state =
4647 drm_atomic_get_existing_plane_state(old_state, primary);
4648 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004649
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004650 if (old_pri_state) {
4651 struct intel_plane_state *primary_state =
4652 to_intel_plane_state(primary->state);
4653 struct intel_plane_state *old_primary_state =
4654 to_intel_plane_state(old_pri_state);
4655
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004656 intel_fbc_pre_update(crtc);
4657
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004658 if (old_primary_state->visible &&
4659 (modeset || !primary_state->visible))
4660 intel_pre_disable_primary(&crtc->base);
4661 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004662
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004663 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004664 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004665
Ville Syrjälä2622a082016-03-09 19:07:26 +02004666 /*
4667 * Vblank time updates from the shadow to live plane control register
4668 * are blocked if the memory self-refresh mode is active at that
4669 * moment. So to make sure the plane gets truly disabled, disable
4670 * first the self-refresh mode. The self-refresh enable bit in turn
4671 * will be checked/applied by the HW only at the next frame start
4672 * event which is after the vblank start event, so we need to have a
4673 * wait-for-vblank between disabling the plane and the pipe.
4674 */
4675 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004676 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004677 dev_priv->wm.vlv.cxsr = false;
4678 intel_wait_for_vblank(dev, crtc->pipe);
4679 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004680 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004681
Matt Ropered4a6a72016-02-23 17:20:13 -08004682 /*
4683 * IVB workaround: must disable low power watermarks for at least
4684 * one frame before enabling scaling. LP watermarks can be re-enabled
4685 * when scaling is disabled.
4686 *
4687 * WaCxSRDisabledForSpriteScaling:ivb
4688 */
4689 if (pipe_config->disable_lp_wm) {
4690 ilk_disable_lp_wm(dev);
4691 intel_wait_for_vblank(dev, crtc->pipe);
4692 }
4693
4694 /*
4695 * If we're doing a modeset, we're done. No need to do any pre-vblank
4696 * watermark programming here.
4697 */
4698 if (needs_modeset(&pipe_config->base))
4699 return;
4700
4701 /*
4702 * For platforms that support atomic watermarks, program the
4703 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4704 * will be the intermediate values that are safe for both pre- and
4705 * post- vblank; when vblank happens, the 'active' values will be set
4706 * to the final 'target' values and we'll do this again to get the
4707 * optimal watermarks. For gen9+ platforms, the values we program here
4708 * will be the final target values which will get automatically latched
4709 * at vblank time; no further programming will be necessary.
4710 *
4711 * If a platform hasn't been transitioned to atomic watermarks yet,
4712 * we'll continue to update watermarks the old way, if flags tell
4713 * us to.
4714 */
4715 if (dev_priv->display.initial_watermarks != NULL)
4716 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004717 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004718 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004719}
4720
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004721static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004722{
4723 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004725 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004726 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004727
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004728 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004729
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004730 drm_for_each_plane_mask(p, dev, plane_mask)
4731 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004732
Daniel Vetterf99d7062014-06-19 16:01:59 +02004733 /*
4734 * FIXME: Once we grow proper nuclear flip support out of this we need
4735 * to compute the mask of flip planes precisely. For the time being
4736 * consider this a flip to a NULL plane.
4737 */
4738 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004739}
4740
Jesse Barnesf67a5592011-01-05 10:31:48 -08004741static void ironlake_crtc_enable(struct drm_crtc *crtc)
4742{
4743 struct drm_device *dev = crtc->dev;
4744 struct drm_i915_private *dev_priv = dev->dev_private;
4745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004746 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004747 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004748
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004749 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004750 return;
4751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004752 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004753 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4754
4755 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004756 intel_prepare_shared_dpll(intel_crtc);
4757
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004758 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304759 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004760
4761 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004762 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004763
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004765 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004766 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004767 }
4768
4769 ironlake_set_pipeconf(crtc);
4770
Jesse Barnesf67a5592011-01-05 10:31:48 -08004771 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004772
Daniel Vettera72e4c92014-09-30 10:56:47 +02004773 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004774
Daniel Vetterf6736a12013-06-05 13:34:30 +02004775 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004776 if (encoder->pre_enable)
4777 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004780 /* Note: FDI PLL enabling _must_ be done before we enable the
4781 * cpu pipes, hence this is separate from all the other fdi/pch
4782 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004783 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004784 } else {
4785 assert_fdi_tx_disabled(dev_priv, pipe);
4786 assert_fdi_rx_disabled(dev_priv, pipe);
4787 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004788
Jesse Barnesb074cec2013-04-25 12:55:02 -07004789 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004790
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004791 /*
4792 * On ILK+ LUT must be loaded before the pipe is running but with
4793 * clocks enabled
4794 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004795 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004796
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004797 if (dev_priv->display.initial_watermarks != NULL)
4798 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004799 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004801 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004802 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004803
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004804 assert_vblank_disabled(crtc);
4805 drm_crtc_vblank_on(crtc);
4806
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004807 for_each_encoder_on_crtc(dev, crtc, encoder)
4808 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004809
4810 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004811 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004812
4813 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4814 if (intel_crtc->config->has_pch_encoder)
4815 intel_wait_for_vblank(dev, pipe);
4816 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004817}
4818
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004819/* IPS only exists on ULT machines and is tied to pipe A. */
4820static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4821{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004822 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004823}
4824
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004825static void haswell_crtc_enable(struct drm_crtc *crtc)
4826{
4827 struct drm_device *dev = crtc->dev;
4828 struct drm_i915_private *dev_priv = dev->dev_private;
4829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4830 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004831 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004832 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004833 struct intel_crtc_state *pipe_config =
4834 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004835
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004836 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004837 return;
4838
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004839 if (intel_crtc->config->has_pch_encoder)
4840 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4841 false);
4842
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004843 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004844 intel_enable_shared_dpll(intel_crtc);
4845
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004846 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304847 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004848
Jani Nikula4d1de972016-03-18 17:05:42 +02004849 if (!intel_crtc->config->has_dsi_encoder)
4850 intel_set_pipe_timings(intel_crtc);
4851
Jani Nikulabc58be62016-03-18 17:05:39 +02004852 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004853
Jani Nikula4d1de972016-03-18 17:05:42 +02004854 if (cpu_transcoder != TRANSCODER_EDP &&
4855 !transcoder_is_dsi(cpu_transcoder)) {
4856 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004857 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004858 }
4859
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004860 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004861 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004862 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004863 }
4864
Jani Nikula4d1de972016-03-18 17:05:42 +02004865 if (!intel_crtc->config->has_dsi_encoder)
4866 haswell_set_pipeconf(crtc);
4867
Jani Nikula391bf042016-03-18 17:05:40 +02004868 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004869
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004870 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004871
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004872 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004873
Daniel Vetter6b698512015-11-28 11:05:39 +01004874 if (intel_crtc->config->has_pch_encoder)
4875 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4876 else
4877 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4878
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304879 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880 if (encoder->pre_enable)
4881 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304882 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004883
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004884 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004885 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004886
Jani Nikulaa65347b2015-11-27 12:21:46 +02004887 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304888 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004889
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004890 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004891 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004892 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004893 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004894
4895 /*
4896 * On ILK+ LUT must be loaded before the pipe is running but with
4897 * clocks enabled
4898 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004899 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900
Paulo Zanoni1f544382012-10-24 11:32:00 -02004901 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004902 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304903 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004904
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004905 if (dev_priv->display.initial_watermarks != NULL)
4906 dev_priv->display.initial_watermarks(pipe_config);
4907 else
4908 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004909
4910 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4911 if (!intel_crtc->config->has_dsi_encoder)
4912 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004913
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004914 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004915 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004916
Jani Nikulaa65347b2015-11-27 12:21:46 +02004917 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004918 intel_ddi_set_vc_payload_alloc(crtc, true);
4919
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004920 assert_vblank_disabled(crtc);
4921 drm_crtc_vblank_on(crtc);
4922
Jani Nikula8807e552013-08-30 19:40:32 +03004923 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004924 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004925 intel_opregion_notify_encoder(encoder, true);
4926 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004927
Daniel Vetter6b698512015-11-28 11:05:39 +01004928 if (intel_crtc->config->has_pch_encoder) {
4929 intel_wait_for_vblank(dev, pipe);
4930 intel_wait_for_vblank(dev, pipe);
4931 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004932 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4933 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004934 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004935
Paulo Zanonie4916942013-09-20 16:21:19 -03004936 /* If we change the relative order between pipe/planes enabling, we need
4937 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004938 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4939 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4940 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4941 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4942 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004943}
4944
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004945static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004946{
4947 struct drm_device *dev = crtc->base.dev;
4948 struct drm_i915_private *dev_priv = dev->dev_private;
4949 int pipe = crtc->pipe;
4950
4951 /* To avoid upsetting the power well on haswell only disable the pfit if
4952 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004953 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004954 I915_WRITE(PF_CTL(pipe), 0);
4955 I915_WRITE(PF_WIN_POS(pipe), 0);
4956 I915_WRITE(PF_WIN_SZ(pipe), 0);
4957 }
4958}
4959
Jesse Barnes6be4a602010-09-10 10:26:01 -07004960static void ironlake_crtc_disable(struct drm_crtc *crtc)
4961{
4962 struct drm_device *dev = crtc->dev;
4963 struct drm_i915_private *dev_priv = dev->dev_private;
4964 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004965 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004966 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004967
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004968 if (intel_crtc->config->has_pch_encoder)
4969 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4970
Daniel Vetterea9d7582012-07-10 10:42:52 +02004971 for_each_encoder_on_crtc(dev, crtc, encoder)
4972 encoder->disable(encoder);
4973
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004974 drm_crtc_vblank_off(crtc);
4975 assert_vblank_disabled(crtc);
4976
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004977 /*
4978 * Sometimes spurious CPU pipe underruns happen when the
4979 * pipe is already disabled, but FDI RX/TX is still enabled.
4980 * Happens at least with VGA+HDMI cloning. Suppress them.
4981 */
4982 if (intel_crtc->config->has_pch_encoder)
4983 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4984
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004985 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004986
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004987 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004988
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004989 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004990 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004991 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4992 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004993
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004994 for_each_encoder_on_crtc(dev, crtc, encoder)
4995 if (encoder->post_disable)
4996 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004998 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005000
Daniel Vetterd925c592013-06-05 13:34:04 +02005001 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005002 i915_reg_t reg;
5003 u32 temp;
5004
Daniel Vetterd925c592013-06-05 13:34:04 +02005005 /* disable TRANS_DP_CTL */
5006 reg = TRANS_DP_CTL(pipe);
5007 temp = I915_READ(reg);
5008 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5009 TRANS_DP_PORT_SEL_MASK);
5010 temp |= TRANS_DP_PORT_SEL_NONE;
5011 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005012
Daniel Vetterd925c592013-06-05 13:34:04 +02005013 /* disable DPLL_SEL */
5014 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005015 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005016 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005017 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005018
Daniel Vetterd925c592013-06-05 13:34:04 +02005019 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005020 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005021
5022 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005023}
5024
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025static void haswell_crtc_disable(struct drm_crtc *crtc)
5026{
5027 struct drm_device *dev = crtc->dev;
5028 struct drm_i915_private *dev_priv = dev->dev_private;
5029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5030 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005032
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005033 if (intel_crtc->config->has_pch_encoder)
5034 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5035 false);
5036
Jani Nikula8807e552013-08-30 19:40:32 +03005037 for_each_encoder_on_crtc(dev, crtc, encoder) {
5038 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005040 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005042 drm_crtc_vblank_off(crtc);
5043 assert_vblank_disabled(crtc);
5044
Jani Nikula4d1de972016-03-18 17:05:42 +02005045 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5046 if (!intel_crtc->config->has_dsi_encoder)
5047 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005048
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005049 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005050 intel_ddi_set_vc_payload_alloc(crtc, false);
5051
Jani Nikulaa65347b2015-11-27 12:21:46 +02005052 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305053 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005054
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005055 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005056 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005057 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005058 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059
Jani Nikulaa65347b2015-11-27 12:21:46 +02005060 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305061 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062
Imre Deak97b040a2014-06-25 22:01:50 +03005063 for_each_encoder_on_crtc(dev, crtc, encoder)
5064 if (encoder->post_disable)
5065 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005066
Ville Syrjälä92966a32015-12-08 16:05:48 +02005067 if (intel_crtc->config->has_pch_encoder) {
5068 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005069 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005070 intel_ddi_fdi_disable(crtc);
5071
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005072 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5073 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005074 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005075}
5076
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077static void i9xx_pfit_enable(struct intel_crtc *crtc)
5078{
5079 struct drm_device *dev = crtc->base.dev;
5080 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005081 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005082
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005083 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005084 return;
5085
Daniel Vetterc0b03412013-05-28 12:05:54 +02005086 /*
5087 * The panel fitter should only be adjusted whilst the pipe is disabled,
5088 * according to register description and PRM.
5089 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005090 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5091 assert_pipe_disabled(dev_priv, crtc->pipe);
5092
Jesse Barnesb074cec2013-04-25 12:55:02 -07005093 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5094 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005095
5096 /* Border color in case we don't scale up to the full screen. Black by
5097 * default, change to something else for debugging. */
5098 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005099}
5100
Dave Airlied05410f2014-06-05 13:22:59 +10005101static enum intel_display_power_domain port_to_power_domain(enum port port)
5102{
5103 switch (port) {
5104 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005105 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005106 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005107 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005108 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005109 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005110 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005111 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005112 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005113 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005114 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005115 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005116 return POWER_DOMAIN_PORT_OTHER;
5117 }
5118}
5119
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005120static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5121{
5122 switch (port) {
5123 case PORT_A:
5124 return POWER_DOMAIN_AUX_A;
5125 case PORT_B:
5126 return POWER_DOMAIN_AUX_B;
5127 case PORT_C:
5128 return POWER_DOMAIN_AUX_C;
5129 case PORT_D:
5130 return POWER_DOMAIN_AUX_D;
5131 case PORT_E:
5132 /* FIXME: Check VBT for actual wiring of PORT E */
5133 return POWER_DOMAIN_AUX_D;
5134 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005135 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005136 return POWER_DOMAIN_AUX_A;
5137 }
5138}
5139
Imre Deak319be8a2014-03-04 19:22:57 +02005140enum intel_display_power_domain
5141intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005142{
Imre Deak319be8a2014-03-04 19:22:57 +02005143 struct drm_device *dev = intel_encoder->base.dev;
5144 struct intel_digital_port *intel_dig_port;
5145
5146 switch (intel_encoder->type) {
5147 case INTEL_OUTPUT_UNKNOWN:
5148 /* Only DDI platforms should ever use this output type */
5149 WARN_ON_ONCE(!HAS_DDI(dev));
5150 case INTEL_OUTPUT_DISPLAYPORT:
5151 case INTEL_OUTPUT_HDMI:
5152 case INTEL_OUTPUT_EDP:
5153 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005154 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005155 case INTEL_OUTPUT_DP_MST:
5156 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5157 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005158 case INTEL_OUTPUT_ANALOG:
5159 return POWER_DOMAIN_PORT_CRT;
5160 case INTEL_OUTPUT_DSI:
5161 return POWER_DOMAIN_PORT_DSI;
5162 default:
5163 return POWER_DOMAIN_PORT_OTHER;
5164 }
5165}
5166
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005167enum intel_display_power_domain
5168intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5169{
5170 struct drm_device *dev = intel_encoder->base.dev;
5171 struct intel_digital_port *intel_dig_port;
5172
5173 switch (intel_encoder->type) {
5174 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005175 case INTEL_OUTPUT_HDMI:
5176 /*
5177 * Only DDI platforms should ever use these output types.
5178 * We can get here after the HDMI detect code has already set
5179 * the type of the shared encoder. Since we can't be sure
5180 * what's the status of the given connectors, play safe and
5181 * run the DP detection too.
5182 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005183 WARN_ON_ONCE(!HAS_DDI(dev));
5184 case INTEL_OUTPUT_DISPLAYPORT:
5185 case INTEL_OUTPUT_EDP:
5186 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5187 return port_to_aux_power_domain(intel_dig_port->port);
5188 case INTEL_OUTPUT_DP_MST:
5189 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5190 return port_to_aux_power_domain(intel_dig_port->port);
5191 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005192 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005193 return POWER_DOMAIN_AUX_A;
5194 }
5195}
5196
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005197static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5198 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005199{
5200 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005201 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5203 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005204 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005205 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005206
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005207 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005208 return 0;
5209
Imre Deak77d22dc2014-03-05 16:20:52 +02005210 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5211 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005212 if (crtc_state->pch_pfit.enabled ||
5213 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005214 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5215
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005216 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5217 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5218
Imre Deak319be8a2014-03-04 19:22:57 +02005219 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005220 }
Imre Deak319be8a2014-03-04 19:22:57 +02005221
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005222 if (crtc_state->shared_dpll)
5223 mask |= BIT(POWER_DOMAIN_PLLS);
5224
Imre Deak77d22dc2014-03-05 16:20:52 +02005225 return mask;
5226}
5227
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005228static unsigned long
5229modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5230 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005231{
5232 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5234 enum intel_display_power_domain domain;
5235 unsigned long domains, new_domains, old_domains;
5236
5237 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005238 intel_crtc->enabled_power_domains = new_domains =
5239 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005240
5241 domains = new_domains & ~old_domains;
5242
5243 for_each_power_domain(domain, domains)
5244 intel_display_power_get(dev_priv, domain);
5245
5246 return old_domains & ~new_domains;
5247}
5248
5249static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5250 unsigned long domains)
5251{
5252 enum intel_display_power_domain domain;
5253
5254 for_each_power_domain(domain, domains)
5255 intel_display_power_put(dev_priv, domain);
5256}
5257
Mika Kaholaadafdc62015-08-18 14:36:59 +03005258static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5259{
5260 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5261
5262 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5263 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5264 return max_cdclk_freq;
5265 else if (IS_CHERRYVIEW(dev_priv))
5266 return max_cdclk_freq*95/100;
5267 else if (INTEL_INFO(dev_priv)->gen < 4)
5268 return 2*max_cdclk_freq*90/100;
5269 else
5270 return max_cdclk_freq*90/100;
5271}
5272
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005273static void intel_update_max_cdclk(struct drm_device *dev)
5274{
5275 struct drm_i915_private *dev_priv = dev->dev_private;
5276
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005277 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5279
5280 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5281 dev_priv->max_cdclk_freq = 675000;
5282 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5283 dev_priv->max_cdclk_freq = 540000;
5284 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5285 dev_priv->max_cdclk_freq = 450000;
5286 else
5287 dev_priv->max_cdclk_freq = 337500;
5288 } else if (IS_BROADWELL(dev)) {
5289 /*
5290 * FIXME with extra cooling we can allow
5291 * 540 MHz for ULX and 675 Mhz for ULT.
5292 * How can we know if extra cooling is
5293 * available? PCI ID, VTB, something else?
5294 */
5295 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5296 dev_priv->max_cdclk_freq = 450000;
5297 else if (IS_BDW_ULX(dev))
5298 dev_priv->max_cdclk_freq = 450000;
5299 else if (IS_BDW_ULT(dev))
5300 dev_priv->max_cdclk_freq = 540000;
5301 else
5302 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005303 } else if (IS_CHERRYVIEW(dev)) {
5304 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005305 } else if (IS_VALLEYVIEW(dev)) {
5306 dev_priv->max_cdclk_freq = 400000;
5307 } else {
5308 /* otherwise assume cdclk is fixed */
5309 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5310 }
5311
Mika Kaholaadafdc62015-08-18 14:36:59 +03005312 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5313
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005314 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5315 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005316
5317 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5318 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005319}
5320
5321static void intel_update_cdclk(struct drm_device *dev)
5322{
5323 struct drm_i915_private *dev_priv = dev->dev_private;
5324
5325 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5326 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5327 dev_priv->cdclk_freq);
5328
5329 /*
5330 * Program the gmbus_freq based on the cdclk frequency.
5331 * BSpec erroneously claims we should aim for 4MHz, but
5332 * in fact 1MHz is the correct frequency.
5333 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005334 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005335 /*
5336 * Program the gmbus_freq based on the cdclk frequency.
5337 * BSpec erroneously claims we should aim for 4MHz, but
5338 * in fact 1MHz is the correct frequency.
5339 */
5340 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5341 }
5342
5343 if (dev_priv->max_cdclk_freq == 0)
5344 intel_update_max_cdclk(dev);
5345}
5346
Damien Lespiau70d0c572015-06-04 18:21:29 +01005347static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305348{
5349 struct drm_i915_private *dev_priv = dev->dev_private;
5350 uint32_t divider;
5351 uint32_t ratio;
5352 uint32_t current_freq;
5353 int ret;
5354
5355 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5356 switch (frequency) {
5357 case 144000:
5358 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5359 ratio = BXT_DE_PLL_RATIO(60);
5360 break;
5361 case 288000:
5362 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5363 ratio = BXT_DE_PLL_RATIO(60);
5364 break;
5365 case 384000:
5366 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5367 ratio = BXT_DE_PLL_RATIO(60);
5368 break;
5369 case 576000:
5370 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5371 ratio = BXT_DE_PLL_RATIO(60);
5372 break;
5373 case 624000:
5374 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5375 ratio = BXT_DE_PLL_RATIO(65);
5376 break;
5377 case 19200:
5378 /*
5379 * Bypass frequency with DE PLL disabled. Init ratio, divider
5380 * to suppress GCC warning.
5381 */
5382 ratio = 0;
5383 divider = 0;
5384 break;
5385 default:
5386 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5387
5388 return;
5389 }
5390
5391 mutex_lock(&dev_priv->rps.hw_lock);
5392 /* Inform power controller of upcoming frequency change */
5393 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5394 0x80000000);
5395 mutex_unlock(&dev_priv->rps.hw_lock);
5396
5397 if (ret) {
5398 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5399 ret, frequency);
5400 return;
5401 }
5402
5403 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5404 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5405 current_freq = current_freq * 500 + 1000;
5406
5407 /*
5408 * DE PLL has to be disabled when
5409 * - setting to 19.2MHz (bypass, PLL isn't used)
5410 * - before setting to 624MHz (PLL needs toggling)
5411 * - before setting to any frequency from 624MHz (PLL needs toggling)
5412 */
5413 if (frequency == 19200 || frequency == 624000 ||
5414 current_freq == 624000) {
5415 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5418 1))
5419 DRM_ERROR("timout waiting for DE PLL unlock\n");
5420 }
5421
5422 if (frequency != 19200) {
5423 uint32_t val;
5424
5425 val = I915_READ(BXT_DE_PLL_CTL);
5426 val &= ~BXT_DE_PLL_RATIO_MASK;
5427 val |= ratio;
5428 I915_WRITE(BXT_DE_PLL_CTL, val);
5429
5430 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5431 /* Timeout 200us */
5432 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5433 DRM_ERROR("timeout waiting for DE PLL lock\n");
5434
5435 val = I915_READ(CDCLK_CTL);
5436 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5437 val |= divider;
5438 /*
5439 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5440 * enable otherwise.
5441 */
5442 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5443 if (frequency >= 500000)
5444 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5445
5446 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5447 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5448 val |= (frequency - 1000) / 500;
5449 I915_WRITE(CDCLK_CTL, val);
5450 }
5451
5452 mutex_lock(&dev_priv->rps.hw_lock);
5453 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5454 DIV_ROUND_UP(frequency, 25000));
5455 mutex_unlock(&dev_priv->rps.hw_lock);
5456
5457 if (ret) {
5458 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5459 ret, frequency);
5460 return;
5461 }
5462
Damien Lespiaua47871b2015-06-04 18:21:34 +01005463 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305464}
5465
5466void broxton_init_cdclk(struct drm_device *dev)
5467{
5468 struct drm_i915_private *dev_priv = dev->dev_private;
5469 uint32_t val;
5470
5471 /*
5472 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5473 * or else the reset will hang because there is no PCH to respond.
5474 * Move the handshake programming to initialization sequence.
5475 * Previously was left up to BIOS.
5476 */
5477 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5478 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5479 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5480
5481 /* Enable PG1 for cdclk */
5482 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5483
5484 /* check if cd clock is enabled */
5485 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5486 DRM_DEBUG_KMS("Display already initialized\n");
5487 return;
5488 }
5489
5490 /*
5491 * FIXME:
5492 * - The initial CDCLK needs to be read from VBT.
5493 * Need to make this change after VBT has changes for BXT.
5494 * - check if setting the max (or any) cdclk freq is really necessary
5495 * here, it belongs to modeset time
5496 */
5497 broxton_set_cdclk(dev, 624000);
5498
5499 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005500 POSTING_READ(DBUF_CTL);
5501
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305502 udelay(10);
5503
5504 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5505 DRM_ERROR("DBuf power enable timeout!\n");
5506}
5507
5508void broxton_uninit_cdclk(struct drm_device *dev)
5509{
5510 struct drm_i915_private *dev_priv = dev->dev_private;
5511
5512 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005513 POSTING_READ(DBUF_CTL);
5514
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305515 udelay(10);
5516
5517 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5518 DRM_ERROR("DBuf power disable timeout!\n");
5519
5520 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5521 broxton_set_cdclk(dev, 19200);
5522
5523 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5524}
5525
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005526static const struct skl_cdclk_entry {
5527 unsigned int freq;
5528 unsigned int vco;
5529} skl_cdclk_frequencies[] = {
5530 { .freq = 308570, .vco = 8640 },
5531 { .freq = 337500, .vco = 8100 },
5532 { .freq = 432000, .vco = 8640 },
5533 { .freq = 450000, .vco = 8100 },
5534 { .freq = 540000, .vco = 8100 },
5535 { .freq = 617140, .vco = 8640 },
5536 { .freq = 675000, .vco = 8100 },
5537};
5538
5539static unsigned int skl_cdclk_decimal(unsigned int freq)
5540{
5541 return (freq - 1000) / 500;
5542}
5543
5544static unsigned int skl_cdclk_get_vco(unsigned int freq)
5545{
5546 unsigned int i;
5547
5548 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5549 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5550
5551 if (e->freq == freq)
5552 return e->vco;
5553 }
5554
5555 return 8100;
5556}
5557
5558static void
5559skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5560{
5561 unsigned int min_freq;
5562 u32 val;
5563
5564 /* select the minimum CDCLK before enabling DPLL 0 */
5565 val = I915_READ(CDCLK_CTL);
5566 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5567 val |= CDCLK_FREQ_337_308;
5568
5569 if (required_vco == 8640)
5570 min_freq = 308570;
5571 else
5572 min_freq = 337500;
5573
5574 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5575
5576 I915_WRITE(CDCLK_CTL, val);
5577 POSTING_READ(CDCLK_CTL);
5578
5579 /*
5580 * We always enable DPLL0 with the lowest link rate possible, but still
5581 * taking into account the VCO required to operate the eDP panel at the
5582 * desired frequency. The usual DP link rates operate with a VCO of
5583 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5584 * The modeset code is responsible for the selection of the exact link
5585 * rate later on, with the constraint of choosing a frequency that
5586 * works with required_vco.
5587 */
5588 val = I915_READ(DPLL_CTRL1);
5589
5590 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5591 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5592 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5593 if (required_vco == 8640)
5594 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5595 SKL_DPLL0);
5596 else
5597 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5598 SKL_DPLL0);
5599
5600 I915_WRITE(DPLL_CTRL1, val);
5601 POSTING_READ(DPLL_CTRL1);
5602
5603 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5604
5605 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5606 DRM_ERROR("DPLL0 not locked\n");
5607}
5608
5609static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5610{
5611 int ret;
5612 u32 val;
5613
5614 /* inform PCU we want to change CDCLK */
5615 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5616 mutex_lock(&dev_priv->rps.hw_lock);
5617 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5618 mutex_unlock(&dev_priv->rps.hw_lock);
5619
5620 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5621}
5622
5623static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5624{
5625 unsigned int i;
5626
5627 for (i = 0; i < 15; i++) {
5628 if (skl_cdclk_pcu_ready(dev_priv))
5629 return true;
5630 udelay(10);
5631 }
5632
5633 return false;
5634}
5635
5636static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5637{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005638 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005639 u32 freq_select, pcu_ack;
5640
5641 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5642
5643 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5644 DRM_ERROR("failed to inform PCU about cdclk change\n");
5645 return;
5646 }
5647
5648 /* set CDCLK_CTL */
5649 switch(freq) {
5650 case 450000:
5651 case 432000:
5652 freq_select = CDCLK_FREQ_450_432;
5653 pcu_ack = 1;
5654 break;
5655 case 540000:
5656 freq_select = CDCLK_FREQ_540;
5657 pcu_ack = 2;
5658 break;
5659 case 308570:
5660 case 337500:
5661 default:
5662 freq_select = CDCLK_FREQ_337_308;
5663 pcu_ack = 0;
5664 break;
5665 case 617140:
5666 case 675000:
5667 freq_select = CDCLK_FREQ_675_617;
5668 pcu_ack = 3;
5669 break;
5670 }
5671
5672 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5673 POSTING_READ(CDCLK_CTL);
5674
5675 /* inform PCU of the change */
5676 mutex_lock(&dev_priv->rps.hw_lock);
5677 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5678 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005679
5680 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005681}
5682
5683void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5684{
5685 /* disable DBUF power */
5686 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5687 POSTING_READ(DBUF_CTL);
5688
5689 udelay(10);
5690
5691 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5692 DRM_ERROR("DBuf power disable timeout\n");
5693
Imre Deakab96c1ee2015-11-04 19:24:18 +02005694 /* disable DPLL0 */
5695 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5696 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5697 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005698}
5699
5700void skl_init_cdclk(struct drm_i915_private *dev_priv)
5701{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005702 unsigned int required_vco;
5703
Gary Wang39d9b852015-08-28 16:40:34 +08005704 /* DPLL0 not enabled (happens on early BIOS versions) */
5705 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5706 /* enable DPLL0 */
5707 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5708 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005709 }
5710
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005711 /* set CDCLK to the frequency the BIOS chose */
5712 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5713
5714 /* enable DBUF power */
5715 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5716 POSTING_READ(DBUF_CTL);
5717
5718 udelay(10);
5719
5720 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5721 DRM_ERROR("DBuf power enable timeout\n");
5722}
5723
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305724int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5725{
5726 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5727 uint32_t cdctl = I915_READ(CDCLK_CTL);
5728 int freq = dev_priv->skl_boot_cdclk;
5729
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305730 /*
5731 * check if the pre-os intialized the display
5732 * There is SWF18 scratchpad register defined which is set by the
5733 * pre-os which can be used by the OS drivers to check the status
5734 */
5735 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5736 goto sanitize;
5737
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305738 /* Is PLL enabled and locked ? */
5739 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5740 goto sanitize;
5741
5742 /* DPLL okay; verify the cdclock
5743 *
5744 * Noticed in some instances that the freq selection is correct but
5745 * decimal part is programmed wrong from BIOS where pre-os does not
5746 * enable display. Verify the same as well.
5747 */
5748 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5749 /* All well; nothing to sanitize */
5750 return false;
5751sanitize:
5752 /*
5753 * As of now initialize with max cdclk till
5754 * we get dynamic cdclk support
5755 * */
5756 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5757 skl_init_cdclk(dev_priv);
5758
5759 /* we did have to sanitize */
5760 return true;
5761}
5762
Jesse Barnes30a970c2013-11-04 13:48:12 -08005763/* Adjust CDclk dividers to allow high res or save power if possible */
5764static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5765{
5766 struct drm_i915_private *dev_priv = dev->dev_private;
5767 u32 val, cmd;
5768
Vandana Kannan164dfd22014-11-24 13:37:41 +05305769 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5770 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005771
Ville Syrjälädfcab172014-06-13 13:37:47 +03005772 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005774 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005775 cmd = 1;
5776 else
5777 cmd = 0;
5778
5779 mutex_lock(&dev_priv->rps.hw_lock);
5780 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5781 val &= ~DSPFREQGUAR_MASK;
5782 val |= (cmd << DSPFREQGUAR_SHIFT);
5783 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5784 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5785 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5786 50)) {
5787 DRM_ERROR("timed out waiting for CDclk change\n");
5788 }
5789 mutex_unlock(&dev_priv->rps.hw_lock);
5790
Ville Syrjälä54433e92015-05-26 20:42:31 +03005791 mutex_lock(&dev_priv->sb_lock);
5792
Ville Syrjälädfcab172014-06-13 13:37:47 +03005793 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005794 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005796 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005797
Jesse Barnes30a970c2013-11-04 13:48:12 -08005798 /* adjust cdclk divider */
5799 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005800 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005801 val |= divider;
5802 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005803
5804 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005805 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005806 50))
5807 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005808 }
5809
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810 /* adjust self-refresh exit latency value */
5811 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5812 val &= ~0x7f;
5813
5814 /*
5815 * For high bandwidth configs, we set a higher latency in the bunit
5816 * so that the core display fetch happens in time to avoid underruns.
5817 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005818 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819 val |= 4500 / 250; /* 4.5 usec */
5820 else
5821 val |= 3000 / 250; /* 3.0 usec */
5822 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005823
Ville Syrjäläa5805162015-05-26 20:42:30 +03005824 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825
Ville Syrjäläb6283052015-06-03 15:45:07 +03005826 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005827}
5828
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005829static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5830{
5831 struct drm_i915_private *dev_priv = dev->dev_private;
5832 u32 val, cmd;
5833
Vandana Kannan164dfd22014-11-24 13:37:41 +05305834 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5835 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005836
5837 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005838 case 333333:
5839 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005841 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005842 break;
5843 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005844 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005845 return;
5846 }
5847
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005848 /*
5849 * Specs are full of misinformation, but testing on actual
5850 * hardware has shown that we just need to write the desired
5851 * CCK divider into the Punit register.
5852 */
5853 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5854
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005855 mutex_lock(&dev_priv->rps.hw_lock);
5856 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5857 val &= ~DSPFREQGUAR_MASK_CHV;
5858 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5859 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5860 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5861 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5862 50)) {
5863 DRM_ERROR("timed out waiting for CDclk change\n");
5864 }
5865 mutex_unlock(&dev_priv->rps.hw_lock);
5866
Ville Syrjäläb6283052015-06-03 15:45:07 +03005867 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005868}
5869
Jesse Barnes30a970c2013-11-04 13:48:12 -08005870static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5871 int max_pixclk)
5872{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005873 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005874 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005875
Jesse Barnes30a970c2013-11-04 13:48:12 -08005876 /*
5877 * Really only a few cases to deal with, as only 4 CDclks are supported:
5878 * 200MHz
5879 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005880 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005881 * 400MHz (VLV only)
5882 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5883 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005884 *
5885 * We seem to get an unstable or solid color picture at 200MHz.
5886 * Not sure what's wrong. For now use 200MHz only when all pipes
5887 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005889 if (!IS_CHERRYVIEW(dev_priv) &&
5890 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005891 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005892 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005893 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005894 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005895 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005896 else
5897 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005898}
5899
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305900static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5901 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005902{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305903 /*
5904 * FIXME:
5905 * - remove the guardband, it's not needed on BXT
5906 * - set 19.2MHz bypass frequency if there are no active pipes
5907 */
5908 if (max_pixclk > 576000*9/10)
5909 return 624000;
5910 else if (max_pixclk > 384000*9/10)
5911 return 576000;
5912 else if (max_pixclk > 288000*9/10)
5913 return 384000;
5914 else if (max_pixclk > 144000*9/10)
5915 return 288000;
5916 else
5917 return 144000;
5918}
5919
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005920/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005921static int intel_mode_max_pixclk(struct drm_device *dev,
5922 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005923{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005924 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5925 struct drm_i915_private *dev_priv = dev->dev_private;
5926 struct drm_crtc *crtc;
5927 struct drm_crtc_state *crtc_state;
5928 unsigned max_pixclk = 0, i;
5929 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005930
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005931 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5932 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005933
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005934 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5935 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005936
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005937 if (crtc_state->enable)
5938 pixclk = crtc_state->adjusted_mode.crtc_clock;
5939
5940 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005941 }
5942
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005943 for_each_pipe(dev_priv, pipe)
5944 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5945
Jesse Barnes30a970c2013-11-04 13:48:12 -08005946 return max_pixclk;
5947}
5948
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005950{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005951 struct drm_device *dev = state->dev;
5952 struct drm_i915_private *dev_priv = dev->dev_private;
5953 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005954 struct intel_atomic_state *intel_state =
5955 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005956
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005957 if (max_pixclk < 0)
5958 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005960 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005961 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305962
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005963 if (!intel_state->active_crtcs)
5964 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5965
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005966 return 0;
5967}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005968
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005969static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5970{
5971 struct drm_device *dev = state->dev;
5972 struct drm_i915_private *dev_priv = dev->dev_private;
5973 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005974 struct intel_atomic_state *intel_state =
5975 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005976
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005977 if (max_pixclk < 0)
5978 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005979
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005980 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005981 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005982
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005983 if (!intel_state->active_crtcs)
5984 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5985
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005986 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005987}
5988
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005989static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5990{
5991 unsigned int credits, default_credits;
5992
5993 if (IS_CHERRYVIEW(dev_priv))
5994 default_credits = PFI_CREDIT(12);
5995 else
5996 default_credits = PFI_CREDIT(8);
5997
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005998 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005999 /* CHV suggested value is 31 or 63 */
6000 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006001 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006002 else
6003 credits = PFI_CREDIT(15);
6004 } else {
6005 credits = default_credits;
6006 }
6007
6008 /*
6009 * WA - write default credits before re-programming
6010 * FIXME: should we also set the resend bit here?
6011 */
6012 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6013 default_credits);
6014
6015 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6016 credits | PFI_CREDIT_RESEND);
6017
6018 /*
6019 * FIXME is this guaranteed to clear
6020 * immediately or should we poll for it?
6021 */
6022 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6023}
6024
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006025static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006027 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006029 struct intel_atomic_state *old_intel_state =
6030 to_intel_atomic_state(old_state);
6031 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006032
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006033 /*
6034 * FIXME: We can end up here with all power domains off, yet
6035 * with a CDCLK frequency other than the minimum. To account
6036 * for this take the PIPE-A power domain, which covers the HW
6037 * blocks needed for the following programming. This can be
6038 * removed once it's guaranteed that we get here either with
6039 * the minimum CDCLK set, or the required power domains
6040 * enabled.
6041 */
6042 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006043
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006044 if (IS_CHERRYVIEW(dev))
6045 cherryview_set_cdclk(dev, req_cdclk);
6046 else
6047 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006048
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006049 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006050
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006051 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006052}
6053
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054static void valleyview_crtc_enable(struct drm_crtc *crtc)
6055{
6056 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006057 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6059 struct intel_encoder *encoder;
6060 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006062 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006063 return;
6064
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006065 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306066 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006067
6068 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006069 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006070
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006071 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6072 struct drm_i915_private *dev_priv = dev->dev_private;
6073
6074 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6075 I915_WRITE(CHV_CANVAS(pipe), 0);
6076 }
6077
Daniel Vetter5b18e572014-04-24 23:55:06 +02006078 i9xx_set_pipeconf(intel_crtc);
6079
Jesse Barnes89b667f2013-04-18 14:51:36 -07006080 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006081
Daniel Vettera72e4c92014-09-30 10:56:47 +02006082 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006083
Jesse Barnes89b667f2013-04-18 14:51:36 -07006084 for_each_encoder_on_crtc(dev, crtc, encoder)
6085 if (encoder->pre_pll_enable)
6086 encoder->pre_pll_enable(encoder);
6087
Jani Nikulaa65347b2015-11-27 12:21:46 +02006088 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006089 if (IS_CHERRYVIEW(dev)) {
6090 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006091 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006092 } else {
6093 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006094 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006095 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006096 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006097
6098 for_each_encoder_on_crtc(dev, crtc, encoder)
6099 if (encoder->pre_enable)
6100 encoder->pre_enable(encoder);
6101
Jesse Barnes2dd24552013-04-25 12:55:01 -07006102 i9xx_pfit_enable(intel_crtc);
6103
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006104 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006105
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006106 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006107 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006108
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006109 assert_vblank_disabled(crtc);
6110 drm_crtc_vblank_on(crtc);
6111
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006112 for_each_encoder_on_crtc(dev, crtc, encoder)
6113 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006114}
6115
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006116static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6117{
6118 struct drm_device *dev = crtc->base.dev;
6119 struct drm_i915_private *dev_priv = dev->dev_private;
6120
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006121 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6122 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006123}
6124
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006125static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006126{
6127 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006128 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006130 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006131 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006132
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006133 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006134 return;
6135
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006136 i9xx_set_pll_dividers(intel_crtc);
6137
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006138 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306139 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006140
6141 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006142 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006143
Daniel Vetter5b18e572014-04-24 23:55:06 +02006144 i9xx_set_pipeconf(intel_crtc);
6145
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006146 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006147
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006148 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006149 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006150
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006151 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006152 if (encoder->pre_enable)
6153 encoder->pre_enable(encoder);
6154
Daniel Vetterf6736a12013-06-05 13:34:30 +02006155 i9xx_enable_pll(intel_crtc);
6156
Jesse Barnes2dd24552013-04-25 12:55:01 -07006157 i9xx_pfit_enable(intel_crtc);
6158
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006159 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006160
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006161 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006162 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006163
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006164 assert_vblank_disabled(crtc);
6165 drm_crtc_vblank_on(crtc);
6166
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006167 for_each_encoder_on_crtc(dev, crtc, encoder)
6168 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006169}
6170
Daniel Vetter87476d62013-04-11 16:29:06 +02006171static void i9xx_pfit_disable(struct intel_crtc *crtc)
6172{
6173 struct drm_device *dev = crtc->base.dev;
6174 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006175
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006176 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006177 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006178
6179 assert_pipe_disabled(dev_priv, crtc->pipe);
6180
Daniel Vetter328d8e82013-05-08 10:36:31 +02006181 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6182 I915_READ(PFIT_CONTROL));
6183 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006184}
6185
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006186static void i9xx_crtc_disable(struct drm_crtc *crtc)
6187{
6188 struct drm_device *dev = crtc->dev;
6189 struct drm_i915_private *dev_priv = dev->dev_private;
6190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006191 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006192 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006193
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006194 /*
6195 * On gen2 planes are double buffered but the pipe isn't, so we must
6196 * wait for planes to fully turn off before disabling the pipe.
6197 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006198 if (IS_GEN2(dev))
6199 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006200
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006201 for_each_encoder_on_crtc(dev, crtc, encoder)
6202 encoder->disable(encoder);
6203
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006204 drm_crtc_vblank_off(crtc);
6205 assert_vblank_disabled(crtc);
6206
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006207 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006208
Daniel Vetter87476d62013-04-11 16:29:06 +02006209 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006210
Jesse Barnes89b667f2013-04-18 14:51:36 -07006211 for_each_encoder_on_crtc(dev, crtc, encoder)
6212 if (encoder->post_disable)
6213 encoder->post_disable(encoder);
6214
Jani Nikulaa65347b2015-11-27 12:21:46 +02006215 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006216 if (IS_CHERRYVIEW(dev))
6217 chv_disable_pll(dev_priv, pipe);
6218 else if (IS_VALLEYVIEW(dev))
6219 vlv_disable_pll(dev_priv, pipe);
6220 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006221 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006222 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006223
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006224 for_each_encoder_on_crtc(dev, crtc, encoder)
6225 if (encoder->post_pll_disable)
6226 encoder->post_pll_disable(encoder);
6227
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006228 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006229 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006230}
6231
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006232static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006233{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006234 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006236 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006237 enum intel_display_power_domain domain;
6238 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006239
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006240 if (!intel_crtc->active)
6241 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006242
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006243 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006244 WARN_ON(intel_crtc->unpin_work);
6245
Ville Syrjälä2622a082016-03-09 19:07:26 +02006246 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006247
6248 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6249 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006250 }
6251
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006252 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006253
6254 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6255 crtc->base.id);
6256
6257 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6258 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006259 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006260 crtc->enabled = false;
6261 crtc->state->connector_mask = 0;
6262 crtc->state->encoder_mask = 0;
6263
6264 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6265 encoder->base.crtc = NULL;
6266
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006267 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006268 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006269 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006270
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006271 domains = intel_crtc->enabled_power_domains;
6272 for_each_power_domain(domain, domains)
6273 intel_display_power_put(dev_priv, domain);
6274 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006275
6276 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6277 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006278}
6279
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006280/*
6281 * turn all crtc's off, but do not adjust state
6282 * This has to be paired with a call to intel_modeset_setup_hw_state.
6283 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006284int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006285{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006286 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006287 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006288 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006289
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006290 state = drm_atomic_helper_suspend(dev);
6291 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006292 if (ret)
6293 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006294 else
6295 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006296 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006297}
6298
Chris Wilsonea5b2132010-08-04 13:50:23 +01006299void intel_encoder_destroy(struct drm_encoder *encoder)
6300{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006301 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006302
Chris Wilsonea5b2132010-08-04 13:50:23 +01006303 drm_encoder_cleanup(encoder);
6304 kfree(intel_encoder);
6305}
6306
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307/* Cross check the actual hw state with our own modeset state tracking (and it's
6308 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006309static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 struct drm_crtc *crtc = connector->base.state->crtc;
6312
6313 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6314 connector->base.base.id,
6315 connector->base.name);
6316
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006317 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006318 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006319 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006320
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 I915_STATE_WARN(!crtc,
6322 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006323
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006324 if (!crtc)
6325 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006327 I915_STATE_WARN(!crtc->state->active,
6328 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006329
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006330 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006331 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006332
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006333 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006334 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006335
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006336 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006337 "attached encoder crtc differs from connector crtc\n");
6338 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006339 I915_STATE_WARN(crtc && crtc->state->active,
6340 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006341 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6342 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006343 }
6344}
6345
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006346int intel_connector_init(struct intel_connector *connector)
6347{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006348 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006349
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006350 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006351 return -ENOMEM;
6352
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006353 return 0;
6354}
6355
6356struct intel_connector *intel_connector_alloc(void)
6357{
6358 struct intel_connector *connector;
6359
6360 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6361 if (!connector)
6362 return NULL;
6363
6364 if (intel_connector_init(connector) < 0) {
6365 kfree(connector);
6366 return NULL;
6367 }
6368
6369 return connector;
6370}
6371
Daniel Vetterf0947c32012-07-02 13:10:34 +02006372/* Simple connector->get_hw_state implementation for encoders that support only
6373 * one connector and no cloning and hence the encoder state determines the state
6374 * of the connector. */
6375bool intel_connector_get_hw_state(struct intel_connector *connector)
6376{
Daniel Vetter24929352012-07-02 20:28:59 +02006377 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006378 struct intel_encoder *encoder = connector->encoder;
6379
6380 return encoder->get_hw_state(encoder, &pipe);
6381}
6382
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006383static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006384{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006385 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6386 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006387
6388 return 0;
6389}
6390
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006391static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006392 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006393{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 struct drm_atomic_state *state = pipe_config->base.state;
6395 struct intel_crtc *other_crtc;
6396 struct intel_crtc_state *other_crtc_state;
6397
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006398 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6399 pipe_name(pipe), pipe_config->fdi_lanes);
6400 if (pipe_config->fdi_lanes > 4) {
6401 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6402 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006404 }
6405
Paulo Zanonibafb6552013-11-02 21:07:44 -07006406 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006407 if (pipe_config->fdi_lanes > 2) {
6408 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6409 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006412 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 }
6414 }
6415
6416 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418
6419 /* Ivybridge 3 pipe is really complicated */
6420 switch (pipe) {
6421 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006424 if (pipe_config->fdi_lanes <= 2)
6425 return 0;
6426
6427 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6428 other_crtc_state =
6429 intel_atomic_get_crtc_state(state, other_crtc);
6430 if (IS_ERR(other_crtc_state))
6431 return PTR_ERR(other_crtc_state);
6432
6433 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6435 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006438 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006439 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006440 if (pipe_config->fdi_lanes > 2) {
6441 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6442 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006444 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445
6446 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6447 other_crtc_state =
6448 intel_atomic_get_crtc_state(state, other_crtc);
6449 if (IS_ERR(other_crtc_state))
6450 return PTR_ERR(other_crtc_state);
6451
6452 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006455 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006456 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006457 default:
6458 BUG();
6459 }
6460}
6461
Daniel Vettere29c22c2013-02-21 00:00:16 +01006462#define RETRY 1
6463static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006464 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006465{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006466 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006467 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006468 int lane, link_bw, fdi_dotclock, ret;
6469 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006470
Daniel Vettere29c22c2013-02-21 00:00:16 +01006471retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006472 /* FDI is a binary signal running at ~2.7GHz, encoding
6473 * each output octet as 10 bits. The actual frequency
6474 * is stored as a divider into a 100MHz clock, and the
6475 * mode pixel clock is stored in units of 1KHz.
6476 * Hence the bw of each lane in terms of the mode signal
6477 * is:
6478 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006479 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006480
Damien Lespiau241bfc32013-09-25 16:45:37 +01006481 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006482
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006483 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006484 pipe_config->pipe_bpp);
6485
6486 pipe_config->fdi_lanes = lane;
6487
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006488 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006489 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006491 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006493 pipe_config->pipe_bpp -= 2*3;
6494 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6495 pipe_config->pipe_bpp);
6496 needs_recompute = true;
6497 pipe_config->bw_constrained = true;
6498
6499 goto retry;
6500 }
6501
6502 if (needs_recompute)
6503 return RETRY;
6504
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006505 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006506}
6507
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006508static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6509 struct intel_crtc_state *pipe_config)
6510{
6511 if (pipe_config->pipe_bpp > 24)
6512 return false;
6513
6514 /* HSW can handle pixel rate up to cdclk? */
6515 if (IS_HASWELL(dev_priv->dev))
6516 return true;
6517
6518 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006519 * We compare against max which means we must take
6520 * the increased cdclk requirement into account when
6521 * calculating the new cdclk.
6522 *
6523 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006524 */
6525 return ilk_pipe_pixel_rate(pipe_config) <=
6526 dev_priv->max_cdclk_freq * 95 / 100;
6527}
6528
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006529static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006530 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006531{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006532 struct drm_device *dev = crtc->base.dev;
6533 struct drm_i915_private *dev_priv = dev->dev_private;
6534
Jani Nikulad330a952014-01-21 11:24:25 +02006535 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006536 hsw_crtc_supports_ips(crtc) &&
6537 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006538}
6539
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006540static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6541{
6542 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6543
6544 /* GDG double wide on either pipe, otherwise pipe A only */
6545 return INTEL_INFO(dev_priv)->gen < 4 &&
6546 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6547}
6548
Daniel Vettera43f6e02013-06-07 23:10:32 +02006549static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006550 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006551{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006552 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006553 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006554 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006555
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006556 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006557 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006558 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006559
6560 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006561 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006562 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006563 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006564 if (intel_crtc_supports_double_wide(crtc) &&
6565 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006566 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006567 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006568 }
6569
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006570 if (adjusted_mode->crtc_clock > clock_limit) {
6571 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6572 adjusted_mode->crtc_clock, clock_limit,
6573 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006574 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006575 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006576 }
Chris Wilson89749352010-09-12 18:25:19 +01006577
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006578 /*
6579 * Pipe horizontal size must be even in:
6580 * - DVO ganged mode
6581 * - LVDS dual channel mode
6582 * - Double wide pipe
6583 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006584 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006585 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6586 pipe_config->pipe_src_w &= ~1;
6587
Damien Lespiau8693a822013-05-03 18:48:11 +01006588 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6589 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006590 */
6591 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006592 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006593 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006594
Damien Lespiauf5adf942013-06-24 18:29:34 +01006595 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006596 hsw_compute_ips_config(crtc, pipe_config);
6597
Daniel Vetter877d48d2013-04-19 11:24:43 +02006598 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006599 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006600
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006601 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006602}
6603
Ville Syrjälä1652d192015-03-31 14:12:01 +03006604static int skylake_get_display_clock_speed(struct drm_device *dev)
6605{
6606 struct drm_i915_private *dev_priv = to_i915(dev);
6607 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6608 uint32_t cdctl = I915_READ(CDCLK_CTL);
6609 uint32_t linkrate;
6610
Damien Lespiau414355a2015-06-04 18:21:31 +01006611 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006612 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006613
6614 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6615 return 540000;
6616
6617 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006618 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006619
Damien Lespiau71cd8422015-04-30 16:39:17 +01006620 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6621 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006622 /* vco 8640 */
6623 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6624 case CDCLK_FREQ_450_432:
6625 return 432000;
6626 case CDCLK_FREQ_337_308:
6627 return 308570;
6628 case CDCLK_FREQ_675_617:
6629 return 617140;
6630 default:
6631 WARN(1, "Unknown cd freq selection\n");
6632 }
6633 } else {
6634 /* vco 8100 */
6635 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6636 case CDCLK_FREQ_450_432:
6637 return 450000;
6638 case CDCLK_FREQ_337_308:
6639 return 337500;
6640 case CDCLK_FREQ_675_617:
6641 return 675000;
6642 default:
6643 WARN(1, "Unknown cd freq selection\n");
6644 }
6645 }
6646
6647 /* error case, do as if DPLL0 isn't enabled */
6648 return 24000;
6649}
6650
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006651static int broxton_get_display_clock_speed(struct drm_device *dev)
6652{
6653 struct drm_i915_private *dev_priv = to_i915(dev);
6654 uint32_t cdctl = I915_READ(CDCLK_CTL);
6655 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6656 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6657 int cdclk;
6658
6659 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6660 return 19200;
6661
6662 cdclk = 19200 * pll_ratio / 2;
6663
6664 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6665 case BXT_CDCLK_CD2X_DIV_SEL_1:
6666 return cdclk; /* 576MHz or 624MHz */
6667 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6668 return cdclk * 2 / 3; /* 384MHz */
6669 case BXT_CDCLK_CD2X_DIV_SEL_2:
6670 return cdclk / 2; /* 288MHz */
6671 case BXT_CDCLK_CD2X_DIV_SEL_4:
6672 return cdclk / 4; /* 144MHz */
6673 }
6674
6675 /* error case, do as if DE PLL isn't enabled */
6676 return 19200;
6677}
6678
Ville Syrjälä1652d192015-03-31 14:12:01 +03006679static int broadwell_get_display_clock_speed(struct drm_device *dev)
6680{
6681 struct drm_i915_private *dev_priv = dev->dev_private;
6682 uint32_t lcpll = I915_READ(LCPLL_CTL);
6683 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6684
6685 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6686 return 800000;
6687 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6688 return 450000;
6689 else if (freq == LCPLL_CLK_FREQ_450)
6690 return 450000;
6691 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6692 return 540000;
6693 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6694 return 337500;
6695 else
6696 return 675000;
6697}
6698
6699static int haswell_get_display_clock_speed(struct drm_device *dev)
6700{
6701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 uint32_t lcpll = I915_READ(LCPLL_CTL);
6703 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6704
6705 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6706 return 800000;
6707 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6708 return 450000;
6709 else if (freq == LCPLL_CLK_FREQ_450)
6710 return 450000;
6711 else if (IS_HSW_ULT(dev))
6712 return 337500;
6713 else
6714 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006715}
6716
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006717static int valleyview_get_display_clock_speed(struct drm_device *dev)
6718{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006719 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6720 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006721}
6722
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006723static int ilk_get_display_clock_speed(struct drm_device *dev)
6724{
6725 return 450000;
6726}
6727
Jesse Barnese70236a2009-09-21 10:42:27 -07006728static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006729{
Jesse Barnese70236a2009-09-21 10:42:27 -07006730 return 400000;
6731}
Jesse Barnes79e53942008-11-07 14:24:08 -08006732
Jesse Barnese70236a2009-09-21 10:42:27 -07006733static int i915_get_display_clock_speed(struct drm_device *dev)
6734{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006735 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006736}
Jesse Barnes79e53942008-11-07 14:24:08 -08006737
Jesse Barnese70236a2009-09-21 10:42:27 -07006738static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6739{
6740 return 200000;
6741}
Jesse Barnes79e53942008-11-07 14:24:08 -08006742
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006743static int pnv_get_display_clock_speed(struct drm_device *dev)
6744{
6745 u16 gcfgc = 0;
6746
6747 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6748
6749 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6750 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006751 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006752 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006753 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006754 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006755 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006756 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6757 return 200000;
6758 default:
6759 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6760 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006761 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006762 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006763 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006764 }
6765}
6766
Jesse Barnese70236a2009-09-21 10:42:27 -07006767static int i915gm_get_display_clock_speed(struct drm_device *dev)
6768{
6769 u16 gcfgc = 0;
6770
6771 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6772
6773 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006775 else {
6776 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6777 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006778 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006779 default:
6780 case GC_DISPLAY_CLOCK_190_200_MHZ:
6781 return 190000;
6782 }
6783 }
6784}
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
Jesse Barnese70236a2009-09-21 10:42:27 -07006786static int i865_get_display_clock_speed(struct drm_device *dev)
6787{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006788 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006789}
6790
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006791static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006792{
6793 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006794
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006795 /*
6796 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6797 * encoding is different :(
6798 * FIXME is this the right way to detect 852GM/852GMV?
6799 */
6800 if (dev->pdev->revision == 0x1)
6801 return 133333;
6802
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006803 pci_bus_read_config_word(dev->pdev->bus,
6804 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6805
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 /* Assume that the hardware is in the high speed state. This
6807 * should be the default.
6808 */
6809 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6810 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006811 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006812 case GC_CLOCK_100_200:
6813 return 200000;
6814 case GC_CLOCK_166_250:
6815 return 250000;
6816 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006817 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006818 case GC_CLOCK_133_266:
6819 case GC_CLOCK_133_266_2:
6820 case GC_CLOCK_166_266:
6821 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006822 }
6823
6824 /* Shouldn't happen */
6825 return 0;
6826}
6827
6828static int i830_get_display_clock_speed(struct drm_device *dev)
6829{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006830 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006831}
6832
Ville Syrjälä34edce22015-05-22 11:22:33 +03006833static unsigned int intel_hpll_vco(struct drm_device *dev)
6834{
6835 struct drm_i915_private *dev_priv = dev->dev_private;
6836 static const unsigned int blb_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 [4] = 6400000,
6842 };
6843 static const unsigned int pnv_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 4800000,
6848 [4] = 2666667,
6849 };
6850 static const unsigned int cl_vco[8] = {
6851 [0] = 3200000,
6852 [1] = 4000000,
6853 [2] = 5333333,
6854 [3] = 6400000,
6855 [4] = 3333333,
6856 [5] = 3566667,
6857 [6] = 4266667,
6858 };
6859 static const unsigned int elk_vco[8] = {
6860 [0] = 3200000,
6861 [1] = 4000000,
6862 [2] = 5333333,
6863 [3] = 4800000,
6864 };
6865 static const unsigned int ctg_vco[8] = {
6866 [0] = 3200000,
6867 [1] = 4000000,
6868 [2] = 5333333,
6869 [3] = 6400000,
6870 [4] = 2666667,
6871 [5] = 4266667,
6872 };
6873 const unsigned int *vco_table;
6874 unsigned int vco;
6875 uint8_t tmp = 0;
6876
6877 /* FIXME other chipsets? */
6878 if (IS_GM45(dev))
6879 vco_table = ctg_vco;
6880 else if (IS_G4X(dev))
6881 vco_table = elk_vco;
6882 else if (IS_CRESTLINE(dev))
6883 vco_table = cl_vco;
6884 else if (IS_PINEVIEW(dev))
6885 vco_table = pnv_vco;
6886 else if (IS_G33(dev))
6887 vco_table = blb_vco;
6888 else
6889 return 0;
6890
6891 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6892
6893 vco = vco_table[tmp & 0x7];
6894 if (vco == 0)
6895 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6896 else
6897 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6898
6899 return vco;
6900}
6901
6902static int gm45_get_display_clock_speed(struct drm_device *dev)
6903{
6904 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6905 uint16_t tmp = 0;
6906
6907 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6908
6909 cdclk_sel = (tmp >> 12) & 0x1;
6910
6911 switch (vco) {
6912 case 2666667:
6913 case 4000000:
6914 case 5333333:
6915 return cdclk_sel ? 333333 : 222222;
6916 case 3200000:
6917 return cdclk_sel ? 320000 : 228571;
6918 default:
6919 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6920 return 222222;
6921 }
6922}
6923
6924static int i965gm_get_display_clock_speed(struct drm_device *dev)
6925{
6926 static const uint8_t div_3200[] = { 16, 10, 8 };
6927 static const uint8_t div_4000[] = { 20, 12, 10 };
6928 static const uint8_t div_5333[] = { 24, 16, 14 };
6929 const uint8_t *div_table;
6930 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6931 uint16_t tmp = 0;
6932
6933 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6934
6935 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6936
6937 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6938 goto fail;
6939
6940 switch (vco) {
6941 case 3200000:
6942 div_table = div_3200;
6943 break;
6944 case 4000000:
6945 div_table = div_4000;
6946 break;
6947 case 5333333:
6948 div_table = div_5333;
6949 break;
6950 default:
6951 goto fail;
6952 }
6953
6954 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6955
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006956fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006957 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6958 return 200000;
6959}
6960
6961static int g33_get_display_clock_speed(struct drm_device *dev)
6962{
6963 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6964 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6965 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6966 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6967 const uint8_t *div_table;
6968 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6969 uint16_t tmp = 0;
6970
6971 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6972
6973 cdclk_sel = (tmp >> 4) & 0x7;
6974
6975 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6976 goto fail;
6977
6978 switch (vco) {
6979 case 3200000:
6980 div_table = div_3200;
6981 break;
6982 case 4000000:
6983 div_table = div_4000;
6984 break;
6985 case 4800000:
6986 div_table = div_4800;
6987 break;
6988 case 5333333:
6989 div_table = div_5333;
6990 break;
6991 default:
6992 goto fail;
6993 }
6994
6995 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6996
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006997fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006998 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6999 return 190476;
7000}
7001
Zhenyu Wang2c072452009-06-05 15:38:42 +08007002static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007003intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007004{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007005 while (*num > DATA_LINK_M_N_MASK ||
7006 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007007 *num >>= 1;
7008 *den >>= 1;
7009 }
7010}
7011
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007012static void compute_m_n(unsigned int m, unsigned int n,
7013 uint32_t *ret_m, uint32_t *ret_n)
7014{
7015 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7016 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7017 intel_reduce_m_n_ratio(ret_m, ret_n);
7018}
7019
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007020void
7021intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7022 int pixel_clock, int link_clock,
7023 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007024{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007025 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007026
7027 compute_m_n(bits_per_pixel * pixel_clock,
7028 link_clock * nlanes * 8,
7029 &m_n->gmch_m, &m_n->gmch_n);
7030
7031 compute_m_n(pixel_clock, link_clock,
7032 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007033}
7034
Chris Wilsona7615032011-01-12 17:04:08 +00007035static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7036{
Jani Nikulad330a952014-01-21 11:24:25 +02007037 if (i915.panel_use_ssc >= 0)
7038 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007039 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007040 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007041}
7042
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007044{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007045 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007046}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007047
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007048static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7049{
7050 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007051}
7052
Daniel Vetterf47709a2013-03-28 10:42:02 +01007053static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007054 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055 intel_clock_t *reduced_clock)
7056{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007057 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007058 u32 fp, fp2 = 0;
7059
7060 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007061 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007062 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007063 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007064 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007065 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007066 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007067 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007068 }
7069
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007070 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007071
Daniel Vetterf47709a2013-03-28 10:42:02 +01007072 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007073 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007074 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007075 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007076 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007077 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007078 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007079 }
7080}
7081
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007082static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7083 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084{
7085 u32 reg_val;
7086
7087 /*
7088 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7089 * and set it to a reasonable value instead.
7090 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007091 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007092 reg_val &= 0xffffff00;
7093 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007094 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007096 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007097 reg_val &= 0x8cffffff;
7098 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007099 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007100
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007101 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007102 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007103 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007104
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007105 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007106 reg_val &= 0x00ffffff;
7107 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007108 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007109}
7110
Daniel Vetterb5518422013-05-03 11:49:48 +02007111static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7112 struct intel_link_m_n *m_n)
7113{
7114 struct drm_device *dev = crtc->base.dev;
7115 struct drm_i915_private *dev_priv = dev->dev_private;
7116 int pipe = crtc->pipe;
7117
Daniel Vettere3b95f12013-05-03 11:49:49 +02007118 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7119 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7120 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7121 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007122}
7123
7124static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007125 struct intel_link_m_n *m_n,
7126 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007127{
7128 struct drm_device *dev = crtc->base.dev;
7129 struct drm_i915_private *dev_priv = dev->dev_private;
7130 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007131 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007132
7133 if (INTEL_INFO(dev)->gen >= 5) {
7134 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7135 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7136 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7137 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007138 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7139 * for gen < 8) and if DRRS is supported (to make sure the
7140 * registers are not unnecessarily accessed).
7141 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307142 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007143 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007144 I915_WRITE(PIPE_DATA_M2(transcoder),
7145 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7146 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7147 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7148 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7149 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007150 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007151 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7152 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7153 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7154 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007155 }
7156}
7157
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307158void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007159{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307160 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7161
7162 if (m_n == M1_N1) {
7163 dp_m_n = &crtc->config->dp_m_n;
7164 dp_m2_n2 = &crtc->config->dp_m2_n2;
7165 } else if (m_n == M2_N2) {
7166
7167 /*
7168 * M2_N2 registers are not supported. Hence m2_n2 divider value
7169 * needs to be programmed into M1_N1.
7170 */
7171 dp_m_n = &crtc->config->dp_m2_n2;
7172 } else {
7173 DRM_ERROR("Unsupported divider value\n");
7174 return;
7175 }
7176
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007177 if (crtc->config->has_pch_encoder)
7178 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007179 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307180 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007181}
7182
Daniel Vetter251ac862015-06-18 10:30:24 +02007183static void vlv_compute_dpll(struct intel_crtc *crtc,
7184 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007185{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007186 u32 dpll, dpll_md;
7187
7188 /*
7189 * Enable DPIO clock input. We should never disable the reference
7190 * clock for pipe B, since VGA hotplug / manual detection depends
7191 * on it.
7192 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007193 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7194 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007195 /* We should never disable this, set it here for state tracking */
7196 if (crtc->pipe == PIPE_B)
7197 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7198 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007199 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007200
Ville Syrjäläd288f652014-10-28 13:20:22 +02007201 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007202 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007203 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007204}
7205
Ville Syrjäläd288f652014-10-28 13:20:22 +02007206static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007207 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007208{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007209 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007210 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007211 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007212 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007213 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007214 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007215
Ville Syrjäläa5805162015-05-26 20:42:30 +03007216 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007217
Ville Syrjäläd288f652014-10-28 13:20:22 +02007218 bestn = pipe_config->dpll.n;
7219 bestm1 = pipe_config->dpll.m1;
7220 bestm2 = pipe_config->dpll.m2;
7221 bestp1 = pipe_config->dpll.p1;
7222 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007223
Jesse Barnes89b667f2013-04-18 14:51:36 -07007224 /* See eDP HDMI DPIO driver vbios notes doc */
7225
7226 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007227 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007228 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007229
7230 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007231 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232
7233 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007234 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007235 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007236 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007237
7238 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007240
7241 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007242 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7243 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7244 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007245 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007246
7247 /*
7248 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7249 * but we don't support that).
7250 * Note: don't use the DAC post divider as it seems unstable.
7251 */
7252 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007253 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007254
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007255 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007257
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007259 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007260 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7261 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007263 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007265 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007266 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007267
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007268 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007269 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272 0x0df40000);
7273 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275 0x0df70000);
7276 } else { /* HDMI or VGA */
7277 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007278 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007279 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 0x0df70000);
7281 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007282 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283 0x0df40000);
7284 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007285
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007288 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7289 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007291 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007292
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007293 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007294 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007295}
7296
Daniel Vetter251ac862015-06-18 10:30:24 +02007297static void chv_compute_dpll(struct intel_crtc *crtc,
7298 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007299{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007300 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7301 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007302 DPLL_VCO_ENABLE;
7303 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007304 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007305
Ville Syrjäläd288f652014-10-28 13:20:22 +02007306 pipe_config->dpll_hw_state.dpll_md =
7307 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007308}
7309
Ville Syrjäläd288f652014-10-28 13:20:22 +02007310static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007311 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007312{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007313 struct drm_device *dev = crtc->base.dev;
7314 struct drm_i915_private *dev_priv = dev->dev_private;
7315 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007316 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007317 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307318 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007319 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307320 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307321 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007322
Ville Syrjäläd288f652014-10-28 13:20:22 +02007323 bestn = pipe_config->dpll.n;
7324 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7325 bestm1 = pipe_config->dpll.m1;
7326 bestm2 = pipe_config->dpll.m2 >> 22;
7327 bestp1 = pipe_config->dpll.p1;
7328 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307329 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307330 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307331 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007332
7333 /*
7334 * Enable Refclk and SSC
7335 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007336 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007337 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007338
Ville Syrjäläa5805162015-05-26 20:42:30 +03007339 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007340
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007341 /* p1 and p2 divider */
7342 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7343 5 << DPIO_CHV_S1_DIV_SHIFT |
7344 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7345 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7346 1 << DPIO_CHV_K_DIV_SHIFT);
7347
7348 /* Feedback post-divider - m2 */
7349 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7350
7351 /* Feedback refclk divider - n and m1 */
7352 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7353 DPIO_CHV_M1_DIV_BY_2 |
7354 1 << DPIO_CHV_N_DIV_SHIFT);
7355
7356 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007357 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007358
7359 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307360 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7361 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7362 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7363 if (bestm2_frac)
7364 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7365 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007366
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307367 /* Program digital lock detect threshold */
7368 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7369 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7370 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7371 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7372 if (!bestm2_frac)
7373 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7374 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7375
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307377 if (vco == 5400000) {
7378 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7379 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7380 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7381 tribuf_calcntr = 0x9;
7382 } else if (vco <= 6200000) {
7383 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7384 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7385 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7386 tribuf_calcntr = 0x9;
7387 } else if (vco <= 6480000) {
7388 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7389 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7390 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7391 tribuf_calcntr = 0x8;
7392 } else {
7393 /* Not supported. Apply the same limits as in the max case */
7394 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7395 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7396 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7397 tribuf_calcntr = 0;
7398 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007399 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7400
Ville Syrjälä968040b2015-03-11 22:52:08 +02007401 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307402 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7403 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7404 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7405
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007406 /* AFC Recal */
7407 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7408 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7409 DPIO_AFC_RECAL);
7410
Ville Syrjäläa5805162015-05-26 20:42:30 +03007411 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007412}
7413
Ville Syrjäläd288f652014-10-28 13:20:22 +02007414/**
7415 * vlv_force_pll_on - forcibly enable just the PLL
7416 * @dev_priv: i915 private structure
7417 * @pipe: pipe PLL to enable
7418 * @dpll: PLL configuration
7419 *
7420 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7421 * in cases where we need the PLL enabled even when @pipe is not going to
7422 * be enabled.
7423 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007424int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7425 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007426{
7427 struct intel_crtc *crtc =
7428 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007429 struct intel_crtc_state *pipe_config;
7430
7431 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7432 if (!pipe_config)
7433 return -ENOMEM;
7434
7435 pipe_config->base.crtc = &crtc->base;
7436 pipe_config->pixel_multiplier = 1;
7437 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007438
7439 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007440 chv_compute_dpll(crtc, pipe_config);
7441 chv_prepare_pll(crtc, pipe_config);
7442 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007443 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007444 vlv_compute_dpll(crtc, pipe_config);
7445 vlv_prepare_pll(crtc, pipe_config);
7446 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007447 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007448
7449 kfree(pipe_config);
7450
7451 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007452}
7453
7454/**
7455 * vlv_force_pll_off - forcibly disable just the PLL
7456 * @dev_priv: i915 private structure
7457 * @pipe: pipe PLL to disable
7458 *
7459 * Disable the PLL for @pipe. To be used in cases where we need
7460 * the PLL enabled even when @pipe is not going to be enabled.
7461 */
7462void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7463{
7464 if (IS_CHERRYVIEW(dev))
7465 chv_disable_pll(to_i915(dev), pipe);
7466 else
7467 vlv_disable_pll(to_i915(dev), pipe);
7468}
7469
Daniel Vetter251ac862015-06-18 10:30:24 +02007470static void i9xx_compute_dpll(struct intel_crtc *crtc,
7471 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007472 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007473{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007474 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007475 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007476 u32 dpll;
7477 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007478 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007479
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007480 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307481
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007482 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7483 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007484
7485 dpll = DPLL_VGA_MODE_DIS;
7486
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007487 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007488 dpll |= DPLLB_MODE_LVDS;
7489 else
7490 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007491
Daniel Vetteref1b4602013-06-01 17:17:04 +02007492 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007493 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007494 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007495 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007496
7497 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007498 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007499
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007500 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007501 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007502
7503 /* compute bitmask from p1 value */
7504 if (IS_PINEVIEW(dev))
7505 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7506 else {
7507 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7508 if (IS_G4X(dev) && reduced_clock)
7509 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7510 }
7511 switch (clock->p2) {
7512 case 5:
7513 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7514 break;
7515 case 7:
7516 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7517 break;
7518 case 10:
7519 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7520 break;
7521 case 14:
7522 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7523 break;
7524 }
7525 if (INTEL_INFO(dev)->gen >= 4)
7526 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7527
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007530 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007531 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7533 else
7534 dpll |= PLL_REF_INPUT_DREFCLK;
7535
7536 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007537 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007538
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007540 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007541 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007542 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543 }
7544}
7545
Daniel Vetter251ac862015-06-18 10:30:24 +02007546static void i8xx_compute_dpll(struct intel_crtc *crtc,
7547 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007548 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007550 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007552 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007553 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007554
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307556
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007557 dpll = DPLL_VGA_MODE_DIS;
7558
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007559 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007560 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7561 } else {
7562 if (clock->p1 == 2)
7563 dpll |= PLL_P1_DIVIDE_BY_TWO;
7564 else
7565 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7566 if (clock->p2 == 4)
7567 dpll |= PLL_P2_DIVIDE_BY_4;
7568 }
7569
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007570 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007571 dpll |= DPLL_DVO_2X_MODE;
7572
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007573 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007574 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007575 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7576 else
7577 dpll |= PLL_REF_INPUT_DREFCLK;
7578
7579 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007580 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007581}
7582
Daniel Vetter8a654f32013-06-01 17:16:22 +02007583static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007584{
7585 struct drm_device *dev = intel_crtc->base.dev;
7586 struct drm_i915_private *dev_priv = dev->dev_private;
7587 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007588 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007589 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007590 uint32_t crtc_vtotal, crtc_vblank_end;
7591 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007592
7593 /* We need to be careful not to changed the adjusted mode, for otherwise
7594 * the hw state checker will get angry at the mismatch. */
7595 crtc_vtotal = adjusted_mode->crtc_vtotal;
7596 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007597
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007598 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007599 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007600 crtc_vtotal -= 1;
7601 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007602
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007603 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007604 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7605 else
7606 vsyncshift = adjusted_mode->crtc_hsync_start -
7607 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007608 if (vsyncshift < 0)
7609 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007610 }
7611
7612 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007613 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007614
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007615 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007616 (adjusted_mode->crtc_hdisplay - 1) |
7617 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007618 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007619 (adjusted_mode->crtc_hblank_start - 1) |
7620 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007621 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007622 (adjusted_mode->crtc_hsync_start - 1) |
7623 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7624
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007625 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007626 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007627 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007628 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007629 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007630 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007631 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007632 (adjusted_mode->crtc_vsync_start - 1) |
7633 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7634
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007635 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7636 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7637 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7638 * bits. */
7639 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7640 (pipe == PIPE_B || pipe == PIPE_C))
7641 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7642
Jani Nikulabc58be62016-03-18 17:05:39 +02007643}
7644
7645static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7646{
7647 struct drm_device *dev = intel_crtc->base.dev;
7648 struct drm_i915_private *dev_priv = dev->dev_private;
7649 enum pipe pipe = intel_crtc->pipe;
7650
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007651 /* pipesrc controls the size that is scaled from, which should
7652 * always be the user's requested size.
7653 */
7654 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007655 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7656 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007657}
7658
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007659static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007660 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007661{
7662 struct drm_device *dev = crtc->base.dev;
7663 struct drm_i915_private *dev_priv = dev->dev_private;
7664 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7665 uint32_t tmp;
7666
7667 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007668 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7669 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007670 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007671 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7672 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007673 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007674 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7675 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007676
7677 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007678 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7679 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007680 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007681 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7682 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007683 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007684 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7685 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007686
7687 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007688 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7689 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7690 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007691 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007692}
7693
7694static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7695 struct intel_crtc_state *pipe_config)
7696{
7697 struct drm_device *dev = crtc->base.dev;
7698 struct drm_i915_private *dev_priv = dev->dev_private;
7699 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007700
7701 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007702 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7703 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7704
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007705 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7706 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007707}
7708
Daniel Vetterf6a83282014-02-11 15:28:57 -08007709void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007710 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007711{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007712 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7713 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7714 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7715 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007716
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007717 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7718 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7719 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7720 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007721
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007722 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007723 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007724
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007725 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7726 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007727
7728 mode->hsync = drm_mode_hsync(mode);
7729 mode->vrefresh = drm_mode_vrefresh(mode);
7730 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007731}
7732
Daniel Vetter84b046f2013-02-19 18:48:54 +01007733static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7734{
7735 struct drm_device *dev = intel_crtc->base.dev;
7736 struct drm_i915_private *dev_priv = dev->dev_private;
7737 uint32_t pipeconf;
7738
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007739 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007740
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007741 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7742 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7743 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007744
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007745 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007746 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007747
Daniel Vetterff9ce462013-04-24 14:57:17 +02007748 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007749 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007750 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007751 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007752 pipeconf |= PIPECONF_DITHER_EN |
7753 PIPECONF_DITHER_TYPE_SP;
7754
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007755 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007756 case 18:
7757 pipeconf |= PIPECONF_6BPC;
7758 break;
7759 case 24:
7760 pipeconf |= PIPECONF_8BPC;
7761 break;
7762 case 30:
7763 pipeconf |= PIPECONF_10BPC;
7764 break;
7765 default:
7766 /* Case prevented by intel_choose_pipe_bpp_dither. */
7767 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007768 }
7769 }
7770
7771 if (HAS_PIPE_CXSR(dev)) {
7772 if (intel_crtc->lowfreq_avail) {
7773 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7774 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7775 } else {
7776 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007777 }
7778 }
7779
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007780 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007781 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007782 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007783 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7784 else
7785 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7786 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007787 pipeconf |= PIPECONF_PROGRESSIVE;
7788
Wayne Boyer666a4532015-12-09 12:29:35 -08007789 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7790 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007791 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007792
Daniel Vetter84b046f2013-02-19 18:48:54 +01007793 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7794 POSTING_READ(PIPECONF(intel_crtc->pipe));
7795}
7796
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007797static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7798 struct intel_crtc_state *crtc_state)
7799{
7800 struct drm_device *dev = crtc->base.dev;
7801 struct drm_i915_private *dev_priv = dev->dev_private;
7802 const intel_limit_t *limit;
7803 int refclk = 48000;
7804
7805 memset(&crtc_state->dpll_hw_state, 0,
7806 sizeof(crtc_state->dpll_hw_state));
7807
7808 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7809 if (intel_panel_use_ssc(dev_priv)) {
7810 refclk = dev_priv->vbt.lvds_ssc_freq;
7811 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7812 }
7813
7814 limit = &intel_limits_i8xx_lvds;
7815 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7816 limit = &intel_limits_i8xx_dvo;
7817 } else {
7818 limit = &intel_limits_i8xx_dac;
7819 }
7820
7821 if (!crtc_state->clock_set &&
7822 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7823 refclk, NULL, &crtc_state->dpll)) {
7824 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7825 return -EINVAL;
7826 }
7827
7828 i8xx_compute_dpll(crtc, crtc_state, NULL);
7829
7830 return 0;
7831}
7832
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007833static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7834 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007835{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007836 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007837 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007838 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007839 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007840 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007841
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007842 memset(&crtc_state->dpll_hw_state, 0,
7843 sizeof(crtc_state->dpll_hw_state));
7844
Jani Nikulaa65347b2015-11-27 12:21:46 +02007845 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007846 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007847
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007848 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7849 intel_panel_use_ssc(dev_priv)) {
7850 refclk = dev_priv->vbt.lvds_ssc_freq;
7851 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7852 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007853
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007854 if (!crtc_state->clock_set) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007855 /*
7856 * Returns a set of divisors for the desired target clock with
7857 * the given refclk, or FALSE. The returned values represent
7858 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7859 * 2) / p1 / p2.
7860 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007861 limit = intel_limit(crtc_state, refclk);
7862 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007863 crtc_state->port_clock,
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02007864 refclk, NULL,
7865 &crtc_state->dpll);
Jani Nikulaf2335332013-09-13 11:03:09 +03007866 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007867 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7868 return -EINVAL;
7869 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007870 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007871
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007872 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007873
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007874 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007875}
7876
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007877static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7878 struct intel_crtc_state *crtc_state)
7879{
7880 int refclk = 100000;
7881 const intel_limit_t *limit = &intel_limits_chv;
7882
7883 memset(&crtc_state->dpll_hw_state, 0,
7884 sizeof(crtc_state->dpll_hw_state));
7885
7886 if (crtc_state->has_dsi_encoder)
7887 return 0;
7888
7889 if (!crtc_state->clock_set &&
7890 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7891 refclk, NULL, &crtc_state->dpll)) {
7892 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7893 return -EINVAL;
7894 }
7895
7896 chv_compute_dpll(crtc, crtc_state);
7897
7898 return 0;
7899}
7900
7901static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7902 struct intel_crtc_state *crtc_state)
7903{
7904 int refclk = 100000;
7905 const intel_limit_t *limit = &intel_limits_vlv;
7906
7907 memset(&crtc_state->dpll_hw_state, 0,
7908 sizeof(crtc_state->dpll_hw_state));
7909
7910 if (crtc_state->has_dsi_encoder)
7911 return 0;
7912
7913 if (!crtc_state->clock_set &&
7914 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7915 refclk, NULL, &crtc_state->dpll)) {
7916 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7917 return -EINVAL;
7918 }
7919
7920 vlv_compute_dpll(crtc, crtc_state);
7921
7922 return 0;
7923}
7924
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007925static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007926 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007927{
7928 struct drm_device *dev = crtc->base.dev;
7929 struct drm_i915_private *dev_priv = dev->dev_private;
7930 uint32_t tmp;
7931
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007932 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7933 return;
7934
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007935 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007936 if (!(tmp & PFIT_ENABLE))
7937 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007938
Daniel Vetter06922822013-07-11 13:35:40 +02007939 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007940 if (INTEL_INFO(dev)->gen < 4) {
7941 if (crtc->pipe != PIPE_B)
7942 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007943 } else {
7944 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7945 return;
7946 }
7947
Daniel Vetter06922822013-07-11 13:35:40 +02007948 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007949 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7950 if (INTEL_INFO(dev)->gen < 5)
7951 pipe_config->gmch_pfit.lvds_border_bits =
7952 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7953}
7954
Jesse Barnesacbec812013-09-20 11:29:32 -07007955static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007956 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007957{
7958 struct drm_device *dev = crtc->base.dev;
7959 struct drm_i915_private *dev_priv = dev->dev_private;
7960 int pipe = pipe_config->cpu_transcoder;
7961 intel_clock_t clock;
7962 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007963 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007964
Shobhit Kumarf573de52014-07-30 20:32:37 +05307965 /* In case of MIPI DPLL will not even be used */
7966 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7967 return;
7968
Ville Syrjäläa5805162015-05-26 20:42:30 +03007969 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007970 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007971 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007972
7973 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7974 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7975 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7976 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7977 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7978
Imre Deakdccbea32015-06-22 23:35:51 +03007979 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007980}
7981
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007982static void
7983i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7984 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007985{
7986 struct drm_device *dev = crtc->base.dev;
7987 struct drm_i915_private *dev_priv = dev->dev_private;
7988 u32 val, base, offset;
7989 int pipe = crtc->pipe, plane = crtc->plane;
7990 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007991 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007992 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007993 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007994
Damien Lespiau42a7b082015-02-05 19:35:13 +00007995 val = I915_READ(DSPCNTR(plane));
7996 if (!(val & DISPLAY_PLANE_ENABLE))
7997 return;
7998
Damien Lespiaud9806c92015-01-21 14:07:19 +00007999 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008000 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008001 DRM_DEBUG_KMS("failed to alloc fb\n");
8002 return;
8003 }
8004
Damien Lespiau1b842c82015-01-21 13:50:54 +00008005 fb = &intel_fb->base;
8006
Daniel Vetter18c52472015-02-10 17:16:09 +00008007 if (INTEL_INFO(dev)->gen >= 4) {
8008 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008009 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008010 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8011 }
8012 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008013
8014 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008015 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008016 fb->pixel_format = fourcc;
8017 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008018
8019 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008020 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008021 offset = I915_READ(DSPTILEOFF(plane));
8022 else
8023 offset = I915_READ(DSPLINOFF(plane));
8024 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8025 } else {
8026 base = I915_READ(DSPADDR(plane));
8027 }
8028 plane_config->base = base;
8029
8030 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008031 fb->width = ((val >> 16) & 0xfff) + 1;
8032 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008033
8034 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008035 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008036
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008037 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008038 fb->pixel_format,
8039 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008040
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008041 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008042
Damien Lespiau2844a922015-01-20 12:51:48 +00008043 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8044 pipe_name(pipe), plane, fb->width, fb->height,
8045 fb->bits_per_pixel, base, fb->pitches[0],
8046 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008047
Damien Lespiau2d140302015-02-05 17:22:18 +00008048 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008049}
8050
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008051static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008052 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008053{
8054 struct drm_device *dev = crtc->base.dev;
8055 struct drm_i915_private *dev_priv = dev->dev_private;
8056 int pipe = pipe_config->cpu_transcoder;
8057 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8058 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008059 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008060 int refclk = 100000;
8061
Ville Syrjäläa5805162015-05-26 20:42:30 +03008062 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008063 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8064 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8065 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8066 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008067 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008068 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008069
8070 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008071 clock.m2 = (pll_dw0 & 0xff) << 22;
8072 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8073 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008074 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8075 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8076 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8077
Imre Deakdccbea32015-06-22 23:35:51 +03008078 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008079}
8080
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008081static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008082 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008083{
8084 struct drm_device *dev = crtc->base.dev;
8085 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008086 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008087 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008088 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008089
Imre Deak17290502016-02-12 18:55:11 +02008090 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8091 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008092 return false;
8093
Daniel Vettere143a212013-07-04 12:01:15 +02008094 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008095 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008096
Imre Deak17290502016-02-12 18:55:11 +02008097 ret = false;
8098
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008099 tmp = I915_READ(PIPECONF(crtc->pipe));
8100 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008101 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008102
Wayne Boyer666a4532015-12-09 12:29:35 -08008103 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008104 switch (tmp & PIPECONF_BPC_MASK) {
8105 case PIPECONF_6BPC:
8106 pipe_config->pipe_bpp = 18;
8107 break;
8108 case PIPECONF_8BPC:
8109 pipe_config->pipe_bpp = 24;
8110 break;
8111 case PIPECONF_10BPC:
8112 pipe_config->pipe_bpp = 30;
8113 break;
8114 default:
8115 break;
8116 }
8117 }
8118
Wayne Boyer666a4532015-12-09 12:29:35 -08008119 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8120 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008121 pipe_config->limited_color_range = true;
8122
Ville Syrjälä282740f2013-09-04 18:30:03 +03008123 if (INTEL_INFO(dev)->gen < 4)
8124 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8125
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008126 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008127 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008128
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008129 i9xx_get_pfit_config(crtc, pipe_config);
8130
Daniel Vetter6c49f242013-06-06 12:45:25 +02008131 if (INTEL_INFO(dev)->gen >= 4) {
8132 tmp = I915_READ(DPLL_MD(crtc->pipe));
8133 pipe_config->pixel_multiplier =
8134 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8135 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008136 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008137 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8138 tmp = I915_READ(DPLL(crtc->pipe));
8139 pipe_config->pixel_multiplier =
8140 ((tmp & SDVO_MULTIPLIER_MASK)
8141 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8142 } else {
8143 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8144 * port and will be fixed up in the encoder->get_config
8145 * function. */
8146 pipe_config->pixel_multiplier = 1;
8147 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008148 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008149 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008150 /*
8151 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8152 * on 830. Filter it out here so that we don't
8153 * report errors due to that.
8154 */
8155 if (IS_I830(dev))
8156 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8157
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008158 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8159 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008160 } else {
8161 /* Mask out read-only status bits. */
8162 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8163 DPLL_PORTC_READY_MASK |
8164 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008165 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008166
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008167 if (IS_CHERRYVIEW(dev))
8168 chv_crtc_clock_get(crtc, pipe_config);
8169 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008170 vlv_crtc_clock_get(crtc, pipe_config);
8171 else
8172 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008173
Ville Syrjälä0f646142015-08-26 19:39:18 +03008174 /*
8175 * Normally the dotclock is filled in by the encoder .get_config()
8176 * but in case the pipe is enabled w/o any ports we need a sane
8177 * default.
8178 */
8179 pipe_config->base.adjusted_mode.crtc_clock =
8180 pipe_config->port_clock / pipe_config->pixel_multiplier;
8181
Imre Deak17290502016-02-12 18:55:11 +02008182 ret = true;
8183
8184out:
8185 intel_display_power_put(dev_priv, power_domain);
8186
8187 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008188}
8189
Paulo Zanonidde86e22012-12-01 12:04:25 -02008190static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008191{
8192 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008193 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008194 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008195 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008196 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008197 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008198 bool has_ck505 = false;
8199 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008200
8201 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008202 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008203 switch (encoder->type) {
8204 case INTEL_OUTPUT_LVDS:
8205 has_panel = true;
8206 has_lvds = true;
8207 break;
8208 case INTEL_OUTPUT_EDP:
8209 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008210 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008211 has_cpu_edp = true;
8212 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008213 default:
8214 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008215 }
8216 }
8217
Keith Packard99eb6a02011-09-26 14:29:12 -07008218 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008219 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008220 can_ssc = has_ck505;
8221 } else {
8222 has_ck505 = false;
8223 can_ssc = true;
8224 }
8225
Imre Deak2de69052013-05-08 13:14:04 +03008226 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8227 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008228
8229 /* Ironlake: try to setup display ref clock before DPLL
8230 * enabling. This is only under driver's control after
8231 * PCH B stepping, previous chipset stepping should be
8232 * ignoring this setting.
8233 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008234 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008235
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008236 /* As we must carefully and slowly disable/enable each source in turn,
8237 * compute the final state we want first and check if we need to
8238 * make any changes at all.
8239 */
8240 final = val;
8241 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008242 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008243 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008244 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008245 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8246
8247 final &= ~DREF_SSC_SOURCE_MASK;
8248 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8249 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250
Keith Packard199e5d72011-09-22 12:01:57 -07008251 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008252 final |= DREF_SSC_SOURCE_ENABLE;
8253
8254 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8255 final |= DREF_SSC1_ENABLE;
8256
8257 if (has_cpu_edp) {
8258 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8259 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8260 else
8261 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8262 } else
8263 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8264 } else {
8265 final |= DREF_SSC_SOURCE_DISABLE;
8266 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8267 }
8268
8269 if (final == val)
8270 return;
8271
8272 /* Always enable nonspread source */
8273 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8274
8275 if (has_ck505)
8276 val |= DREF_NONSPREAD_CK505_ENABLE;
8277 else
8278 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8279
8280 if (has_panel) {
8281 val &= ~DREF_SSC_SOURCE_MASK;
8282 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008283
Keith Packard199e5d72011-09-22 12:01:57 -07008284 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008285 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008286 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008288 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008289 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008290
8291 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008292 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008293 POSTING_READ(PCH_DREF_CONTROL);
8294 udelay(200);
8295
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008296 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008297
8298 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008299 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008300 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008301 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008303 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008304 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008305 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008306 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008307
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008308 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008309 POSTING_READ(PCH_DREF_CONTROL);
8310 udelay(200);
8311 } else {
8312 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8313
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008314 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008315
8316 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008317 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008318
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008320 POSTING_READ(PCH_DREF_CONTROL);
8321 udelay(200);
8322
8323 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008324 val &= ~DREF_SSC_SOURCE_MASK;
8325 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008326
8327 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008328 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008329
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008330 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008331 POSTING_READ(PCH_DREF_CONTROL);
8332 udelay(200);
8333 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008334
8335 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008336}
8337
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008338static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008339{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008340 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008341
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008342 tmp = I915_READ(SOUTH_CHICKEN2);
8343 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8344 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008345
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008346 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8347 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8348 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008349
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008350 tmp = I915_READ(SOUTH_CHICKEN2);
8351 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8352 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008353
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008354 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8355 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8356 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008357}
8358
8359/* WaMPhyProgramming:hsw */
8360static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8361{
8362 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008363
8364 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8365 tmp &= ~(0xFF << 24);
8366 tmp |= (0x12 << 24);
8367 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8368
Paulo Zanonidde86e22012-12-01 12:04:25 -02008369 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8370 tmp |= (1 << 11);
8371 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8372
8373 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8374 tmp |= (1 << 11);
8375 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8376
Paulo Zanonidde86e22012-12-01 12:04:25 -02008377 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8378 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8379 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8380
8381 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8382 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8383 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8384
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008385 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8386 tmp &= ~(7 << 13);
8387 tmp |= (5 << 13);
8388 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008389
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008390 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8391 tmp &= ~(7 << 13);
8392 tmp |= (5 << 13);
8393 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008394
8395 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8396 tmp &= ~0xFF;
8397 tmp |= 0x1C;
8398 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8399
8400 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8401 tmp &= ~0xFF;
8402 tmp |= 0x1C;
8403 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8404
8405 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8406 tmp &= ~(0xFF << 16);
8407 tmp |= (0x1C << 16);
8408 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8409
8410 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8411 tmp &= ~(0xFF << 16);
8412 tmp |= (0x1C << 16);
8413 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8414
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008415 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8416 tmp |= (1 << 27);
8417 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008419 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8420 tmp |= (1 << 27);
8421 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008422
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008423 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8424 tmp &= ~(0xF << 28);
8425 tmp |= (4 << 28);
8426 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008427
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008428 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8429 tmp &= ~(0xF << 28);
8430 tmp |= (4 << 28);
8431 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008432}
8433
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008434/* Implements 3 different sequences from BSpec chapter "Display iCLK
8435 * Programming" based on the parameters passed:
8436 * - Sequence to enable CLKOUT_DP
8437 * - Sequence to enable CLKOUT_DP without spread
8438 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8439 */
8440static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8441 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008442{
8443 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008444 uint32_t reg, tmp;
8445
8446 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8447 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008448 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008449 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008450
Ville Syrjäläa5805162015-05-26 20:42:30 +03008451 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008452
8453 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8454 tmp &= ~SBI_SSCCTL_DISABLE;
8455 tmp |= SBI_SSCCTL_PATHALT;
8456 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8457
8458 udelay(24);
8459
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008460 if (with_spread) {
8461 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8462 tmp &= ~SBI_SSCCTL_PATHALT;
8463 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008464
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008465 if (with_fdi) {
8466 lpt_reset_fdi_mphy(dev_priv);
8467 lpt_program_fdi_mphy(dev_priv);
8468 }
8469 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008470
Ville Syrjäläc2699522015-08-27 23:55:59 +03008471 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008472 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8473 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8474 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008475
Ville Syrjäläa5805162015-05-26 20:42:30 +03008476 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008477}
8478
Paulo Zanoni47701c32013-07-23 11:19:25 -03008479/* Sequence to disable CLKOUT_DP */
8480static void lpt_disable_clkout_dp(struct drm_device *dev)
8481{
8482 struct drm_i915_private *dev_priv = dev->dev_private;
8483 uint32_t reg, tmp;
8484
Ville Syrjäläa5805162015-05-26 20:42:30 +03008485 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008486
Ville Syrjäläc2699522015-08-27 23:55:59 +03008487 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008488 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8489 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8490 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8491
8492 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8493 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8494 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8495 tmp |= SBI_SSCCTL_PATHALT;
8496 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8497 udelay(32);
8498 }
8499 tmp |= SBI_SSCCTL_DISABLE;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8501 }
8502
Ville Syrjäläa5805162015-05-26 20:42:30 +03008503 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008504}
8505
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008506#define BEND_IDX(steps) ((50 + (steps)) / 5)
8507
8508static const uint16_t sscdivintphase[] = {
8509 [BEND_IDX( 50)] = 0x3B23,
8510 [BEND_IDX( 45)] = 0x3B23,
8511 [BEND_IDX( 40)] = 0x3C23,
8512 [BEND_IDX( 35)] = 0x3C23,
8513 [BEND_IDX( 30)] = 0x3D23,
8514 [BEND_IDX( 25)] = 0x3D23,
8515 [BEND_IDX( 20)] = 0x3E23,
8516 [BEND_IDX( 15)] = 0x3E23,
8517 [BEND_IDX( 10)] = 0x3F23,
8518 [BEND_IDX( 5)] = 0x3F23,
8519 [BEND_IDX( 0)] = 0x0025,
8520 [BEND_IDX( -5)] = 0x0025,
8521 [BEND_IDX(-10)] = 0x0125,
8522 [BEND_IDX(-15)] = 0x0125,
8523 [BEND_IDX(-20)] = 0x0225,
8524 [BEND_IDX(-25)] = 0x0225,
8525 [BEND_IDX(-30)] = 0x0325,
8526 [BEND_IDX(-35)] = 0x0325,
8527 [BEND_IDX(-40)] = 0x0425,
8528 [BEND_IDX(-45)] = 0x0425,
8529 [BEND_IDX(-50)] = 0x0525,
8530};
8531
8532/*
8533 * Bend CLKOUT_DP
8534 * steps -50 to 50 inclusive, in steps of 5
8535 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8536 * change in clock period = -(steps / 10) * 5.787 ps
8537 */
8538static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8539{
8540 uint32_t tmp;
8541 int idx = BEND_IDX(steps);
8542
8543 if (WARN_ON(steps % 5 != 0))
8544 return;
8545
8546 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8547 return;
8548
8549 mutex_lock(&dev_priv->sb_lock);
8550
8551 if (steps % 10 != 0)
8552 tmp = 0xAAAAAAAB;
8553 else
8554 tmp = 0x00000000;
8555 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8556
8557 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8558 tmp &= 0xffff0000;
8559 tmp |= sscdivintphase[idx];
8560 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8561
8562 mutex_unlock(&dev_priv->sb_lock);
8563}
8564
8565#undef BEND_IDX
8566
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008567static void lpt_init_pch_refclk(struct drm_device *dev)
8568{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008569 struct intel_encoder *encoder;
8570 bool has_vga = false;
8571
Damien Lespiaub2784e12014-08-05 11:29:37 +01008572 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008573 switch (encoder->type) {
8574 case INTEL_OUTPUT_ANALOG:
8575 has_vga = true;
8576 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008577 default:
8578 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008579 }
8580 }
8581
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008582 if (has_vga) {
8583 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008584 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008585 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008586 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008587 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008588}
8589
Paulo Zanonidde86e22012-12-01 12:04:25 -02008590/*
8591 * Initialize reference clocks when the driver loads
8592 */
8593void intel_init_pch_refclk(struct drm_device *dev)
8594{
8595 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8596 ironlake_init_pch_refclk(dev);
8597 else if (HAS_PCH_LPT(dev))
8598 lpt_init_pch_refclk(dev);
8599}
8600
Daniel Vetter6ff93602013-04-19 11:24:36 +02008601static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008602{
8603 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8605 int pipe = intel_crtc->pipe;
8606 uint32_t val;
8607
Daniel Vetter78114072013-06-13 00:54:57 +02008608 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008609
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008610 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008611 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008612 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008613 break;
8614 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008615 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008616 break;
8617 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008618 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008619 break;
8620 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008621 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008622 break;
8623 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008624 /* Case prevented by intel_choose_pipe_bpp_dither. */
8625 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008626 }
8627
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008628 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008629 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8630
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008631 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008632 val |= PIPECONF_INTERLACED_ILK;
8633 else
8634 val |= PIPECONF_PROGRESSIVE;
8635
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008636 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008637 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008638
Paulo Zanonic8203562012-09-12 10:06:29 -03008639 I915_WRITE(PIPECONF(pipe), val);
8640 POSTING_READ(PIPECONF(pipe));
8641}
8642
Daniel Vetter6ff93602013-04-19 11:24:36 +02008643static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008644{
Jani Nikula391bf042016-03-18 17:05:40 +02008645 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008647 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008648 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008649
Jani Nikula391bf042016-03-18 17:05:40 +02008650 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008651 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8652
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008653 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008654 val |= PIPECONF_INTERLACED_ILK;
8655 else
8656 val |= PIPECONF_PROGRESSIVE;
8657
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008658 I915_WRITE(PIPECONF(cpu_transcoder), val);
8659 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008660}
8661
Jani Nikula391bf042016-03-18 17:05:40 +02008662static void haswell_set_pipemisc(struct drm_crtc *crtc)
8663{
8664 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8666
8667 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8668 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008669
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008670 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008671 case 18:
8672 val |= PIPEMISC_DITHER_6_BPC;
8673 break;
8674 case 24:
8675 val |= PIPEMISC_DITHER_8_BPC;
8676 break;
8677 case 30:
8678 val |= PIPEMISC_DITHER_10_BPC;
8679 break;
8680 case 36:
8681 val |= PIPEMISC_DITHER_12_BPC;
8682 break;
8683 default:
8684 /* Case prevented by pipe_config_set_bpp. */
8685 BUG();
8686 }
8687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008689 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8690
Jani Nikula391bf042016-03-18 17:05:40 +02008691 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008692 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008693}
8694
Paulo Zanonid4b19312012-11-29 11:29:32 -02008695int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8696{
8697 /*
8698 * Account for spread spectrum to avoid
8699 * oversubscribing the link. Max center spread
8700 * is 2.5%; use 5% for safety's sake.
8701 */
8702 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008703 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008704}
8705
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008706static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008707{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008708 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008709}
8710
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008711static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8712 struct intel_crtc_state *crtc_state,
8713 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008714{
8715 struct drm_crtc *crtc = &intel_crtc->base;
8716 struct drm_device *dev = crtc->dev;
8717 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008718 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008719 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008720 struct drm_connector_state *connector_state;
8721 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008722 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008723 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008724 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008725
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008726 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008727 if (connector_state->crtc != crtc_state->base.crtc)
8728 continue;
8729
8730 encoder = to_intel_encoder(connector_state->best_encoder);
8731
8732 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008733 case INTEL_OUTPUT_LVDS:
8734 is_lvds = true;
8735 break;
8736 case INTEL_OUTPUT_SDVO:
8737 case INTEL_OUTPUT_HDMI:
8738 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008739 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008740 default:
8741 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008742 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008743 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008744
Chris Wilsonc1858122010-12-03 21:35:48 +00008745 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008746 factor = 21;
8747 if (is_lvds) {
8748 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008749 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008750 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008751 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008752 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008753 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008754
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008755 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008756
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008757 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8758 fp |= FP_CB_TUNE;
8759
8760 if (reduced_clock) {
8761 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8762
8763 if (reduced_clock->m < factor * reduced_clock->n)
8764 fp2 |= FP_CB_TUNE;
8765 } else {
8766 fp2 = fp;
8767 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008768
Chris Wilson5eddb702010-09-11 13:48:45 +01008769 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008770
Eric Anholta07d6782011-03-30 13:01:08 -07008771 if (is_lvds)
8772 dpll |= DPLLB_MODE_LVDS;
8773 else
8774 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008775
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008776 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008777 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008778
8779 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008780 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008781 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008782 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008783
Eric Anholta07d6782011-03-30 13:01:08 -07008784 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008785 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008786 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008787 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008788
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008789 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008790 case 5:
8791 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8792 break;
8793 case 7:
8794 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8795 break;
8796 case 10:
8797 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8798 break;
8799 case 14:
8800 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8801 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008802 }
8803
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008804 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008805 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008806 else
8807 dpll |= PLL_REF_INPUT_DREFCLK;
8808
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008809 dpll |= DPLL_VCO_ENABLE;
8810
8811 crtc_state->dpll_hw_state.dpll = dpll;
8812 crtc_state->dpll_hw_state.fp0 = fp;
8813 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008814}
8815
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008816static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8817 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008818{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008819 struct drm_device *dev = crtc->base.dev;
8820 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008821 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008822 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008823 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008824 const intel_limit_t *limit;
8825 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008826
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008827 memset(&crtc_state->dpll_hw_state, 0,
8828 sizeof(crtc_state->dpll_hw_state));
8829
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008830 crtc->lowfreq_avail = false;
8831
8832 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8833 if (!crtc_state->has_pch_encoder)
8834 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008835
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008836 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8837 if (intel_panel_use_ssc(dev_priv)) {
8838 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8839 dev_priv->vbt.lvds_ssc_freq);
8840 refclk = dev_priv->vbt.lvds_ssc_freq;
8841 }
8842
8843 if (intel_is_dual_link_lvds(dev)) {
8844 if (refclk == 100000)
8845 limit = &intel_limits_ironlake_dual_lvds_100m;
8846 else
8847 limit = &intel_limits_ironlake_dual_lvds;
8848 } else {
8849 if (refclk == 100000)
8850 limit = &intel_limits_ironlake_single_lvds_100m;
8851 else
8852 limit = &intel_limits_ironlake_single_lvds;
8853 }
8854 } else {
8855 limit = &intel_limits_ironlake_dac;
8856 }
8857
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008858 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008859 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8860 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008861 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8862 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008863 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008864
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008865 ironlake_compute_dpll(crtc, crtc_state,
8866 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008867
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008868 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8869 if (pll == NULL) {
8870 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8871 pipe_name(crtc->pipe));
8872 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008873 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008874
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008875 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8876 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008877 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008878
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008879 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008880}
8881
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008882static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8883 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008884{
8885 struct drm_device *dev = crtc->base.dev;
8886 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008887 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008888
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008889 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8890 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8891 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8892 & ~TU_SIZE_MASK;
8893 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8894 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8895 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8896}
8897
8898static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8899 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008900 struct intel_link_m_n *m_n,
8901 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008902{
8903 struct drm_device *dev = crtc->base.dev;
8904 struct drm_i915_private *dev_priv = dev->dev_private;
8905 enum pipe pipe = crtc->pipe;
8906
8907 if (INTEL_INFO(dev)->gen >= 5) {
8908 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8909 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8910 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8911 & ~TU_SIZE_MASK;
8912 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8913 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8914 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008915 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8916 * gen < 8) and if DRRS is supported (to make sure the
8917 * registers are not unnecessarily read).
8918 */
8919 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008920 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008921 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8922 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8923 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8924 & ~TU_SIZE_MASK;
8925 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8926 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8927 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8928 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008929 } else {
8930 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8931 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8932 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8933 & ~TU_SIZE_MASK;
8934 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8935 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8936 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8937 }
8938}
8939
8940void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008941 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008942{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008943 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008944 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8945 else
8946 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008947 &pipe_config->dp_m_n,
8948 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008949}
8950
Daniel Vetter72419202013-04-04 13:28:53 +02008951static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008952 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008953{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008954 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008955 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008956}
8957
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008958static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008959 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008960{
8961 struct drm_device *dev = crtc->base.dev;
8962 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008963 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8964 uint32_t ps_ctrl = 0;
8965 int id = -1;
8966 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008967
Chandra Kondurua1b22782015-04-07 15:28:45 -07008968 /* find scaler attached to this pipe */
8969 for (i = 0; i < crtc->num_scalers; i++) {
8970 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8971 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8972 id = i;
8973 pipe_config->pch_pfit.enabled = true;
8974 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8975 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8976 break;
8977 }
8978 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008979
Chandra Kondurua1b22782015-04-07 15:28:45 -07008980 scaler_state->scaler_id = id;
8981 if (id >= 0) {
8982 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8983 } else {
8984 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008985 }
8986}
8987
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008988static void
8989skylake_get_initial_plane_config(struct intel_crtc *crtc,
8990 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008991{
8992 struct drm_device *dev = crtc->base.dev;
8993 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008994 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008995 int pipe = crtc->pipe;
8996 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008997 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008998 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008999 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009000
Damien Lespiaud9806c92015-01-21 14:07:19 +00009001 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009002 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009003 DRM_DEBUG_KMS("failed to alloc fb\n");
9004 return;
9005 }
9006
Damien Lespiau1b842c82015-01-21 13:50:54 +00009007 fb = &intel_fb->base;
9008
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009009 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009010 if (!(val & PLANE_CTL_ENABLE))
9011 goto error;
9012
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009013 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9014 fourcc = skl_format_to_fourcc(pixel_format,
9015 val & PLANE_CTL_ORDER_RGBX,
9016 val & PLANE_CTL_ALPHA_MASK);
9017 fb->pixel_format = fourcc;
9018 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9019
Damien Lespiau40f46282015-02-27 11:15:21 +00009020 tiling = val & PLANE_CTL_TILED_MASK;
9021 switch (tiling) {
9022 case PLANE_CTL_TILED_LINEAR:
9023 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9024 break;
9025 case PLANE_CTL_TILED_X:
9026 plane_config->tiling = I915_TILING_X;
9027 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9028 break;
9029 case PLANE_CTL_TILED_Y:
9030 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9031 break;
9032 case PLANE_CTL_TILED_YF:
9033 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9034 break;
9035 default:
9036 MISSING_CASE(tiling);
9037 goto error;
9038 }
9039
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009040 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9041 plane_config->base = base;
9042
9043 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9044
9045 val = I915_READ(PLANE_SIZE(pipe, 0));
9046 fb->height = ((val >> 16) & 0xfff) + 1;
9047 fb->width = ((val >> 0) & 0x1fff) + 1;
9048
9049 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009050 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009051 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009052 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9053
9054 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009055 fb->pixel_format,
9056 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009057
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009058 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059
9060 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9061 pipe_name(pipe), fb->width, fb->height,
9062 fb->bits_per_pixel, base, fb->pitches[0],
9063 plane_config->size);
9064
Damien Lespiau2d140302015-02-05 17:22:18 +00009065 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009066 return;
9067
9068error:
9069 kfree(fb);
9070}
9071
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009072static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009073 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009074{
9075 struct drm_device *dev = crtc->base.dev;
9076 struct drm_i915_private *dev_priv = dev->dev_private;
9077 uint32_t tmp;
9078
9079 tmp = I915_READ(PF_CTL(crtc->pipe));
9080
9081 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009082 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009083 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9084 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009085
9086 /* We currently do not free assignements of panel fitters on
9087 * ivb/hsw (since we don't use the higher upscaling modes which
9088 * differentiates them) so just WARN about this case for now. */
9089 if (IS_GEN7(dev)) {
9090 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9091 PF_PIPE_SEL_IVB(crtc->pipe));
9092 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009093 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009094}
9095
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009096static void
9097ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9098 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009099{
9100 struct drm_device *dev = crtc->base.dev;
9101 struct drm_i915_private *dev_priv = dev->dev_private;
9102 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009103 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009104 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009105 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009106 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009107 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009108
Damien Lespiau42a7b082015-02-05 19:35:13 +00009109 val = I915_READ(DSPCNTR(pipe));
9110 if (!(val & DISPLAY_PLANE_ENABLE))
9111 return;
9112
Damien Lespiaud9806c92015-01-21 14:07:19 +00009113 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009114 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009115 DRM_DEBUG_KMS("failed to alloc fb\n");
9116 return;
9117 }
9118
Damien Lespiau1b842c82015-01-21 13:50:54 +00009119 fb = &intel_fb->base;
9120
Daniel Vetter18c52472015-02-10 17:16:09 +00009121 if (INTEL_INFO(dev)->gen >= 4) {
9122 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009123 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009124 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9125 }
9126 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009127
9128 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009129 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009130 fb->pixel_format = fourcc;
9131 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009132
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009133 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009134 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009135 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009136 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009137 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009138 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009139 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009140 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009141 }
9142 plane_config->base = base;
9143
9144 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009145 fb->width = ((val >> 16) & 0xfff) + 1;
9146 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009147
9148 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009149 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009150
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009151 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009152 fb->pixel_format,
9153 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009154
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009155 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009156
Damien Lespiau2844a922015-01-20 12:51:48 +00009157 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9158 pipe_name(pipe), fb->width, fb->height,
9159 fb->bits_per_pixel, base, fb->pitches[0],
9160 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009161
Damien Lespiau2d140302015-02-05 17:22:18 +00009162 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009163}
9164
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009165static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009166 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009167{
9168 struct drm_device *dev = crtc->base.dev;
9169 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009170 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009171 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009172 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009173
Imre Deak17290502016-02-12 18:55:11 +02009174 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9175 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009176 return false;
9177
Daniel Vettere143a212013-07-04 12:01:15 +02009178 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009179 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009180
Imre Deak17290502016-02-12 18:55:11 +02009181 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009182 tmp = I915_READ(PIPECONF(crtc->pipe));
9183 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009184 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009185
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009186 switch (tmp & PIPECONF_BPC_MASK) {
9187 case PIPECONF_6BPC:
9188 pipe_config->pipe_bpp = 18;
9189 break;
9190 case PIPECONF_8BPC:
9191 pipe_config->pipe_bpp = 24;
9192 break;
9193 case PIPECONF_10BPC:
9194 pipe_config->pipe_bpp = 30;
9195 break;
9196 case PIPECONF_12BPC:
9197 pipe_config->pipe_bpp = 36;
9198 break;
9199 default:
9200 break;
9201 }
9202
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009203 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9204 pipe_config->limited_color_range = true;
9205
Daniel Vetterab9412b2013-05-03 11:49:46 +02009206 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009207 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009208 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009209
Daniel Vetter88adfff2013-03-28 10:42:01 +01009210 pipe_config->has_pch_encoder = true;
9211
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009212 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9213 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9214 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009215
9216 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009217
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009218 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009219 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009220 } else {
9221 tmp = I915_READ(PCH_DPLL_SEL);
9222 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009223 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009224 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009225 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009226 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009227
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009228 pipe_config->shared_dpll =
9229 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9230 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009231
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009232 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9233 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009234
9235 tmp = pipe_config->dpll_hw_state.dpll;
9236 pipe_config->pixel_multiplier =
9237 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9238 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009239
9240 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009241 } else {
9242 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009243 }
9244
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009245 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009246 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009247
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009248 ironlake_get_pfit_config(crtc, pipe_config);
9249
Imre Deak17290502016-02-12 18:55:11 +02009250 ret = true;
9251
9252out:
9253 intel_display_power_put(dev_priv, power_domain);
9254
9255 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009256}
9257
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009258static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9259{
9260 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009261 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009262
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009263 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009264 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009265 pipe_name(crtc->pipe));
9266
Rob Clarke2c719b2014-12-15 13:56:32 -05009267 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9268 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009269 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9270 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009271 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9272 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009273 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009274 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009275 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009276 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009277 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009278 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009279 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009280 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009281 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009282
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009283 /*
9284 * In theory we can still leave IRQs enabled, as long as only the HPD
9285 * interrupts remain enabled. We used to check for that, but since it's
9286 * gen-specific and since we only disable LCPLL after we fully disable
9287 * the interrupts, the check below should be enough.
9288 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009289 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009290}
9291
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009292static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9293{
9294 struct drm_device *dev = dev_priv->dev;
9295
9296 if (IS_HASWELL(dev))
9297 return I915_READ(D_COMP_HSW);
9298 else
9299 return I915_READ(D_COMP_BDW);
9300}
9301
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009302static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9303{
9304 struct drm_device *dev = dev_priv->dev;
9305
9306 if (IS_HASWELL(dev)) {
9307 mutex_lock(&dev_priv->rps.hw_lock);
9308 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9309 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009310 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009311 mutex_unlock(&dev_priv->rps.hw_lock);
9312 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009313 I915_WRITE(D_COMP_BDW, val);
9314 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009315 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009316}
9317
9318/*
9319 * This function implements pieces of two sequences from BSpec:
9320 * - Sequence for display software to disable LCPLL
9321 * - Sequence for display software to allow package C8+
9322 * The steps implemented here are just the steps that actually touch the LCPLL
9323 * register. Callers should take care of disabling all the display engine
9324 * functions, doing the mode unset, fixing interrupts, etc.
9325 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009326static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9327 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009328{
9329 uint32_t val;
9330
9331 assert_can_disable_lcpll(dev_priv);
9332
9333 val = I915_READ(LCPLL_CTL);
9334
9335 if (switch_to_fclk) {
9336 val |= LCPLL_CD_SOURCE_FCLK;
9337 I915_WRITE(LCPLL_CTL, val);
9338
9339 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9340 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9341 DRM_ERROR("Switching to FCLK failed\n");
9342
9343 val = I915_READ(LCPLL_CTL);
9344 }
9345
9346 val |= LCPLL_PLL_DISABLE;
9347 I915_WRITE(LCPLL_CTL, val);
9348 POSTING_READ(LCPLL_CTL);
9349
9350 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9351 DRM_ERROR("LCPLL still locked\n");
9352
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009353 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009354 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009355 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009356 ndelay(100);
9357
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009358 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9359 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009360 DRM_ERROR("D_COMP RCOMP still in progress\n");
9361
9362 if (allow_power_down) {
9363 val = I915_READ(LCPLL_CTL);
9364 val |= LCPLL_POWER_DOWN_ALLOW;
9365 I915_WRITE(LCPLL_CTL, val);
9366 POSTING_READ(LCPLL_CTL);
9367 }
9368}
9369
9370/*
9371 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9372 * source.
9373 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009374static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009375{
9376 uint32_t val;
9377
9378 val = I915_READ(LCPLL_CTL);
9379
9380 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9381 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9382 return;
9383
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009384 /*
9385 * Make sure we're not on PC8 state before disabling PC8, otherwise
9386 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009387 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009388 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009389
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009390 if (val & LCPLL_POWER_DOWN_ALLOW) {
9391 val &= ~LCPLL_POWER_DOWN_ALLOW;
9392 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009393 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009394 }
9395
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009396 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009397 val |= D_COMP_COMP_FORCE;
9398 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009399 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009400
9401 val = I915_READ(LCPLL_CTL);
9402 val &= ~LCPLL_PLL_DISABLE;
9403 I915_WRITE(LCPLL_CTL, val);
9404
9405 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9406 DRM_ERROR("LCPLL not locked yet\n");
9407
9408 if (val & LCPLL_CD_SOURCE_FCLK) {
9409 val = I915_READ(LCPLL_CTL);
9410 val &= ~LCPLL_CD_SOURCE_FCLK;
9411 I915_WRITE(LCPLL_CTL, val);
9412
9413 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9414 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9415 DRM_ERROR("Switching back to LCPLL failed\n");
9416 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009417
Mika Kuoppala59bad942015-01-16 11:34:40 +02009418 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009419 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009420}
9421
Paulo Zanoni765dab672014-03-07 20:08:18 -03009422/*
9423 * Package states C8 and deeper are really deep PC states that can only be
9424 * reached when all the devices on the system allow it, so even if the graphics
9425 * device allows PC8+, it doesn't mean the system will actually get to these
9426 * states. Our driver only allows PC8+ when going into runtime PM.
9427 *
9428 * The requirements for PC8+ are that all the outputs are disabled, the power
9429 * well is disabled and most interrupts are disabled, and these are also
9430 * requirements for runtime PM. When these conditions are met, we manually do
9431 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9432 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9433 * hang the machine.
9434 *
9435 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9436 * the state of some registers, so when we come back from PC8+ we need to
9437 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9438 * need to take care of the registers kept by RC6. Notice that this happens even
9439 * if we don't put the device in PCI D3 state (which is what currently happens
9440 * because of the runtime PM support).
9441 *
9442 * For more, read "Display Sequences for Package C8" on the hardware
9443 * documentation.
9444 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009445void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009446{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009447 struct drm_device *dev = dev_priv->dev;
9448 uint32_t val;
9449
Paulo Zanonic67a4702013-08-19 13:18:09 -03009450 DRM_DEBUG_KMS("Enabling package C8+\n");
9451
Ville Syrjäläc2699522015-08-27 23:55:59 +03009452 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009453 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9454 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9455 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9456 }
9457
9458 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009459 hsw_disable_lcpll(dev_priv, true, true);
9460}
9461
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009462void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009463{
9464 struct drm_device *dev = dev_priv->dev;
9465 uint32_t val;
9466
Paulo Zanonic67a4702013-08-19 13:18:09 -03009467 DRM_DEBUG_KMS("Disabling package C8+\n");
9468
9469 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009470 lpt_init_pch_refclk(dev);
9471
Ville Syrjäläc2699522015-08-27 23:55:59 +03009472 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009473 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9474 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9475 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9476 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009477}
9478
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009479static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309480{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009481 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009482 struct intel_atomic_state *old_intel_state =
9483 to_intel_atomic_state(old_state);
9484 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309485
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009486 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309487}
9488
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009489/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009490static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009491{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009492 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9493 struct drm_i915_private *dev_priv = state->dev->dev_private;
9494 struct drm_crtc *crtc;
9495 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009496 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009497 unsigned max_pixel_rate = 0, i;
9498 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009499
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009500 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9501 sizeof(intel_state->min_pixclk));
9502
9503 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009504 int pixel_rate;
9505
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009506 crtc_state = to_intel_crtc_state(cstate);
9507 if (!crtc_state->base.enable) {
9508 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009509 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009510 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009511
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009512 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009513
9514 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009515 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009516 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9517
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009518 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009519 }
9520
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009521 for_each_pipe(dev_priv, pipe)
9522 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9523
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009524 return max_pixel_rate;
9525}
9526
9527static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9528{
9529 struct drm_i915_private *dev_priv = dev->dev_private;
9530 uint32_t val, data;
9531 int ret;
9532
9533 if (WARN((I915_READ(LCPLL_CTL) &
9534 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9535 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9536 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9537 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9538 "trying to change cdclk frequency with cdclk not enabled\n"))
9539 return;
9540
9541 mutex_lock(&dev_priv->rps.hw_lock);
9542 ret = sandybridge_pcode_write(dev_priv,
9543 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9544 mutex_unlock(&dev_priv->rps.hw_lock);
9545 if (ret) {
9546 DRM_ERROR("failed to inform pcode about cdclk change\n");
9547 return;
9548 }
9549
9550 val = I915_READ(LCPLL_CTL);
9551 val |= LCPLL_CD_SOURCE_FCLK;
9552 I915_WRITE(LCPLL_CTL, val);
9553
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009554 if (wait_for_us(I915_READ(LCPLL_CTL) &
9555 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009556 DRM_ERROR("Switching to FCLK failed\n");
9557
9558 val = I915_READ(LCPLL_CTL);
9559 val &= ~LCPLL_CLK_FREQ_MASK;
9560
9561 switch (cdclk) {
9562 case 450000:
9563 val |= LCPLL_CLK_FREQ_450;
9564 data = 0;
9565 break;
9566 case 540000:
9567 val |= LCPLL_CLK_FREQ_54O_BDW;
9568 data = 1;
9569 break;
9570 case 337500:
9571 val |= LCPLL_CLK_FREQ_337_5_BDW;
9572 data = 2;
9573 break;
9574 case 675000:
9575 val |= LCPLL_CLK_FREQ_675_BDW;
9576 data = 3;
9577 break;
9578 default:
9579 WARN(1, "invalid cdclk frequency\n");
9580 return;
9581 }
9582
9583 I915_WRITE(LCPLL_CTL, val);
9584
9585 val = I915_READ(LCPLL_CTL);
9586 val &= ~LCPLL_CD_SOURCE_FCLK;
9587 I915_WRITE(LCPLL_CTL, val);
9588
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009589 if (wait_for_us((I915_READ(LCPLL_CTL) &
9590 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009591 DRM_ERROR("Switching back to LCPLL failed\n");
9592
9593 mutex_lock(&dev_priv->rps.hw_lock);
9594 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9595 mutex_unlock(&dev_priv->rps.hw_lock);
9596
9597 intel_update_cdclk(dev);
9598
9599 WARN(cdclk != dev_priv->cdclk_freq,
9600 "cdclk requested %d kHz but got %d kHz\n",
9601 cdclk, dev_priv->cdclk_freq);
9602}
9603
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009604static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009605{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009606 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009607 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009608 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009609 int cdclk;
9610
9611 /*
9612 * FIXME should also account for plane ratio
9613 * once 64bpp pixel formats are supported.
9614 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009615 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009616 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009617 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009618 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009619 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009620 cdclk = 450000;
9621 else
9622 cdclk = 337500;
9623
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009624 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009625 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9626 cdclk, dev_priv->max_cdclk_freq);
9627 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009628 }
9629
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009630 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9631 if (!intel_state->active_crtcs)
9632 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009633
9634 return 0;
9635}
9636
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009637static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009638{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009639 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009640 struct intel_atomic_state *old_intel_state =
9641 to_intel_atomic_state(old_state);
9642 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009643
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009644 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009645}
9646
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009647static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9648 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009649{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009650 struct intel_encoder *intel_encoder =
9651 intel_ddi_get_crtc_new_encoder(crtc_state);
9652
9653 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9654 if (!intel_ddi_pll_select(crtc, crtc_state))
9655 return -EINVAL;
9656 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009657
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009658 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009659
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009660 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009661}
9662
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309663static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9664 enum port port,
9665 struct intel_crtc_state *pipe_config)
9666{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009667 enum intel_dpll_id id;
9668
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309669 switch (port) {
9670 case PORT_A:
9671 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009672 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309673 break;
9674 case PORT_B:
9675 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009676 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309677 break;
9678 case PORT_C:
9679 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009680 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309681 break;
9682 default:
9683 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009684 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309685 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009686
9687 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309688}
9689
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009690static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9691 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009692 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009693{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009694 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009695 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009696
9697 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9698 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9699
9700 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009701 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009702 id = DPLL_ID_SKL_DPLL0;
9703 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009704 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009705 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009706 break;
9707 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009708 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009709 break;
9710 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009711 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009712 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009713 default:
9714 MISSING_CASE(pipe_config->ddi_pll_sel);
9715 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009716 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009717
9718 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009719}
9720
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009721static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9722 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009723 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009724{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009725 enum intel_dpll_id id;
9726
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009727 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9728
9729 switch (pipe_config->ddi_pll_sel) {
9730 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009731 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009732 break;
9733 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009734 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009735 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009736 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009737 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009738 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009739 case PORT_CLK_SEL_LCPLL_810:
9740 id = DPLL_ID_LCPLL_810;
9741 break;
9742 case PORT_CLK_SEL_LCPLL_1350:
9743 id = DPLL_ID_LCPLL_1350;
9744 break;
9745 case PORT_CLK_SEL_LCPLL_2700:
9746 id = DPLL_ID_LCPLL_2700;
9747 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009748 default:
9749 MISSING_CASE(pipe_config->ddi_pll_sel);
9750 /* fall through */
9751 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009752 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009753 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009754
9755 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009756}
9757
Jani Nikulacf304292016-03-18 17:05:41 +02009758static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9759 struct intel_crtc_state *pipe_config,
9760 unsigned long *power_domain_mask)
9761{
9762 struct drm_device *dev = crtc->base.dev;
9763 struct drm_i915_private *dev_priv = dev->dev_private;
9764 enum intel_display_power_domain power_domain;
9765 u32 tmp;
9766
9767 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9768
9769 /*
9770 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9771 * consistency and less surprising code; it's in always on power).
9772 */
9773 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9774 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9775 enum pipe trans_edp_pipe;
9776 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9777 default:
9778 WARN(1, "unknown pipe linked to edp transcoder\n");
9779 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9780 case TRANS_DDI_EDP_INPUT_A_ON:
9781 trans_edp_pipe = PIPE_A;
9782 break;
9783 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9784 trans_edp_pipe = PIPE_B;
9785 break;
9786 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9787 trans_edp_pipe = PIPE_C;
9788 break;
9789 }
9790
9791 if (trans_edp_pipe == crtc->pipe)
9792 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9793 }
9794
9795 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9796 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9797 return false;
9798 *power_domain_mask |= BIT(power_domain);
9799
9800 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9801
9802 return tmp & PIPECONF_ENABLE;
9803}
9804
Jani Nikula4d1de972016-03-18 17:05:42 +02009805static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9806 struct intel_crtc_state *pipe_config,
9807 unsigned long *power_domain_mask)
9808{
9809 struct drm_device *dev = crtc->base.dev;
9810 struct drm_i915_private *dev_priv = dev->dev_private;
9811 enum intel_display_power_domain power_domain;
9812 enum port port;
9813 enum transcoder cpu_transcoder;
9814 u32 tmp;
9815
9816 pipe_config->has_dsi_encoder = false;
9817
9818 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9819 if (port == PORT_A)
9820 cpu_transcoder = TRANSCODER_DSI_A;
9821 else
9822 cpu_transcoder = TRANSCODER_DSI_C;
9823
9824 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9825 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9826 continue;
9827 *power_domain_mask |= BIT(power_domain);
9828
9829 /* XXX: this works for video mode only */
9830 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9831 if (!(tmp & DPI_ENABLE))
9832 continue;
9833
9834 tmp = I915_READ(MIPI_CTRL(port));
9835 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9836 continue;
9837
9838 pipe_config->cpu_transcoder = cpu_transcoder;
9839 pipe_config->has_dsi_encoder = true;
9840 break;
9841 }
9842
9843 return pipe_config->has_dsi_encoder;
9844}
9845
Daniel Vetter26804af2014-06-25 22:01:55 +03009846static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009847 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009848{
9849 struct drm_device *dev = crtc->base.dev;
9850 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009851 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009852 enum port port;
9853 uint32_t tmp;
9854
9855 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9856
9857 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9858
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009859 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009860 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309861 else if (IS_BROXTON(dev))
9862 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009863 else
9864 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009865
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009866 pll = pipe_config->shared_dpll;
9867 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009868 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9869 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009870 }
9871
Daniel Vetter26804af2014-06-25 22:01:55 +03009872 /*
9873 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9874 * DDI E. So just check whether this pipe is wired to DDI E and whether
9875 * the PCH transcoder is on.
9876 */
Damien Lespiauca370452013-12-03 13:56:24 +00009877 if (INTEL_INFO(dev)->gen < 9 &&
9878 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009879 pipe_config->has_pch_encoder = true;
9880
9881 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9882 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9883 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9884
9885 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9886 }
9887}
9888
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009889static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009890 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009891{
9892 struct drm_device *dev = crtc->base.dev;
9893 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009894 enum intel_display_power_domain power_domain;
9895 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009896 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009897
Imre Deak17290502016-02-12 18:55:11 +02009898 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9899 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009900 return false;
Imre Deak17290502016-02-12 18:55:11 +02009901 power_domain_mask = BIT(power_domain);
9902
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009903 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009904
Jani Nikulacf304292016-03-18 17:05:41 +02009905 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009906
Jani Nikula4d1de972016-03-18 17:05:42 +02009907 if (IS_BROXTON(dev_priv)) {
9908 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9909 &power_domain_mask);
9910 WARN_ON(active && pipe_config->has_dsi_encoder);
9911 if (pipe_config->has_dsi_encoder)
9912 active = true;
9913 }
9914
Jani Nikulacf304292016-03-18 17:05:41 +02009915 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009916 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009917
Jani Nikula4d1de972016-03-18 17:05:42 +02009918 if (!pipe_config->has_dsi_encoder) {
9919 haswell_get_ddi_port_state(crtc, pipe_config);
9920 intel_get_pipe_timings(crtc, pipe_config);
9921 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009922
Jani Nikulabc58be62016-03-18 17:05:39 +02009923 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009924
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009925 pipe_config->gamma_mode =
9926 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9927
Chandra Kondurua1b22782015-04-07 15:28:45 -07009928 if (INTEL_INFO(dev)->gen >= 9) {
9929 skl_init_scalers(dev, crtc, pipe_config);
9930 }
9931
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009932 if (INTEL_INFO(dev)->gen >= 9) {
9933 pipe_config->scaler_state.scaler_id = -1;
9934 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9935 }
9936
Imre Deak17290502016-02-12 18:55:11 +02009937 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9938 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9939 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009940 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009941 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009942 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009943 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009944 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009945
Jesse Barnese59150d2014-01-07 13:30:45 -08009946 if (IS_HASWELL(dev))
9947 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9948 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009949
Jani Nikula4d1de972016-03-18 17:05:42 +02009950 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9951 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009952 pipe_config->pixel_multiplier =
9953 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9954 } else {
9955 pipe_config->pixel_multiplier = 1;
9956 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009957
Imre Deak17290502016-02-12 18:55:11 +02009958out:
9959 for_each_power_domain(power_domain, power_domain_mask)
9960 intel_display_power_put(dev_priv, power_domain);
9961
Jani Nikulacf304292016-03-18 17:05:41 +02009962 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009963}
9964
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009965static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9966 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009967{
9968 struct drm_device *dev = crtc->dev;
9969 struct drm_i915_private *dev_priv = dev->dev_private;
9970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009971 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009972
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009973 if (plane_state && plane_state->visible) {
9974 unsigned int width = plane_state->base.crtc_w;
9975 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009976 unsigned int stride = roundup_pow_of_two(width) * 4;
9977
9978 switch (stride) {
9979 default:
9980 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9981 width, stride);
9982 stride = 256;
9983 /* fallthrough */
9984 case 256:
9985 case 512:
9986 case 1024:
9987 case 2048:
9988 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009989 }
9990
Ville Syrjälädc41c152014-08-13 11:57:05 +03009991 cntl |= CURSOR_ENABLE |
9992 CURSOR_GAMMA_ENABLE |
9993 CURSOR_FORMAT_ARGB |
9994 CURSOR_STRIDE(stride);
9995
9996 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009997 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009998
Ville Syrjälädc41c152014-08-13 11:57:05 +03009999 if (intel_crtc->cursor_cntl != 0 &&
10000 (intel_crtc->cursor_base != base ||
10001 intel_crtc->cursor_size != size ||
10002 intel_crtc->cursor_cntl != cntl)) {
10003 /* On these chipsets we can only modify the base/size/stride
10004 * whilst the cursor is disabled.
10005 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010006 I915_WRITE(CURCNTR(PIPE_A), 0);
10007 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010008 intel_crtc->cursor_cntl = 0;
10009 }
10010
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010011 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010012 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010013 intel_crtc->cursor_base = base;
10014 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010015
10016 if (intel_crtc->cursor_size != size) {
10017 I915_WRITE(CURSIZE, size);
10018 intel_crtc->cursor_size = size;
10019 }
10020
Chris Wilson4b0e3332014-05-30 16:35:26 +030010021 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010022 I915_WRITE(CURCNTR(PIPE_A), cntl);
10023 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010024 intel_crtc->cursor_cntl = cntl;
10025 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010026}
10027
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010028static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10029 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010030{
10031 struct drm_device *dev = crtc->dev;
10032 struct drm_i915_private *dev_priv = dev->dev_private;
10033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10034 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010035 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010036
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010037 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010038 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010039 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010040 case 64:
10041 cntl |= CURSOR_MODE_64_ARGB_AX;
10042 break;
10043 case 128:
10044 cntl |= CURSOR_MODE_128_ARGB_AX;
10045 break;
10046 case 256:
10047 cntl |= CURSOR_MODE_256_ARGB_AX;
10048 break;
10049 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010050 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010051 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010052 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010053 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010054
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010055 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010056 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010057
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010058 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10059 cntl |= CURSOR_ROTATE_180;
10060 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010061
Chris Wilson4b0e3332014-05-30 16:35:26 +030010062 if (intel_crtc->cursor_cntl != cntl) {
10063 I915_WRITE(CURCNTR(pipe), cntl);
10064 POSTING_READ(CURCNTR(pipe));
10065 intel_crtc->cursor_cntl = cntl;
10066 }
10067
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010068 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010069 I915_WRITE(CURBASE(pipe), base);
10070 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010071
10072 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010073}
10074
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010075/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010076static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010077 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010078{
10079 struct drm_device *dev = crtc->dev;
10080 struct drm_i915_private *dev_priv = dev->dev_private;
10081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10082 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010083 u32 base = intel_crtc->cursor_addr;
10084 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010085
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010086 if (plane_state) {
10087 int x = plane_state->base.crtc_x;
10088 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010089
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010090 if (x < 0) {
10091 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10092 x = -x;
10093 }
10094 pos |= x << CURSOR_X_SHIFT;
10095
10096 if (y < 0) {
10097 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10098 y = -y;
10099 }
10100 pos |= y << CURSOR_Y_SHIFT;
10101
10102 /* ILK+ do this automagically */
10103 if (HAS_GMCH_DISPLAY(dev) &&
10104 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10105 base += (plane_state->base.crtc_h *
10106 plane_state->base.crtc_w - 1) * 4;
10107 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010108 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010109
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010110 I915_WRITE(CURPOS(pipe), pos);
10111
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010112 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010113 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010114 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010115 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010116}
10117
Ville Syrjälädc41c152014-08-13 11:57:05 +030010118static bool cursor_size_ok(struct drm_device *dev,
10119 uint32_t width, uint32_t height)
10120{
10121 if (width == 0 || height == 0)
10122 return false;
10123
10124 /*
10125 * 845g/865g are special in that they are only limited by
10126 * the width of their cursors, the height is arbitrary up to
10127 * the precision of the register. Everything else requires
10128 * square cursors, limited to a few power-of-two sizes.
10129 */
10130 if (IS_845G(dev) || IS_I865G(dev)) {
10131 if ((width & 63) != 0)
10132 return false;
10133
10134 if (width > (IS_845G(dev) ? 64 : 512))
10135 return false;
10136
10137 if (height > 1023)
10138 return false;
10139 } else {
10140 switch (width | height) {
10141 case 256:
10142 case 128:
10143 if (IS_GEN2(dev))
10144 return false;
10145 case 64:
10146 break;
10147 default:
10148 return false;
10149 }
10150 }
10151
10152 return true;
10153}
10154
Jesse Barnes79e53942008-11-07 14:24:08 -080010155/* VESA 640x480x72Hz mode to set on the pipe */
10156static struct drm_display_mode load_detect_mode = {
10157 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10158 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10159};
10160
Daniel Vettera8bb6812014-02-10 18:00:39 +010010161struct drm_framebuffer *
10162__intel_framebuffer_create(struct drm_device *dev,
10163 struct drm_mode_fb_cmd2 *mode_cmd,
10164 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010165{
10166 struct intel_framebuffer *intel_fb;
10167 int ret;
10168
10169 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010170 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010171 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010172
10173 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010174 if (ret)
10175 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010176
10177 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010178
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010179err:
10180 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010181 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010182}
10183
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010184static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010185intel_framebuffer_create(struct drm_device *dev,
10186 struct drm_mode_fb_cmd2 *mode_cmd,
10187 struct drm_i915_gem_object *obj)
10188{
10189 struct drm_framebuffer *fb;
10190 int ret;
10191
10192 ret = i915_mutex_lock_interruptible(dev);
10193 if (ret)
10194 return ERR_PTR(ret);
10195 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10196 mutex_unlock(&dev->struct_mutex);
10197
10198 return fb;
10199}
10200
Chris Wilsond2dff872011-04-19 08:36:26 +010010201static u32
10202intel_framebuffer_pitch_for_width(int width, int bpp)
10203{
10204 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10205 return ALIGN(pitch, 64);
10206}
10207
10208static u32
10209intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10210{
10211 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010212 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010213}
10214
10215static struct drm_framebuffer *
10216intel_framebuffer_create_for_mode(struct drm_device *dev,
10217 struct drm_display_mode *mode,
10218 int depth, int bpp)
10219{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010220 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010221 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010222 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010223
10224 obj = i915_gem_alloc_object(dev,
10225 intel_framebuffer_size_for_mode(mode, bpp));
10226 if (obj == NULL)
10227 return ERR_PTR(-ENOMEM);
10228
10229 mode_cmd.width = mode->hdisplay;
10230 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010231 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10232 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010233 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010234
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010235 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10236 if (IS_ERR(fb))
10237 drm_gem_object_unreference_unlocked(&obj->base);
10238
10239 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010240}
10241
10242static struct drm_framebuffer *
10243mode_fits_in_fbdev(struct drm_device *dev,
10244 struct drm_display_mode *mode)
10245{
Daniel Vetter06957262015-08-10 13:34:08 +020010246#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010247 struct drm_i915_private *dev_priv = dev->dev_private;
10248 struct drm_i915_gem_object *obj;
10249 struct drm_framebuffer *fb;
10250
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010251 if (!dev_priv->fbdev)
10252 return NULL;
10253
10254 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010255 return NULL;
10256
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010257 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010258 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010259
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010260 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010261 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10262 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010263 return NULL;
10264
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010265 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010266 return NULL;
10267
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010268 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010269 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010270#else
10271 return NULL;
10272#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010273}
10274
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010275static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10276 struct drm_crtc *crtc,
10277 struct drm_display_mode *mode,
10278 struct drm_framebuffer *fb,
10279 int x, int y)
10280{
10281 struct drm_plane_state *plane_state;
10282 int hdisplay, vdisplay;
10283 int ret;
10284
10285 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10286 if (IS_ERR(plane_state))
10287 return PTR_ERR(plane_state);
10288
10289 if (mode)
10290 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10291 else
10292 hdisplay = vdisplay = 0;
10293
10294 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10295 if (ret)
10296 return ret;
10297 drm_atomic_set_fb_for_plane(plane_state, fb);
10298 plane_state->crtc_x = 0;
10299 plane_state->crtc_y = 0;
10300 plane_state->crtc_w = hdisplay;
10301 plane_state->crtc_h = vdisplay;
10302 plane_state->src_x = x << 16;
10303 plane_state->src_y = y << 16;
10304 plane_state->src_w = hdisplay << 16;
10305 plane_state->src_h = vdisplay << 16;
10306
10307 return 0;
10308}
10309
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010310bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010311 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010312 struct intel_load_detect_pipe *old,
10313 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010314{
10315 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010316 struct intel_encoder *intel_encoder =
10317 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010318 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010319 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010320 struct drm_crtc *crtc = NULL;
10321 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010322 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010323 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010324 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010325 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010326 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010327 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010328
Chris Wilsond2dff872011-04-19 08:36:26 +010010329 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010330 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010331 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010332
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010333 old->restore_state = NULL;
10334
Rob Clark51fd3712013-11-19 12:10:12 -050010335retry:
10336 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10337 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010338 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010339
Jesse Barnes79e53942008-11-07 14:24:08 -080010340 /*
10341 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010342 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010343 * - if the connector already has an assigned crtc, use it (but make
10344 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010345 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010346 * - try to find the first unused crtc that can drive this connector,
10347 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010348 */
10349
10350 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010351 if (connector->state->crtc) {
10352 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010353
Rob Clark51fd3712013-11-19 12:10:12 -050010354 ret = drm_modeset_lock(&crtc->mutex, ctx);
10355 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010356 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010357
10358 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010359 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010360 }
10361
10362 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010363 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010364 i++;
10365 if (!(encoder->possible_crtcs & (1 << i)))
10366 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010367
10368 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10369 if (ret)
10370 goto fail;
10371
10372 if (possible_crtc->state->enable) {
10373 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010374 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010375 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010376
10377 crtc = possible_crtc;
10378 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 }
10380
10381 /*
10382 * If we didn't find an unused CRTC, don't use any.
10383 */
10384 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010385 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010386 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010387 }
10388
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010389found:
10390 intel_crtc = to_intel_crtc(crtc);
10391
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010392 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10393 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010394 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010395
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010396 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010397 restore_state = drm_atomic_state_alloc(dev);
10398 if (!state || !restore_state) {
10399 ret = -ENOMEM;
10400 goto fail;
10401 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010402
10403 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010404 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010405
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010406 connector_state = drm_atomic_get_connector_state(state, connector);
10407 if (IS_ERR(connector_state)) {
10408 ret = PTR_ERR(connector_state);
10409 goto fail;
10410 }
10411
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010412 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10413 if (ret)
10414 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010415
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010416 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10417 if (IS_ERR(crtc_state)) {
10418 ret = PTR_ERR(crtc_state);
10419 goto fail;
10420 }
10421
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010422 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010423
Chris Wilson64927112011-04-20 07:25:26 +010010424 if (!mode)
10425 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010426
Chris Wilsond2dff872011-04-19 08:36:26 +010010427 /* We need a framebuffer large enough to accommodate all accesses
10428 * that the plane may generate whilst we perform load detection.
10429 * We can not rely on the fbcon either being present (we get called
10430 * during its initialisation to detect all boot displays, or it may
10431 * not even exist) or that it is large enough to satisfy the
10432 * requested mode.
10433 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010434 fb = mode_fits_in_fbdev(dev, mode);
10435 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010436 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010437 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010438 } else
10439 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010440 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010441 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010442 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010443 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010444
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010445 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10446 if (ret)
10447 goto fail;
10448
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010449 drm_framebuffer_unreference(fb);
10450
10451 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10452 if (ret)
10453 goto fail;
10454
10455 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10456 if (!ret)
10457 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10458 if (!ret)
10459 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10460 if (ret) {
10461 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10462 goto fail;
10463 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010464
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010465 ret = drm_atomic_commit(state);
10466 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010467 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010468 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010469 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010470
10471 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010472
Jesse Barnes79e53942008-11-07 14:24:08 -080010473 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010474 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010475 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010476
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010477fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010478 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010479 drm_atomic_state_free(restore_state);
10480 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010481
Rob Clark51fd3712013-11-19 12:10:12 -050010482 if (ret == -EDEADLK) {
10483 drm_modeset_backoff(ctx);
10484 goto retry;
10485 }
10486
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010487 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010488}
10489
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010490void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010491 struct intel_load_detect_pipe *old,
10492 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010493{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010494 struct intel_encoder *intel_encoder =
10495 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010496 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010497 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010498 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010499
Chris Wilsond2dff872011-04-19 08:36:26 +010010500 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010501 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010502 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010503
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010504 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010505 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010506
10507 ret = drm_atomic_commit(state);
10508 if (ret) {
10509 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10510 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010511 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010512}
10513
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010514static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010515 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010516{
10517 struct drm_i915_private *dev_priv = dev->dev_private;
10518 u32 dpll = pipe_config->dpll_hw_state.dpll;
10519
10520 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010521 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010522 else if (HAS_PCH_SPLIT(dev))
10523 return 120000;
10524 else if (!IS_GEN2(dev))
10525 return 96000;
10526 else
10527 return 48000;
10528}
10529
Jesse Barnes79e53942008-11-07 14:24:08 -080010530/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010531static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010532 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010533{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010534 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010535 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010536 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010537 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010538 u32 fp;
10539 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010540 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010541 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010542
10543 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010544 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010545 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010546 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010547
10548 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010549 if (IS_PINEVIEW(dev)) {
10550 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10551 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010552 } else {
10553 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10554 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10555 }
10556
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010557 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010558 if (IS_PINEVIEW(dev))
10559 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10560 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010561 else
10562 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 DPLL_FPA01_P1_POST_DIV_SHIFT);
10564
10565 switch (dpll & DPLL_MODE_MASK) {
10566 case DPLLB_MODE_DAC_SERIAL:
10567 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10568 5 : 10;
10569 break;
10570 case DPLLB_MODE_LVDS:
10571 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10572 7 : 14;
10573 break;
10574 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010575 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010577 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010578 }
10579
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010580 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010581 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010582 else
Imre Deakdccbea32015-06-22 23:35:51 +030010583 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010584 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010585 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010586 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010587
10588 if (is_lvds) {
10589 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10590 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010591
10592 if (lvds & LVDS_CLKB_POWER_UP)
10593 clock.p2 = 7;
10594 else
10595 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596 } else {
10597 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10598 clock.p1 = 2;
10599 else {
10600 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10601 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10602 }
10603 if (dpll & PLL_P2_DIVIDE_BY_4)
10604 clock.p2 = 4;
10605 else
10606 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010607 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010608
Imre Deakdccbea32015-06-22 23:35:51 +030010609 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010610 }
10611
Ville Syrjälä18442d02013-09-13 16:00:08 +030010612 /*
10613 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010614 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010615 * encoder's get_config() function.
10616 */
Imre Deakdccbea32015-06-22 23:35:51 +030010617 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010618}
10619
Ville Syrjälä6878da02013-09-13 15:59:11 +030010620int intel_dotclock_calculate(int link_freq,
10621 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010622{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010623 /*
10624 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010625 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010626 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010627 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010628 *
10629 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010630 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010631 */
10632
Ville Syrjälä6878da02013-09-13 15:59:11 +030010633 if (!m_n->link_n)
10634 return 0;
10635
10636 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10637}
10638
Ville Syrjälä18442d02013-09-13 16:00:08 +030010639static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010640 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010641{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010642 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010643
10644 /* read out port_clock from the DPLL */
10645 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010646
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010647 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010648 * In case there is an active pipe without active ports,
10649 * we may need some idea for the dotclock anyway.
10650 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010651 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010652 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010653 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010654 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010655}
10656
10657/** Returns the currently programmed mode of the given pipe. */
10658struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10659 struct drm_crtc *crtc)
10660{
Jesse Barnes548f2452011-02-17 10:40:53 -080010661 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010663 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010664 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010665 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010666 int htot = I915_READ(HTOTAL(cpu_transcoder));
10667 int hsync = I915_READ(HSYNC(cpu_transcoder));
10668 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10669 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010670 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010671
10672 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10673 if (!mode)
10674 return NULL;
10675
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010676 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10677 if (!pipe_config) {
10678 kfree(mode);
10679 return NULL;
10680 }
10681
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010682 /*
10683 * Construct a pipe_config sufficient for getting the clock info
10684 * back out of crtc_clock_get.
10685 *
10686 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10687 * to use a real value here instead.
10688 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010689 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10690 pipe_config->pixel_multiplier = 1;
10691 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10692 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10693 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10694 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010695
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010696 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010697 mode->hdisplay = (htot & 0xffff) + 1;
10698 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10699 mode->hsync_start = (hsync & 0xffff) + 1;
10700 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10701 mode->vdisplay = (vtot & 0xffff) + 1;
10702 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10703 mode->vsync_start = (vsync & 0xffff) + 1;
10704 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10705
10706 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010707
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010708 kfree(pipe_config);
10709
Jesse Barnes79e53942008-11-07 14:24:08 -080010710 return mode;
10711}
10712
Chris Wilsonf047e392012-07-21 12:31:41 +010010713void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010714{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010715 struct drm_i915_private *dev_priv = dev->dev_private;
10716
Chris Wilsonf62a0072014-02-21 17:55:39 +000010717 if (dev_priv->mm.busy)
10718 return;
10719
Paulo Zanoni43694d62014-03-07 20:08:08 -030010720 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010721 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010722 if (INTEL_INFO(dev)->gen >= 6)
10723 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010724 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010725}
10726
10727void intel_mark_idle(struct drm_device *dev)
10728{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010729 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010730
Chris Wilsonf62a0072014-02-21 17:55:39 +000010731 if (!dev_priv->mm.busy)
10732 return;
10733
10734 dev_priv->mm.busy = false;
10735
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010736 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010737 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010738
Paulo Zanoni43694d62014-03-07 20:08:08 -030010739 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010740}
10741
Jesse Barnes79e53942008-11-07 14:24:08 -080010742static void intel_crtc_destroy(struct drm_crtc *crtc)
10743{
10744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010745 struct drm_device *dev = crtc->dev;
10746 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010747
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010748 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010749 work = intel_crtc->unpin_work;
10750 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010751 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010752
10753 if (work) {
10754 cancel_work_sync(&work->work);
10755 kfree(work);
10756 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010757
10758 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010759
Jesse Barnes79e53942008-11-07 14:24:08 -080010760 kfree(intel_crtc);
10761}
10762
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010763static void intel_unpin_work_fn(struct work_struct *__work)
10764{
10765 struct intel_unpin_work *work =
10766 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010767 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10768 struct drm_device *dev = crtc->base.dev;
10769 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010770
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010771 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010772 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010773 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010774
John Harrisonf06cc1b2014-11-24 18:49:37 +000010775 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010776 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010777 mutex_unlock(&dev->struct_mutex);
10778
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010779 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010780 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010781 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010782
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010783 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10784 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010785
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010786 kfree(work);
10787}
10788
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010789static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010790 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010791{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10793 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010794 unsigned long flags;
10795
10796 /* Ignore early vblank irqs */
10797 if (intel_crtc == NULL)
10798 return;
10799
Daniel Vetterf3260382014-09-15 14:55:23 +020010800 /*
10801 * This is called both by irq handlers and the reset code (to complete
10802 * lost pageflips) so needs the full irqsave spinlocks.
10803 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010804 spin_lock_irqsave(&dev->event_lock, flags);
10805 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010806
10807 /* Ensure we don't miss a work->pending update ... */
10808 smp_rmb();
10809
10810 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010811 spin_unlock_irqrestore(&dev->event_lock, flags);
10812 return;
10813 }
10814
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010815 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010816
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010817 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010818}
10819
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010820void intel_finish_page_flip(struct drm_device *dev, int pipe)
10821{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010822 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010823 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10824
Mario Kleiner49b14a52010-12-09 07:00:07 +010010825 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010826}
10827
10828void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10829{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010830 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010831 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10832
Mario Kleiner49b14a52010-12-09 07:00:07 +010010833 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010834}
10835
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010836/* Is 'a' after or equal to 'b'? */
10837static bool g4x_flip_count_after_eq(u32 a, u32 b)
10838{
10839 return !((a - b) & 0x80000000);
10840}
10841
10842static bool page_flip_finished(struct intel_crtc *crtc)
10843{
10844 struct drm_device *dev = crtc->base.dev;
10845 struct drm_i915_private *dev_priv = dev->dev_private;
10846
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010847 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10848 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10849 return true;
10850
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010851 /*
10852 * The relevant registers doen't exist on pre-ctg.
10853 * As the flip done interrupt doesn't trigger for mmio
10854 * flips on gmch platforms, a flip count check isn't
10855 * really needed there. But since ctg has the registers,
10856 * include it in the check anyway.
10857 */
10858 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10859 return true;
10860
10861 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010862 * BDW signals flip done immediately if the plane
10863 * is disabled, even if the plane enable is already
10864 * armed to occur at the next vblank :(
10865 */
10866
10867 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010868 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10869 * used the same base address. In that case the mmio flip might
10870 * have completed, but the CS hasn't even executed the flip yet.
10871 *
10872 * A flip count check isn't enough as the CS might have updated
10873 * the base address just after start of vblank, but before we
10874 * managed to process the interrupt. This means we'd complete the
10875 * CS flip too soon.
10876 *
10877 * Combining both checks should get us a good enough result. It may
10878 * still happen that the CS flip has been executed, but has not
10879 * yet actually completed. But in case the base address is the same
10880 * anyway, we don't really care.
10881 */
10882 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10883 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010884 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010885 crtc->unpin_work->flip_count);
10886}
10887
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010888void intel_prepare_page_flip(struct drm_device *dev, int plane)
10889{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010890 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010891 struct intel_crtc *intel_crtc =
10892 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10893 unsigned long flags;
10894
Daniel Vetterf3260382014-09-15 14:55:23 +020010895
10896 /*
10897 * This is called both by irq handlers and the reset code (to complete
10898 * lost pageflips) so needs the full irqsave spinlocks.
10899 *
10900 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010901 * generate a page-flip completion irq, i.e. every modeset
10902 * is also accompanied by a spurious intel_prepare_page_flip().
10903 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010904 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010905 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010906 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010907 spin_unlock_irqrestore(&dev->event_lock, flags);
10908}
10909
Chris Wilson60426392015-10-10 10:44:32 +010010910static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010911{
10912 /* Ensure that the work item is consistent when activating it ... */
10913 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010914 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010915 /* and that it is marked active as soon as the irq could fire. */
10916 smp_wmb();
10917}
10918
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010919static int intel_gen2_queue_flip(struct drm_device *dev,
10920 struct drm_crtc *crtc,
10921 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010922 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010923 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010924 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010925{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010926 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010928 u32 flip_mask;
10929 int ret;
10930
John Harrison5fb9de12015-05-29 17:44:07 +010010931 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010932 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010933 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010934
10935 /* Can't queue multiple flips, so wait for the previous
10936 * one to finish before executing the next.
10937 */
10938 if (intel_crtc->plane)
10939 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10940 else
10941 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010942 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10943 intel_ring_emit(engine, MI_NOOP);
10944 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010945 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010946 intel_ring_emit(engine, fb->pitches[0]);
10947 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10948 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010949
Chris Wilson60426392015-10-10 10:44:32 +010010950 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010951 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010952}
10953
10954static int intel_gen3_queue_flip(struct drm_device *dev,
10955 struct drm_crtc *crtc,
10956 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010957 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010958 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010959 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010960{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010961 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010963 u32 flip_mask;
10964 int ret;
10965
John Harrison5fb9de12015-05-29 17:44:07 +010010966 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010967 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010968 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010969
10970 if (intel_crtc->plane)
10971 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10972 else
10973 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010974 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10975 intel_ring_emit(engine, MI_NOOP);
10976 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010977 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010978 intel_ring_emit(engine, fb->pitches[0]);
10979 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10980 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010981
Chris Wilson60426392015-10-10 10:44:32 +010010982 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010983 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010984}
10985
10986static int intel_gen4_queue_flip(struct drm_device *dev,
10987 struct drm_crtc *crtc,
10988 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010989 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010990 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010991 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010993 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010994 struct drm_i915_private *dev_priv = dev->dev_private;
10995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10996 uint32_t pf, pipesrc;
10997 int ret;
10998
John Harrison5fb9de12015-05-29 17:44:07 +010010999 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011000 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011001 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011002
11003 /* i965+ uses the linear or tiled offsets from the
11004 * Display Registers (which do not change across a page-flip)
11005 * so we need only reprogram the base address.
11006 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011007 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011008 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011009 intel_ring_emit(engine, fb->pitches[0]);
11010 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011011 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012
11013 /* XXX Enabling the panel-fitter across page-flip is so far
11014 * untested on non-native modes, so ignore it for now.
11015 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11016 */
11017 pf = 0;
11018 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011019 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011020
Chris Wilson60426392015-10-10 10:44:32 +010011021 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011022 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023}
11024
11025static int intel_gen6_queue_flip(struct drm_device *dev,
11026 struct drm_crtc *crtc,
11027 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011028 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011029 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011030 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011031{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011032 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011033 struct drm_i915_private *dev_priv = dev->dev_private;
11034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11035 uint32_t pf, pipesrc;
11036 int ret;
11037
John Harrison5fb9de12015-05-29 17:44:07 +010011038 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011040 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011042 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011043 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011044 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11045 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011046
Chris Wilson99d9acd2012-04-17 20:37:00 +010011047 /* Contrary to the suggestions in the documentation,
11048 * "Enable Panel Fitter" does not seem to be required when page
11049 * flipping with a non-native mode, and worse causes a normal
11050 * modeset to fail.
11051 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11052 */
11053 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011054 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011055 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011056
Chris Wilson60426392015-10-10 10:44:32 +010011057 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011058 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011059}
11060
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011061static int intel_gen7_queue_flip(struct drm_device *dev,
11062 struct drm_crtc *crtc,
11063 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011064 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011065 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011066 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011067{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011068 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011070 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011071 int len, ret;
11072
Robin Schroereba905b2014-05-18 02:24:50 +020011073 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011074 case PLANE_A:
11075 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11076 break;
11077 case PLANE_B:
11078 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11079 break;
11080 case PLANE_C:
11081 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11082 break;
11083 default:
11084 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011085 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011086 }
11087
Chris Wilsonffe74d72013-08-26 20:58:12 +010011088 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011089 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011090 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011091 /*
11092 * On Gen 8, SRM is now taking an extra dword to accommodate
11093 * 48bits addresses, and we need a NOOP for the batch size to
11094 * stay even.
11095 */
11096 if (IS_GEN8(dev))
11097 len += 2;
11098 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011099
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011100 /*
11101 * BSpec MI_DISPLAY_FLIP for IVB:
11102 * "The full packet must be contained within the same cache line."
11103 *
11104 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11105 * cacheline, if we ever start emitting more commands before
11106 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11107 * then do the cacheline alignment, and finally emit the
11108 * MI_DISPLAY_FLIP.
11109 */
John Harrisonbba09b12015-05-29 17:44:06 +010011110 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011111 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011112 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011113
John Harrison5fb9de12015-05-29 17:44:07 +010011114 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011115 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011116 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011117
Chris Wilsonffe74d72013-08-26 20:58:12 +010011118 /* Unmask the flip-done completion message. Note that the bspec says that
11119 * we should do this for both the BCS and RCS, and that we must not unmask
11120 * more than one flip event at any time (or ensure that one flip message
11121 * can be sent by waiting for flip-done prior to queueing new flips).
11122 * Experimentation says that BCS works despite DERRMR masking all
11123 * flip-done completion events and that unmasking all planes at once
11124 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11125 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11126 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011127 if (engine->id == RCS) {
11128 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11129 intel_ring_emit_reg(engine, DERRMR);
11130 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11131 DERRMR_PIPEB_PRI_FLIP_DONE |
11132 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011133 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011134 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011135 MI_SRM_LRM_GLOBAL_GTT);
11136 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011137 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011138 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011139 intel_ring_emit_reg(engine, DERRMR);
11140 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011141 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011142 intel_ring_emit(engine, 0);
11143 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011144 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011145 }
11146
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011147 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11148 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11149 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11150 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011151
Chris Wilson60426392015-10-10 10:44:32 +010011152 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011153 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011154}
11155
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011156static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011157 struct drm_i915_gem_object *obj)
11158{
11159 /*
11160 * This is not being used for older platforms, because
11161 * non-availability of flip done interrupt forces us to use
11162 * CS flips. Older platforms derive flip done using some clever
11163 * tricks involving the flip_pending status bits and vblank irqs.
11164 * So using MMIO flips there would disrupt this mechanism.
11165 */
11166
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011167 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011168 return true;
11169
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011170 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011171 return false;
11172
11173 if (i915.use_mmio_flip < 0)
11174 return false;
11175 else if (i915.use_mmio_flip > 0)
11176 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011177 else if (i915.enable_execlists)
11178 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011179 else if (obj->base.dma_buf &&
11180 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11181 false))
11182 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011183 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011184 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011185}
11186
Chris Wilson60426392015-10-10 10:44:32 +010011187static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011188 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011189 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011190{
11191 struct drm_device *dev = intel_crtc->base.dev;
11192 struct drm_i915_private *dev_priv = dev->dev_private;
11193 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011194 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011195 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011196
11197 ctl = I915_READ(PLANE_CTL(pipe, 0));
11198 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011199 switch (fb->modifier[0]) {
11200 case DRM_FORMAT_MOD_NONE:
11201 break;
11202 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011203 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011204 break;
11205 case I915_FORMAT_MOD_Y_TILED:
11206 ctl |= PLANE_CTL_TILED_Y;
11207 break;
11208 case I915_FORMAT_MOD_Yf_TILED:
11209 ctl |= PLANE_CTL_TILED_YF;
11210 break;
11211 default:
11212 MISSING_CASE(fb->modifier[0]);
11213 }
Damien Lespiauff944562014-11-20 14:58:16 +000011214
11215 /*
11216 * The stride is either expressed as a multiple of 64 bytes chunks for
11217 * linear buffers or in number of tiles for tiled buffers.
11218 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011219 if (intel_rotation_90_or_270(rotation)) {
11220 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011221 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011222 stride = DIV_ROUND_UP(fb->height, tile_height);
11223 } else {
11224 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011225 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11226 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011227 }
Damien Lespiauff944562014-11-20 14:58:16 +000011228
11229 /*
11230 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11231 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11232 */
11233 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11234 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11235
Chris Wilson60426392015-10-10 10:44:32 +010011236 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011237 POSTING_READ(PLANE_SURF(pipe, 0));
11238}
11239
Chris Wilson60426392015-10-10 10:44:32 +010011240static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11241 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011242{
11243 struct drm_device *dev = intel_crtc->base.dev;
11244 struct drm_i915_private *dev_priv = dev->dev_private;
11245 struct intel_framebuffer *intel_fb =
11246 to_intel_framebuffer(intel_crtc->base.primary->fb);
11247 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011248 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011249 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011250
Sourab Gupta84c33a62014-06-02 16:47:17 +053011251 dspcntr = I915_READ(reg);
11252
Damien Lespiauc5d97472014-10-25 00:11:11 +010011253 if (obj->tiling_mode != I915_TILING_NONE)
11254 dspcntr |= DISPPLANE_TILED;
11255 else
11256 dspcntr &= ~DISPPLANE_TILED;
11257
Sourab Gupta84c33a62014-06-02 16:47:17 +053011258 I915_WRITE(reg, dspcntr);
11259
Chris Wilson60426392015-10-10 10:44:32 +010011260 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011261 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011262}
11263
11264/*
11265 * XXX: This is the temporary way to update the plane registers until we get
11266 * around to using the usual plane update functions for MMIO flips
11267 */
Chris Wilson60426392015-10-10 10:44:32 +010011268static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011269{
Chris Wilson60426392015-10-10 10:44:32 +010011270 struct intel_crtc *crtc = mmio_flip->crtc;
11271 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011272
Chris Wilson60426392015-10-10 10:44:32 +010011273 spin_lock_irq(&crtc->base.dev->event_lock);
11274 work = crtc->unpin_work;
11275 spin_unlock_irq(&crtc->base.dev->event_lock);
11276 if (work == NULL)
11277 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011278
Chris Wilson60426392015-10-10 10:44:32 +010011279 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011280
Chris Wilson60426392015-10-10 10:44:32 +010011281 intel_pipe_update_start(crtc);
11282
11283 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011284 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011285 else
11286 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011287 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011288
Chris Wilson60426392015-10-10 10:44:32 +010011289 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011290}
11291
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011292static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011293{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011294 struct intel_mmio_flip *mmio_flip =
11295 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011296 struct intel_framebuffer *intel_fb =
11297 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11298 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011299
Chris Wilson60426392015-10-10 10:44:32 +010011300 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011301 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011302 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011303 false, NULL,
11304 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011305 i915_gem_request_unreference__unlocked(mmio_flip->req);
11306 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011307
Alex Goinsfd8e0582015-11-25 18:43:38 -080011308 /* For framebuffer backed by dmabuf, wait for fence */
11309 if (obj->base.dma_buf)
11310 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11311 false, false,
11312 MAX_SCHEDULE_TIMEOUT) < 0);
11313
Chris Wilson60426392015-10-10 10:44:32 +010011314 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011315 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011316}
11317
11318static int intel_queue_mmio_flip(struct drm_device *dev,
11319 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011320 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011321{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011322 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011323
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011324 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11325 if (mmio_flip == NULL)
11326 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011327
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011328 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011329 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011330 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011331 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011332
11333 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11334 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011335
Sourab Gupta84c33a62014-06-02 16:47:17 +053011336 return 0;
11337}
11338
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011339static int intel_default_queue_flip(struct drm_device *dev,
11340 struct drm_crtc *crtc,
11341 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011342 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011343 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011344 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011345{
11346 return -ENODEV;
11347}
11348
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011349static bool __intel_pageflip_stall_check(struct drm_device *dev,
11350 struct drm_crtc *crtc)
11351{
11352 struct drm_i915_private *dev_priv = dev->dev_private;
11353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11354 struct intel_unpin_work *work = intel_crtc->unpin_work;
11355 u32 addr;
11356
11357 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11358 return true;
11359
Chris Wilson908565c2015-08-12 13:08:22 +010011360 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11361 return false;
11362
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011363 if (!work->enable_stall_check)
11364 return false;
11365
11366 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011367 if (work->flip_queued_req &&
11368 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011369 return false;
11370
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011371 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011372 }
11373
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011374 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011375 return false;
11376
11377 /* Potential stall - if we see that the flip has happened,
11378 * assume a missed interrupt. */
11379 if (INTEL_INFO(dev)->gen >= 4)
11380 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11381 else
11382 addr = I915_READ(DSPADDR(intel_crtc->plane));
11383
11384 /* There is a potential issue here with a false positive after a flip
11385 * to the same address. We could address this by checking for a
11386 * non-incrementing frame counter.
11387 */
11388 return addr == work->gtt_offset;
11389}
11390
11391void intel_check_page_flip(struct drm_device *dev, int pipe)
11392{
11393 struct drm_i915_private *dev_priv = dev->dev_private;
11394 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011396 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011397
Dave Gordon6c51d462015-03-06 15:34:26 +000011398 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011399
11400 if (crtc == NULL)
11401 return;
11402
Daniel Vetterf3260382014-09-15 14:55:23 +020011403 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011404 work = intel_crtc->unpin_work;
11405 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011406 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011407 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011408 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011409 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011410 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011411 if (work != NULL &&
11412 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11413 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011414 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011415}
11416
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011417static int intel_crtc_page_flip(struct drm_crtc *crtc,
11418 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011419 struct drm_pending_vblank_event *event,
11420 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011421{
11422 struct drm_device *dev = crtc->dev;
11423 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011424 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011425 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011427 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011428 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011429 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011430 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011431 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011432 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011433 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011434
Matt Roper2ff8fde2014-07-08 07:50:07 -070011435 /*
11436 * drm_mode_page_flip_ioctl() should already catch this, but double
11437 * check to be safe. In the future we may enable pageflipping from
11438 * a disabled primary plane.
11439 */
11440 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11441 return -EBUSY;
11442
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011443 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011444 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011445 return -EINVAL;
11446
11447 /*
11448 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11449 * Note that pitch changes could also affect these register.
11450 */
11451 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011452 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11453 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011454 return -EINVAL;
11455
Chris Wilsonf900db42014-02-20 09:26:13 +000011456 if (i915_terminally_wedged(&dev_priv->gpu_error))
11457 goto out_hang;
11458
Daniel Vetterb14c5672013-09-19 12:18:32 +020011459 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011460 if (work == NULL)
11461 return -ENOMEM;
11462
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011463 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011464 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011465 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011466 INIT_WORK(&work->work, intel_unpin_work_fn);
11467
Daniel Vetter87b6b102014-05-15 15:33:46 +020011468 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011469 if (ret)
11470 goto free_work;
11471
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011472 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011473 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011474 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011475 /* Before declaring the flip queue wedged, check if
11476 * the hardware completed the operation behind our backs.
11477 */
11478 if (__intel_pageflip_stall_check(dev, crtc)) {
11479 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11480 page_flip_completed(intel_crtc);
11481 } else {
11482 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011483 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011484
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011485 drm_crtc_vblank_put(crtc);
11486 kfree(work);
11487 return -EBUSY;
11488 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011489 }
11490 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011491 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011492
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011493 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11494 flush_workqueue(dev_priv->wq);
11495
Jesse Barnes75dfca82010-02-10 15:09:44 -080011496 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011497 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011498 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011499
Matt Roperf4510a22014-04-01 15:22:40 -070011500 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011501 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011502 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011503
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011504 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011505
Chris Wilson89ed88b2015-02-16 14:31:49 +000011506 ret = i915_mutex_lock_interruptible(dev);
11507 if (ret)
11508 goto cleanup;
11509
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011510 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011511 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011512
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011513 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011514 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011515
Wayne Boyer666a4532015-12-09 12:29:35 -080011516 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011517 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011518 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011519 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011520 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011521 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011522 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011523 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011524 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011525 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011526 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011527 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011528 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011529 }
11530
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011531 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011532
11533 /* When using CS flips, we want to emit semaphores between rings.
11534 * However, when using mmio flips we will create a task to do the
11535 * synchronisation, so all we want here is to pin the framebuffer
11536 * into the display plane and skip any waits.
11537 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011538 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011539 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011540 if (ret)
11541 goto cleanup_pending;
11542 }
11543
Ville Syrjälä3465c582016-02-15 22:54:43 +020011544 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011545 if (ret)
11546 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011547
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011548 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11549 obj, 0);
11550 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011551
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011552 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011553 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011554 if (ret)
11555 goto cleanup_unpin;
11556
John Harrisonf06cc1b2014-11-24 18:49:37 +000011557 i915_gem_request_assign(&work->flip_queued_req,
11558 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011559 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011560 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011561 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011562 if (IS_ERR(request)) {
11563 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011564 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011565 }
John Harrison6258fbe2015-05-29 17:43:48 +010011566 }
11567
11568 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011569 page_flip_flags);
11570 if (ret)
11571 goto cleanup_unpin;
11572
John Harrison6258fbe2015-05-29 17:43:48 +010011573 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011574 }
11575
John Harrison91af1272015-06-18 13:14:56 +010011576 if (request)
John Harrison75289872015-05-29 17:43:49 +010011577 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011578
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011579 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011580 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011581
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011582 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011583 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011584 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011585
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011586 intel_frontbuffer_flip_prepare(dev,
11587 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011588
Jesse Barnese5510fa2010-07-01 16:48:37 -070011589 trace_i915_flip_request(intel_crtc->plane, obj);
11590
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011591 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011592
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011593cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011594 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011595cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011596 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011597 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011598 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011599 mutex_unlock(&dev->struct_mutex);
11600cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011601 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011602 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011603
Chris Wilson89ed88b2015-02-16 14:31:49 +000011604 drm_gem_object_unreference_unlocked(&obj->base);
11605 drm_framebuffer_unreference(work->old_fb);
11606
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011607 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011608 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011609 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011610
Daniel Vetter87b6b102014-05-15 15:33:46 +020011611 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011612free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011613 kfree(work);
11614
Chris Wilsonf900db42014-02-20 09:26:13 +000011615 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011616 struct drm_atomic_state *state;
11617 struct drm_plane_state *plane_state;
11618
Chris Wilsonf900db42014-02-20 09:26:13 +000011619out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011620 state = drm_atomic_state_alloc(dev);
11621 if (!state)
11622 return -ENOMEM;
11623 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11624
11625retry:
11626 plane_state = drm_atomic_get_plane_state(state, primary);
11627 ret = PTR_ERR_OR_ZERO(plane_state);
11628 if (!ret) {
11629 drm_atomic_set_fb_for_plane(plane_state, fb);
11630
11631 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11632 if (!ret)
11633 ret = drm_atomic_commit(state);
11634 }
11635
11636 if (ret == -EDEADLK) {
11637 drm_modeset_backoff(state->acquire_ctx);
11638 drm_atomic_state_clear(state);
11639 goto retry;
11640 }
11641
11642 if (ret)
11643 drm_atomic_state_free(state);
11644
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011645 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011646 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011647 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011648 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011649 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011650 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011651 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011652}
11653
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011654
11655/**
11656 * intel_wm_need_update - Check whether watermarks need updating
11657 * @plane: drm plane
11658 * @state: new plane state
11659 *
11660 * Check current plane state versus the new one to determine whether
11661 * watermarks need to be recalculated.
11662 *
11663 * Returns true or false.
11664 */
11665static bool intel_wm_need_update(struct drm_plane *plane,
11666 struct drm_plane_state *state)
11667{
Matt Roperd21fbe82015-09-24 15:53:12 -070011668 struct intel_plane_state *new = to_intel_plane_state(state);
11669 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11670
11671 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011672 if (new->visible != cur->visible)
11673 return true;
11674
11675 if (!cur->base.fb || !new->base.fb)
11676 return false;
11677
11678 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11679 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011680 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11681 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11682 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11683 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011684 return true;
11685
11686 return false;
11687}
11688
Matt Roperd21fbe82015-09-24 15:53:12 -070011689static bool needs_scaling(struct intel_plane_state *state)
11690{
11691 int src_w = drm_rect_width(&state->src) >> 16;
11692 int src_h = drm_rect_height(&state->src) >> 16;
11693 int dst_w = drm_rect_width(&state->dst);
11694 int dst_h = drm_rect_height(&state->dst);
11695
11696 return (src_w != dst_w || src_h != dst_h);
11697}
11698
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011699int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11700 struct drm_plane_state *plane_state)
11701{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011702 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011703 struct drm_crtc *crtc = crtc_state->crtc;
11704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11705 struct drm_plane *plane = plane_state->plane;
11706 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011707 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011708 struct intel_plane_state *old_plane_state =
11709 to_intel_plane_state(plane->state);
11710 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011711 bool mode_changed = needs_modeset(crtc_state);
11712 bool was_crtc_enabled = crtc->state->active;
11713 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011714 bool turn_off, turn_on, visible, was_visible;
11715 struct drm_framebuffer *fb = plane_state->fb;
11716
11717 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11718 plane->type != DRM_PLANE_TYPE_CURSOR) {
11719 ret = skl_update_scaler_plane(
11720 to_intel_crtc_state(crtc_state),
11721 to_intel_plane_state(plane_state));
11722 if (ret)
11723 return ret;
11724 }
11725
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011726 was_visible = old_plane_state->visible;
11727 visible = to_intel_plane_state(plane_state)->visible;
11728
11729 if (!was_crtc_enabled && WARN_ON(was_visible))
11730 was_visible = false;
11731
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011732 /*
11733 * Visibility is calculated as if the crtc was on, but
11734 * after scaler setup everything depends on it being off
11735 * when the crtc isn't active.
11736 */
11737 if (!is_crtc_enabled)
11738 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011739
11740 if (!was_visible && !visible)
11741 return 0;
11742
Maarten Lankhorste8861672016-02-24 11:24:26 +010011743 if (fb != old_plane_state->base.fb)
11744 pipe_config->fb_changed = true;
11745
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011746 turn_off = was_visible && (!visible || mode_changed);
11747 turn_on = visible && (!was_visible || mode_changed);
11748
11749 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11750 plane->base.id, fb ? fb->base.id : -1);
11751
11752 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11753 plane->base.id, was_visible, visible,
11754 turn_off, turn_on, mode_changed);
11755
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011756 if (turn_on) {
11757 pipe_config->update_wm_pre = true;
11758
11759 /* must disable cxsr around plane enable/disable */
11760 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11761 pipe_config->disable_cxsr = true;
11762 } else if (turn_off) {
11763 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011764
Ville Syrjälä852eb002015-06-24 22:00:07 +030011765 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011766 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011767 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011768 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011769 /* FIXME bollocks */
11770 pipe_config->update_wm_pre = true;
11771 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011772 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011773
Matt Ropered4a6a72016-02-23 17:20:13 -080011774 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011775 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11776 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011777 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11778
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011779 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011780 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011781
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011782 /*
11783 * WaCxSRDisabledForSpriteScaling:ivb
11784 *
11785 * cstate->update_wm was already set above, so this flag will
11786 * take effect when we commit and program watermarks.
11787 */
11788 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11789 needs_scaling(to_intel_plane_state(plane_state)) &&
11790 !needs_scaling(old_plane_state))
11791 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011792
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011793 return 0;
11794}
11795
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011796static bool encoders_cloneable(const struct intel_encoder *a,
11797 const struct intel_encoder *b)
11798{
11799 /* masks could be asymmetric, so check both ways */
11800 return a == b || (a->cloneable & (1 << b->type) &&
11801 b->cloneable & (1 << a->type));
11802}
11803
11804static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11805 struct intel_crtc *crtc,
11806 struct intel_encoder *encoder)
11807{
11808 struct intel_encoder *source_encoder;
11809 struct drm_connector *connector;
11810 struct drm_connector_state *connector_state;
11811 int i;
11812
11813 for_each_connector_in_state(state, connector, connector_state, i) {
11814 if (connector_state->crtc != &crtc->base)
11815 continue;
11816
11817 source_encoder =
11818 to_intel_encoder(connector_state->best_encoder);
11819 if (!encoders_cloneable(encoder, source_encoder))
11820 return false;
11821 }
11822
11823 return true;
11824}
11825
11826static bool check_encoder_cloning(struct drm_atomic_state *state,
11827 struct intel_crtc *crtc)
11828{
11829 struct intel_encoder *encoder;
11830 struct drm_connector *connector;
11831 struct drm_connector_state *connector_state;
11832 int i;
11833
11834 for_each_connector_in_state(state, connector, connector_state, i) {
11835 if (connector_state->crtc != &crtc->base)
11836 continue;
11837
11838 encoder = to_intel_encoder(connector_state->best_encoder);
11839 if (!check_single_encoder_cloning(state, crtc, encoder))
11840 return false;
11841 }
11842
11843 return true;
11844}
11845
11846static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11847 struct drm_crtc_state *crtc_state)
11848{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011849 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011850 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011852 struct intel_crtc_state *pipe_config =
11853 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011854 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011855 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011856 bool mode_changed = needs_modeset(crtc_state);
11857
11858 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11859 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11860 return -EINVAL;
11861 }
11862
Ville Syrjälä852eb002015-06-24 22:00:07 +030011863 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011864 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011865
Maarten Lankhorstad421372015-06-15 12:33:42 +020011866 if (mode_changed && crtc_state->enable &&
11867 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011868 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011869 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11870 pipe_config);
11871 if (ret)
11872 return ret;
11873 }
11874
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011875 if (crtc_state->color_mgmt_changed) {
11876 ret = intel_color_check(crtc, crtc_state);
11877 if (ret)
11878 return ret;
11879 }
11880
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011881 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011882 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011883 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011884 if (ret) {
11885 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011886 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011887 }
11888 }
11889
11890 if (dev_priv->display.compute_intermediate_wm &&
11891 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11892 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11893 return 0;
11894
11895 /*
11896 * Calculate 'intermediate' watermarks that satisfy both the
11897 * old state and the new state. We can program these
11898 * immediately.
11899 */
11900 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11901 intel_crtc,
11902 pipe_config);
11903 if (ret) {
11904 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11905 return ret;
11906 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011907 }
11908
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011909 if (INTEL_INFO(dev)->gen >= 9) {
11910 if (mode_changed)
11911 ret = skl_update_scaler_crtc(pipe_config);
11912
11913 if (!ret)
11914 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11915 pipe_config);
11916 }
11917
11918 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011919}
11920
Jani Nikula65b38e02015-04-13 11:26:56 +030011921static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011922 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011923 .atomic_begin = intel_begin_crtc_commit,
11924 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011925 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011926};
11927
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011928static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11929{
11930 struct intel_connector *connector;
11931
11932 for_each_intel_connector(dev, connector) {
11933 if (connector->base.encoder) {
11934 connector->base.state->best_encoder =
11935 connector->base.encoder;
11936 connector->base.state->crtc =
11937 connector->base.encoder->crtc;
11938 } else {
11939 connector->base.state->best_encoder = NULL;
11940 connector->base.state->crtc = NULL;
11941 }
11942 }
11943}
11944
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011945static void
Robin Schroereba905b2014-05-18 02:24:50 +020011946connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011947 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011948{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011949 int bpp = pipe_config->pipe_bpp;
11950
11951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11952 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011953 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011954
11955 /* Don't use an invalid EDID bpc value */
11956 if (connector->base.display_info.bpc &&
11957 connector->base.display_info.bpc * 3 < bpp) {
11958 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11959 bpp, connector->base.display_info.bpc*3);
11960 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11961 }
11962
Jani Nikula013dd9e2016-01-13 16:35:20 +020011963 /* Clamp bpp to default limit on screens without EDID 1.4 */
11964 if (connector->base.display_info.bpc == 0) {
11965 int type = connector->base.connector_type;
11966 int clamp_bpp = 24;
11967
11968 /* Fall back to 18 bpp when DP sink capability is unknown. */
11969 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11970 type == DRM_MODE_CONNECTOR_eDP)
11971 clamp_bpp = 18;
11972
11973 if (bpp > clamp_bpp) {
11974 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11975 bpp, clamp_bpp);
11976 pipe_config->pipe_bpp = clamp_bpp;
11977 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011978 }
11979}
11980
11981static int
11982compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011983 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011984{
11985 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011986 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011987 struct drm_connector *connector;
11988 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011989 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011990
Wayne Boyer666a4532015-12-09 12:29:35 -080011991 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011992 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011993 else if (INTEL_INFO(dev)->gen >= 5)
11994 bpp = 12*3;
11995 else
11996 bpp = 8*3;
11997
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011998
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011999 pipe_config->pipe_bpp = bpp;
12000
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012001 state = pipe_config->base.state;
12002
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012003 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012004 for_each_connector_in_state(state, connector, connector_state, i) {
12005 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012006 continue;
12007
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012008 connected_sink_compute_bpp(to_intel_connector(connector),
12009 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012010 }
12011
12012 return bpp;
12013}
12014
Daniel Vetter644db712013-09-19 14:53:58 +020012015static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12016{
12017 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12018 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012019 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012020 mode->crtc_hdisplay, mode->crtc_hsync_start,
12021 mode->crtc_hsync_end, mode->crtc_htotal,
12022 mode->crtc_vdisplay, mode->crtc_vsync_start,
12023 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12024}
12025
Daniel Vetterc0b03412013-05-28 12:05:54 +020012026static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012027 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012028 const char *context)
12029{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012030 struct drm_device *dev = crtc->base.dev;
12031 struct drm_plane *plane;
12032 struct intel_plane *intel_plane;
12033 struct intel_plane_state *state;
12034 struct drm_framebuffer *fb;
12035
12036 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12037 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012038
Jani Nikulada205632016-03-15 21:51:10 +020012039 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012040 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12041 pipe_config->pipe_bpp, pipe_config->dither);
12042 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12043 pipe_config->has_pch_encoder,
12044 pipe_config->fdi_lanes,
12045 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12046 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12047 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012048 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 +030012049 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012050 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012051 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12052 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12053 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012054
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012055 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 -070012056 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012057 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012058 pipe_config->dp_m2_n2.gmch_m,
12059 pipe_config->dp_m2_n2.gmch_n,
12060 pipe_config->dp_m2_n2.link_m,
12061 pipe_config->dp_m2_n2.link_n,
12062 pipe_config->dp_m2_n2.tu);
12063
Daniel Vetter55072d12014-11-20 16:10:28 +010012064 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12065 pipe_config->has_audio,
12066 pipe_config->has_infoframe);
12067
Daniel Vetterc0b03412013-05-28 12:05:54 +020012068 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012069 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012070 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012071 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12072 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012073 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012074 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12075 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012076 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12077 crtc->num_scalers,
12078 pipe_config->scaler_state.scaler_users,
12079 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012080 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12081 pipe_config->gmch_pfit.control,
12082 pipe_config->gmch_pfit.pgm_ratios,
12083 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012084 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012085 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012086 pipe_config->pch_pfit.size,
12087 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012088 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012089 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012090
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012091 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012092 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012093 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012094 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012095 pipe_config->ddi_pll_sel,
12096 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012097 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012098 pipe_config->dpll_hw_state.pll0,
12099 pipe_config->dpll_hw_state.pll1,
12100 pipe_config->dpll_hw_state.pll2,
12101 pipe_config->dpll_hw_state.pll3,
12102 pipe_config->dpll_hw_state.pll6,
12103 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012104 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012105 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012106 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012107 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012108 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12109 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12110 pipe_config->ddi_pll_sel,
12111 pipe_config->dpll_hw_state.ctrl1,
12112 pipe_config->dpll_hw_state.cfgcr1,
12113 pipe_config->dpll_hw_state.cfgcr2);
12114 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012115 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 +010012116 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012117 pipe_config->dpll_hw_state.wrpll,
12118 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012119 } else {
12120 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12121 "fp0: 0x%x, fp1: 0x%x\n",
12122 pipe_config->dpll_hw_state.dpll,
12123 pipe_config->dpll_hw_state.dpll_md,
12124 pipe_config->dpll_hw_state.fp0,
12125 pipe_config->dpll_hw_state.fp1);
12126 }
12127
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012128 DRM_DEBUG_KMS("planes on this crtc\n");
12129 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12130 intel_plane = to_intel_plane(plane);
12131 if (intel_plane->pipe != crtc->pipe)
12132 continue;
12133
12134 state = to_intel_plane_state(plane->state);
12135 fb = state->base.fb;
12136 if (!fb) {
12137 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12138 "disabled, scaler_id = %d\n",
12139 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12140 plane->base.id, intel_plane->pipe,
12141 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12142 drm_plane_index(plane), state->scaler_id);
12143 continue;
12144 }
12145
12146 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12147 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12148 plane->base.id, intel_plane->pipe,
12149 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12150 drm_plane_index(plane));
12151 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12152 fb->base.id, fb->width, fb->height, fb->pixel_format);
12153 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12154 state->scaler_id,
12155 state->src.x1 >> 16, state->src.y1 >> 16,
12156 drm_rect_width(&state->src) >> 16,
12157 drm_rect_height(&state->src) >> 16,
12158 state->dst.x1, state->dst.y1,
12159 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12160 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012161}
12162
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012163static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012164{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012165 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012166 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012167 unsigned int used_ports = 0;
12168
12169 /*
12170 * Walk the connector list instead of the encoder
12171 * list to detect the problem on ddi platforms
12172 * where there's just one encoder per digital port.
12173 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012174 drm_for_each_connector(connector, dev) {
12175 struct drm_connector_state *connector_state;
12176 struct intel_encoder *encoder;
12177
12178 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12179 if (!connector_state)
12180 connector_state = connector->state;
12181
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012182 if (!connector_state->best_encoder)
12183 continue;
12184
12185 encoder = to_intel_encoder(connector_state->best_encoder);
12186
12187 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012188
12189 switch (encoder->type) {
12190 unsigned int port_mask;
12191 case INTEL_OUTPUT_UNKNOWN:
12192 if (WARN_ON(!HAS_DDI(dev)))
12193 break;
12194 case INTEL_OUTPUT_DISPLAYPORT:
12195 case INTEL_OUTPUT_HDMI:
12196 case INTEL_OUTPUT_EDP:
12197 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12198
12199 /* the same port mustn't appear more than once */
12200 if (used_ports & port_mask)
12201 return false;
12202
12203 used_ports |= port_mask;
12204 default:
12205 break;
12206 }
12207 }
12208
12209 return true;
12210}
12211
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012212static void
12213clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12214{
12215 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012216 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012217 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012218 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012219 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012220 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012221
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012222 /* FIXME: before the switch to atomic started, a new pipe_config was
12223 * kzalloc'd. Code that depends on any field being zero should be
12224 * fixed, so that the crtc_state can be safely duplicated. For now,
12225 * only fields that are know to not cause problems are preserved. */
12226
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012227 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012228 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012229 shared_dpll = crtc_state->shared_dpll;
12230 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012231 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012232 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012233
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012234 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012235
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012236 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012237 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012238 crtc_state->shared_dpll = shared_dpll;
12239 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012240 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012241 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012242}
12243
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012244static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012245intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012246 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012247{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012248 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012249 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012250 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012251 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012252 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012253 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012254 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012255
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012256 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012257
Daniel Vettere143a212013-07-04 12:01:15 +020012258 pipe_config->cpu_transcoder =
12259 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012260
Imre Deak2960bc92013-07-30 13:36:32 +030012261 /*
12262 * Sanitize sync polarity flags based on requested ones. If neither
12263 * positive or negative polarity is requested, treat this as meaning
12264 * negative polarity.
12265 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012266 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012267 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012268 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012269
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012270 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012271 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012272 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012273
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012274 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12275 pipe_config);
12276 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012277 goto fail;
12278
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012279 /*
12280 * Determine the real pipe dimensions. Note that stereo modes can
12281 * increase the actual pipe size due to the frame doubling and
12282 * insertion of additional space for blanks between the frame. This
12283 * is stored in the crtc timings. We use the requested mode to do this
12284 * computation to clearly distinguish it from the adjusted mode, which
12285 * can be changed by the connectors in the below retry loop.
12286 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012287 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012288 &pipe_config->pipe_src_w,
12289 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012290
Daniel Vettere29c22c2013-02-21 00:00:16 +010012291encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012292 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012293 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012294 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012295
Daniel Vetter135c81b2013-07-21 21:37:09 +020012296 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012297 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12298 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012299
Daniel Vetter7758a112012-07-08 19:40:39 +020012300 /* Pass our mode to the connectors and the CRTC to give them a chance to
12301 * adjust it according to limitations or connector properties, and also
12302 * a chance to reject the mode entirely.
12303 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012304 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012305 if (connector_state->crtc != crtc)
12306 continue;
12307
12308 encoder = to_intel_encoder(connector_state->best_encoder);
12309
Daniel Vetterefea6e82013-07-21 21:36:59 +020012310 if (!(encoder->compute_config(encoder, pipe_config))) {
12311 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012312 goto fail;
12313 }
12314 }
12315
Daniel Vetterff9a6752013-06-01 17:16:21 +020012316 /* Set default port clock if not overwritten by the encoder. Needs to be
12317 * done afterwards in case the encoder adjusts the mode. */
12318 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012319 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012320 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012321
Daniel Vettera43f6e02013-06-07 23:10:32 +020012322 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012323 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012324 DRM_DEBUG_KMS("CRTC fixup failed\n");
12325 goto fail;
12326 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012327
12328 if (ret == RETRY) {
12329 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12330 ret = -EINVAL;
12331 goto fail;
12332 }
12333
12334 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12335 retry = false;
12336 goto encoder_retry;
12337 }
12338
Daniel Vettere8fa4272015-08-12 11:43:34 +020012339 /* Dithering seems to not pass-through bits correctly when it should, so
12340 * only enable it on 6bpc panels. */
12341 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012342 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012343 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012344
Daniel Vetter7758a112012-07-08 19:40:39 +020012345fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012346 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012347}
12348
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012349static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012350intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012351{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012352 struct drm_crtc *crtc;
12353 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012354 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012355
Ville Syrjälä76688512014-01-10 11:28:06 +020012356 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012358 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012359
12360 /* Update hwmode for vblank functions */
12361 if (crtc->state->active)
12362 crtc->hwmode = crtc->state->adjusted_mode;
12363 else
12364 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012365
12366 /*
12367 * Update legacy state to satisfy fbc code. This can
12368 * be removed when fbc uses the atomic state.
12369 */
12370 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12371 struct drm_plane_state *plane_state = crtc->primary->state;
12372
12373 crtc->primary->fb = plane_state->fb;
12374 crtc->x = plane_state->src_x >> 16;
12375 crtc->y = plane_state->src_y >> 16;
12376 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012377 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012378}
12379
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012380static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012381{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012382 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012383
12384 if (clock1 == clock2)
12385 return true;
12386
12387 if (!clock1 || !clock2)
12388 return false;
12389
12390 diff = abs(clock1 - clock2);
12391
12392 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12393 return true;
12394
12395 return false;
12396}
12397
Daniel Vetter25c5b262012-07-08 22:08:04 +020012398#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12399 list_for_each_entry((intel_crtc), \
12400 &(dev)->mode_config.crtc_list, \
12401 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012402 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012403
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012404static bool
12405intel_compare_m_n(unsigned int m, unsigned int n,
12406 unsigned int m2, unsigned int n2,
12407 bool exact)
12408{
12409 if (m == m2 && n == n2)
12410 return true;
12411
12412 if (exact || !m || !n || !m2 || !n2)
12413 return false;
12414
12415 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12416
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012417 if (n > n2) {
12418 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012419 m2 <<= 1;
12420 n2 <<= 1;
12421 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012422 } else if (n < n2) {
12423 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012424 m <<= 1;
12425 n <<= 1;
12426 }
12427 }
12428
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012429 if (n != n2)
12430 return false;
12431
12432 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012433}
12434
12435static bool
12436intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12437 struct intel_link_m_n *m2_n2,
12438 bool adjust)
12439{
12440 if (m_n->tu == m2_n2->tu &&
12441 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12442 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12443 intel_compare_m_n(m_n->link_m, m_n->link_n,
12444 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12445 if (adjust)
12446 *m2_n2 = *m_n;
12447
12448 return true;
12449 }
12450
12451 return false;
12452}
12453
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012454static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012455intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012456 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012457 struct intel_crtc_state *pipe_config,
12458 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012459{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012460 bool ret = true;
12461
12462#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12463 do { \
12464 if (!adjust) \
12465 DRM_ERROR(fmt, ##__VA_ARGS__); \
12466 else \
12467 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12468 } while (0)
12469
Daniel Vetter66e985c2013-06-05 13:34:20 +020012470#define PIPE_CONF_CHECK_X(name) \
12471 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012472 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012473 "(expected 0x%08x, found 0x%08x)\n", \
12474 current_config->name, \
12475 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012476 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012477 }
12478
Daniel Vetter08a24032013-04-19 11:25:34 +020012479#define PIPE_CONF_CHECK_I(name) \
12480 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012481 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012482 "(expected %i, found %i)\n", \
12483 current_config->name, \
12484 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012485 ret = false; \
12486 }
12487
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012488#define PIPE_CONF_CHECK_P(name) \
12489 if (current_config->name != pipe_config->name) { \
12490 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12491 "(expected %p, found %p)\n", \
12492 current_config->name, \
12493 pipe_config->name); \
12494 ret = false; \
12495 }
12496
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012497#define PIPE_CONF_CHECK_M_N(name) \
12498 if (!intel_compare_link_m_n(&current_config->name, \
12499 &pipe_config->name,\
12500 adjust)) { \
12501 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12502 "(expected tu %i gmch %i/%i link %i/%i, " \
12503 "found tu %i, gmch %i/%i link %i/%i)\n", \
12504 current_config->name.tu, \
12505 current_config->name.gmch_m, \
12506 current_config->name.gmch_n, \
12507 current_config->name.link_m, \
12508 current_config->name.link_n, \
12509 pipe_config->name.tu, \
12510 pipe_config->name.gmch_m, \
12511 pipe_config->name.gmch_n, \
12512 pipe_config->name.link_m, \
12513 pipe_config->name.link_n); \
12514 ret = false; \
12515 }
12516
12517#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12518 if (!intel_compare_link_m_n(&current_config->name, \
12519 &pipe_config->name, adjust) && \
12520 !intel_compare_link_m_n(&current_config->alt_name, \
12521 &pipe_config->name, adjust)) { \
12522 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12523 "(expected tu %i gmch %i/%i link %i/%i, " \
12524 "or tu %i gmch %i/%i link %i/%i, " \
12525 "found tu %i, gmch %i/%i link %i/%i)\n", \
12526 current_config->name.tu, \
12527 current_config->name.gmch_m, \
12528 current_config->name.gmch_n, \
12529 current_config->name.link_m, \
12530 current_config->name.link_n, \
12531 current_config->alt_name.tu, \
12532 current_config->alt_name.gmch_m, \
12533 current_config->alt_name.gmch_n, \
12534 current_config->alt_name.link_m, \
12535 current_config->alt_name.link_n, \
12536 pipe_config->name.tu, \
12537 pipe_config->name.gmch_m, \
12538 pipe_config->name.gmch_n, \
12539 pipe_config->name.link_m, \
12540 pipe_config->name.link_n); \
12541 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012542 }
12543
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012544/* This is required for BDW+ where there is only one set of registers for
12545 * switching between high and low RR.
12546 * This macro can be used whenever a comparison has to be made between one
12547 * hw state and multiple sw state variables.
12548 */
12549#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12550 if ((current_config->name != pipe_config->name) && \
12551 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012552 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012553 "(expected %i or %i, found %i)\n", \
12554 current_config->name, \
12555 current_config->alt_name, \
12556 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012557 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012558 }
12559
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012560#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12561 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012562 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012563 "(expected %i, found %i)\n", \
12564 current_config->name & (mask), \
12565 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012566 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012567 }
12568
Ville Syrjälä5e550652013-09-06 23:29:07 +030012569#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12570 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012571 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012572 "(expected %i, found %i)\n", \
12573 current_config->name, \
12574 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012575 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012576 }
12577
Daniel Vetterbb760062013-06-06 14:55:52 +020012578#define PIPE_CONF_QUIRK(quirk) \
12579 ((current_config->quirks | pipe_config->quirks) & (quirk))
12580
Daniel Vettereccb1402013-05-22 00:50:22 +020012581 PIPE_CONF_CHECK_I(cpu_transcoder);
12582
Daniel Vetter08a24032013-04-19 11:25:34 +020012583 PIPE_CONF_CHECK_I(has_pch_encoder);
12584 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012585 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012586
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012587 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012588 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012589
12590 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012591 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012592
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012593 if (current_config->has_drrs)
12594 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12595 } else
12596 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012597
Jani Nikulaa65347b2015-11-27 12:21:46 +020012598 PIPE_CONF_CHECK_I(has_dsi_encoder);
12599
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012600 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12601 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12602 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12603 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12604 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12605 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012606
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012607 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12608 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12609 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12610 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12611 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12612 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012613
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012614 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012615 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012616 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012617 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012618 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012619 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012620
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012621 PIPE_CONF_CHECK_I(has_audio);
12622
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012623 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012624 DRM_MODE_FLAG_INTERLACE);
12625
Daniel Vetterbb760062013-06-06 14:55:52 +020012626 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012627 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012628 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012629 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012630 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012631 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012632 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012633 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012634 DRM_MODE_FLAG_NVSYNC);
12635 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012636
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012637 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012638 /* pfit ratios are autocomputed by the hw on gen4+ */
12639 if (INTEL_INFO(dev)->gen < 4)
12640 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012641 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012642
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012643 if (!adjust) {
12644 PIPE_CONF_CHECK_I(pipe_src_w);
12645 PIPE_CONF_CHECK_I(pipe_src_h);
12646
12647 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12648 if (current_config->pch_pfit.enabled) {
12649 PIPE_CONF_CHECK_X(pch_pfit.pos);
12650 PIPE_CONF_CHECK_X(pch_pfit.size);
12651 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012652
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012653 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12654 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012655
Jesse Barnese59150d2014-01-07 13:30:45 -080012656 /* BDW+ don't expose a synchronous way to read the state */
12657 if (IS_HASWELL(dev))
12658 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012659
Ville Syrjälä282740f2013-09-04 18:30:03 +030012660 PIPE_CONF_CHECK_I(double_wide);
12661
Daniel Vetter26804af2014-06-25 22:01:55 +030012662 PIPE_CONF_CHECK_X(ddi_pll_sel);
12663
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012664 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012665 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012666 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012667 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12668 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012669 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012670 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012671 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12672 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12673 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012674
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012675 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12676 PIPE_CONF_CHECK_I(pipe_bpp);
12677
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012678 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012679 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012680
Daniel Vetter66e985c2013-06-05 13:34:20 +020012681#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012682#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012683#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012684#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012685#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012686#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012687#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012688#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012689
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012690 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012691}
12692
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012693static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12694 const struct intel_crtc_state *pipe_config)
12695{
12696 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012697 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012698 &pipe_config->fdi_m_n);
12699 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12700
12701 /*
12702 * FDI already provided one idea for the dotclock.
12703 * Yell if the encoder disagrees.
12704 */
12705 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12706 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12707 fdi_dotclock, dotclock);
12708 }
12709}
12710
Damien Lespiau08db6652014-11-04 17:06:52 +000012711static void check_wm_state(struct drm_device *dev)
12712{
12713 struct drm_i915_private *dev_priv = dev->dev_private;
12714 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12715 struct intel_crtc *intel_crtc;
12716 int plane;
12717
12718 if (INTEL_INFO(dev)->gen < 9)
12719 return;
12720
12721 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12722 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12723
12724 for_each_intel_crtc(dev, intel_crtc) {
12725 struct skl_ddb_entry *hw_entry, *sw_entry;
12726 const enum pipe pipe = intel_crtc->pipe;
12727
12728 if (!intel_crtc->active)
12729 continue;
12730
12731 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012732 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012733 hw_entry = &hw_ddb.plane[pipe][plane];
12734 sw_entry = &sw_ddb->plane[pipe][plane];
12735
12736 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12737 continue;
12738
12739 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12740 "(expected (%u,%u), found (%u,%u))\n",
12741 pipe_name(pipe), plane + 1,
12742 sw_entry->start, sw_entry->end,
12743 hw_entry->start, hw_entry->end);
12744 }
12745
12746 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012747 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12748 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012749
12750 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12751 continue;
12752
12753 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12754 "(expected (%u,%u), found (%u,%u))\n",
12755 pipe_name(pipe),
12756 sw_entry->start, sw_entry->end,
12757 hw_entry->start, hw_entry->end);
12758 }
12759}
12760
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012761static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012762check_connector_state(struct drm_device *dev,
12763 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012764{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012765 struct drm_connector_state *old_conn_state;
12766 struct drm_connector *connector;
12767 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012768
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012769 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12770 struct drm_encoder *encoder = connector->encoder;
12771 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012772
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012773 /* This also checks the encoder/connector hw state with the
12774 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012775 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012776
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012777 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012778 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012779 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012780}
12781
12782static void
12783check_encoder_state(struct drm_device *dev)
12784{
12785 struct intel_encoder *encoder;
12786 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012787
Damien Lespiaub2784e12014-08-05 11:29:37 +010012788 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012789 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012790 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012791
12792 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12793 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012794 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012795
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012796 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012797 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012798 continue;
12799 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012800
12801 I915_STATE_WARN(connector->base.state->crtc !=
12802 encoder->base.crtc,
12803 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012804 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012805
Rob Clarke2c719b2014-12-15 13:56:32 -050012806 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807 "encoder's enabled state mismatch "
12808 "(expected %i, found %i)\n",
12809 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012810
12811 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012812 bool active;
12813
12814 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012815 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012816 "encoder detached but still enabled on pipe %c.\n",
12817 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012818 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012819 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012820}
12821
12822static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012823check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012824{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012825 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012826 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012827 struct drm_crtc_state *old_crtc_state;
12828 struct drm_crtc *crtc;
12829 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012830
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012831 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12833 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012834 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012835
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012836 if (!needs_modeset(crtc->state) &&
12837 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012838 continue;
12839
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012840 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12841 pipe_config = to_intel_crtc_state(old_crtc_state);
12842 memset(pipe_config, 0, sizeof(*pipe_config));
12843 pipe_config->base.crtc = crtc;
12844 pipe_config->base.state = old_state;
12845
12846 DRM_DEBUG_KMS("[CRTC:%d]\n",
12847 crtc->base.id);
12848
12849 active = dev_priv->display.get_pipe_config(intel_crtc,
12850 pipe_config);
12851
12852 /* hw state is inconsistent with the pipe quirk */
12853 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12854 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12855 active = crtc->state->active;
12856
12857 I915_STATE_WARN(crtc->state->active != active,
12858 "crtc active state doesn't match with hw state "
12859 "(expected %i, found %i)\n", crtc->state->active, active);
12860
12861 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12862 "transitional active state does not match atomic hw state "
12863 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12864
12865 for_each_encoder_on_crtc(dev, crtc, encoder) {
12866 enum pipe pipe;
12867
12868 active = encoder->get_hw_state(encoder, &pipe);
12869 I915_STATE_WARN(active != crtc->state->active,
12870 "[ENCODER:%i] active %i with crtc active %i\n",
12871 encoder->base.base.id, active, crtc->state->active);
12872
12873 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12874 "Encoder connected to wrong pipe %c\n",
12875 pipe_name(pipe));
12876
12877 if (active)
12878 encoder->get_config(encoder, pipe_config);
12879 }
12880
12881 if (!crtc->state->active)
12882 continue;
12883
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012884 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12885
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012886 sw_config = to_intel_crtc_state(crtc->state);
12887 if (!intel_pipe_config_compare(dev, sw_config,
12888 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012889 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012890 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012891 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012892 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012893 "[sw state]");
12894 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012895 }
12896}
12897
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012898static void
12899check_shared_dpll_state(struct drm_device *dev)
12900{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012901 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012902 struct intel_crtc *crtc;
12903 struct intel_dpll_hw_state dpll_hw_state;
12904 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012905
12906 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012907 struct intel_shared_dpll *pll =
12908 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012909 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012910 bool active;
12911
12912 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12913
12914 DRM_DEBUG_KMS("%s\n", pll->name);
12915
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012916 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012917
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012918 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12919 "more active pll users than references: %x vs %x\n",
12920 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012921
12922 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012923 I915_STATE_WARN(!pll->on && pll->active_mask,
12924 "pll in active use but not on in sw tracking\n");
12925 I915_STATE_WARN(pll->on && !pll->active_mask,
12926 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012927 I915_STATE_WARN(pll->on != active,
12928 "pll on state mismatch (expected %i, found %i)\n",
12929 pll->on, active);
12930 }
Daniel Vetter53589012013-06-05 13:34:16 +020012931
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012932 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012933 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012934 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12935 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12936 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012937 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012938
12939 I915_STATE_WARN(pll->active_mask != active_crtcs,
12940 "pll active crtcs mismatch (expected %x, found %x)\n",
12941 pll->active_mask, active_crtcs);
12942 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12943 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12944 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012945
Rob Clarke2c719b2014-12-15 13:56:32 -050012946 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012947 sizeof(dpll_hw_state)),
12948 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012949 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012950}
12951
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012952static void
12953intel_modeset_check_state(struct drm_device *dev,
12954 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012955{
Damien Lespiau08db6652014-11-04 17:06:52 +000012956 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012957 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012958 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012959 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012960 check_shared_dpll_state(dev);
12961}
12962
Ville Syrjälä80715b22014-05-15 20:23:23 +030012963static void update_scanline_offset(struct intel_crtc *crtc)
12964{
12965 struct drm_device *dev = crtc->base.dev;
12966
12967 /*
12968 * The scanline counter increments at the leading edge of hsync.
12969 *
12970 * On most platforms it starts counting from vtotal-1 on the
12971 * first active line. That means the scanline counter value is
12972 * always one less than what we would expect. Ie. just after
12973 * start of vblank, which also occurs at start of hsync (on the
12974 * last active line), the scanline counter will read vblank_start-1.
12975 *
12976 * On gen2 the scanline counter starts counting from 1 instead
12977 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12978 * to keep the value positive), instead of adding one.
12979 *
12980 * On HSW+ the behaviour of the scanline counter depends on the output
12981 * type. For DP ports it behaves like most other platforms, but on HDMI
12982 * there's an extra 1 line difference. So we need to add two instead of
12983 * one to the value.
12984 */
12985 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012986 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012987 int vtotal;
12988
Ville Syrjälä124abe02015-09-08 13:40:45 +030012989 vtotal = adjusted_mode->crtc_vtotal;
12990 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012991 vtotal /= 2;
12992
12993 crtc->scanline_offset = vtotal - 1;
12994 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012995 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012996 crtc->scanline_offset = 2;
12997 } else
12998 crtc->scanline_offset = 1;
12999}
13000
Maarten Lankhorstad421372015-06-15 12:33:42 +020013001static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013002{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013003 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013004 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013005 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013006 struct drm_crtc *crtc;
13007 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013008 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013009
13010 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013011 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013012
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013013 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013015 struct intel_shared_dpll *old_dpll =
13016 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013017
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013018 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013019 continue;
13020
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013021 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013022
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013023 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013024 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013025
Maarten Lankhorstad421372015-06-15 12:33:42 +020013026 if (!shared_dpll)
13027 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13028
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013029 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013030 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013031}
13032
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013033/*
13034 * This implements the workaround described in the "notes" section of the mode
13035 * set sequence documentation. When going from no pipes or single pipe to
13036 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13037 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13038 */
13039static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13040{
13041 struct drm_crtc_state *crtc_state;
13042 struct intel_crtc *intel_crtc;
13043 struct drm_crtc *crtc;
13044 struct intel_crtc_state *first_crtc_state = NULL;
13045 struct intel_crtc_state *other_crtc_state = NULL;
13046 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13047 int i;
13048
13049 /* look at all crtc's that are going to be enabled in during modeset */
13050 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13051 intel_crtc = to_intel_crtc(crtc);
13052
13053 if (!crtc_state->active || !needs_modeset(crtc_state))
13054 continue;
13055
13056 if (first_crtc_state) {
13057 other_crtc_state = to_intel_crtc_state(crtc_state);
13058 break;
13059 } else {
13060 first_crtc_state = to_intel_crtc_state(crtc_state);
13061 first_pipe = intel_crtc->pipe;
13062 }
13063 }
13064
13065 /* No workaround needed? */
13066 if (!first_crtc_state)
13067 return 0;
13068
13069 /* w/a possibly needed, check how many crtc's are already enabled. */
13070 for_each_intel_crtc(state->dev, intel_crtc) {
13071 struct intel_crtc_state *pipe_config;
13072
13073 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13074 if (IS_ERR(pipe_config))
13075 return PTR_ERR(pipe_config);
13076
13077 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13078
13079 if (!pipe_config->base.active ||
13080 needs_modeset(&pipe_config->base))
13081 continue;
13082
13083 /* 2 or more enabled crtcs means no need for w/a */
13084 if (enabled_pipe != INVALID_PIPE)
13085 return 0;
13086
13087 enabled_pipe = intel_crtc->pipe;
13088 }
13089
13090 if (enabled_pipe != INVALID_PIPE)
13091 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13092 else if (other_crtc_state)
13093 other_crtc_state->hsw_workaround_pipe = first_pipe;
13094
13095 return 0;
13096}
13097
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013098static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13099{
13100 struct drm_crtc *crtc;
13101 struct drm_crtc_state *crtc_state;
13102 int ret = 0;
13103
13104 /* add all active pipes to the state */
13105 for_each_crtc(state->dev, crtc) {
13106 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13107 if (IS_ERR(crtc_state))
13108 return PTR_ERR(crtc_state);
13109
13110 if (!crtc_state->active || needs_modeset(crtc_state))
13111 continue;
13112
13113 crtc_state->mode_changed = true;
13114
13115 ret = drm_atomic_add_affected_connectors(state, crtc);
13116 if (ret)
13117 break;
13118
13119 ret = drm_atomic_add_affected_planes(state, crtc);
13120 if (ret)
13121 break;
13122 }
13123
13124 return ret;
13125}
13126
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013127static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013128{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013129 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13130 struct drm_i915_private *dev_priv = state->dev->dev_private;
13131 struct drm_crtc *crtc;
13132 struct drm_crtc_state *crtc_state;
13133 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013134
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013135 if (!check_digital_port_conflicts(state)) {
13136 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13137 return -EINVAL;
13138 }
13139
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013140 intel_state->modeset = true;
13141 intel_state->active_crtcs = dev_priv->active_crtcs;
13142
13143 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13144 if (crtc_state->active)
13145 intel_state->active_crtcs |= 1 << i;
13146 else
13147 intel_state->active_crtcs &= ~(1 << i);
13148 }
13149
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013150 /*
13151 * See if the config requires any additional preparation, e.g.
13152 * to adjust global state with pipes off. We need to do this
13153 * here so we can get the modeset_pipe updated config for the new
13154 * mode set on this crtc. For other crtcs we need to use the
13155 * adjusted_mode bits in the crtc directly.
13156 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013157 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013158 ret = dev_priv->display.modeset_calc_cdclk(state);
13159
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013160 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013161 ret = intel_modeset_all_pipes(state);
13162
13163 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013164 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013165
13166 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13167 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013168 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013169 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013170
Maarten Lankhorstad421372015-06-15 12:33:42 +020013171 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013172
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013173 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013174 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013175
Maarten Lankhorstad421372015-06-15 12:33:42 +020013176 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013177}
13178
Matt Roperaa363132015-09-24 15:53:18 -070013179/*
13180 * Handle calculation of various watermark data at the end of the atomic check
13181 * phase. The code here should be run after the per-crtc and per-plane 'check'
13182 * handlers to ensure that all derived state has been updated.
13183 */
13184static void calc_watermark_data(struct drm_atomic_state *state)
13185{
13186 struct drm_device *dev = state->dev;
13187 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13188 struct drm_crtc *crtc;
13189 struct drm_crtc_state *cstate;
13190 struct drm_plane *plane;
13191 struct drm_plane_state *pstate;
13192
13193 /*
13194 * Calculate watermark configuration details now that derived
13195 * plane/crtc state is all properly updated.
13196 */
13197 drm_for_each_crtc(crtc, dev) {
13198 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13199 crtc->state;
13200
13201 if (cstate->active)
13202 intel_state->wm_config.num_pipes_active++;
13203 }
13204 drm_for_each_legacy_plane(plane, dev) {
13205 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13206 plane->state;
13207
13208 if (!to_intel_plane_state(pstate)->visible)
13209 continue;
13210
13211 intel_state->wm_config.sprites_enabled = true;
13212 if (pstate->crtc_w != pstate->src_w >> 16 ||
13213 pstate->crtc_h != pstate->src_h >> 16)
13214 intel_state->wm_config.sprites_scaled = true;
13215 }
13216}
13217
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013218/**
13219 * intel_atomic_check - validate state object
13220 * @dev: drm device
13221 * @state: state to validate
13222 */
13223static int intel_atomic_check(struct drm_device *dev,
13224 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013225{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013226 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013227 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013228 struct drm_crtc *crtc;
13229 struct drm_crtc_state *crtc_state;
13230 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013231 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013232
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013233 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013234 if (ret)
13235 return ret;
13236
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013237 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013238 struct intel_crtc_state *pipe_config =
13239 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013240
13241 /* Catch I915_MODE_FLAG_INHERITED */
13242 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13243 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013244
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013245 if (!crtc_state->enable) {
13246 if (needs_modeset(crtc_state))
13247 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013248 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013249 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013250
Daniel Vetter26495482015-07-15 14:15:52 +020013251 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013252 continue;
13253
Daniel Vetter26495482015-07-15 14:15:52 +020013254 /* FIXME: For only active_changed we shouldn't need to do any
13255 * state recomputation at all. */
13256
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013257 ret = drm_atomic_add_affected_connectors(state, crtc);
13258 if (ret)
13259 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013260
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013261 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013262 if (ret)
13263 return ret;
13264
Jani Nikula73831232015-11-19 10:26:30 +020013265 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013266 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013267 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013268 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013269 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013270 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013271 }
13272
13273 if (needs_modeset(crtc_state)) {
13274 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013275
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013276 ret = drm_atomic_add_affected_planes(state, crtc);
13277 if (ret)
13278 return ret;
13279 }
13280
Daniel Vetter26495482015-07-15 14:15:52 +020013281 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13282 needs_modeset(crtc_state) ?
13283 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013284 }
13285
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013286 if (any_ms) {
13287 ret = intel_modeset_checks(state);
13288
13289 if (ret)
13290 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013291 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013292 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013293
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013294 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013295 if (ret)
13296 return ret;
13297
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013298 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013299 calc_watermark_data(state);
13300
13301 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013302}
13303
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013304static int intel_atomic_prepare_commit(struct drm_device *dev,
13305 struct drm_atomic_state *state,
13306 bool async)
13307{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013308 struct drm_i915_private *dev_priv = dev->dev_private;
13309 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013310 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013311 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013312 struct drm_crtc *crtc;
13313 int i, ret;
13314
13315 if (async) {
13316 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13317 return -EINVAL;
13318 }
13319
13320 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13321 ret = intel_crtc_wait_for_pending_flips(crtc);
13322 if (ret)
13323 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013324
13325 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13326 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013327 }
13328
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013329 ret = mutex_lock_interruptible(&dev->struct_mutex);
13330 if (ret)
13331 return ret;
13332
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013333 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013334 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13335 u32 reset_counter;
13336
13337 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13338 mutex_unlock(&dev->struct_mutex);
13339
13340 for_each_plane_in_state(state, plane, plane_state, i) {
13341 struct intel_plane_state *intel_plane_state =
13342 to_intel_plane_state(plane_state);
13343
13344 if (!intel_plane_state->wait_req)
13345 continue;
13346
13347 ret = __i915_wait_request(intel_plane_state->wait_req,
13348 reset_counter, true,
13349 NULL, NULL);
13350
13351 /* Swallow -EIO errors to allow updates during hw lockup. */
13352 if (ret == -EIO)
13353 ret = 0;
13354
13355 if (ret)
13356 break;
13357 }
13358
13359 if (!ret)
13360 return 0;
13361
13362 mutex_lock(&dev->struct_mutex);
13363 drm_atomic_helper_cleanup_planes(dev, state);
13364 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013365
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013366 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013367 return ret;
13368}
13369
Maarten Lankhorste8861672016-02-24 11:24:26 +010013370static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13371 struct drm_i915_private *dev_priv,
13372 unsigned crtc_mask)
13373{
13374 unsigned last_vblank_count[I915_MAX_PIPES];
13375 enum pipe pipe;
13376 int ret;
13377
13378 if (!crtc_mask)
13379 return;
13380
13381 for_each_pipe(dev_priv, pipe) {
13382 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13383
13384 if (!((1 << pipe) & crtc_mask))
13385 continue;
13386
13387 ret = drm_crtc_vblank_get(crtc);
13388 if (WARN_ON(ret != 0)) {
13389 crtc_mask &= ~(1 << pipe);
13390 continue;
13391 }
13392
13393 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13394 }
13395
13396 for_each_pipe(dev_priv, pipe) {
13397 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13398 long lret;
13399
13400 if (!((1 << pipe) & crtc_mask))
13401 continue;
13402
13403 lret = wait_event_timeout(dev->vblank[pipe].queue,
13404 last_vblank_count[pipe] !=
13405 drm_crtc_vblank_count(crtc),
13406 msecs_to_jiffies(50));
13407
13408 WARN_ON(!lret);
13409
13410 drm_crtc_vblank_put(crtc);
13411 }
13412}
13413
13414static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13415{
13416 /* fb updated, need to unpin old fb */
13417 if (crtc_state->fb_changed)
13418 return true;
13419
13420 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013421 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013422 return true;
13423
13424 /*
13425 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013426 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013427 * but added for clarity.
13428 */
13429 if (crtc_state->disable_cxsr)
13430 return true;
13431
13432 return false;
13433}
13434
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013435/**
13436 * intel_atomic_commit - commit validated state object
13437 * @dev: DRM device
13438 * @state: the top-level driver state object
13439 * @async: asynchronous commit
13440 *
13441 * This function commits a top-level state object that has been validated
13442 * with drm_atomic_helper_check().
13443 *
13444 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13445 * we can only handle plane-related operations and do not yet support
13446 * asynchronous commit.
13447 *
13448 * RETURNS
13449 * Zero for success or -errno.
13450 */
13451static int intel_atomic_commit(struct drm_device *dev,
13452 struct drm_atomic_state *state,
13453 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013454{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013455 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013456 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013457 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013458 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013459 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013460 int ret = 0, i;
13461 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013462 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013463 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013464
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013465 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013466 if (ret) {
13467 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013468 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013469 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013470
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013471 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013472 dev_priv->wm.config = intel_state->wm_config;
13473 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013474
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013475 if (intel_state->modeset) {
13476 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13477 sizeof(intel_state->min_pixclk));
13478 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013479 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013480
13481 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013482 }
13483
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013484 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13486
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013487 if (needs_modeset(crtc->state) ||
13488 to_intel_crtc_state(crtc->state)->update_pipe) {
13489 hw_check = true;
13490
13491 put_domains[to_intel_crtc(crtc)->pipe] =
13492 modeset_get_crtc_power_domains(crtc,
13493 to_intel_crtc_state(crtc->state));
13494 }
13495
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013496 if (!needs_modeset(crtc->state))
13497 continue;
13498
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013499 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013500
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013501 if (old_crtc_state->active) {
13502 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013503 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013504 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013505 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013506 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013507
13508 /*
13509 * Underruns don't always raise
13510 * interrupts, so check manually.
13511 */
13512 intel_check_cpu_fifo_underruns(dev_priv);
13513 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013514
13515 if (!crtc->state->active)
13516 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013517 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013518 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013519
Daniel Vetterea9d7582012-07-10 10:42:52 +020013520 /* Only after disabling all output pipelines that will be changed can we
13521 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013522 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013523
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013524 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013525 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013526
13527 if (dev_priv->display.modeset_commit_cdclk &&
13528 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13529 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013530 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013531
Daniel Vettera6778b32012-07-02 09:56:42 +020013532 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013533 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13535 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013536 struct intel_crtc_state *pipe_config =
13537 to_intel_crtc_state(crtc->state);
13538 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013539
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013540 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013541 update_scanline_offset(to_intel_crtc(crtc));
13542 dev_priv->display.crtc_enable(crtc);
13543 }
13544
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013545 if (!modeset &&
13546 crtc->state->active &&
13547 crtc->state->color_mgmt_changed) {
13548 /*
13549 * Only update color management when not doing
13550 * a modeset as this will be done by
13551 * crtc_enable already.
13552 */
13553 intel_color_set_csc(crtc);
13554 intel_color_load_luts(crtc);
13555 }
13556
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013557 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013558 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013559
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013560 if (crtc->state->active &&
13561 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013562 intel_fbc_enable(intel_crtc);
13563
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013564 if (crtc->state->active &&
13565 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013566 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013567
Maarten Lankhorste8861672016-02-24 11:24:26 +010013568 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13569 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013570 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013571
Daniel Vettera6778b32012-07-02 09:56:42 +020013572 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013573
Maarten Lankhorste8861672016-02-24 11:24:26 +010013574 if (!state->legacy_cursor_update)
13575 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013576
Matt Ropered4a6a72016-02-23 17:20:13 -080013577 /*
13578 * Now that the vblank has passed, we can go ahead and program the
13579 * optimal watermarks on platforms that need two-step watermark
13580 * programming.
13581 *
13582 * TODO: Move this (and other cleanup) to an async worker eventually.
13583 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013584 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013585 intel_cstate = to_intel_crtc_state(crtc->state);
13586
13587 if (dev_priv->display.optimize_watermarks)
13588 dev_priv->display.optimize_watermarks(intel_cstate);
13589 }
13590
Matt Roper177246a2016-03-04 15:59:39 -080013591 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13592 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13593
13594 if (put_domains[i])
13595 modeset_put_power_domains(dev_priv, put_domains[i]);
13596 }
13597
13598 if (intel_state->modeset)
13599 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13600
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013601 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013602 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013603 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013604
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013605 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013606 intel_modeset_check_state(dev, state);
13607
13608 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013609
Mika Kuoppala75714942015-12-16 09:26:48 +020013610 /* As one of the primary mmio accessors, KMS has a high likelihood
13611 * of triggering bugs in unclaimed access. After we finish
13612 * modesetting, see if an error has been flagged, and if so
13613 * enable debugging for the next modeset - and hope we catch
13614 * the culprit.
13615 *
13616 * XXX note that we assume display power is on at this point.
13617 * This might hold true now but we need to add pm helper to check
13618 * unclaimed only when the hardware is on, as atomic commits
13619 * can happen also when the device is completely off.
13620 */
13621 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13622
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013623 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013624}
13625
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013626void intel_crtc_restore_mode(struct drm_crtc *crtc)
13627{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013628 struct drm_device *dev = crtc->dev;
13629 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013630 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013631 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013632
13633 state = drm_atomic_state_alloc(dev);
13634 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013635 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013636 crtc->base.id);
13637 return;
13638 }
13639
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013640 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013641
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013642retry:
13643 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13644 ret = PTR_ERR_OR_ZERO(crtc_state);
13645 if (!ret) {
13646 if (!crtc_state->active)
13647 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013648
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013649 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013650 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013651 }
13652
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013653 if (ret == -EDEADLK) {
13654 drm_atomic_state_clear(state);
13655 drm_modeset_backoff(state->acquire_ctx);
13656 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013657 }
13658
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013659 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013660out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013661 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013662}
13663
Daniel Vetter25c5b262012-07-08 22:08:04 +020013664#undef for_each_intel_crtc_masked
13665
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013666static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013667 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013668 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013669 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013670 .destroy = intel_crtc_destroy,
13671 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013672 .atomic_duplicate_state = intel_crtc_duplicate_state,
13673 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013674};
13675
Matt Roper6beb8c232014-12-01 15:40:14 -080013676/**
13677 * intel_prepare_plane_fb - Prepare fb for usage on plane
13678 * @plane: drm plane to prepare for
13679 * @fb: framebuffer to prepare for presentation
13680 *
13681 * Prepares a framebuffer for usage on a display plane. Generally this
13682 * involves pinning the underlying object and updating the frontbuffer tracking
13683 * bits. Some older platforms need special physical address handling for
13684 * cursor planes.
13685 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013686 * Must be called with struct_mutex held.
13687 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013688 * Returns 0 on success, negative error code on failure.
13689 */
13690int
13691intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013692 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013693{
13694 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013695 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013696 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013697 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013698 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013699 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013700
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013701 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013702 return 0;
13703
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013704 if (old_obj) {
13705 struct drm_crtc_state *crtc_state =
13706 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13707
13708 /* Big Hammer, we also need to ensure that any pending
13709 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13710 * current scanout is retired before unpinning the old
13711 * framebuffer. Note that we rely on userspace rendering
13712 * into the buffer attached to the pipe they are waiting
13713 * on. If not, userspace generates a GPU hang with IPEHR
13714 * point to the MI_WAIT_FOR_EVENT.
13715 *
13716 * This should only fail upon a hung GPU, in which case we
13717 * can safely continue.
13718 */
13719 if (needs_modeset(crtc_state))
13720 ret = i915_gem_object_wait_rendering(old_obj, true);
13721
13722 /* Swallow -EIO errors to allow updates during hw lockup. */
13723 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013724 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013725 }
13726
Alex Goins3c28ff22015-11-25 18:43:39 -080013727 /* For framebuffer backed by dmabuf, wait for fence */
13728 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013729 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013730
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013731 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13732 false, true,
13733 MAX_SCHEDULE_TIMEOUT);
13734 if (lret == -ERESTARTSYS)
13735 return lret;
13736
13737 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013738 }
13739
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013740 if (!obj) {
13741 ret = 0;
13742 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013743 INTEL_INFO(dev)->cursor_needs_physical) {
13744 int align = IS_I830(dev) ? 16 * 1024 : 256;
13745 ret = i915_gem_object_attach_phys(obj, align);
13746 if (ret)
13747 DRM_DEBUG_KMS("failed to attach phys object\n");
13748 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013749 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013750 }
13751
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013752 if (ret == 0) {
13753 if (obj) {
13754 struct intel_plane_state *plane_state =
13755 to_intel_plane_state(new_state);
13756
13757 i915_gem_request_assign(&plane_state->wait_req,
13758 obj->last_write_req);
13759 }
13760
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013761 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013762 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013763
Matt Roper6beb8c232014-12-01 15:40:14 -080013764 return ret;
13765}
13766
Matt Roper38f3ce32014-12-02 07:45:25 -080013767/**
13768 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13769 * @plane: drm plane to clean up for
13770 * @fb: old framebuffer that was on plane
13771 *
13772 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013773 *
13774 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013775 */
13776void
13777intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013778 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013779{
13780 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013781 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013782 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013783 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13784 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013785
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013786 old_intel_state = to_intel_plane_state(old_state);
13787
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013788 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013789 return;
13790
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013791 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13792 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013793 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013794
13795 /* prepare_fb aborted? */
13796 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13797 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13798 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013799
13800 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013801}
13802
Chandra Konduru6156a452015-04-27 13:48:39 -070013803int
13804skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13805{
13806 int max_scale;
13807 struct drm_device *dev;
13808 struct drm_i915_private *dev_priv;
13809 int crtc_clock, cdclk;
13810
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013811 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013812 return DRM_PLANE_HELPER_NO_SCALING;
13813
13814 dev = intel_crtc->base.dev;
13815 dev_priv = dev->dev_private;
13816 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013817 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013818
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013819 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013820 return DRM_PLANE_HELPER_NO_SCALING;
13821
13822 /*
13823 * skl max scale is lower of:
13824 * close to 3 but not 3, -1 is for that purpose
13825 * or
13826 * cdclk/crtc_clock
13827 */
13828 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13829
13830 return max_scale;
13831}
13832
Matt Roper465c1202014-05-29 08:06:54 -070013833static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013834intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013835 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013836 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013837{
Matt Roper2b875c22014-12-01 15:40:13 -080013838 struct drm_crtc *crtc = state->base.crtc;
13839 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013840 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013841 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13842 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013843
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013844 if (INTEL_INFO(plane->dev)->gen >= 9) {
13845 /* use scaler when colorkey is not required */
13846 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13847 min_scale = 1;
13848 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13849 }
Sonika Jindald8106362015-04-10 14:37:28 +053013850 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013851 }
Sonika Jindald8106362015-04-10 14:37:28 +053013852
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013853 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13854 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013855 min_scale, max_scale,
13856 can_position, true,
13857 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013858}
13859
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013860static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13861 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013862{
13863 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013865 struct intel_crtc_state *old_intel_state =
13866 to_intel_crtc_state(old_crtc_state);
13867 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013868
Matt Roperc34c9ee2014-12-23 10:41:50 -080013869 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013870 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013871
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013872 if (modeset)
13873 return;
13874
13875 if (to_intel_crtc_state(crtc->state)->update_pipe)
13876 intel_update_pipe_config(intel_crtc, old_intel_state);
13877 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013878 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013879}
13880
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013881static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13882 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013883{
Matt Roper32b7eee2014-12-24 07:59:06 -080013884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013885
Maarten Lankhorst62852622015-09-23 16:29:38 +020013886 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013887}
13888
Matt Ropercf4c7c12014-12-04 10:27:42 -080013889/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013890 * intel_plane_destroy - destroy a plane
13891 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013892 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013893 * Common destruction function for all types of planes (primary, cursor,
13894 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013895 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013896void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013897{
13898 struct intel_plane *intel_plane = to_intel_plane(plane);
13899 drm_plane_cleanup(plane);
13900 kfree(intel_plane);
13901}
13902
Matt Roper65a3fea2015-01-21 16:35:42 -080013903const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013904 .update_plane = drm_atomic_helper_update_plane,
13905 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013906 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013907 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013908 .atomic_get_property = intel_plane_atomic_get_property,
13909 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013910 .atomic_duplicate_state = intel_plane_duplicate_state,
13911 .atomic_destroy_state = intel_plane_destroy_state,
13912
Matt Roper465c1202014-05-29 08:06:54 -070013913};
13914
13915static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13916 int pipe)
13917{
13918 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013919 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013920 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013921 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013922
13923 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13924 if (primary == NULL)
13925 return NULL;
13926
Matt Roper8e7d6882015-01-21 16:35:41 -080013927 state = intel_create_plane_state(&primary->base);
13928 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013929 kfree(primary);
13930 return NULL;
13931 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013932 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013933
Matt Roper465c1202014-05-29 08:06:54 -070013934 primary->can_scale = false;
13935 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013936 if (INTEL_INFO(dev)->gen >= 9) {
13937 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013938 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013939 }
Matt Roper465c1202014-05-29 08:06:54 -070013940 primary->pipe = pipe;
13941 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013942 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013943 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013944 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13945 primary->plane = !pipe;
13946
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013947 if (INTEL_INFO(dev)->gen >= 9) {
13948 intel_primary_formats = skl_primary_formats;
13949 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013950
13951 primary->update_plane = skylake_update_primary_plane;
13952 primary->disable_plane = skylake_disable_primary_plane;
13953 } else if (HAS_PCH_SPLIT(dev)) {
13954 intel_primary_formats = i965_primary_formats;
13955 num_formats = ARRAY_SIZE(i965_primary_formats);
13956
13957 primary->update_plane = ironlake_update_primary_plane;
13958 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013959 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013960 intel_primary_formats = i965_primary_formats;
13961 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013962
13963 primary->update_plane = i9xx_update_primary_plane;
13964 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013965 } else {
13966 intel_primary_formats = i8xx_primary_formats;
13967 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013968
13969 primary->update_plane = i9xx_update_primary_plane;
13970 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013971 }
13972
13973 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013974 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013975 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020013976 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053013977
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013978 if (INTEL_INFO(dev)->gen >= 4)
13979 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013980
Matt Roperea2c67b2014-12-23 10:41:52 -080013981 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13982
Matt Roper465c1202014-05-29 08:06:54 -070013983 return &primary->base;
13984}
13985
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013986void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13987{
13988 if (!dev->mode_config.rotation_property) {
13989 unsigned long flags = BIT(DRM_ROTATE_0) |
13990 BIT(DRM_ROTATE_180);
13991
13992 if (INTEL_INFO(dev)->gen >= 9)
13993 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13994
13995 dev->mode_config.rotation_property =
13996 drm_mode_create_rotation_property(dev, flags);
13997 }
13998 if (dev->mode_config.rotation_property)
13999 drm_object_attach_property(&plane->base.base,
14000 dev->mode_config.rotation_property,
14001 plane->base.state->rotation);
14002}
14003
Matt Roper3d7d6512014-06-10 08:28:13 -070014004static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014005intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014006 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014007 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014008{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014009 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014010 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014011 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014012 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014013 unsigned stride;
14014 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014015
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014016 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14017 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014018 DRM_PLANE_HELPER_NO_SCALING,
14019 DRM_PLANE_HELPER_NO_SCALING,
14020 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014021 if (ret)
14022 return ret;
14023
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014024 /* if we want to turn off the cursor ignore width and height */
14025 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014026 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014027
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014028 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014029 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014030 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14031 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014032 return -EINVAL;
14033 }
14034
Matt Roperea2c67b2014-12-23 10:41:52 -080014035 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14036 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014037 DRM_DEBUG_KMS("buffer is too small\n");
14038 return -ENOMEM;
14039 }
14040
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014041 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014042 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014043 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014044 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014045
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014046 /*
14047 * There's something wrong with the cursor on CHV pipe C.
14048 * If it straddles the left edge of the screen then
14049 * moving it away from the edge or disabling it often
14050 * results in a pipe underrun, and often that can lead to
14051 * dead pipe (constant underrun reported, and it scans
14052 * out just a solid color). To recover from that, the
14053 * display power well must be turned off and on again.
14054 * Refuse the put the cursor into that compromised position.
14055 */
14056 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14057 state->visible && state->base.crtc_x < 0) {
14058 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14059 return -EINVAL;
14060 }
14061
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014062 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014063}
14064
Matt Roperf4a2cf22014-12-01 15:40:12 -080014065static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014066intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014067 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014068{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14070
14071 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014072 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014073}
14074
14075static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014076intel_update_cursor_plane(struct drm_plane *plane,
14077 const struct intel_crtc_state *crtc_state,
14078 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014079{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014080 struct drm_crtc *crtc = crtc_state->base.crtc;
14081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014082 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014083 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014084 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014085
Matt Roperf4a2cf22014-12-01 15:40:12 -080014086 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014087 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014088 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014089 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014090 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014091 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014092
Gustavo Padovana912f122014-12-01 15:40:10 -080014093 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014094 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014095}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014096
Matt Roper3d7d6512014-06-10 08:28:13 -070014097static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14098 int pipe)
14099{
14100 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014101 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014102
14103 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14104 if (cursor == NULL)
14105 return NULL;
14106
Matt Roper8e7d6882015-01-21 16:35:41 -080014107 state = intel_create_plane_state(&cursor->base);
14108 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014109 kfree(cursor);
14110 return NULL;
14111 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014112 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014113
Matt Roper3d7d6512014-06-10 08:28:13 -070014114 cursor->can_scale = false;
14115 cursor->max_downscale = 1;
14116 cursor->pipe = pipe;
14117 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014118 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014119 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014120 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014121 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014122
14123 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014124 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014125 intel_cursor_formats,
14126 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014127 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014128
14129 if (INTEL_INFO(dev)->gen >= 4) {
14130 if (!dev->mode_config.rotation_property)
14131 dev->mode_config.rotation_property =
14132 drm_mode_create_rotation_property(dev,
14133 BIT(DRM_ROTATE_0) |
14134 BIT(DRM_ROTATE_180));
14135 if (dev->mode_config.rotation_property)
14136 drm_object_attach_property(&cursor->base.base,
14137 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014138 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014139 }
14140
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014141 if (INTEL_INFO(dev)->gen >=9)
14142 state->scaler_id = -1;
14143
Matt Roperea2c67b2014-12-23 10:41:52 -080014144 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14145
Matt Roper3d7d6512014-06-10 08:28:13 -070014146 return &cursor->base;
14147}
14148
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014149static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14150 struct intel_crtc_state *crtc_state)
14151{
14152 int i;
14153 struct intel_scaler *intel_scaler;
14154 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14155
14156 for (i = 0; i < intel_crtc->num_scalers; i++) {
14157 intel_scaler = &scaler_state->scalers[i];
14158 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014159 intel_scaler->mode = PS_SCALER_MODE_DYN;
14160 }
14161
14162 scaler_state->scaler_id = -1;
14163}
14164
Hannes Ederb358d0a2008-12-18 21:18:47 +010014165static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014166{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014167 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014168 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014169 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014170 struct drm_plane *primary = NULL;
14171 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014172 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014173
Daniel Vetter955382f2013-09-19 14:05:45 +020014174 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014175 if (intel_crtc == NULL)
14176 return;
14177
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014178 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14179 if (!crtc_state)
14180 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014181 intel_crtc->config = crtc_state;
14182 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014183 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014184
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014185 /* initialize shared scalers */
14186 if (INTEL_INFO(dev)->gen >= 9) {
14187 if (pipe == PIPE_C)
14188 intel_crtc->num_scalers = 1;
14189 else
14190 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14191
14192 skl_init_scalers(dev, intel_crtc, crtc_state);
14193 }
14194
Matt Roper465c1202014-05-29 08:06:54 -070014195 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014196 if (!primary)
14197 goto fail;
14198
14199 cursor = intel_cursor_plane_create(dev, pipe);
14200 if (!cursor)
14201 goto fail;
14202
Matt Roper465c1202014-05-29 08:06:54 -070014203 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014204 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014205 if (ret)
14206 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014207
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014208 /*
14209 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014210 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014211 */
Jesse Barnes80824002009-09-10 15:28:06 -070014212 intel_crtc->pipe = pipe;
14213 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014214 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014215 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014216 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014217 }
14218
Chris Wilson4b0e3332014-05-30 16:35:26 +030014219 intel_crtc->cursor_base = ~0;
14220 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014221 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014222
Ville Syrjälä852eb002015-06-24 22:00:07 +030014223 intel_crtc->wm.cxsr_allowed = true;
14224
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014225 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14226 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14227 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14228 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14229
Jesse Barnes79e53942008-11-07 14:24:08 -080014230 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014231
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014232 intel_color_init(&intel_crtc->base);
14233
Daniel Vetter87b6b102014-05-15 15:33:46 +020014234 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014235 return;
14236
14237fail:
14238 if (primary)
14239 drm_plane_cleanup(primary);
14240 if (cursor)
14241 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014242 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014243 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014244}
14245
Jesse Barnes752aa882013-10-31 18:55:49 +020014246enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14247{
14248 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014249 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014250
Rob Clark51fd3712013-11-19 12:10:12 -050014251 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014252
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014253 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014254 return INVALID_PIPE;
14255
14256 return to_intel_crtc(encoder->crtc)->pipe;
14257}
14258
Carl Worth08d7b3d2009-04-29 14:43:54 -070014259int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014260 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014261{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014262 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014263 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014264 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014265
Rob Clark7707e652014-07-17 23:30:04 -040014266 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014267
Rob Clark7707e652014-07-17 23:30:04 -040014268 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014269 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014270 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014271 }
14272
Rob Clark7707e652014-07-17 23:30:04 -040014273 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014274 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014275
Daniel Vetterc05422d2009-08-11 16:05:30 +020014276 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014277}
14278
Daniel Vetter66a92782012-07-12 20:08:18 +020014279static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014280{
Daniel Vetter66a92782012-07-12 20:08:18 +020014281 struct drm_device *dev = encoder->base.dev;
14282 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014283 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014284 int entry = 0;
14285
Damien Lespiaub2784e12014-08-05 11:29:37 +010014286 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014287 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014288 index_mask |= (1 << entry);
14289
Jesse Barnes79e53942008-11-07 14:24:08 -080014290 entry++;
14291 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014292
Jesse Barnes79e53942008-11-07 14:24:08 -080014293 return index_mask;
14294}
14295
Chris Wilson4d302442010-12-14 19:21:29 +000014296static bool has_edp_a(struct drm_device *dev)
14297{
14298 struct drm_i915_private *dev_priv = dev->dev_private;
14299
14300 if (!IS_MOBILE(dev))
14301 return false;
14302
14303 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14304 return false;
14305
Damien Lespiaue3589902014-02-07 19:12:50 +000014306 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014307 return false;
14308
14309 return true;
14310}
14311
Jesse Barnes84b4e042014-06-25 08:24:29 -070014312static bool intel_crt_present(struct drm_device *dev)
14313{
14314 struct drm_i915_private *dev_priv = dev->dev_private;
14315
Damien Lespiau884497e2013-12-03 13:56:23 +000014316 if (INTEL_INFO(dev)->gen >= 9)
14317 return false;
14318
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014319 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014320 return false;
14321
14322 if (IS_CHERRYVIEW(dev))
14323 return false;
14324
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014325 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14326 return false;
14327
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014328 /* DDI E can't be used if DDI A requires 4 lanes */
14329 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14330 return false;
14331
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014332 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014333 return false;
14334
14335 return true;
14336}
14337
Jesse Barnes79e53942008-11-07 14:24:08 -080014338static void intel_setup_outputs(struct drm_device *dev)
14339{
Eric Anholt725e30a2009-01-22 13:01:02 -080014340 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014341 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014342 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014343
Daniel Vetterc9093352013-06-06 22:22:47 +020014344 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014345
Jesse Barnes84b4e042014-06-25 08:24:29 -070014346 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014347 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014348
Vandana Kannanc776eb22014-08-19 12:05:01 +053014349 if (IS_BROXTON(dev)) {
14350 /*
14351 * FIXME: Broxton doesn't support port detection via the
14352 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14353 * detect the ports.
14354 */
14355 intel_ddi_init(dev, PORT_A);
14356 intel_ddi_init(dev, PORT_B);
14357 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014358
14359 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014360 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014361 int found;
14362
Jesse Barnesde31fac2015-03-06 15:53:32 -080014363 /*
14364 * Haswell uses DDI functions to detect digital outputs.
14365 * On SKL pre-D0 the strap isn't connected, so we assume
14366 * it's there.
14367 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014368 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014369 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014370 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014371 intel_ddi_init(dev, PORT_A);
14372
14373 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14374 * register */
14375 found = I915_READ(SFUSE_STRAP);
14376
14377 if (found & SFUSE_STRAP_DDIB_DETECTED)
14378 intel_ddi_init(dev, PORT_B);
14379 if (found & SFUSE_STRAP_DDIC_DETECTED)
14380 intel_ddi_init(dev, PORT_C);
14381 if (found & SFUSE_STRAP_DDID_DETECTED)
14382 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014383 /*
14384 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14385 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014386 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014387 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14388 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14389 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14390 intel_ddi_init(dev, PORT_E);
14391
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014392 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014393 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014394 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014395
14396 if (has_edp_a(dev))
14397 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014398
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014399 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014400 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014401 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014402 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014403 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014404 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014405 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014406 }
14407
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014408 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014409 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014410
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014411 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014412 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014413
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014414 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014415 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014416
Daniel Vetter270b3042012-10-27 15:52:05 +020014417 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014418 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014419 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014420 /*
14421 * The DP_DETECTED bit is the latched state of the DDC
14422 * SDA pin at boot. However since eDP doesn't require DDC
14423 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14424 * eDP ports may have been muxed to an alternate function.
14425 * Thus we can't rely on the DP_DETECTED bit alone to detect
14426 * eDP ports. Consult the VBT as well as DP_DETECTED to
14427 * detect eDP ports.
14428 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014429 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014430 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014431 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14432 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014433 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014434 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014435
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014436 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014437 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014438 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14439 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014440 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014441 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014442
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014443 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014444 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014445 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14446 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14447 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14448 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014449 }
14450
Jani Nikula3cfca972013-08-27 15:12:26 +030014451 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014452 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014453 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014454
Paulo Zanonie2debe92013-02-18 19:00:27 -030014455 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014456 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014457 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014458 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014459 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014460 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014461 }
Ma Ling27185ae2009-08-24 13:50:23 +080014462
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014463 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014464 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014465 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014466
14467 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014468
Paulo Zanonie2debe92013-02-18 19:00:27 -030014469 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014470 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014471 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014472 }
Ma Ling27185ae2009-08-24 13:50:23 +080014473
Paulo Zanonie2debe92013-02-18 19:00:27 -030014474 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014475
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014476 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014477 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014478 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014479 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014480 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014481 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014482 }
Ma Ling27185ae2009-08-24 13:50:23 +080014483
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014484 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014485 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014486 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014487 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014488 intel_dvo_init(dev);
14489
Zhenyu Wang103a1962009-11-27 11:44:36 +080014490 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014491 intel_tv_init(dev);
14492
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014493 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014494
Damien Lespiaub2784e12014-08-05 11:29:37 +010014495 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014496 encoder->base.possible_crtcs = encoder->crtc_mask;
14497 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014498 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014499 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014500
Paulo Zanonidde86e22012-12-01 12:04:25 -020014501 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014502
14503 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014504}
14505
14506static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14507{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014508 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014509 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014510
Daniel Vetteref2d6332014-02-10 18:00:38 +010014511 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014512 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014513 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014514 drm_gem_object_unreference(&intel_fb->obj->base);
14515 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014516 kfree(intel_fb);
14517}
14518
14519static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014520 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014521 unsigned int *handle)
14522{
14523 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014524 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014525
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014526 if (obj->userptr.mm) {
14527 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14528 return -EINVAL;
14529 }
14530
Chris Wilson05394f32010-11-08 19:18:58 +000014531 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014532}
14533
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014534static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14535 struct drm_file *file,
14536 unsigned flags, unsigned color,
14537 struct drm_clip_rect *clips,
14538 unsigned num_clips)
14539{
14540 struct drm_device *dev = fb->dev;
14541 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14542 struct drm_i915_gem_object *obj = intel_fb->obj;
14543
14544 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014545 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014546 mutex_unlock(&dev->struct_mutex);
14547
14548 return 0;
14549}
14550
Jesse Barnes79e53942008-11-07 14:24:08 -080014551static const struct drm_framebuffer_funcs intel_fb_funcs = {
14552 .destroy = intel_user_framebuffer_destroy,
14553 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014554 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014555};
14556
Damien Lespiaub3218032015-02-27 11:15:18 +000014557static
14558u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14559 uint32_t pixel_format)
14560{
14561 u32 gen = INTEL_INFO(dev)->gen;
14562
14563 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014564 int cpp = drm_format_plane_cpp(pixel_format, 0);
14565
Damien Lespiaub3218032015-02-27 11:15:18 +000014566 /* "The stride in bytes must not exceed the of the size of 8K
14567 * pixels and 32K bytes."
14568 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014569 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014570 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014571 return 32*1024;
14572 } else if (gen >= 4) {
14573 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14574 return 16*1024;
14575 else
14576 return 32*1024;
14577 } else if (gen >= 3) {
14578 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14579 return 8*1024;
14580 else
14581 return 16*1024;
14582 } else {
14583 /* XXX DSPC is limited to 4k tiled */
14584 return 8*1024;
14585 }
14586}
14587
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014588static int intel_framebuffer_init(struct drm_device *dev,
14589 struct intel_framebuffer *intel_fb,
14590 struct drm_mode_fb_cmd2 *mode_cmd,
14591 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014592{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014593 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014594 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014595 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014596 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014597
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014598 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14599
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014600 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14601 /* Enforce that fb modifier and tiling mode match, but only for
14602 * X-tiled. This is needed for FBC. */
14603 if (!!(obj->tiling_mode == I915_TILING_X) !=
14604 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14605 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14606 return -EINVAL;
14607 }
14608 } else {
14609 if (obj->tiling_mode == I915_TILING_X)
14610 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14611 else if (obj->tiling_mode == I915_TILING_Y) {
14612 DRM_DEBUG("No Y tiling for legacy addfb\n");
14613 return -EINVAL;
14614 }
14615 }
14616
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014617 /* Passed in modifier sanity checking. */
14618 switch (mode_cmd->modifier[0]) {
14619 case I915_FORMAT_MOD_Y_TILED:
14620 case I915_FORMAT_MOD_Yf_TILED:
14621 if (INTEL_INFO(dev)->gen < 9) {
14622 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14623 mode_cmd->modifier[0]);
14624 return -EINVAL;
14625 }
14626 case DRM_FORMAT_MOD_NONE:
14627 case I915_FORMAT_MOD_X_TILED:
14628 break;
14629 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014630 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14631 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014632 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014633 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014634
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014635 stride_alignment = intel_fb_stride_alignment(dev_priv,
14636 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014637 mode_cmd->pixel_format);
14638 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14639 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14640 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014641 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014642 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014643
Damien Lespiaub3218032015-02-27 11:15:18 +000014644 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14645 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014646 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014647 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14648 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014649 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014650 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014651 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014652 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014653
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014654 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014655 mode_cmd->pitches[0] != obj->stride) {
14656 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14657 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014658 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014659 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014660
Ville Syrjälä57779d02012-10-31 17:50:14 +020014661 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014662 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014663 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014664 case DRM_FORMAT_RGB565:
14665 case DRM_FORMAT_XRGB8888:
14666 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014667 break;
14668 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014669 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014670 DRM_DEBUG("unsupported pixel format: %s\n",
14671 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014672 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014673 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014674 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014675 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014676 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14677 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014678 DRM_DEBUG("unsupported pixel format: %s\n",
14679 drm_get_format_name(mode_cmd->pixel_format));
14680 return -EINVAL;
14681 }
14682 break;
14683 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014684 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014685 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014686 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014687 DRM_DEBUG("unsupported pixel format: %s\n",
14688 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014689 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014690 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014691 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014692 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014693 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014694 DRM_DEBUG("unsupported pixel format: %s\n",
14695 drm_get_format_name(mode_cmd->pixel_format));
14696 return -EINVAL;
14697 }
14698 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014699 case DRM_FORMAT_YUYV:
14700 case DRM_FORMAT_UYVY:
14701 case DRM_FORMAT_YVYU:
14702 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014703 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014704 DRM_DEBUG("unsupported pixel format: %s\n",
14705 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014706 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014707 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014708 break;
14709 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014710 DRM_DEBUG("unsupported pixel format: %s\n",
14711 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014712 return -EINVAL;
14713 }
14714
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014715 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14716 if (mode_cmd->offsets[0] != 0)
14717 return -EINVAL;
14718
Damien Lespiauec2c9812015-01-20 12:51:45 +000014719 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014720 mode_cmd->pixel_format,
14721 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014722 /* FIXME drm helper for size checks (especially planar formats)? */
14723 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14724 return -EINVAL;
14725
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014726 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14727 intel_fb->obj = obj;
14728
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014729 intel_fill_fb_info(dev_priv, &intel_fb->base);
14730
Jesse Barnes79e53942008-11-07 14:24:08 -080014731 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14732 if (ret) {
14733 DRM_ERROR("framebuffer init failed %d\n", ret);
14734 return ret;
14735 }
14736
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014737 intel_fb->obj->framebuffer_references++;
14738
Jesse Barnes79e53942008-11-07 14:24:08 -080014739 return 0;
14740}
14741
Jesse Barnes79e53942008-11-07 14:24:08 -080014742static struct drm_framebuffer *
14743intel_user_framebuffer_create(struct drm_device *dev,
14744 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014745 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014746{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014747 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014748 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014749 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014750
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014751 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014752 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014753 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014754 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014755
Daniel Vetter92907cb2015-11-23 09:04:05 +010014756 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014757 if (IS_ERR(fb))
14758 drm_gem_object_unreference_unlocked(&obj->base);
14759
14760 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014761}
14762
Daniel Vetter06957262015-08-10 13:34:08 +020014763#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014764static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014765{
14766}
14767#endif
14768
Jesse Barnes79e53942008-11-07 14:24:08 -080014769static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014770 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014771 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014772 .atomic_check = intel_atomic_check,
14773 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014774 .atomic_state_alloc = intel_atomic_state_alloc,
14775 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014776};
14777
Imre Deak88212942016-03-16 13:38:53 +020014778/**
14779 * intel_init_display_hooks - initialize the display modesetting hooks
14780 * @dev_priv: device private
14781 */
14782void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014783{
Imre Deak88212942016-03-16 13:38:53 +020014784 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014785 dev_priv->display.find_dpll = g4x_find_best_dpll;
Imre Deak88212942016-03-16 13:38:53 +020014786 else if (IS_PINEVIEW(dev_priv))
Daniel Vetteree9300b2013-06-03 22:40:22 +020014787 dev_priv->display.find_dpll = pnv_find_best_dpll;
14788 else
14789 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14790
Imre Deak88212942016-03-16 13:38:53 +020014791 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014792 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014793 dev_priv->display.get_initial_plane_config =
14794 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014795 dev_priv->display.crtc_compute_clock =
14796 haswell_crtc_compute_clock;
14797 dev_priv->display.crtc_enable = haswell_crtc_enable;
14798 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014799 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014800 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014801 dev_priv->display.get_initial_plane_config =
14802 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014803 dev_priv->display.crtc_compute_clock =
14804 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014805 dev_priv->display.crtc_enable = haswell_crtc_enable;
14806 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014807 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014808 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014809 dev_priv->display.get_initial_plane_config =
14810 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014811 dev_priv->display.crtc_compute_clock =
14812 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014813 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14814 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014815 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014816 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014817 dev_priv->display.get_initial_plane_config =
14818 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014819 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14820 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14821 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14822 } else if (IS_VALLEYVIEW(dev_priv)) {
14823 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14824 dev_priv->display.get_initial_plane_config =
14825 i9xx_get_initial_plane_config;
14826 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014827 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14828 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014829 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014830 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014831 dev_priv->display.get_initial_plane_config =
14832 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014833 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014834 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14835 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014836 } else {
14837 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14838 dev_priv->display.get_initial_plane_config =
14839 i9xx_get_initial_plane_config;
14840 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14841 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14842 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014843 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014844
Jesse Barnese70236a2009-09-21 10:42:27 -070014845 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014846 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014847 dev_priv->display.get_display_clock_speed =
14848 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014849 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014850 dev_priv->display.get_display_clock_speed =
14851 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014852 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014853 dev_priv->display.get_display_clock_speed =
14854 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014855 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014856 dev_priv->display.get_display_clock_speed =
14857 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014858 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014859 dev_priv->display.get_display_clock_speed =
14860 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014861 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014862 dev_priv->display.get_display_clock_speed =
14863 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014864 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14865 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014866 dev_priv->display.get_display_clock_speed =
14867 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014868 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014869 dev_priv->display.get_display_clock_speed =
14870 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014871 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014872 dev_priv->display.get_display_clock_speed =
14873 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014874 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014875 dev_priv->display.get_display_clock_speed =
14876 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014877 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014878 dev_priv->display.get_display_clock_speed =
14879 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014880 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014881 dev_priv->display.get_display_clock_speed =
14882 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014883 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014884 dev_priv->display.get_display_clock_speed =
14885 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014886 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014887 dev_priv->display.get_display_clock_speed =
14888 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014889 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014890 dev_priv->display.get_display_clock_speed =
14891 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014892 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014893 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014894 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014895 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014896 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014897 dev_priv->display.get_display_clock_speed =
14898 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014899 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014900
Imre Deak88212942016-03-16 13:38:53 +020014901 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014902 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014903 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014904 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014905 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014906 /* FIXME: detect B0+ stepping and use auto training */
14907 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014908 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014909 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014910 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014911 dev_priv->display.modeset_commit_cdclk =
14912 broadwell_modeset_commit_cdclk;
14913 dev_priv->display.modeset_calc_cdclk =
14914 broadwell_modeset_calc_cdclk;
14915 }
Imre Deak88212942016-03-16 13:38:53 +020014916 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014917 dev_priv->display.modeset_commit_cdclk =
14918 valleyview_modeset_commit_cdclk;
14919 dev_priv->display.modeset_calc_cdclk =
14920 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014921 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014922 dev_priv->display.modeset_commit_cdclk =
14923 broxton_modeset_commit_cdclk;
14924 dev_priv->display.modeset_calc_cdclk =
14925 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014926 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014927
Imre Deak88212942016-03-16 13:38:53 +020014928 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014929 case 2:
14930 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14931 break;
14932
14933 case 3:
14934 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14935 break;
14936
14937 case 4:
14938 case 5:
14939 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14940 break;
14941
14942 case 6:
14943 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14944 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014945 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014946 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014947 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14948 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014949 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014950 /* Drop through - unsupported since execlist only. */
14951 default:
14952 /* Default just returns -ENODEV to indicate unsupported */
14953 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014954 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014955}
14956
Jesse Barnesb690e962010-07-19 13:53:12 -070014957/*
14958 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14959 * resume, or other times. This quirk makes sure that's the case for
14960 * affected systems.
14961 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014962static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014963{
14964 struct drm_i915_private *dev_priv = dev->dev_private;
14965
14966 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014967 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014968}
14969
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014970static void quirk_pipeb_force(struct drm_device *dev)
14971{
14972 struct drm_i915_private *dev_priv = dev->dev_private;
14973
14974 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14975 DRM_INFO("applying pipe b force quirk\n");
14976}
14977
Keith Packard435793d2011-07-12 14:56:22 -070014978/*
14979 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14980 */
14981static void quirk_ssc_force_disable(struct drm_device *dev)
14982{
14983 struct drm_i915_private *dev_priv = dev->dev_private;
14984 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014985 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014986}
14987
Carsten Emde4dca20e2012-03-15 15:56:26 +010014988/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014989 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14990 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014991 */
14992static void quirk_invert_brightness(struct drm_device *dev)
14993{
14994 struct drm_i915_private *dev_priv = dev->dev_private;
14995 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014996 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014997}
14998
Scot Doyle9c72cc62014-07-03 23:27:50 +000014999/* Some VBT's incorrectly indicate no backlight is present */
15000static void quirk_backlight_present(struct drm_device *dev)
15001{
15002 struct drm_i915_private *dev_priv = dev->dev_private;
15003 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15004 DRM_INFO("applying backlight present quirk\n");
15005}
15006
Jesse Barnesb690e962010-07-19 13:53:12 -070015007struct intel_quirk {
15008 int device;
15009 int subsystem_vendor;
15010 int subsystem_device;
15011 void (*hook)(struct drm_device *dev);
15012};
15013
Egbert Eich5f85f172012-10-14 15:46:38 +020015014/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15015struct intel_dmi_quirk {
15016 void (*hook)(struct drm_device *dev);
15017 const struct dmi_system_id (*dmi_id_list)[];
15018};
15019
15020static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15021{
15022 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15023 return 1;
15024}
15025
15026static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15027 {
15028 .dmi_id_list = &(const struct dmi_system_id[]) {
15029 {
15030 .callback = intel_dmi_reverse_brightness,
15031 .ident = "NCR Corporation",
15032 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15033 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15034 },
15035 },
15036 { } /* terminating entry */
15037 },
15038 .hook = quirk_invert_brightness,
15039 },
15040};
15041
Ben Widawskyc43b5632012-04-16 14:07:40 -070015042static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015043 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15044 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15045
Jesse Barnesb690e962010-07-19 13:53:12 -070015046 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15047 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15048
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015049 /* 830 needs to leave pipe A & dpll A up */
15050 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15051
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015052 /* 830 needs to leave pipe B & dpll B up */
15053 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15054
Keith Packard435793d2011-07-12 14:56:22 -070015055 /* Lenovo U160 cannot use SSC on LVDS */
15056 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015057
15058 /* Sony Vaio Y cannot use SSC on LVDS */
15059 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015060
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015061 /* Acer Aspire 5734Z must invert backlight brightness */
15062 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15063
15064 /* Acer/eMachines G725 */
15065 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15066
15067 /* Acer/eMachines e725 */
15068 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15069
15070 /* Acer/Packard Bell NCL20 */
15071 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15072
15073 /* Acer Aspire 4736Z */
15074 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015075
15076 /* Acer Aspire 5336 */
15077 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015078
15079 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15080 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015081
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015082 /* Acer C720 Chromebook (Core i3 4005U) */
15083 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15084
jens steinb2a96012014-10-28 20:25:53 +010015085 /* Apple Macbook 2,1 (Core 2 T7400) */
15086 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15087
Jani Nikula1b9448b2015-11-05 11:49:59 +020015088 /* Apple Macbook 4,1 */
15089 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15090
Scot Doyled4967d82014-07-03 23:27:52 +000015091 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15092 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015093
15094 /* HP Chromebook 14 (Celeron 2955U) */
15095 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015096
15097 /* Dell Chromebook 11 */
15098 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015099
15100 /* Dell Chromebook 11 (2015 version) */
15101 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015102};
15103
15104static void intel_init_quirks(struct drm_device *dev)
15105{
15106 struct pci_dev *d = dev->pdev;
15107 int i;
15108
15109 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15110 struct intel_quirk *q = &intel_quirks[i];
15111
15112 if (d->device == q->device &&
15113 (d->subsystem_vendor == q->subsystem_vendor ||
15114 q->subsystem_vendor == PCI_ANY_ID) &&
15115 (d->subsystem_device == q->subsystem_device ||
15116 q->subsystem_device == PCI_ANY_ID))
15117 q->hook(dev);
15118 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015119 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15120 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15121 intel_dmi_quirks[i].hook(dev);
15122 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015123}
15124
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015125/* Disable the VGA plane that we never use */
15126static void i915_disable_vga(struct drm_device *dev)
15127{
15128 struct drm_i915_private *dev_priv = dev->dev_private;
15129 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015130 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015131
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015132 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015133 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015134 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015135 sr1 = inb(VGA_SR_DATA);
15136 outb(sr1 | 1<<5, VGA_SR_DATA);
15137 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15138 udelay(300);
15139
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015140 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015141 POSTING_READ(vga_reg);
15142}
15143
Daniel Vetterf8175862012-04-10 15:50:11 +020015144void intel_modeset_init_hw(struct drm_device *dev)
15145{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015146 struct drm_i915_private *dev_priv = dev->dev_private;
15147
Ville Syrjäläb6283052015-06-03 15:45:07 +030015148 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015149
15150 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15151
Daniel Vetterf8175862012-04-10 15:50:11 +020015152 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015153 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015154}
15155
Matt Roperd93c0372015-12-03 11:37:41 -080015156/*
15157 * Calculate what we think the watermarks should be for the state we've read
15158 * out of the hardware and then immediately program those watermarks so that
15159 * we ensure the hardware settings match our internal state.
15160 *
15161 * We can calculate what we think WM's should be by creating a duplicate of the
15162 * current state (which was constructed during hardware readout) and running it
15163 * through the atomic check code to calculate new watermark values in the
15164 * state object.
15165 */
15166static void sanitize_watermarks(struct drm_device *dev)
15167{
15168 struct drm_i915_private *dev_priv = to_i915(dev);
15169 struct drm_atomic_state *state;
15170 struct drm_crtc *crtc;
15171 struct drm_crtc_state *cstate;
15172 struct drm_modeset_acquire_ctx ctx;
15173 int ret;
15174 int i;
15175
15176 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015177 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015178 return;
15179
15180 /*
15181 * We need to hold connection_mutex before calling duplicate_state so
15182 * that the connector loop is protected.
15183 */
15184 drm_modeset_acquire_init(&ctx, 0);
15185retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015186 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015187 if (ret == -EDEADLK) {
15188 drm_modeset_backoff(&ctx);
15189 goto retry;
15190 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015191 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015192 }
15193
15194 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15195 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015196 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015197
Matt Ropered4a6a72016-02-23 17:20:13 -080015198 /*
15199 * Hardware readout is the only time we don't want to calculate
15200 * intermediate watermarks (since we don't trust the current
15201 * watermarks).
15202 */
15203 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15204
Matt Roperd93c0372015-12-03 11:37:41 -080015205 ret = intel_atomic_check(dev, state);
15206 if (ret) {
15207 /*
15208 * If we fail here, it means that the hardware appears to be
15209 * programmed in a way that shouldn't be possible, given our
15210 * understanding of watermark requirements. This might mean a
15211 * mistake in the hardware readout code or a mistake in the
15212 * watermark calculations for a given platform. Raise a WARN
15213 * so that this is noticeable.
15214 *
15215 * If this actually happens, we'll have to just leave the
15216 * BIOS-programmed watermarks untouched and hope for the best.
15217 */
15218 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015219 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015220 }
15221
15222 /* Write calculated watermark values back */
15223 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15224 for_each_crtc_in_state(state, crtc, cstate, i) {
15225 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15226
Matt Ropered4a6a72016-02-23 17:20:13 -080015227 cs->wm.need_postvbl_update = true;
15228 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015229 }
15230
15231 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015232fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015233 drm_modeset_drop_locks(&ctx);
15234 drm_modeset_acquire_fini(&ctx);
15235}
15236
Jesse Barnes79e53942008-11-07 14:24:08 -080015237void intel_modeset_init(struct drm_device *dev)
15238{
Jesse Barnes652c3932009-08-17 13:31:43 -070015239 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015240 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015241 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015242 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015243
15244 drm_mode_config_init(dev);
15245
15246 dev->mode_config.min_width = 0;
15247 dev->mode_config.min_height = 0;
15248
Dave Airlie019d96c2011-09-29 16:20:42 +010015249 dev->mode_config.preferred_depth = 24;
15250 dev->mode_config.prefer_shadow = 1;
15251
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015252 dev->mode_config.allow_fb_modifiers = true;
15253
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015254 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015255
Jesse Barnesb690e962010-07-19 13:53:12 -070015256 intel_init_quirks(dev);
15257
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015258 intel_init_pm(dev);
15259
Ben Widawskye3c74752013-04-05 13:12:39 -070015260 if (INTEL_INFO(dev)->num_pipes == 0)
15261 return;
15262
Lukas Wunner69f92f62015-07-15 13:57:35 +020015263 /*
15264 * There may be no VBT; and if the BIOS enabled SSC we can
15265 * just keep using it to avoid unnecessary flicker. Whereas if the
15266 * BIOS isn't using it, don't assume it will work even if the VBT
15267 * indicates as much.
15268 */
15269 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15270 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15271 DREF_SSC1_ENABLE);
15272
15273 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15274 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15275 bios_lvds_use_ssc ? "en" : "dis",
15276 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15277 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15278 }
15279 }
15280
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015281 if (IS_GEN2(dev)) {
15282 dev->mode_config.max_width = 2048;
15283 dev->mode_config.max_height = 2048;
15284 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015285 dev->mode_config.max_width = 4096;
15286 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015287 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015288 dev->mode_config.max_width = 8192;
15289 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015290 }
Damien Lespiau068be562014-03-28 14:17:49 +000015291
Ville Syrjälädc41c152014-08-13 11:57:05 +030015292 if (IS_845G(dev) || IS_I865G(dev)) {
15293 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15294 dev->mode_config.cursor_height = 1023;
15295 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015296 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15297 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15298 } else {
15299 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15300 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15301 }
15302
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015303 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015304
Zhao Yakui28c97732009-10-09 11:39:41 +080015305 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015306 INTEL_INFO(dev)->num_pipes,
15307 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015308
Damien Lespiau055e3932014-08-18 13:49:10 +010015309 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015310 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015311 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015312 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015313 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015314 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015315 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015316 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015317 }
15318
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015319 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015320 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015321 intel_update_cdclk(dev);
15322
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015323 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015324
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015325 /* Just disable it once at startup */
15326 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015327 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015328
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015329 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015330 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015331 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015332
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015333 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015334 struct intel_initial_plane_config plane_config = {};
15335
Jesse Barnes46f297f2014-03-07 08:57:48 -080015336 if (!crtc->active)
15337 continue;
15338
Jesse Barnes46f297f2014-03-07 08:57:48 -080015339 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015340 * Note that reserving the BIOS fb up front prevents us
15341 * from stuffing other stolen allocations like the ring
15342 * on top. This prevents some ugliness at boot time, and
15343 * can even allow for smooth boot transitions if the BIOS
15344 * fb is large enough for the active pipe configuration.
15345 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015346 dev_priv->display.get_initial_plane_config(crtc,
15347 &plane_config);
15348
15349 /*
15350 * If the fb is shared between multiple heads, we'll
15351 * just get the first one.
15352 */
15353 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015354 }
Matt Roperd93c0372015-12-03 11:37:41 -080015355
15356 /*
15357 * Make sure hardware watermarks really match the state we read out.
15358 * Note that we need to do this after reconstructing the BIOS fb's
15359 * since the watermark calculation done here will use pstate->fb.
15360 */
15361 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015362}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015363
Daniel Vetter7fad7982012-07-04 17:51:47 +020015364static void intel_enable_pipe_a(struct drm_device *dev)
15365{
15366 struct intel_connector *connector;
15367 struct drm_connector *crt = NULL;
15368 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015369 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015370
15371 /* We can't just switch on the pipe A, we need to set things up with a
15372 * proper mode and output configuration. As a gross hack, enable pipe A
15373 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015374 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015375 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15376 crt = &connector->base;
15377 break;
15378 }
15379 }
15380
15381 if (!crt)
15382 return;
15383
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015384 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015385 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015386}
15387
Daniel Vetterfa555832012-10-10 23:14:00 +020015388static bool
15389intel_check_plane_mapping(struct intel_crtc *crtc)
15390{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015391 struct drm_device *dev = crtc->base.dev;
15392 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015393 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015394
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015395 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015396 return true;
15397
Ville Syrjälä649636e2015-09-22 19:50:01 +030015398 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015399
15400 if ((val & DISPLAY_PLANE_ENABLE) &&
15401 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15402 return false;
15403
15404 return true;
15405}
15406
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015407static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15408{
15409 struct drm_device *dev = crtc->base.dev;
15410 struct intel_encoder *encoder;
15411
15412 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15413 return true;
15414
15415 return false;
15416}
15417
Ville Syrjälädd756192016-02-17 21:28:45 +020015418static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15419{
15420 struct drm_device *dev = encoder->base.dev;
15421 struct intel_connector *connector;
15422
15423 for_each_connector_on_encoder(dev, &encoder->base, connector)
15424 return true;
15425
15426 return false;
15427}
15428
Daniel Vetter24929352012-07-02 20:28:59 +020015429static void intel_sanitize_crtc(struct intel_crtc *crtc)
15430{
15431 struct drm_device *dev = crtc->base.dev;
15432 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015433 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015434
Daniel Vetter24929352012-07-02 20:28:59 +020015435 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015436 if (!transcoder_is_dsi(cpu_transcoder)) {
15437 i915_reg_t reg = PIPECONF(cpu_transcoder);
15438
15439 I915_WRITE(reg,
15440 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15441 }
Daniel Vetter24929352012-07-02 20:28:59 +020015442
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015443 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015444 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015445 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015446 struct intel_plane *plane;
15447
Daniel Vetter96256042015-02-13 21:03:42 +010015448 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015449
15450 /* Disable everything but the primary plane */
15451 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15452 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15453 continue;
15454
15455 plane->disable_plane(&plane->base, &crtc->base);
15456 }
Daniel Vetter96256042015-02-13 21:03:42 +010015457 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015458
Daniel Vetter24929352012-07-02 20:28:59 +020015459 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015460 * disable the crtc (and hence change the state) if it is wrong. Note
15461 * that gen4+ has a fixed plane -> pipe mapping. */
15462 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015463 bool plane;
15464
Daniel Vetter24929352012-07-02 20:28:59 +020015465 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15466 crtc->base.base.id);
15467
15468 /* Pipe has the wrong plane attached and the plane is active.
15469 * Temporarily change the plane mapping and disable everything
15470 * ... */
15471 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015472 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015473 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015474 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015475 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015476 }
Daniel Vetter24929352012-07-02 20:28:59 +020015477
Daniel Vetter7fad7982012-07-04 17:51:47 +020015478 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15479 crtc->pipe == PIPE_A && !crtc->active) {
15480 /* BIOS forgot to enable pipe A, this mostly happens after
15481 * resume. Force-enable the pipe to fix this, the update_dpms
15482 * call below we restore the pipe to the right state, but leave
15483 * the required bits on. */
15484 intel_enable_pipe_a(dev);
15485 }
15486
Daniel Vetter24929352012-07-02 20:28:59 +020015487 /* Adjust the state of the output pipe according to whether we
15488 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015489 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015490 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015491
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015492 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015493 /*
15494 * We start out with underrun reporting disabled to avoid races.
15495 * For correct bookkeeping mark this on active crtcs.
15496 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015497 * Also on gmch platforms we dont have any hardware bits to
15498 * disable the underrun reporting. Which means we need to start
15499 * out with underrun reporting disabled also on inactive pipes,
15500 * since otherwise we'll complain about the garbage we read when
15501 * e.g. coming up after runtime pm.
15502 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015503 * No protection against concurrent access is required - at
15504 * worst a fifo underrun happens which also sets this to false.
15505 */
15506 crtc->cpu_fifo_underrun_disabled = true;
15507 crtc->pch_fifo_underrun_disabled = true;
15508 }
Daniel Vetter24929352012-07-02 20:28:59 +020015509}
15510
15511static void intel_sanitize_encoder(struct intel_encoder *encoder)
15512{
15513 struct intel_connector *connector;
15514 struct drm_device *dev = encoder->base.dev;
15515
15516 /* We need to check both for a crtc link (meaning that the
15517 * encoder is active and trying to read from a pipe) and the
15518 * pipe itself being active. */
15519 bool has_active_crtc = encoder->base.crtc &&
15520 to_intel_crtc(encoder->base.crtc)->active;
15521
Ville Syrjälädd756192016-02-17 21:28:45 +020015522 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015523 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15524 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015525 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015526
15527 /* Connector is active, but has no active pipe. This is
15528 * fallout from our resume register restoring. Disable
15529 * the encoder manually again. */
15530 if (encoder->base.crtc) {
15531 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15532 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015533 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015534 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015535 if (encoder->post_disable)
15536 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015537 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015538 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015539
15540 /* Inconsistent output/port/pipe state happens presumably due to
15541 * a bug in one of the get_hw_state functions. Or someplace else
15542 * in our code, like the register restore mess on resume. Clamp
15543 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015544 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015545 if (connector->encoder != encoder)
15546 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015547 connector->base.dpms = DRM_MODE_DPMS_OFF;
15548 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015549 }
15550 }
15551 /* Enabled encoders without active connectors will be fixed in
15552 * the crtc fixup. */
15553}
15554
Imre Deak04098752014-02-18 00:02:16 +020015555void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015556{
15557 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015558 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015559
Imre Deak04098752014-02-18 00:02:16 +020015560 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15561 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15562 i915_disable_vga(dev);
15563 }
15564}
15565
15566void i915_redisable_vga(struct drm_device *dev)
15567{
15568 struct drm_i915_private *dev_priv = dev->dev_private;
15569
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015570 /* This function can be called both from intel_modeset_setup_hw_state or
15571 * at a very early point in our resume sequence, where the power well
15572 * structures are not yet restored. Since this function is at a very
15573 * paranoid "someone might have enabled VGA while we were not looking"
15574 * level, just check if the power well is enabled instead of trying to
15575 * follow the "don't touch the power well if we don't need it" policy
15576 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015577 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015578 return;
15579
Imre Deak04098752014-02-18 00:02:16 +020015580 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015581
15582 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015583}
15584
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015585static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015586{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015587 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015588
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015589 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015590}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015591
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015592/* FIXME read out full plane state for all planes */
15593static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015594{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015595 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015596 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015597 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015598
Matt Roper19b8d382015-09-24 15:53:17 -070015599 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015600 primary_get_hw_state(to_intel_plane(primary));
15601
15602 if (plane_state->visible)
15603 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015604}
15605
Daniel Vetter30e984d2013-06-05 13:34:17 +020015606static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015607{
15608 struct drm_i915_private *dev_priv = dev->dev_private;
15609 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015610 struct intel_crtc *crtc;
15611 struct intel_encoder *encoder;
15612 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015613 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015614
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015615 dev_priv->active_crtcs = 0;
15616
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015617 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015618 struct intel_crtc_state *crtc_state = crtc->config;
15619 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015620
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015621 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15622 memset(crtc_state, 0, sizeof(*crtc_state));
15623 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015624
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015625 crtc_state->base.active = crtc_state->base.enable =
15626 dev_priv->display.get_pipe_config(crtc, crtc_state);
15627
15628 crtc->base.enabled = crtc_state->base.enable;
15629 crtc->active = crtc_state->base.active;
15630
15631 if (crtc_state->base.active) {
15632 dev_priv->active_crtcs |= 1 << crtc->pipe;
15633
15634 if (IS_BROADWELL(dev_priv)) {
15635 pixclk = ilk_pipe_pixel_rate(crtc_state);
15636
15637 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15638 if (crtc_state->ips_enabled)
15639 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15640 } else if (IS_VALLEYVIEW(dev_priv) ||
15641 IS_CHERRYVIEW(dev_priv) ||
15642 IS_BROXTON(dev_priv))
15643 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15644 else
15645 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15646 }
15647
15648 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015649
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015650 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015651
15652 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15653 crtc->base.base.id,
15654 crtc->active ? "enabled" : "disabled");
15655 }
15656
Daniel Vetter53589012013-06-05 13:34:16 +020015657 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15658 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15659
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015660 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15661 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015662 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015663 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015664 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015665 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015666 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015667 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015668
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015669 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015670 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015671 }
15672
Damien Lespiaub2784e12014-08-05 11:29:37 +010015673 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015674 pipe = 0;
15675
15676 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015677 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15678 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015679 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015680 } else {
15681 encoder->base.crtc = NULL;
15682 }
15683
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015684 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015685 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015686 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015687 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015688 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015689 }
15690
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015691 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015692 if (connector->get_hw_state(connector)) {
15693 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015694
15695 encoder = connector->encoder;
15696 connector->base.encoder = &encoder->base;
15697
15698 if (encoder->base.crtc &&
15699 encoder->base.crtc->state->active) {
15700 /*
15701 * This has to be done during hardware readout
15702 * because anything calling .crtc_disable may
15703 * rely on the connector_mask being accurate.
15704 */
15705 encoder->base.crtc->state->connector_mask |=
15706 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015707 encoder->base.crtc->state->encoder_mask |=
15708 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015709 }
15710
Daniel Vetter24929352012-07-02 20:28:59 +020015711 } else {
15712 connector->base.dpms = DRM_MODE_DPMS_OFF;
15713 connector->base.encoder = NULL;
15714 }
15715 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15716 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015717 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015718 connector->base.encoder ? "enabled" : "disabled");
15719 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015720
15721 for_each_intel_crtc(dev, crtc) {
15722 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15723
15724 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15725 if (crtc->base.state->active) {
15726 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15727 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15728 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15729
15730 /*
15731 * The initial mode needs to be set in order to keep
15732 * the atomic core happy. It wants a valid mode if the
15733 * crtc's enabled, so we do the above call.
15734 *
15735 * At this point some state updated by the connectors
15736 * in their ->detect() callback has not run yet, so
15737 * no recalculation can be done yet.
15738 *
15739 * Even if we could do a recalculation and modeset
15740 * right now it would cause a double modeset if
15741 * fbdev or userspace chooses a different initial mode.
15742 *
15743 * If that happens, someone indicated they wanted a
15744 * mode change, which means it's safe to do a full
15745 * recalculation.
15746 */
15747 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015748
15749 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15750 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015751 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015752
15753 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015754 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015755}
15756
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015757/* Scan out the current hw modeset state,
15758 * and sanitizes it to the current state
15759 */
15760static void
15761intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015762{
15763 struct drm_i915_private *dev_priv = dev->dev_private;
15764 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015765 struct intel_crtc *crtc;
15766 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015767 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015768
15769 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015770
15771 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015772 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015773 intel_sanitize_encoder(encoder);
15774 }
15775
Damien Lespiau055e3932014-08-18 13:49:10 +010015776 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015777 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15778 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015779 intel_dump_pipe_config(crtc, crtc->config,
15780 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015781 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015782
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015783 intel_modeset_update_connector_atomic_state(dev);
15784
Daniel Vetter35c95372013-07-17 06:55:04 +020015785 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15786 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15787
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015788 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015789 continue;
15790
15791 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15792
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015793 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015794 pll->on = false;
15795 }
15796
Wayne Boyer666a4532015-12-09 12:29:35 -080015797 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015798 vlv_wm_get_hw_state(dev);
15799 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015800 skl_wm_get_hw_state(dev);
15801 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015802 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015803
15804 for_each_intel_crtc(dev, crtc) {
15805 unsigned long put_domains;
15806
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015807 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015808 if (WARN_ON(put_domains))
15809 modeset_put_power_domains(dev_priv, put_domains);
15810 }
15811 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015812
15813 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015814}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015815
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015816void intel_display_resume(struct drm_device *dev)
15817{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015818 struct drm_i915_private *dev_priv = to_i915(dev);
15819 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15820 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015821 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015822 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015823
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015824 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015825
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015826 /*
15827 * This is a cludge because with real atomic modeset mode_config.mutex
15828 * won't be taken. Unfortunately some probed state like
15829 * audio_codec_enable is still protected by mode_config.mutex, so lock
15830 * it here for now.
15831 */
15832 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015833 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015834
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015835retry:
15836 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015837
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015838 if (ret == 0 && !setup) {
15839 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015840
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015841 intel_modeset_setup_hw_state(dev);
15842 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015843 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015844
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015845 if (ret == 0 && state) {
15846 struct drm_crtc_state *crtc_state;
15847 struct drm_crtc *crtc;
15848 int i;
15849
15850 state->acquire_ctx = &ctx;
15851
15852 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15853 /*
15854 * Force recalculation even if we restore
15855 * current state. With fast modeset this may not result
15856 * in a modeset when the state is compatible.
15857 */
15858 crtc_state->mode_changed = true;
15859 }
15860
15861 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015862 }
15863
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015864 if (ret == -EDEADLK) {
15865 drm_modeset_backoff(&ctx);
15866 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015867 }
15868
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015869 drm_modeset_drop_locks(&ctx);
15870 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015871 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015872
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015873 if (ret) {
15874 DRM_ERROR("Restoring old state failed with %i\n", ret);
15875 drm_atomic_state_free(state);
15876 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015877}
15878
15879void intel_modeset_gem_init(struct drm_device *dev)
15880{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015881 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015882 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015883 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015884
Imre Deakae484342014-03-31 15:10:44 +030015885 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015886
Chris Wilson1833b132012-05-09 11:56:28 +010015887 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015888
15889 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015890
15891 /*
15892 * Make sure any fbs we allocated at startup are properly
15893 * pinned & fenced. When we do the allocation it's too early
15894 * for this.
15895 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015896 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015897 obj = intel_fb_obj(c->primary->fb);
15898 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015899 continue;
15900
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015901 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015902 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15903 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015904 mutex_unlock(&dev->struct_mutex);
15905 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015906 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15907 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015908 drm_framebuffer_unreference(c->primary->fb);
15909 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015910 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015911 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015912 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015913 }
15914 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015915
15916 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015917}
15918
Imre Deak4932e2c2014-02-11 17:12:48 +020015919void intel_connector_unregister(struct intel_connector *intel_connector)
15920{
15921 struct drm_connector *connector = &intel_connector->base;
15922
15923 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015924 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015925}
15926
Jesse Barnes79e53942008-11-07 14:24:08 -080015927void intel_modeset_cleanup(struct drm_device *dev)
15928{
Jesse Barnes652c3932009-08-17 13:31:43 -070015929 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015930 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015931
Imre Deak2eb52522014-11-19 15:30:05 +020015932 intel_disable_gt_powersave(dev);
15933
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015934 intel_backlight_unregister(dev);
15935
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015936 /*
15937 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015938 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015939 * experience fancy races otherwise.
15940 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015941 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015942
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015943 /*
15944 * Due to the hpd irq storm handling the hotplug work can re-arm the
15945 * poll handlers. Hence disable polling after hpd handling is shut down.
15946 */
Keith Packardf87ea762010-10-03 19:36:26 -070015947 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015948
Jesse Barnes723bfd72010-10-07 16:01:13 -070015949 intel_unregister_dsm_handler();
15950
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015951 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015952
Chris Wilson1630fe72011-07-08 12:22:42 +010015953 /* flush any delayed tasks or pending work */
15954 flush_scheduled_work();
15955
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015956 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020015957 for_each_intel_connector(dev, connector)
15958 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030015959
Jesse Barnes79e53942008-11-07 14:24:08 -080015960 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015961
15962 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015963
Imre Deakae484342014-03-31 15:10:44 +030015964 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010015965
15966 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015967}
15968
Dave Airlie28d52042009-09-21 14:33:58 +100015969/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015970 * Return which encoder is currently attached for connector.
15971 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015972struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015973{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015974 return &intel_attached_encoder(connector)->base;
15975}
Jesse Barnes79e53942008-11-07 14:24:08 -080015976
Chris Wilsondf0e9242010-09-09 16:20:55 +010015977void intel_connector_attach_encoder(struct intel_connector *connector,
15978 struct intel_encoder *encoder)
15979{
15980 connector->encoder = encoder;
15981 drm_mode_connector_attach_encoder(&connector->base,
15982 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015983}
Dave Airlie28d52042009-09-21 14:33:58 +100015984
15985/*
15986 * set vga decode state - true == enable VGA decode
15987 */
15988int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15989{
15990 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015991 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015992 u16 gmch_ctrl;
15993
Chris Wilson75fa0412014-02-07 18:37:02 -020015994 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15995 DRM_ERROR("failed to read control word\n");
15996 return -EIO;
15997 }
15998
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015999 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16000 return 0;
16001
Dave Airlie28d52042009-09-21 14:33:58 +100016002 if (state)
16003 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16004 else
16005 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016006
16007 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16008 DRM_ERROR("failed to write control word\n");
16009 return -EIO;
16010 }
16011
Dave Airlie28d52042009-09-21 14:33:58 +100016012 return 0;
16013}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016014
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016015struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016016
16017 u32 power_well_driver;
16018
Chris Wilson63b66e52013-08-08 15:12:06 +020016019 int num_transcoders;
16020
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016021 struct intel_cursor_error_state {
16022 u32 control;
16023 u32 position;
16024 u32 base;
16025 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016026 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016027
16028 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016029 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016030 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016031 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016032 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016033
16034 struct intel_plane_error_state {
16035 u32 control;
16036 u32 stride;
16037 u32 size;
16038 u32 pos;
16039 u32 addr;
16040 u32 surface;
16041 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016042 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016043
16044 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016045 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016046 enum transcoder cpu_transcoder;
16047
16048 u32 conf;
16049
16050 u32 htotal;
16051 u32 hblank;
16052 u32 hsync;
16053 u32 vtotal;
16054 u32 vblank;
16055 u32 vsync;
16056 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016057};
16058
16059struct intel_display_error_state *
16060intel_display_capture_error_state(struct drm_device *dev)
16061{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016062 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016063 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016064 int transcoders[] = {
16065 TRANSCODER_A,
16066 TRANSCODER_B,
16067 TRANSCODER_C,
16068 TRANSCODER_EDP,
16069 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016070 int i;
16071
Chris Wilson63b66e52013-08-08 15:12:06 +020016072 if (INTEL_INFO(dev)->num_pipes == 0)
16073 return NULL;
16074
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016075 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016076 if (error == NULL)
16077 return NULL;
16078
Imre Deak190be112013-11-25 17:15:31 +020016079 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016080 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16081
Damien Lespiau055e3932014-08-18 13:49:10 +010016082 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016083 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016084 __intel_display_power_is_enabled(dev_priv,
16085 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016086 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016087 continue;
16088
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016089 error->cursor[i].control = I915_READ(CURCNTR(i));
16090 error->cursor[i].position = I915_READ(CURPOS(i));
16091 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016092
16093 error->plane[i].control = I915_READ(DSPCNTR(i));
16094 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016095 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016096 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016097 error->plane[i].pos = I915_READ(DSPPOS(i));
16098 }
Paulo Zanonica291362013-03-06 20:03:14 -030016099 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16100 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016101 if (INTEL_INFO(dev)->gen >= 4) {
16102 error->plane[i].surface = I915_READ(DSPSURF(i));
16103 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16104 }
16105
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016106 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016107
Sonika Jindal3abfce72014-07-21 15:23:43 +053016108 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016109 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016110 }
16111
Jani Nikula4d1de972016-03-18 17:05:42 +020016112 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016113 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16114 if (HAS_DDI(dev_priv->dev))
16115 error->num_transcoders++; /* Account for eDP. */
16116
16117 for (i = 0; i < error->num_transcoders; i++) {
16118 enum transcoder cpu_transcoder = transcoders[i];
16119
Imre Deakddf9c532013-11-27 22:02:02 +020016120 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016121 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016122 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016123 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016124 continue;
16125
Chris Wilson63b66e52013-08-08 15:12:06 +020016126 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16127
16128 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16129 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16130 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16131 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16132 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16133 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16134 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016135 }
16136
16137 return error;
16138}
16139
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016140#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16141
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016142void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016143intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016144 struct drm_device *dev,
16145 struct intel_display_error_state *error)
16146{
Damien Lespiau055e3932014-08-18 13:49:10 +010016147 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016148 int i;
16149
Chris Wilson63b66e52013-08-08 15:12:06 +020016150 if (!error)
16151 return;
16152
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016153 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016154 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016155 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016156 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016157 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016158 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016159 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016160 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016161 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016162 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016163
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016164 err_printf(m, "Plane [%d]:\n", i);
16165 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16166 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016167 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016168 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16169 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016170 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016171 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016172 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016173 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016174 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16175 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016176 }
16177
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016178 err_printf(m, "Cursor [%d]:\n", i);
16179 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16180 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16181 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016182 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016183
16184 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016185 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016186 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016187 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016188 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016189 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16190 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16191 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16192 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16193 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16194 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16195 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16196 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016197}