blob: 7c4ffcaf13c14579a81f432dddc6370501088b24 [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
Imre Deakdccbea32015-06-22 23:35:51 +0300567/*
568 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
569 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
570 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
571 * The helpers' return value is the rate of the clock that is fed to the
572 * display engine's pipe which can be the above fast dot clock rate or a
573 * divided-down version of it.
574 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500575/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300576static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800577{
Shaohua Li21778322009-02-23 15:19:16 +0800578 clock->m = clock->m2 + 2;
579 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200580 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300581 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300582 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
583 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300584
585 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800586}
587
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200588static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
589{
590 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
591}
592
Imre Deakdccbea32015-06-22 23:35:51 +0300593static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800594{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200595 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200597 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300598 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300599 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300601
602 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800603}
604
Imre Deakdccbea32015-06-22 23:35:51 +0300605static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300606{
607 clock->m = clock->m1 * clock->m2;
608 clock->p = clock->p1 * clock->p2;
609 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300610 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300613
614 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300615}
616
Imre Deakdccbea32015-06-22 23:35:51 +0300617int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300622 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300623 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
624 clock->n << 22);
625 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300626
627 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300628}
629
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800630#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800631/**
632 * Returns whether the given set of divisors are valid for a given refclk with
633 * the given connectors.
634 */
635
Chris Wilson1b894b52010-12-14 20:04:54 +0000636static bool intel_PLL_is_valid(struct drm_device *dev,
637 const intel_limit_t *limit,
638 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800639{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300640 if (clock->n < limit->n.min || limit->n.max < clock->n)
641 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400643 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800644 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400645 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400647 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300648
Wayne Boyer666a4532015-12-09 12:29:35 -0800649 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
650 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300651 if (clock->m1 <= clock->m2)
652 INTELPllInvalid("m1 <= m2\n");
653
Wayne Boyer666a4532015-12-09 12:29:35 -0800654 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300655 if (clock->p < limit->p.min || limit->p.max < clock->p)
656 INTELPllInvalid("p out of range\n");
657 if (clock->m < limit->m.min || limit->m.max < clock->m)
658 INTELPllInvalid("m out of range\n");
659 }
660
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400662 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
664 * connector, etc., rather than just a single range.
665 */
666 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400667 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800668
669 return true;
670}
671
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300672static int
673i9xx_select_p2_div(const intel_limit_t *limit,
674 const struct intel_crtc_state *crtc_state,
675 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800676{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300677 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800678
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200679 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100681 * For LVDS just rely on its current settings for dual-channel.
682 * We haven't figured out how to reliably set up different
683 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100685 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800687 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300688 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800689 } else {
690 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300691 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300693 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800694 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300695}
696
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200697/*
698 * Returns a set of divisors for the desired target clock with the given
699 * refclk, or FALSE. The returned values represent the clock equation:
700 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
701 *
702 * Target and reference clocks are specified in kHz.
703 *
704 * If match_clock is provided, then best_clock P divider must match the P
705 * divider from @match_clock used for LVDS downclocking.
706 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300707static bool
708i9xx_find_best_dpll(const intel_limit_t *limit,
709 struct intel_crtc_state *crtc_state,
710 int target, int refclk, intel_clock_t *match_clock,
711 intel_clock_t *best_clock)
712{
713 struct drm_device *dev = crtc_state->base.crtc->dev;
714 intel_clock_t clock;
715 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800716
Akshay Joshi0206e352011-08-16 15:34:10 -0400717 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800718
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
720
Zhao Yakui42158662009-11-20 11:24:18 +0800721 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
722 clock.m1++) {
723 for (clock.m2 = limit->m2.min;
724 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200725 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800726 break;
727 for (clock.n = limit->n.min;
728 clock.n <= limit->n.max; clock.n++) {
729 for (clock.p1 = limit->p1.min;
730 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800731 int this_err;
732
Imre Deakdccbea32015-06-22 23:35:51 +0300733 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000734 if (!intel_PLL_is_valid(dev, limit,
735 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800736 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800737 if (match_clock &&
738 clock.p != match_clock->p)
739 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800740
741 this_err = abs(clock.dot - target);
742 if (this_err < err) {
743 *best_clock = clock;
744 err = this_err;
745 }
746 }
747 }
748 }
749 }
750
751 return (err != target);
752}
753
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200754/*
755 * Returns a set of divisors for the desired target clock with the given
756 * refclk, or FALSE. The returned values represent the clock equation:
757 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
758 *
759 * Target and reference clocks are specified in kHz.
760 *
761 * If match_clock is provided, then best_clock P divider must match the P
762 * divider from @match_clock used for LVDS downclocking.
763 */
Ma Lingd4906092009-03-18 20:13:27 +0800764static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200765pnv_find_best_dpll(const intel_limit_t *limit,
766 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200767 int target, int refclk, intel_clock_t *match_clock,
768 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200769{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300770 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200771 intel_clock_t clock;
772 int err = target;
773
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200774 memset(best_clock, 0, sizeof(*best_clock));
775
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200778 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
779 clock.m1++) {
780 for (clock.m2 = limit->m2.min;
781 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200782 for (clock.n = limit->n.min;
783 clock.n <= limit->n.max; clock.n++) {
784 for (clock.p1 = limit->p1.min;
785 clock.p1 <= limit->p1.max; clock.p1++) {
786 int this_err;
787
Imre Deakdccbea32015-06-22 23:35:51 +0300788 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800789 if (!intel_PLL_is_valid(dev, limit,
790 &clock))
791 continue;
792 if (match_clock &&
793 clock.p != match_clock->p)
794 continue;
795
796 this_err = abs(clock.dot - target);
797 if (this_err < err) {
798 *best_clock = clock;
799 err = this_err;
800 }
801 }
802 }
803 }
804 }
805
806 return (err != target);
807}
808
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200809/*
810 * Returns a set of divisors for the desired target clock with the given
811 * refclk, or FALSE. The returned values represent the clock equation:
812 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200813 *
814 * Target and reference clocks are specified in kHz.
815 *
816 * If match_clock is provided, then best_clock P divider must match the P
817 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200818 */
Ma Lingd4906092009-03-18 20:13:27 +0800819static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200820g4x_find_best_dpll(const intel_limit_t *limit,
821 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200822 int target, int refclk, intel_clock_t *match_clock,
823 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800824{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300825 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800826 intel_clock_t clock;
827 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300828 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400829 /* approximately equals target * 0.00585 */
830 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800831
832 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300833
834 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
835
Ma Lingd4906092009-03-18 20:13:27 +0800836 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200837 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800838 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200839 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800840 for (clock.m1 = limit->m1.max;
841 clock.m1 >= limit->m1.min; clock.m1--) {
842 for (clock.m2 = limit->m2.max;
843 clock.m2 >= limit->m2.min; clock.m2--) {
844 for (clock.p1 = limit->p1.max;
845 clock.p1 >= limit->p1.min; clock.p1--) {
846 int this_err;
847
Imre Deakdccbea32015-06-22 23:35:51 +0300848 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000849 if (!intel_PLL_is_valid(dev, limit,
850 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800851 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000852
853 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800854 if (this_err < err_most) {
855 *best_clock = clock;
856 err_most = this_err;
857 max_n = clock.n;
858 found = true;
859 }
860 }
861 }
862 }
863 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800864 return found;
865}
Ma Lingd4906092009-03-18 20:13:27 +0800866
Imre Deakd5dd62b2015-03-17 11:40:03 +0200867/*
868 * Check if the calculated PLL configuration is more optimal compared to the
869 * best configuration and error found so far. Return the calculated error.
870 */
871static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
872 const intel_clock_t *calculated_clock,
873 const intel_clock_t *best_clock,
874 unsigned int best_error_ppm,
875 unsigned int *error_ppm)
876{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200877 /*
878 * For CHV ignore the error and consider only the P value.
879 * Prefer a bigger P value based on HW requirements.
880 */
881 if (IS_CHERRYVIEW(dev)) {
882 *error_ppm = 0;
883
884 return calculated_clock->p > best_clock->p;
885 }
886
Imre Deak24be4e42015-03-17 11:40:04 +0200887 if (WARN_ON_ONCE(!target_freq))
888 return false;
889
Imre Deakd5dd62b2015-03-17 11:40:03 +0200890 *error_ppm = div_u64(1000000ULL *
891 abs(target_freq - calculated_clock->dot),
892 target_freq);
893 /*
894 * Prefer a better P value over a better (smaller) error if the error
895 * is small. Ensure this preference for future configurations too by
896 * setting the error to 0.
897 */
898 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
899 *error_ppm = 0;
900
901 return true;
902 }
903
904 return *error_ppm + 10 < best_error_ppm;
905}
906
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200907/*
908 * Returns a set of divisors for the desired target clock with the given
909 * refclk, or FALSE. The returned values represent the clock equation:
910 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
911 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800912static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200913vlv_find_best_dpll(const intel_limit_t *limit,
914 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200915 int target, int refclk, intel_clock_t *match_clock,
916 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700917{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200918 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300919 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300920 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300921 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300922 /* min update 19.2 MHz */
923 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300924 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700925
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300926 target *= 5; /* fast clock */
927
928 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929
930 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300931 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300932 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300933 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300934 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300935 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300937 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200938 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300939
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300940 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
941 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300942
Imre Deakdccbea32015-06-22 23:35:51 +0300943 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300944
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300945 if (!intel_PLL_is_valid(dev, limit,
946 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300947 continue;
948
Imre Deakd5dd62b2015-03-17 11:40:03 +0200949 if (!vlv_PLL_is_optimal(dev, target,
950 &clock,
951 best_clock,
952 bestppm, &ppm))
953 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300954
Imre Deakd5dd62b2015-03-17 11:40:03 +0200955 *best_clock = clock;
956 bestppm = ppm;
957 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700958 }
959 }
960 }
961 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700962
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300963 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700964}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700965
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200966/*
967 * Returns a set of divisors for the desired target clock with the given
968 * refclk, or FALSE. The returned values represent the clock equation:
969 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
970 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300971static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200972chv_find_best_dpll(const intel_limit_t *limit,
973 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300974 int target, int refclk, intel_clock_t *match_clock,
975 intel_clock_t *best_clock)
976{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200977 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300978 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200979 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300980 intel_clock_t clock;
981 uint64_t m2;
982 int found = false;
983
984 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200985 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300986
987 /*
988 * Based on hardware doc, the n always set to 1, and m1 always
989 * set to 2. If requires to support 200Mhz refclk, we need to
990 * revisit this because n may not 1 anymore.
991 */
992 clock.n = 1, clock.m1 = 2;
993 target *= 5; /* fast clock */
994
995 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
996 for (clock.p2 = limit->p2.p2_fast;
997 clock.p2 >= limit->p2.p2_slow;
998 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200999 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001000
1001 clock.p = clock.p1 * clock.p2;
1002
1003 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1004 clock.n) << 22, refclk * clock.m1);
1005
1006 if (m2 > INT_MAX/clock.m1)
1007 continue;
1008
1009 clock.m2 = m2;
1010
Imre Deakdccbea32015-06-22 23:35:51 +03001011 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 if (!intel_PLL_is_valid(dev, limit, &clock))
1014 continue;
1015
Imre Deak9ca3ba02015-03-17 11:40:05 +02001016 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1017 best_error_ppm, &error_ppm))
1018 continue;
1019
1020 *best_clock = clock;
1021 best_error_ppm = error_ppm;
1022 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001023 }
1024 }
1025
1026 return found;
1027}
1028
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001029bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1030 intel_clock_t *best_clock)
1031{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001032 int refclk = 100000;
1033 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001034
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001035 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001036 target_clock, refclk, NULL, best_clock);
1037}
1038
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001039bool intel_crtc_active(struct drm_crtc *crtc)
1040{
1041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1042
1043 /* Be paranoid as we can arrive here with only partial
1044 * state retrieved from the hardware during setup.
1045 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001046 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001047 * as Haswell has gained clock readout/fastboot support.
1048 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001049 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001050 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001051 *
1052 * FIXME: The intel_crtc->active here should be switched to
1053 * crtc->state->active once we have proper CRTC states wired up
1054 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001055 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001056 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001057 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001058}
1059
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001060enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1061 enum pipe pipe)
1062{
1063 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1065
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001066 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001067}
1068
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001069static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1070{
1071 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001072 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001073 u32 line1, line2;
1074 u32 line_mask;
1075
1076 if (IS_GEN2(dev))
1077 line_mask = DSL_LINEMASK_GEN2;
1078 else
1079 line_mask = DSL_LINEMASK_GEN3;
1080
1081 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001082 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001083 line2 = I915_READ(reg) & line_mask;
1084
1085 return line1 == line2;
1086}
1087
Keith Packardab7ad7f2010-10-03 00:33:06 -07001088/*
1089 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001090 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001091 *
1092 * After disabling a pipe, we can't wait for vblank in the usual way,
1093 * spinning on the vblank interrupt status bit, since we won't actually
1094 * see an interrupt when the pipe is disabled.
1095 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001096 * On Gen4 and above:
1097 * wait for the pipe register state bit to turn off
1098 *
1099 * Otherwise:
1100 * wait for the display line value to settle (it usually
1101 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001102 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001103 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001104static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001105{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001106 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001107 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001108 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001109 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001110
Keith Packardab7ad7f2010-10-03 00:33:06 -07001111 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001112 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001113
Keith Packardab7ad7f2010-10-03 00:33:06 -07001114 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001115 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1116 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001117 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001118 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001119 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001120 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001121 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001123}
1124
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001126void assert_pll(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129 u32 val;
1130 bool cur_state;
1131
Ville Syrjälä649636e2015-09-22 19:50:01 +03001132 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001134 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001136 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138
Jani Nikula23538ef2013-08-27 15:12:22 +03001139/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001140void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001141{
1142 u32 val;
1143 bool cur_state;
1144
Ville Syrjäläa5805162015-05-26 20:42:30 +03001145 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001146 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001147 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001148
1149 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001150 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001151 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001152 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001153}
Jani Nikula23538ef2013-08-27 15:12:22 +03001154
Jesse Barnes040484a2011-01-03 12:14:26 -08001155static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1156 enum pipe pipe, bool state)
1157{
Jesse Barnes040484a2011-01-03 12:14:26 -08001158 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001159 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1160 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001161
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001162 if (HAS_DDI(dev_priv->dev)) {
1163 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001164 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001165 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001166 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001167 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001168 cur_state = !!(val & FDI_TX_ENABLE);
1169 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001170 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001172 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001173}
1174#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1175#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1176
1177static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
1179{
Jesse Barnes040484a2011-01-03 12:14:26 -08001180 u32 val;
1181 bool cur_state;
1182
Ville Syrjälä649636e2015-09-22 19:50:01 +03001183 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001184 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001186 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001187 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001188}
1189#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1190#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1191
1192static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1193 enum pipe pipe)
1194{
Jesse Barnes040484a2011-01-03 12:14:26 -08001195 u32 val;
1196
1197 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001198 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001199 return;
1200
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001201 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001202 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001203 return;
1204
Ville Syrjälä649636e2015-09-22 19:50:01 +03001205 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001206 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 -08001207}
1208
Daniel Vetter55607e82013-06-16 21:42:39 +02001209void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1210 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001211{
Jesse Barnes040484a2011-01-03 12:14:26 -08001212 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001213 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001214
Ville Syrjälä649636e2015-09-22 19:50:01 +03001215 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001216 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001217 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001218 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001219 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001220}
1221
Daniel Vetterb680c372014-09-19 18:27:27 +02001222void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1223 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001225 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001226 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 u32 val;
1228 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001229 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230
Jani Nikulabedd4db2014-08-22 15:04:13 +03001231 if (WARN_ON(HAS_DDI(dev)))
1232 return;
1233
1234 if (HAS_PCH_SPLIT(dev)) {
1235 u32 port_sel;
1236
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001238 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1239
1240 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1241 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1242 panel_pipe = PIPE_B;
1243 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001244 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001245 /* presumably write lock depends on pipe, not port select */
1246 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1247 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248 } else {
1249 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001250 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1251 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001252 }
1253
1254 val = I915_READ(pp_reg);
1255 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001256 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001257 locked = false;
1258
Rob Clarke2c719b2014-12-15 13:56:32 -05001259 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001261 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001262}
1263
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001264static void assert_cursor(struct drm_i915_private *dev_priv,
1265 enum pipe pipe, bool state)
1266{
1267 struct drm_device *dev = dev_priv->dev;
1268 bool cur_state;
1269
Paulo Zanonid9d82082014-02-27 16:30:56 -03001270 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001271 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001272 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001273 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001274
Rob Clarke2c719b2014-12-15 13:56:32 -05001275 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001276 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001277 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001278}
1279#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1280#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1281
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001282void assert_pipe(struct drm_i915_private *dev_priv,
1283 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001284{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001285 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001286 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1287 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001288 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001289
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001290 /* if we need the pipe quirk it must be always on */
1291 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1292 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001293 state = true;
1294
Imre Deak4feed0e2016-02-12 18:55:14 +02001295 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1296 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001297 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001298 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001299
1300 intel_display_power_put(dev_priv, power_domain);
1301 } else {
1302 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001303 }
1304
Rob Clarke2c719b2014-12-15 13:56:32 -05001305 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001306 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001307 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308}
1309
Chris Wilson931872f2012-01-16 23:01:13 +00001310static void assert_plane(struct drm_i915_private *dev_priv,
1311 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001312{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001314 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001315
Ville Syrjälä649636e2015-09-22 19:50:01 +03001316 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001317 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001318 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001319 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001320 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321}
1322
Chris Wilson931872f2012-01-16 23:01:13 +00001323#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1324#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1325
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1327 enum pipe pipe)
1328{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001329 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001330 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001331
Ville Syrjälä653e1022013-06-04 13:49:05 +03001332 /* Primary planes are fixed to pipes on gen4+ */
1333 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001334 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001336 "plane %c assertion failure, should be disabled but not\n",
1337 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001338 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001339 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001340
Jesse Barnesb24e7172011-01-04 15:09:30 -08001341 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001342 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001343 u32 val = I915_READ(DSPCNTR(i));
1344 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001347 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349 }
1350}
1351
Jesse Barnes19332d72013-03-28 09:55:38 -07001352static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001355 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001356 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001357
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001358 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001359 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001360 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001361 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001362 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1363 sprite, pipe_name(pipe));
1364 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001365 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001366 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001367 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001368 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001369 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001370 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001371 }
1372 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001373 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001374 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001375 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001376 plane_name(pipe), pipe_name(pipe));
1377 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001378 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001380 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1381 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001382 }
1383}
1384
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001385static void assert_vblank_disabled(struct drm_crtc *crtc)
1386{
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001388 drm_crtc_vblank_put(crtc);
1389}
1390
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001391void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001393{
Jesse Barnes92f25842011-01-04 15:09:34 -08001394 u32 val;
1395 bool enabled;
1396
Ville Syrjälä649636e2015-09-22 19:50:01 +03001397 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001398 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001399 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001400 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1401 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001402}
1403
Keith Packard4e634382011-08-06 10:39:45 -07001404static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001406{
1407 if ((val & DP_PORT_EN) == 0)
1408 return false;
1409
1410 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001411 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001412 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1413 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001414 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1415 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1416 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001417 } else {
1418 if ((val & DP_PIPE_MASK) != (pipe << 30))
1419 return false;
1420 }
1421 return true;
1422}
1423
Keith Packard1519b992011-08-06 10:35:34 -07001424static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe, u32 val)
1426{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001427 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001428 return false;
1429
1430 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001431 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001432 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001433 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1434 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1435 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001436 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001437 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001438 return false;
1439 }
1440 return true;
1441}
1442
1443static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1444 enum pipe pipe, u32 val)
1445{
1446 if ((val & LVDS_PORT_EN) == 0)
1447 return false;
1448
1449 if (HAS_PCH_CPT(dev_priv->dev)) {
1450 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1451 return false;
1452 } else {
1453 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1454 return false;
1455 }
1456 return true;
1457}
1458
1459static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1460 enum pipe pipe, u32 val)
1461{
1462 if ((val & ADPA_DAC_ENABLE) == 0)
1463 return false;
1464 if (HAS_PCH_CPT(dev_priv->dev)) {
1465 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1466 return false;
1467 } else {
1468 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1469 return false;
1470 }
1471 return true;
1472}
1473
Jesse Barnes291906f2011-02-02 12:28:03 -08001474static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001475 enum pipe pipe, i915_reg_t reg,
1476 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001477{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001478 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001479 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001480 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001481 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001482
Rob Clarke2c719b2014-12-15 13:56:32 -05001483 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001484 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001485 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001486}
1487
1488static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001489 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001490{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001491 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001492 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001493 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001494 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001495
Rob Clarke2c719b2014-12-15 13:56:32 -05001496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001497 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001498 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001499}
1500
1501static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1502 enum pipe pipe)
1503{
Jesse Barnes291906f2011-02-02 12:28:03 -08001504 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001505
Keith Packardf0575e92011-07-25 22:12:43 -07001506 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1507 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1508 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001509
Ville Syrjälä649636e2015-09-22 19:50:01 +03001510 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001511 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001512 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001513 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001514
Ville Syrjälä649636e2015-09-22 19:50:01 +03001515 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001516 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001517 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001518 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001519
Paulo Zanonie2debe92013-02-18 19:00:27 -03001520 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1521 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1522 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001523}
1524
Ville Syrjäläd288f652014-10-28 13:20:22 +02001525static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001526 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001527{
Daniel Vetter426115c2013-07-11 22:13:42 +02001528 struct drm_device *dev = crtc->base.dev;
1529 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001530 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001531 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001532
Daniel Vetter426115c2013-07-11 22:13:42 +02001533 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001534
Daniel Vetter87442f72013-06-06 00:52:17 +02001535 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001536 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001537 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001538
Daniel Vetter426115c2013-07-11 22:13:42 +02001539 I915_WRITE(reg, dpll);
1540 POSTING_READ(reg);
1541 udelay(150);
1542
1543 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1544 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1545
Ville Syrjäläd288f652014-10-28 13:20:22 +02001546 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001547 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001548
1549 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001550 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001551 POSTING_READ(reg);
1552 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001553 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001554 POSTING_READ(reg);
1555 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001556 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001557 POSTING_READ(reg);
1558 udelay(150); /* wait for warmup */
1559}
1560
Ville Syrjäläd288f652014-10-28 13:20:22 +02001561static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001562 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001563{
1564 struct drm_device *dev = crtc->base.dev;
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566 int pipe = crtc->pipe;
1567 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001568 u32 tmp;
1569
1570 assert_pipe_disabled(dev_priv, crtc->pipe);
1571
Ville Syrjäläa5805162015-05-26 20:42:30 +03001572 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
Ville Syrjälä54433e92015-05-26 20:42:31 +03001579 mutex_unlock(&dev_priv->sb_lock);
1580
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001588
1589 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1592
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001593 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001595 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596}
1597
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
1603 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001604 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001605 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001606
1607 return count;
1608}
1609
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001610static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001611{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001612 struct drm_device *dev = crtc->base.dev;
1613 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001614 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001615 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001616
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001617 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001618
1619 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001620 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001621
1622 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001623 if (IS_MOBILE(dev) && !IS_I830(dev))
1624 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001625
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001626 /* Enable DVO 2x clock on both PLLs if necessary */
1627 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1628 /*
1629 * It appears to be important that we don't enable this
1630 * for the current pipe before otherwise configuring the
1631 * PLL. No idea how this should be handled if multiple
1632 * DVO outputs are enabled simultaneosly.
1633 */
1634 dpll |= DPLL_DVO_2X_MODE;
1635 I915_WRITE(DPLL(!crtc->pipe),
1636 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1637 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001638
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001639 /*
1640 * Apparently we need to have VGA mode enabled prior to changing
1641 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1642 * dividers, even though the register value does change.
1643 */
1644 I915_WRITE(reg, 0);
1645
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001646 I915_WRITE(reg, dpll);
1647
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001648 /* Wait for the clocks to stabilize. */
1649 POSTING_READ(reg);
1650 udelay(150);
1651
1652 if (INTEL_INFO(dev)->gen >= 4) {
1653 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001654 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655 } else {
1656 /* The pixel multiplier can only be updated once the
1657 * DPLL is enabled and the clocks are stable.
1658 *
1659 * So write it again.
1660 */
1661 I915_WRITE(reg, dpll);
1662 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001663
1664 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001665 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001668 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001669 POSTING_READ(reg);
1670 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001671 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001672 POSTING_READ(reg);
1673 udelay(150); /* wait for warmup */
1674}
1675
1676/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001677 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678 * @dev_priv: i915 private structure
1679 * @pipe: pipe PLL to disable
1680 *
1681 * Disable the PLL for @pipe, making sure the pipe is off first.
1682 *
1683 * Note! This is for pre-ILK only.
1684 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 struct drm_device *dev = crtc->base.dev;
1688 struct drm_i915_private *dev_priv = dev->dev_private;
1689 enum pipe pipe = crtc->pipe;
1690
1691 /* Disable DVO 2x clock on both PLLs if necessary */
1692 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001693 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001694 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001695 I915_WRITE(DPLL(PIPE_B),
1696 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1697 I915_WRITE(DPLL(PIPE_A),
1698 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1699 }
1700
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001701 /* Don't disable pipe or pipe PLLs if needed */
1702 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1703 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704 return;
1705
1706 /* Make sure the pipe isn't still relying on us */
1707 assert_pipe_disabled(dev_priv, pipe);
1708
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001709 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001710 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711}
1712
Jesse Barnesf6071162013-10-01 10:41:38 -07001713static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1714{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001715 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001716
1717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
1719
Imre Deake5cbfbf2014-01-09 17:08:16 +02001720 /*
1721 * Leave integrated clock source and reference clock enabled for pipe B.
1722 * The latter is needed for VGA hotplug / manual detection.
1723 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001724 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001725 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001726 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001727 I915_WRITE(DPLL(pipe), val);
1728 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001729
1730}
1731
1732static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1733{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001734 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001735 u32 val;
1736
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001737 /* Make sure the pipe isn't still relying on us */
1738 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001739
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001740 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001741 val = DPLL_SSC_REF_CLK_CHV |
1742 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001743 if (pipe != PIPE_A)
1744 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1745 I915_WRITE(DPLL(pipe), val);
1746 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001747
Ville Syrjäläa5805162015-05-26 20:42:30 +03001748 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001749
1750 /* Disable 10bit clock to display controller */
1751 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1752 val &= ~DPIO_DCLKP_EN;
1753 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1754
Ville Syrjäläa5805162015-05-26 20:42:30 +03001755 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001756}
1757
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001758void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001759 struct intel_digital_port *dport,
1760 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761{
1762 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001763 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001764
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001765 switch (dport->port) {
1766 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001767 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001768 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001769 break;
1770 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001771 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001772 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001773 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001774 break;
1775 case PORT_D:
1776 port_mask = DPLL_PORTD_READY_MASK;
1777 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001778 break;
1779 default:
1780 BUG();
1781 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001782
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001783 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1784 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1785 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001786}
1787
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001788static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1789 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001790{
Daniel Vetter23670b322012-11-01 09:15:30 +01001791 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001792 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001794 i915_reg_t reg;
1795 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001796
1797 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001798 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001799
1800 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001801 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001802
1803 /* FDI must be feeding us bits for PCH ports */
1804 assert_fdi_tx_enabled(dev_priv, pipe);
1805 assert_fdi_rx_enabled(dev_priv, pipe);
1806
Daniel Vetter23670b322012-11-01 09:15:30 +01001807 if (HAS_PCH_CPT(dev)) {
1808 /* Workaround: Set the timing override bit before enabling the
1809 * pch transcoder. */
1810 reg = TRANS_CHICKEN2(pipe);
1811 val = I915_READ(reg);
1812 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1813 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001814 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001815
Daniel Vetterab9412b2013-05-03 11:49:46 +02001816 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001817 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001818 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001819
1820 if (HAS_PCH_IBX(dev_priv->dev)) {
1821 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001822 * Make the BPC in transcoder be consistent with
1823 * that in pipeconf reg. For HDMI we must use 8bpc
1824 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001825 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001826 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001827 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1828 val |= PIPECONF_8BPC;
1829 else
1830 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001831 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001832
1833 val &= ~TRANS_INTERLACE_MASK;
1834 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001835 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001836 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001837 val |= TRANS_LEGACY_INTERLACED_ILK;
1838 else
1839 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001840 else
1841 val |= TRANS_PROGRESSIVE;
1842
Jesse Barnes040484a2011-01-03 12:14:26 -08001843 I915_WRITE(reg, val | TRANS_ENABLE);
1844 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001845 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001846}
1847
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001849 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001850{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001851 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001852
1853 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001854 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001855
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001856 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001857 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001858 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001859
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001860 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001861 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001862 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001863 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001864
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001865 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001866 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001867
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001868 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1869 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001870 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001871 else
1872 val |= TRANS_PROGRESSIVE;
1873
Daniel Vetterab9412b2013-05-03 11:49:46 +02001874 I915_WRITE(LPT_TRANSCONF, val);
1875 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001876 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877}
1878
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001879static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1880 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001881{
Daniel Vetter23670b322012-11-01 09:15:30 +01001882 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001883 i915_reg_t reg;
1884 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001885
1886 /* FDI relies on the transcoder */
1887 assert_fdi_tx_disabled(dev_priv, pipe);
1888 assert_fdi_rx_disabled(dev_priv, pipe);
1889
Jesse Barnes291906f2011-02-02 12:28:03 -08001890 /* Ports must be off as well */
1891 assert_pch_ports_disabled(dev_priv, pipe);
1892
Daniel Vetterab9412b2013-05-03 11:49:46 +02001893 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001894 val = I915_READ(reg);
1895 val &= ~TRANS_ENABLE;
1896 I915_WRITE(reg, val);
1897 /* wait for PCH transcoder off, transcoder state */
1898 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001900
Ville Syrjäläc4656132015-10-29 21:25:56 +02001901 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001908}
1909
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001910static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001912 u32 val;
1913
Daniel Vetterab9412b2013-05-03 11:49:46 +02001914 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001916 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001917 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001918 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001919 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001920
1921 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001922 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001923 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001924 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001925}
1926
1927/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001928 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001929 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001930 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001931 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001933 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001934static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001935{
Paulo Zanoni03722642014-01-17 13:51:09 -02001936 struct drm_device *dev = crtc->base.dev;
1937 struct drm_i915_private *dev_priv = dev->dev_private;
1938 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001939 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001940 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001941 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001942 u32 val;
1943
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001944 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1945
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001946 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001947 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001948 assert_sprites_disabled(dev_priv, pipe);
1949
Paulo Zanoni681e5812012-12-06 11:12:38 -02001950 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001951 pch_transcoder = TRANSCODER_A;
1952 else
1953 pch_transcoder = pipe;
1954
Jesse Barnesb24e7172011-01-04 15:09:30 -08001955 /*
1956 * A pipe without a PLL won't actually be able to drive bits from
1957 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1958 * need the check.
1959 */
Imre Deak50360402015-01-16 00:55:16 -08001960 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001961 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001962 assert_dsi_pll_enabled(dev_priv);
1963 else
1964 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001965 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001966 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001967 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001968 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001969 assert_fdi_tx_pll_enabled(dev_priv,
1970 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001971 }
1972 /* FIXME: assert CPU port conditions for SNB+ */
1973 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001974
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001975 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001977 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001978 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1979 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001980 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001981 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001982
1983 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001984 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001985
1986 /*
1987 * Until the pipe starts DSL will read as 0, which would cause
1988 * an apparent vblank timestamp jump, which messes up also the
1989 * frame count when it's derived from the timestamps. So let's
1990 * wait for the pipe to start properly before we call
1991 * drm_crtc_vblank_on()
1992 */
1993 if (dev->max_vblank_count == 0 &&
1994 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1995 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001996}
1997
1998/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001999 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002000 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002002 * Disable the pipe of @crtc, making sure that various hardware
2003 * specific requirements are met, if applicable, e.g. plane
2004 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002005 *
2006 * Will wait until the pipe has shut down before returning.
2007 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002010 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002011 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002012 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002013 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002014 u32 val;
2015
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002016 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2017
Jesse Barnesb24e7172011-01-04 15:09:30 -08002018 /*
2019 * Make sure planes won't keep trying to pump pixels to us,
2020 * or we might hang the display.
2021 */
2022 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002023 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002024 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002025
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002026 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002028 if ((val & PIPECONF_ENABLE) == 0)
2029 return;
2030
Ville Syrjälä67adc642014-08-15 01:21:57 +03002031 /*
2032 * Double wide has implications for planes
2033 * so best keep it disabled when not needed.
2034 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002035 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002036 val &= ~PIPECONF_DOUBLE_WIDE;
2037
2038 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002039 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2040 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002041 val &= ~PIPECONF_ENABLE;
2042
2043 I915_WRITE(reg, val);
2044 if ((val & PIPECONF_ENABLE) == 0)
2045 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046}
2047
Chris Wilson693db182013-03-05 14:52:39 +00002048static bool need_vtd_wa(struct drm_device *dev)
2049{
2050#ifdef CONFIG_INTEL_IOMMU
2051 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2052 return true;
2053#endif
2054 return false;
2055}
2056
Ville Syrjälä832be822016-01-12 21:08:33 +02002057static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2058{
2059 return IS_GEN2(dev_priv) ? 2048 : 4096;
2060}
2061
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002062static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2063 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002064{
2065 switch (fb_modifier) {
2066 case DRM_FORMAT_MOD_NONE:
2067 return cpp;
2068 case I915_FORMAT_MOD_X_TILED:
2069 if (IS_GEN2(dev_priv))
2070 return 128;
2071 else
2072 return 512;
2073 case I915_FORMAT_MOD_Y_TILED:
2074 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2075 return 128;
2076 else
2077 return 512;
2078 case I915_FORMAT_MOD_Yf_TILED:
2079 switch (cpp) {
2080 case 1:
2081 return 64;
2082 case 2:
2083 case 4:
2084 return 128;
2085 case 8:
2086 case 16:
2087 return 256;
2088 default:
2089 MISSING_CASE(cpp);
2090 return cpp;
2091 }
2092 break;
2093 default:
2094 MISSING_CASE(fb_modifier);
2095 return cpp;
2096 }
2097}
2098
Ville Syrjälä832be822016-01-12 21:08:33 +02002099unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2100 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002101{
Ville Syrjälä832be822016-01-12 21:08:33 +02002102 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2103 return 1;
2104 else
2105 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002106 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002107}
2108
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002109/* Return the tile dimensions in pixel units */
2110static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2111 unsigned int *tile_width,
2112 unsigned int *tile_height,
2113 uint64_t fb_modifier,
2114 unsigned int cpp)
2115{
2116 unsigned int tile_width_bytes =
2117 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2118
2119 *tile_width = tile_width_bytes / cpp;
2120 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2121}
2122
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002123unsigned int
2124intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002125 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002126{
Ville Syrjälä832be822016-01-12 21:08:33 +02002127 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2128 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2129
2130 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002131}
2132
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002133unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2134{
2135 unsigned int size = 0;
2136 int i;
2137
2138 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2139 size += rot_info->plane[i].width * rot_info->plane[i].height;
2140
2141 return size;
2142}
2143
Daniel Vetter75c82a52015-10-14 16:51:04 +02002144static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002145intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2146 const struct drm_framebuffer *fb,
2147 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002148{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002149 if (intel_rotation_90_or_270(rotation)) {
2150 *view = i915_ggtt_view_rotated;
2151 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2152 } else {
2153 *view = i915_ggtt_view_normal;
2154 }
2155}
2156
2157static void
2158intel_fill_fb_info(struct drm_i915_private *dev_priv,
2159 struct drm_framebuffer *fb)
2160{
2161 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002162 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002163
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002164 tile_size = intel_tile_size(dev_priv);
2165
2166 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002167 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2168 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002169
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002170 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2171 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002172
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002173 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002174 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002175 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2176 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002177
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002178 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002179 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2180 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002181 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002182}
2183
Ville Syrjälä603525d2016-01-12 21:08:37 +02002184static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002185{
2186 if (INTEL_INFO(dev_priv)->gen >= 9)
2187 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002188 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002189 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002190 return 128 * 1024;
2191 else if (INTEL_INFO(dev_priv)->gen >= 4)
2192 return 4 * 1024;
2193 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002194 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002195}
2196
Ville Syrjälä603525d2016-01-12 21:08:37 +02002197static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2198 uint64_t fb_modifier)
2199{
2200 switch (fb_modifier) {
2201 case DRM_FORMAT_MOD_NONE:
2202 return intel_linear_alignment(dev_priv);
2203 case I915_FORMAT_MOD_X_TILED:
2204 if (INTEL_INFO(dev_priv)->gen >= 9)
2205 return 256 * 1024;
2206 return 0;
2207 case I915_FORMAT_MOD_Y_TILED:
2208 case I915_FORMAT_MOD_Yf_TILED:
2209 return 1 * 1024 * 1024;
2210 default:
2211 MISSING_CASE(fb_modifier);
2212 return 0;
2213 }
2214}
2215
Chris Wilson127bd2a2010-07-23 23:32:05 +01002216int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002217intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2218 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002219{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002220 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002221 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002222 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002223 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002224 u32 alignment;
2225 int ret;
2226
Matt Roperebcdd392014-07-09 16:22:11 -07002227 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2228
Ville Syrjälä603525d2016-01-12 21:08:37 +02002229 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002230
Ville Syrjälä3465c582016-02-15 22:54:43 +02002231 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002232
Chris Wilson693db182013-03-05 14:52:39 +00002233 /* Note that the w/a also requires 64 PTE of padding following the
2234 * bo. We currently fill all unused PTE with the shadow page and so
2235 * we should always have valid PTE following the scanout preventing
2236 * the VT-d warning.
2237 */
2238 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2239 alignment = 256 * 1024;
2240
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002241 /*
2242 * Global gtt pte registers are special registers which actually forward
2243 * writes to a chunk of system memory. Which means that there is no risk
2244 * that the register values disappear as soon as we call
2245 * intel_runtime_pm_put(), so it is correct to wrap only the
2246 * pin/unpin/fence and not more.
2247 */
2248 intel_runtime_pm_get(dev_priv);
2249
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002250 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2251 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002252 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002253 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002254
2255 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2256 * fence, whereas 965+ only requires a fence if using
2257 * framebuffer compression. For simplicity, we always install
2258 * a fence as the cost is not that onerous.
2259 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002260 if (view.type == I915_GGTT_VIEW_NORMAL) {
2261 ret = i915_gem_object_get_fence(obj);
2262 if (ret == -EDEADLK) {
2263 /*
2264 * -EDEADLK means there are no free fences
2265 * no pending flips.
2266 *
2267 * This is propagated to atomic, but it uses
2268 * -EDEADLK to force a locking recovery, so
2269 * change the returned error to -EBUSY.
2270 */
2271 ret = -EBUSY;
2272 goto err_unpin;
2273 } else if (ret)
2274 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002275
Vivek Kasireddy98072162015-10-29 18:54:38 -07002276 i915_gem_object_pin_fence(obj);
2277 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002278
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002279 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002280 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002281
2282err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002283 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002284err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002285 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002286 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002287}
2288
Ville Syrjälä3465c582016-02-15 22:54:43 +02002289static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002290{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002291 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002292 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002293
Matt Roperebcdd392014-07-09 16:22:11 -07002294 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2295
Ville Syrjälä3465c582016-02-15 22:54:43 +02002296 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002297
Vivek Kasireddy98072162015-10-29 18:54:38 -07002298 if (view.type == I915_GGTT_VIEW_NORMAL)
2299 i915_gem_object_unpin_fence(obj);
2300
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002301 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002302}
2303
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002304/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002305 * Adjust the tile offset by moving the difference into
2306 * the x/y offsets.
2307 *
2308 * Input tile dimensions and pitch must already be
2309 * rotated to match x and y, and in pixel units.
2310 */
2311static u32 intel_adjust_tile_offset(int *x, int *y,
2312 unsigned int tile_width,
2313 unsigned int tile_height,
2314 unsigned int tile_size,
2315 unsigned int pitch_tiles,
2316 u32 old_offset,
2317 u32 new_offset)
2318{
2319 unsigned int tiles;
2320
2321 WARN_ON(old_offset & (tile_size - 1));
2322 WARN_ON(new_offset & (tile_size - 1));
2323 WARN_ON(new_offset > old_offset);
2324
2325 tiles = (old_offset - new_offset) / tile_size;
2326
2327 *y += tiles / pitch_tiles * tile_height;
2328 *x += tiles % pitch_tiles * tile_width;
2329
2330 return new_offset;
2331}
2332
2333/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002334 * Computes the linear offset to the base tile and adjusts
2335 * x, y. bytes per pixel is assumed to be a power-of-two.
2336 *
2337 * In the 90/270 rotated case, x and y are assumed
2338 * to be already rotated to match the rotated GTT view, and
2339 * pitch is the tile_height aligned framebuffer height.
2340 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002341u32 intel_compute_tile_offset(int *x, int *y,
2342 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002343 unsigned int pitch,
2344 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002345{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002346 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2347 uint64_t fb_modifier = fb->modifier[plane];
2348 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002349 u32 offset, offset_aligned, alignment;
2350
2351 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2352 if (alignment)
2353 alignment--;
2354
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002355 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002356 unsigned int tile_size, tile_width, tile_height;
2357 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002358
Ville Syrjäläd8433102016-01-12 21:08:35 +02002359 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002360 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2361 fb_modifier, cpp);
2362
2363 if (intel_rotation_90_or_270(rotation)) {
2364 pitch_tiles = pitch / tile_height;
2365 swap(tile_width, tile_height);
2366 } else {
2367 pitch_tiles = pitch / (tile_width * cpp);
2368 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002369
Ville Syrjäläd8433102016-01-12 21:08:35 +02002370 tile_rows = *y / tile_height;
2371 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002372
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002373 tiles = *x / tile_width;
2374 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002375
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002376 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2377 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002378
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002379 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2380 tile_size, pitch_tiles,
2381 offset, offset_aligned);
2382 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002383 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002384 offset_aligned = offset & ~alignment;
2385
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002386 *y = (offset & alignment) / pitch;
2387 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002388 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002389
2390 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002391}
2392
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002393static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002394{
2395 switch (format) {
2396 case DISPPLANE_8BPP:
2397 return DRM_FORMAT_C8;
2398 case DISPPLANE_BGRX555:
2399 return DRM_FORMAT_XRGB1555;
2400 case DISPPLANE_BGRX565:
2401 return DRM_FORMAT_RGB565;
2402 default:
2403 case DISPPLANE_BGRX888:
2404 return DRM_FORMAT_XRGB8888;
2405 case DISPPLANE_RGBX888:
2406 return DRM_FORMAT_XBGR8888;
2407 case DISPPLANE_BGRX101010:
2408 return DRM_FORMAT_XRGB2101010;
2409 case DISPPLANE_RGBX101010:
2410 return DRM_FORMAT_XBGR2101010;
2411 }
2412}
2413
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002414static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2415{
2416 switch (format) {
2417 case PLANE_CTL_FORMAT_RGB_565:
2418 return DRM_FORMAT_RGB565;
2419 default:
2420 case PLANE_CTL_FORMAT_XRGB_8888:
2421 if (rgb_order) {
2422 if (alpha)
2423 return DRM_FORMAT_ABGR8888;
2424 else
2425 return DRM_FORMAT_XBGR8888;
2426 } else {
2427 if (alpha)
2428 return DRM_FORMAT_ARGB8888;
2429 else
2430 return DRM_FORMAT_XRGB8888;
2431 }
2432 case PLANE_CTL_FORMAT_XRGB_2101010:
2433 if (rgb_order)
2434 return DRM_FORMAT_XBGR2101010;
2435 else
2436 return DRM_FORMAT_XRGB2101010;
2437 }
2438}
2439
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002440static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002441intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2442 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002443{
2444 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002445 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002446 struct drm_i915_gem_object *obj = NULL;
2447 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002448 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002449 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2450 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2451 PAGE_SIZE);
2452
2453 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002454
Chris Wilsonff2652e2014-03-10 08:07:02 +00002455 if (plane_config->size == 0)
2456 return false;
2457
Paulo Zanoni3badb492015-09-23 12:52:23 -03002458 /* If the FB is too big, just don't use it since fbdev is not very
2459 * important and we should probably use that space with FBC or other
2460 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002461 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002462 return false;
2463
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002464 mutex_lock(&dev->struct_mutex);
2465
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002466 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2467 base_aligned,
2468 base_aligned,
2469 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002470 if (!obj) {
2471 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002472 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002473 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002474
Damien Lespiau49af4492015-01-20 12:51:44 +00002475 obj->tiling_mode = plane_config->tiling;
2476 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002477 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 mode_cmd.pixel_format = fb->pixel_format;
2480 mode_cmd.width = fb->width;
2481 mode_cmd.height = fb->height;
2482 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002483 mode_cmd.modifier[0] = fb->modifier[0];
2484 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002486 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002487 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002488 DRM_DEBUG_KMS("intel fb init failed\n");
2489 goto out_unref_obj;
2490 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002491
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493
Daniel Vetterf6936e22015-03-26 12:17:05 +01002494 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002495 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002496
2497out_unref_obj:
2498 drm_gem_object_unreference(&obj->base);
2499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500 return false;
2501}
2502
Matt Roperafd65eb2015-02-03 13:10:04 -08002503/* Update plane->state->fb to match plane->fb after driver-internal updates */
2504static void
2505update_state_fb(struct drm_plane *plane)
2506{
2507 if (plane->fb == plane->state->fb)
2508 return;
2509
2510 if (plane->state->fb)
2511 drm_framebuffer_unreference(plane->state->fb);
2512 plane->state->fb = plane->fb;
2513 if (plane->state->fb)
2514 drm_framebuffer_reference(plane->state->fb);
2515}
2516
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002517static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002518intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2519 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002520{
2521 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002522 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523 struct drm_crtc *c;
2524 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002525 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002526 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002527 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002528 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2529 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002530 struct intel_plane_state *intel_state =
2531 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002532 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002533
Damien Lespiau2d140302015-02-05 17:22:18 +00002534 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002535 return;
2536
Daniel Vetterf6936e22015-03-26 12:17:05 +01002537 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002538 fb = &plane_config->fb->base;
2539 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002540 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002541
Damien Lespiau2d140302015-02-05 17:22:18 +00002542 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002543
2544 /*
2545 * Failed to alloc the obj, check to see if we should share
2546 * an fb with another CRTC instead
2547 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002548 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002549 i = to_intel_crtc(c);
2550
2551 if (c == &intel_crtc->base)
2552 continue;
2553
Matt Roper2ff8fde2014-07-08 07:50:07 -07002554 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555 continue;
2556
Daniel Vetter88595ac2015-03-26 12:42:24 +01002557 fb = c->primary->fb;
2558 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002559 continue;
2560
Daniel Vetter88595ac2015-03-26 12:42:24 +01002561 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002562 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002563 drm_framebuffer_reference(fb);
2564 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565 }
2566 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002567
Matt Roper200757f2015-12-03 11:37:36 -08002568 /*
2569 * We've failed to reconstruct the BIOS FB. Current display state
2570 * indicates that the primary plane is visible, but has a NULL FB,
2571 * which will lead to problems later if we don't fix it up. The
2572 * simplest solution is to just disable the primary plane now and
2573 * pretend the BIOS never had it enabled.
2574 */
2575 to_intel_plane_state(plane_state)->visible = false;
2576 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002577 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002578 intel_plane->disable_plane(primary, &intel_crtc->base);
2579
Daniel Vetter88595ac2015-03-26 12:42:24 +01002580 return;
2581
2582valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002583 plane_state->src_x = 0;
2584 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002585 plane_state->src_w = fb->width << 16;
2586 plane_state->src_h = fb->height << 16;
2587
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002588 plane_state->crtc_x = 0;
2589 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002590 plane_state->crtc_w = fb->width;
2591 plane_state->crtc_h = fb->height;
2592
Matt Roper0a8d8a82015-12-03 11:37:38 -08002593 intel_state->src.x1 = plane_state->src_x;
2594 intel_state->src.y1 = plane_state->src_y;
2595 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2596 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2597 intel_state->dst.x1 = plane_state->crtc_x;
2598 intel_state->dst.y1 = plane_state->crtc_y;
2599 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2600 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2601
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 obj = intel_fb_obj(fb);
2603 if (obj->tiling_mode != I915_TILING_NONE)
2604 dev_priv->preserve_bios_swizzle = true;
2605
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002606 drm_framebuffer_reference(fb);
2607 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002608 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002609 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002610 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002611}
2612
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002613static void i9xx_update_primary_plane(struct drm_plane *primary,
2614 const struct intel_crtc_state *crtc_state,
2615 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002616{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002617 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002618 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2620 struct drm_framebuffer *fb = plane_state->base.fb;
2621 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002622 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002623 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002624 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002625 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002626 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002627 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002628 int x = plane_state->src.x1 >> 16;
2629 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002630
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002631 dspcntr = DISPPLANE_GAMMA_ENABLE;
2632
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002633 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002634
2635 if (INTEL_INFO(dev)->gen < 4) {
2636 if (intel_crtc->pipe == PIPE_B)
2637 dspcntr |= DISPPLANE_SEL_PIPE_B;
2638
2639 /* pipesrc and dspsize control the size that is scaled from,
2640 * which should always be the user's requested size.
2641 */
2642 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002643 ((crtc_state->pipe_src_h - 1) << 16) |
2644 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002645 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002646 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2647 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002648 ((crtc_state->pipe_src_h - 1) << 16) |
2649 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002650 I915_WRITE(PRIMPOS(plane), 0);
2651 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002652 }
2653
Ville Syrjälä57779d02012-10-31 17:50:14 +02002654 switch (fb->pixel_format) {
2655 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002656 dspcntr |= DISPPLANE_8BPP;
2657 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002658 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002660 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 case DRM_FORMAT_RGB565:
2662 dspcntr |= DISPPLANE_BGRX565;
2663 break;
2664 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 dspcntr |= DISPPLANE_BGRX888;
2666 break;
2667 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_RGBX888;
2669 break;
2670 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002671 dspcntr |= DISPPLANE_BGRX101010;
2672 break;
2673 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002674 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002675 break;
2676 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002677 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002678 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002679
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002680 if (INTEL_INFO(dev)->gen >= 4 &&
2681 obj->tiling_mode != I915_TILING_NONE)
2682 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002683
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002684 if (IS_G4X(dev))
2685 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2686
Ville Syrjäläac484962016-01-20 21:05:26 +02002687 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002688
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 if (INTEL_INFO(dev)->gen >= 4) {
2690 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002691 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002692 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002693 linear_offset -= intel_crtc->dspaddr_offset;
2694 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002695 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002696 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002697
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002698 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302699 dspcntr |= DISPPLANE_ROTATE_180;
2700
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002701 x += (crtc_state->pipe_src_w - 1);
2702 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302703
2704 /* Finding the last pixel of the last line of the display
2705 data and adding to linear_offset*/
2706 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002707 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002708 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302709 }
2710
Paulo Zanoni2db33662015-09-14 15:20:03 -03002711 intel_crtc->adjusted_x = x;
2712 intel_crtc->adjusted_y = y;
2713
Sonika Jindal48404c12014-08-22 14:06:04 +05302714 I915_WRITE(reg, dspcntr);
2715
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002716 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002717 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002718 I915_WRITE(DSPSURF(plane),
2719 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002720 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002723 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002724 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725}
2726
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002727static void i9xx_disable_primary_plane(struct drm_plane *primary,
2728 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002729{
2730 struct drm_device *dev = crtc->dev;
2731 struct drm_i915_private *dev_priv = dev->dev_private;
2732 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002733 int plane = intel_crtc->plane;
2734
2735 I915_WRITE(DSPCNTR(plane), 0);
2736 if (INTEL_INFO(dev_priv)->gen >= 4)
2737 I915_WRITE(DSPSURF(plane), 0);
2738 else
2739 I915_WRITE(DSPADDR(plane), 0);
2740 POSTING_READ(DSPCNTR(plane));
2741}
2742
2743static void ironlake_update_primary_plane(struct drm_plane *primary,
2744 const struct intel_crtc_state *crtc_state,
2745 const struct intel_plane_state *plane_state)
2746{
2747 struct drm_device *dev = primary->dev;
2748 struct drm_i915_private *dev_priv = dev->dev_private;
2749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2750 struct drm_framebuffer *fb = plane_state->base.fb;
2751 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002752 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002753 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002754 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002755 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002756 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002757 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002758 int x = plane_state->src.x1 >> 16;
2759 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002760
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002761 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002762 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002763
2764 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2765 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2766
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 switch (fb->pixel_format) {
2768 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002769 dspcntr |= DISPPLANE_8BPP;
2770 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 case DRM_FORMAT_RGB565:
2772 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002775 dspcntr |= DISPPLANE_BGRX888;
2776 break;
2777 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002778 dspcntr |= DISPPLANE_RGBX888;
2779 break;
2780 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002781 dspcntr |= DISPPLANE_BGRX101010;
2782 break;
2783 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002784 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 break;
2786 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002787 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 }
2789
2790 if (obj->tiling_mode != I915_TILING_NONE)
2791 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002793 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002794 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795
Ville Syrjäläac484962016-01-20 21:05:26 +02002796 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002797 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002798 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002799 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002800 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002801 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302802 dspcntr |= DISPPLANE_ROTATE_180;
2803
2804 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002805 x += (crtc_state->pipe_src_w - 1);
2806 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302807
2808 /* Finding the last pixel of the last line of the display
2809 data and adding to linear_offset*/
2810 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002811 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002812 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302813 }
2814 }
2815
Paulo Zanoni2db33662015-09-14 15:20:03 -03002816 intel_crtc->adjusted_x = x;
2817 intel_crtc->adjusted_y = y;
2818
Sonika Jindal48404c12014-08-22 14:06:04 +05302819 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002821 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002822 I915_WRITE(DSPSURF(plane),
2823 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002824 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002825 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2826 } else {
2827 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2828 I915_WRITE(DSPLINOFF(plane), linear_offset);
2829 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002830 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831}
2832
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002833u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2834 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002835{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002836 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2837 return 64;
2838 } else {
2839 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002840
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002841 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002842 }
2843}
2844
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002845u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2846 struct drm_i915_gem_object *obj,
2847 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002851 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002852
Ville Syrjäläe7941292016-01-19 18:23:17 +02002853 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002854 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002855
Daniel Vetterce7f1722015-10-14 16:51:06 +02002856 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002857 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002858 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002859 return -1;
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002862
2863 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002864 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002865 PAGE_SIZE;
2866 }
2867
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002868 WARN_ON(upper_32_bits(offset));
2869
2870 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002871}
2872
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002873static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2874{
2875 struct drm_device *dev = intel_crtc->base.dev;
2876 struct drm_i915_private *dev_priv = dev->dev_private;
2877
2878 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2879 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2880 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002881}
2882
Chandra Kondurua1b22782015-04-07 15:28:45 -07002883/*
2884 * This function detaches (aka. unbinds) unused scalers in hardware
2885 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002886static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002887{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002888 struct intel_crtc_scaler_state *scaler_state;
2889 int i;
2890
Chandra Kondurua1b22782015-04-07 15:28:45 -07002891 scaler_state = &intel_crtc->config->scaler_state;
2892
2893 /* loop through and disable scalers that aren't in use */
2894 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002895 if (!scaler_state->scalers[i].in_use)
2896 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002897 }
2898}
2899
Chandra Konduru6156a452015-04-27 13:48:39 -07002900u32 skl_plane_ctl_format(uint32_t pixel_format)
2901{
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002903 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002904 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002905 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002906 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002907 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002908 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 /*
2912 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2913 * to be already pre-multiplied. We need to add a knob (or a different
2914 * DRM_FORMAT) for user-space to configure that.
2915 */
2916 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002922 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002923 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002925 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002935 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002937
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939}
2940
2941u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2942{
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 switch (fb_modifier) {
2944 case DRM_FORMAT_MOD_NONE:
2945 break;
2946 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 default:
2953 MISSING_CASE(fb_modifier);
2954 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002955
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957}
2958
2959u32 skl_plane_ctl_rotation(unsigned int rotation)
2960{
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 switch (rotation) {
2962 case BIT(DRM_ROTATE_0):
2963 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302964 /*
2965 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2966 * while i915 HW rotation is clockwise, thats why this swapping.
2967 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302969 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302973 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 default:
2975 MISSING_CASE(rotation);
2976 }
2977
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979}
2980
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002981static void skylake_update_primary_plane(struct drm_plane *plane,
2982 const struct intel_crtc_state *crtc_state,
2983 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002984{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002985 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002986 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2988 struct drm_framebuffer *fb = plane_state->base.fb;
2989 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002990 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302991 u32 plane_ctl, stride_div, stride;
2992 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002993 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302994 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002995 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002996 int scaler_id = plane_state->scaler_id;
2997 int src_x = plane_state->src.x1 >> 16;
2998 int src_y = plane_state->src.y1 >> 16;
2999 int src_w = drm_rect_width(&plane_state->src) >> 16;
3000 int src_h = drm_rect_height(&plane_state->src) >> 16;
3001 int dst_x = plane_state->dst.x1;
3002 int dst_y = plane_state->dst.y1;
3003 int dst_w = drm_rect_width(&plane_state->dst);
3004 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005
3006 plane_ctl = PLANE_CTL_ENABLE |
3007 PLANE_CTL_PIPE_GAMMA_ENABLE |
3008 PLANE_CTL_PIPE_CSC_ENABLE;
3009
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3011 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003015 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003016 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003017 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303018
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003019 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003020
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303021 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003022 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3023
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303024 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003025 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303026 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003027 x_offset = stride * tile_height - src_y - src_h;
3028 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003029 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 } else {
3031 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003032 x_offset = src_x;
3033 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 }
3036 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003037
Paulo Zanoni2db33662015-09-14 15:20:03 -03003038 intel_crtc->adjusted_x = x_offset;
3039 intel_crtc->adjusted_y = y_offset;
3040
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3043 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3044 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003045
3046 if (scaler_id >= 0) {
3047 uint32_t ps_ctrl = 0;
3048
3049 WARN_ON(!dst_w || !dst_h);
3050 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3051 crtc_state->scaler_state.scalers[scaler_id].mode;
3052 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3053 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3054 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3055 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3056 I915_WRITE(PLANE_POS(pipe, 0), 0);
3057 } else {
3058 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3059 }
3060
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003061 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003062
3063 POSTING_READ(PLANE_SURF(pipe, 0));
3064}
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066static void skylake_disable_primary_plane(struct drm_plane *primary,
3067 struct drm_crtc *crtc)
3068{
3069 struct drm_device *dev = crtc->dev;
3070 struct drm_i915_private *dev_priv = dev->dev_private;
3071 int pipe = to_intel_crtc(crtc)->pipe;
3072
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003073 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3074 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3075 POSTING_READ(PLANE_SURF(pipe, 0));
3076}
3077
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078/* Assume fb object is pinned & idle & fenced and just update base pointers */
3079static int
3080intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3081 int x, int y, enum mode_set_atomic state)
3082{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003083 /* Support for kgdboc is disabled, this needs a major rework. */
3084 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003085
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003086 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003087}
3088
Ville Syrjälä75147472014-11-24 18:28:11 +02003089static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003090{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003091 struct drm_crtc *crtc;
3092
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003093 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3095 enum plane plane = intel_crtc->plane;
3096
3097 intel_prepare_page_flip(dev, plane);
3098 intel_finish_page_flip_plane(dev, plane);
3099 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003100}
3101
3102static void intel_update_primary_planes(struct drm_device *dev)
3103{
Ville Syrjälä75147472014-11-24 18:28:11 +02003104 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003105
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003106 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003107 struct intel_plane *plane = to_intel_plane(crtc->primary);
3108 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003109
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003110 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003111 plane_state = to_intel_plane_state(plane->base.state);
3112
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003113 if (plane_state->visible)
3114 plane->update_plane(&plane->base,
3115 to_intel_crtc_state(crtc->state),
3116 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003117
3118 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003119 }
3120}
3121
Ville Syrjälä75147472014-11-24 18:28:11 +02003122void intel_prepare_reset(struct drm_device *dev)
3123{
3124 /* no reset support for gen2 */
3125 if (IS_GEN2(dev))
3126 return;
3127
3128 /* reset doesn't touch the display */
3129 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3130 return;
3131
3132 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003133 /*
3134 * Disabling the crtcs gracefully seems nicer. Also the
3135 * g33 docs say we should at least disable all the planes.
3136 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003137 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003138}
3139
3140void intel_finish_reset(struct drm_device *dev)
3141{
3142 struct drm_i915_private *dev_priv = to_i915(dev);
3143
3144 /*
3145 * Flips in the rings will be nuked by the reset,
3146 * so complete all pending flips so that user space
3147 * will get its events and not get stuck.
3148 */
3149 intel_complete_page_flips(dev);
3150
3151 /* no reset support for gen2 */
3152 if (IS_GEN2(dev))
3153 return;
3154
3155 /* reset doesn't touch the display */
3156 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3157 /*
3158 * Flips in the rings have been nuked by the reset,
3159 * so update the base address of all primary
3160 * planes to the the last fb to make sure we're
3161 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003162 *
3163 * FIXME: Atomic will make this obsolete since we won't schedule
3164 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003165 */
3166 intel_update_primary_planes(dev);
3167 return;
3168 }
3169
3170 /*
3171 * The display has been reset as well,
3172 * so need a full re-initialization.
3173 */
3174 intel_runtime_pm_disable_interrupts(dev_priv);
3175 intel_runtime_pm_enable_interrupts(dev_priv);
3176
3177 intel_modeset_init_hw(dev);
3178
3179 spin_lock_irq(&dev_priv->irq_lock);
3180 if (dev_priv->display.hpd_irq_setup)
3181 dev_priv->display.hpd_irq_setup(dev);
3182 spin_unlock_irq(&dev_priv->irq_lock);
3183
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003184 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003185
3186 intel_hpd_init(dev_priv);
3187
3188 drm_modeset_unlock_all(dev);
3189}
3190
Chris Wilson7d5e3792014-03-04 13:15:08 +00003191static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3192{
3193 struct drm_device *dev = crtc->dev;
3194 struct drm_i915_private *dev_priv = dev->dev_private;
3195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003196 bool pending;
3197
3198 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3199 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3200 return false;
3201
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003202 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003203 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003204 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003205
3206 return pending;
3207}
3208
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003209static void intel_update_pipe_config(struct intel_crtc *crtc,
3210 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003211{
3212 struct drm_device *dev = crtc->base.dev;
3213 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003214 struct intel_crtc_state *pipe_config =
3215 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003216
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003217 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3218 crtc->base.mode = crtc->base.state->mode;
3219
3220 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3221 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3222 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003224 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003225 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003226
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003227 /*
3228 * Update pipe size and adjust fitter if needed: the reason for this is
3229 * that in compute_mode_changes we check the native mode (not the pfit
3230 * mode) to see if we can flip rather than do a full mode set. In the
3231 * fastboot case, we'll flip, but if we don't update the pipesrc and
3232 * pfit state, we'll end up with a big fb scanned out into the wrong
3233 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003234 */
3235
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003236 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003237 ((pipe_config->pipe_src_w - 1) << 16) |
3238 (pipe_config->pipe_src_h - 1));
3239
3240 /* on skylake this is done by detaching scalers */
3241 if (INTEL_INFO(dev)->gen >= 9) {
3242 skl_detach_scalers(crtc);
3243
3244 if (pipe_config->pch_pfit.enabled)
3245 skylake_pfit_enable(crtc);
3246 } else if (HAS_PCH_SPLIT(dev)) {
3247 if (pipe_config->pch_pfit.enabled)
3248 ironlake_pfit_enable(crtc);
3249 else if (old_crtc_state->pch_pfit.enabled)
3250 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003251 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003252}
3253
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003254static void intel_fdi_normal_train(struct drm_crtc *crtc)
3255{
3256 struct drm_device *dev = crtc->dev;
3257 struct drm_i915_private *dev_priv = dev->dev_private;
3258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3259 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003260 i915_reg_t reg;
3261 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003262
3263 /* enable normal train */
3264 reg = FDI_TX_CTL(pipe);
3265 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003266 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003267 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3268 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003269 } else {
3270 temp &= ~FDI_LINK_TRAIN_NONE;
3271 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003272 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003273 I915_WRITE(reg, temp);
3274
3275 reg = FDI_RX_CTL(pipe);
3276 temp = I915_READ(reg);
3277 if (HAS_PCH_CPT(dev)) {
3278 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3279 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3280 } else {
3281 temp &= ~FDI_LINK_TRAIN_NONE;
3282 temp |= FDI_LINK_TRAIN_NONE;
3283 }
3284 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3285
3286 /* wait one idle pattern time */
3287 POSTING_READ(reg);
3288 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003289
3290 /* IVB wants error correction enabled */
3291 if (IS_IVYBRIDGE(dev))
3292 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3293 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003294}
3295
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003296/* The FDI link training functions for ILK/Ibexpeak. */
3297static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3298{
3299 struct drm_device *dev = crtc->dev;
3300 struct drm_i915_private *dev_priv = dev->dev_private;
3301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3302 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003303 i915_reg_t reg;
3304 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003305
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003306 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003307 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003308
Adam Jacksone1a44742010-06-25 15:32:14 -04003309 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3310 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 reg = FDI_RX_IMR(pipe);
3312 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003313 temp &= ~FDI_RX_SYMBOL_LOCK;
3314 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 I915_WRITE(reg, temp);
3316 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003317 udelay(150);
3318
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003319 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003320 reg = FDI_TX_CTL(pipe);
3321 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003322 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003323 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003324 temp &= ~FDI_LINK_TRAIN_NONE;
3325 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003326 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327
Chris Wilson5eddb702010-09-11 13:48:45 +01003328 reg = FDI_RX_CTL(pipe);
3329 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3333
3334 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003335 udelay(150);
3336
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003337 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003338 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3339 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3340 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003341
Chris Wilson5eddb702010-09-11 13:48:45 +01003342 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003343 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3346
3347 if ((temp & FDI_RX_BIT_LOCK)) {
3348 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003350 break;
3351 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003352 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003353 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003354 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003355
3356 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 reg = FDI_RX_CTL(pipe);
3364 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp);
3368
3369 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 udelay(150);
3371
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003373 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003375 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3376
3377 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 DRM_DEBUG_KMS("FDI train 2 done.\n");
3380 break;
3381 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003383 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385
3386 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003387
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388}
3389
Akshay Joshi0206e352011-08-16 15:34:10 -04003390static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3392 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3393 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3394 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3395};
3396
3397/* The FDI link training functions for SNB/Cougarpoint. */
3398static void gen6_fdi_link_train(struct drm_crtc *crtc)
3399{
3400 struct drm_device *dev = crtc->dev;
3401 struct drm_i915_private *dev_priv = dev->dev_private;
3402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3403 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003404 i915_reg_t reg;
3405 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406
Adam Jacksone1a44742010-06-25 15:32:14 -04003407 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3408 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_RX_IMR(pipe);
3410 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 temp &= ~FDI_RX_SYMBOL_LOCK;
3412 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp);
3414
3415 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003416 udelay(150);
3417
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003421 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003422 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_1;
3425 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3426 /* SNB-B */
3427 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429
Daniel Vetterd74cf322012-10-26 10:58:13 +02003430 I915_WRITE(FDI_RX_MISC(pipe),
3431 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3432
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 reg = FDI_RX_CTL(pipe);
3434 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 if (HAS_PCH_CPT(dev)) {
3436 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3437 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3438 } else {
3439 temp &= ~FDI_LINK_TRAIN_NONE;
3440 temp |= FDI_LINK_TRAIN_PATTERN_1;
3441 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3443
3444 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 udelay(150);
3446
Akshay Joshi0206e352011-08-16 15:34:10 -04003447 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 reg = FDI_TX_CTL(pipe);
3449 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3451 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 I915_WRITE(reg, temp);
3453
3454 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 udelay(500);
3456
Sean Paulfa37d392012-03-02 12:53:39 -05003457 for (retry = 0; retry < 5; retry++) {
3458 reg = FDI_RX_IIR(pipe);
3459 temp = I915_READ(reg);
3460 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3461 if (temp & FDI_RX_BIT_LOCK) {
3462 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3463 DRM_DEBUG_KMS("FDI train 1 done.\n");
3464 break;
3465 }
3466 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 }
Sean Paulfa37d392012-03-02 12:53:39 -05003468 if (retry < 5)
3469 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 }
3471 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473
3474 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 reg = FDI_TX_CTL(pipe);
3476 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 temp &= ~FDI_LINK_TRAIN_NONE;
3478 temp |= FDI_LINK_TRAIN_PATTERN_2;
3479 if (IS_GEN6(dev)) {
3480 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3481 /* SNB-B */
3482 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3483 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 reg = FDI_RX_CTL(pipe);
3487 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003488 if (HAS_PCH_CPT(dev)) {
3489 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3491 } else {
3492 temp &= ~FDI_LINK_TRAIN_NONE;
3493 temp |= FDI_LINK_TRAIN_PATTERN_2;
3494 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 I915_WRITE(reg, temp);
3496
3497 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 udelay(150);
3499
Akshay Joshi0206e352011-08-16 15:34:10 -04003500 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
3504 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 I915_WRITE(reg, temp);
3506
3507 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 udelay(500);
3509
Sean Paulfa37d392012-03-02 12:53:39 -05003510 for (retry = 0; retry < 5; retry++) {
3511 reg = FDI_RX_IIR(pipe);
3512 temp = I915_READ(reg);
3513 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3514 if (temp & FDI_RX_SYMBOL_LOCK) {
3515 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3516 DRM_DEBUG_KMS("FDI train 2 done.\n");
3517 break;
3518 }
3519 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 }
Sean Paulfa37d392012-03-02 12:53:39 -05003521 if (retry < 5)
3522 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523 }
3524 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003525 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526
3527 DRM_DEBUG_KMS("FDI train done.\n");
3528}
3529
Jesse Barnes357555c2011-04-28 15:09:55 -07003530/* Manual link training for Ivy Bridge A0 parts */
3531static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3532{
3533 struct drm_device *dev = crtc->dev;
3534 struct drm_i915_private *dev_priv = dev->dev_private;
3535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3536 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003537 i915_reg_t reg;
3538 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003539
3540 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3541 for train result */
3542 reg = FDI_RX_IMR(pipe);
3543 temp = I915_READ(reg);
3544 temp &= ~FDI_RX_SYMBOL_LOCK;
3545 temp &= ~FDI_RX_BIT_LOCK;
3546 I915_WRITE(reg, temp);
3547
3548 POSTING_READ(reg);
3549 udelay(150);
3550
Daniel Vetter01a415f2012-10-27 15:58:40 +02003551 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3552 I915_READ(FDI_RX_IIR(pipe)));
3553
Jesse Barnes139ccd32013-08-19 11:04:55 -07003554 /* Try each vswing and preemphasis setting twice before moving on */
3555 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3556 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003557 reg = FDI_TX_CTL(pipe);
3558 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003559 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3560 temp &= ~FDI_TX_ENABLE;
3561 I915_WRITE(reg, temp);
3562
3563 reg = FDI_RX_CTL(pipe);
3564 temp = I915_READ(reg);
3565 temp &= ~FDI_LINK_TRAIN_AUTO;
3566 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3567 temp &= ~FDI_RX_ENABLE;
3568 I915_WRITE(reg, temp);
3569
3570 /* enable CPU FDI TX and PCH FDI RX */
3571 reg = FDI_TX_CTL(pipe);
3572 temp = I915_READ(reg);
3573 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003574 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003575 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003576 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003577 temp |= snb_b_fdi_train_param[j/2];
3578 temp |= FDI_COMPOSITE_SYNC;
3579 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3580
3581 I915_WRITE(FDI_RX_MISC(pipe),
3582 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3583
3584 reg = FDI_RX_CTL(pipe);
3585 temp = I915_READ(reg);
3586 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3587 temp |= FDI_COMPOSITE_SYNC;
3588 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3589
3590 POSTING_READ(reg);
3591 udelay(1); /* should be 0.5us */
3592
3593 for (i = 0; i < 4; i++) {
3594 reg = FDI_RX_IIR(pipe);
3595 temp = I915_READ(reg);
3596 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3597
3598 if (temp & FDI_RX_BIT_LOCK ||
3599 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3600 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3601 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3602 i);
3603 break;
3604 }
3605 udelay(1); /* should be 0.5us */
3606 }
3607 if (i == 4) {
3608 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3609 continue;
3610 }
3611
3612 /* Train 2 */
3613 reg = FDI_TX_CTL(pipe);
3614 temp = I915_READ(reg);
3615 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3616 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3617 I915_WRITE(reg, temp);
3618
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003623 I915_WRITE(reg, temp);
3624
3625 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003626 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003627
Jesse Barnes139ccd32013-08-19 11:04:55 -07003628 for (i = 0; i < 4; i++) {
3629 reg = FDI_RX_IIR(pipe);
3630 temp = I915_READ(reg);
3631 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003632
Jesse Barnes139ccd32013-08-19 11:04:55 -07003633 if (temp & FDI_RX_SYMBOL_LOCK ||
3634 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3635 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3636 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3637 i);
3638 goto train_done;
3639 }
3640 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003641 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003642 if (i == 4)
3643 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003644 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003647 DRM_DEBUG_KMS("FDI train done.\n");
3648}
3649
Daniel Vetter88cefb62012-08-12 19:27:14 +02003650static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003651{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003652 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003653 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003654 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003655 i915_reg_t reg;
3656 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003657
Jesse Barnes0e23b992010-09-10 11:10:00 -07003658 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003659 reg = FDI_RX_CTL(pipe);
3660 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003661 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003662 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003663 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003664 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3665
3666 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003667 udelay(200);
3668
3669 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003670 temp = I915_READ(reg);
3671 I915_WRITE(reg, temp | FDI_PCDCLK);
3672
3673 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003674 udelay(200);
3675
Paulo Zanoni20749732012-11-23 15:30:38 -02003676 /* Enable CPU FDI TX PLL, always on for Ironlake */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3680 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003681
Paulo Zanoni20749732012-11-23 15:30:38 -02003682 POSTING_READ(reg);
3683 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003684 }
3685}
3686
Daniel Vetter88cefb62012-08-12 19:27:14 +02003687static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3688{
3689 struct drm_device *dev = intel_crtc->base.dev;
3690 struct drm_i915_private *dev_priv = dev->dev_private;
3691 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003692 i915_reg_t reg;
3693 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003694
3695 /* Switch from PCDclk to Rawclk */
3696 reg = FDI_RX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3699
3700 /* Disable CPU FDI TX PLL */
3701 reg = FDI_TX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3704
3705 POSTING_READ(reg);
3706 udelay(100);
3707
3708 reg = FDI_RX_CTL(pipe);
3709 temp = I915_READ(reg);
3710 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3711
3712 /* Wait for the clocks to turn off. */
3713 POSTING_READ(reg);
3714 udelay(100);
3715}
3716
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003717static void ironlake_fdi_disable(struct drm_crtc *crtc)
3718{
3719 struct drm_device *dev = crtc->dev;
3720 struct drm_i915_private *dev_priv = dev->dev_private;
3721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3722 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003723 i915_reg_t reg;
3724 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003725
3726 /* disable CPU FDI tx and PCH FDI rx */
3727 reg = FDI_TX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3730 POSTING_READ(reg);
3731
3732 reg = FDI_RX_CTL(pipe);
3733 temp = I915_READ(reg);
3734 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003735 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003736 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3737
3738 POSTING_READ(reg);
3739 udelay(100);
3740
3741 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003742 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003743 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003744
3745 /* still set train pattern 1 */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 temp &= ~FDI_LINK_TRAIN_NONE;
3749 temp |= FDI_LINK_TRAIN_PATTERN_1;
3750 I915_WRITE(reg, temp);
3751
3752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 if (HAS_PCH_CPT(dev)) {
3755 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3756 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3757 } else {
3758 temp &= ~FDI_LINK_TRAIN_NONE;
3759 temp |= FDI_LINK_TRAIN_PATTERN_1;
3760 }
3761 /* BPC in FDI rx is consistent with that in PIPECONF */
3762 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003763 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003764 I915_WRITE(reg, temp);
3765
3766 POSTING_READ(reg);
3767 udelay(100);
3768}
3769
Chris Wilson5dce5b932014-01-20 10:17:36 +00003770bool intel_has_pending_fb_unpin(struct drm_device *dev)
3771{
3772 struct intel_crtc *crtc;
3773
3774 /* Note that we don't need to be called with mode_config.lock here
3775 * as our list of CRTC objects is static for the lifetime of the
3776 * device and so cannot disappear as we iterate. Similarly, we can
3777 * happily treat the predicates as racy, atomic checks as userspace
3778 * cannot claim and pin a new fb without at least acquring the
3779 * struct_mutex and so serialising with us.
3780 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003781 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003782 if (atomic_read(&crtc->unpin_work_count) == 0)
3783 continue;
3784
3785 if (crtc->unpin_work)
3786 intel_wait_for_vblank(dev, crtc->pipe);
3787
3788 return true;
3789 }
3790
3791 return false;
3792}
3793
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003794static void page_flip_completed(struct intel_crtc *intel_crtc)
3795{
3796 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3797 struct intel_unpin_work *work = intel_crtc->unpin_work;
3798
3799 /* ensure that the unpin work is consistent wrt ->pending. */
3800 smp_rmb();
3801 intel_crtc->unpin_work = NULL;
3802
3803 if (work->event)
3804 drm_send_vblank_event(intel_crtc->base.dev,
3805 intel_crtc->pipe,
3806 work->event);
3807
3808 drm_crtc_vblank_put(&intel_crtc->base);
3809
3810 wake_up_all(&dev_priv->pending_flip_queue);
3811 queue_work(dev_priv->wq, &work->work);
3812
3813 trace_i915_flip_complete(intel_crtc->plane,
3814 work->pending_flip_obj);
3815}
3816
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003817static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003818{
Chris Wilson0f911282012-04-17 10:05:38 +01003819 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003820 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003821 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003822
Daniel Vetter2c10d572012-12-20 21:24:07 +01003823 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003824
3825 ret = wait_event_interruptible_timeout(
3826 dev_priv->pending_flip_queue,
3827 !intel_crtc_has_pending_flip(crtc),
3828 60*HZ);
3829
3830 if (ret < 0)
3831 return ret;
3832
3833 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003835
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003836 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003837 if (intel_crtc->unpin_work) {
3838 WARN_ONCE(1, "Removing stuck page flip\n");
3839 page_flip_completed(intel_crtc);
3840 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003841 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003842 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003843
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003844 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003845}
3846
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003847static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3848{
3849 u32 temp;
3850
3851 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3852
3853 mutex_lock(&dev_priv->sb_lock);
3854
3855 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3856 temp |= SBI_SSCCTL_DISABLE;
3857 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3858
3859 mutex_unlock(&dev_priv->sb_lock);
3860}
3861
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003862/* Program iCLKIP clock to the desired frequency */
3863static void lpt_program_iclkip(struct drm_crtc *crtc)
3864{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003865 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003866 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003867 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3868 u32 temp;
3869
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003870 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003871
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003872 /* The iCLK virtual clock root frequency is in MHz,
3873 * but the adjusted_mode->crtc_clock in in KHz. To get the
3874 * divisors, it is necessary to divide one by another, so we
3875 * convert the virtual clock precision to KHz here for higher
3876 * precision.
3877 */
3878 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003879 u32 iclk_virtual_root_freq = 172800 * 1000;
3880 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003881 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003882
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003883 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3884 clock << auxdiv);
3885 divsel = (desired_divisor / iclk_pi_range) - 2;
3886 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003888 /*
3889 * Near 20MHz is a corner case which is
3890 * out of range for the 7-bit divisor
3891 */
3892 if (divsel <= 0x7f)
3893 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 }
3895
3896 /* This should not happen with any sane values */
3897 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3898 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3899 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3900 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3901
3902 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 +03003903 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003904 auxdiv,
3905 divsel,
3906 phasedir,
3907 phaseinc);
3908
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003909 mutex_lock(&dev_priv->sb_lock);
3910
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003911 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003912 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3914 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3915 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3916 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3917 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3918 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003919 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920
3921 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003922 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3924 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003925 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926
3927 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003928 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003930 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003932 mutex_unlock(&dev_priv->sb_lock);
3933
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934 /* Wait for initialization time */
3935 udelay(24);
3936
3937 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3938}
3939
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003940int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3941{
3942 u32 divsel, phaseinc, auxdiv;
3943 u32 iclk_virtual_root_freq = 172800 * 1000;
3944 u32 iclk_pi_range = 64;
3945 u32 desired_divisor;
3946 u32 temp;
3947
3948 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3949 return 0;
3950
3951 mutex_lock(&dev_priv->sb_lock);
3952
3953 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3954 if (temp & SBI_SSCCTL_DISABLE) {
3955 mutex_unlock(&dev_priv->sb_lock);
3956 return 0;
3957 }
3958
3959 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3960 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3961 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3962 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3963 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3964
3965 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3966 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3967 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3968
3969 mutex_unlock(&dev_priv->sb_lock);
3970
3971 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3972
3973 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3974 desired_divisor << auxdiv);
3975}
3976
Daniel Vetter275f01b22013-05-03 11:49:47 +02003977static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3978 enum pipe pch_transcoder)
3979{
3980 struct drm_device *dev = crtc->base.dev;
3981 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003982 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003983
3984 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3985 I915_READ(HTOTAL(cpu_transcoder)));
3986 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3987 I915_READ(HBLANK(cpu_transcoder)));
3988 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3989 I915_READ(HSYNC(cpu_transcoder)));
3990
3991 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3992 I915_READ(VTOTAL(cpu_transcoder)));
3993 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3994 I915_READ(VBLANK(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3996 I915_READ(VSYNC(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3998 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3999}
4000
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004001static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004002{
4003 struct drm_i915_private *dev_priv = dev->dev_private;
4004 uint32_t temp;
4005
4006 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004007 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004008 return;
4009
4010 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4011 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4012
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004013 temp &= ~FDI_BC_BIFURCATION_SELECT;
4014 if (enable)
4015 temp |= FDI_BC_BIFURCATION_SELECT;
4016
4017 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004018 I915_WRITE(SOUTH_CHICKEN1, temp);
4019 POSTING_READ(SOUTH_CHICKEN1);
4020}
4021
4022static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4023{
4024 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004025
4026 switch (intel_crtc->pipe) {
4027 case PIPE_A:
4028 break;
4029 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004030 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004031 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004032 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004033 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034
4035 break;
4036 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004037 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038
4039 break;
4040 default:
4041 BUG();
4042 }
4043}
4044
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004045/* Return which DP Port should be selected for Transcoder DP control */
4046static enum port
4047intel_trans_dp_port_sel(struct drm_crtc *crtc)
4048{
4049 struct drm_device *dev = crtc->dev;
4050 struct intel_encoder *encoder;
4051
4052 for_each_encoder_on_crtc(dev, crtc, encoder) {
4053 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4054 encoder->type == INTEL_OUTPUT_EDP)
4055 return enc_to_dig_port(&encoder->base)->port;
4056 }
4057
4058 return -1;
4059}
4060
Jesse Barnesf67a5592011-01-05 10:31:48 -08004061/*
4062 * Enable PCH resources required for PCH ports:
4063 * - PCH PLLs
4064 * - FDI training & RX/TX
4065 * - update transcoder timings
4066 * - DP transcoding bits
4067 * - transcoder
4068 */
4069static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004070{
4071 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004072 struct drm_i915_private *dev_priv = dev->dev_private;
4073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4074 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004075 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004076
Daniel Vetterab9412b2013-05-03 11:49:46 +02004077 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004078
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004079 if (IS_IVYBRIDGE(dev))
4080 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4081
Daniel Vettercd986ab2012-10-26 10:58:12 +02004082 /* Write the TU size bits before fdi link training, so that error
4083 * detection works. */
4084 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4085 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4086
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004087 /*
4088 * Sometimes spurious CPU pipe underruns happen during FDI
4089 * training, at least with VGA+HDMI cloning. Suppress them.
4090 */
4091 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4092
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004093 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004094 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004095
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004096 /* We need to program the right clock selection before writing the pixel
4097 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004098 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004099 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004100
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004102 temp |= TRANS_DPLL_ENABLE(pipe);
4103 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004104 if (intel_crtc->config->shared_dpll ==
4105 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004106 temp |= sel;
4107 else
4108 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004109 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004112 /* XXX: pch pll's can be enabled any time before we enable the PCH
4113 * transcoder, and we actually should do this to not upset any PCH
4114 * transcoder that already use the clock when we share it.
4115 *
4116 * Note that enable_shared_dpll tries to do the right thing, but
4117 * get_shared_dpll unconditionally resets the pll - we need that to have
4118 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004119 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004120
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004121 /* set transcoder timing, panel must allow it */
4122 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004123 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004125 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004126
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004127 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4128
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004130 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004131 const struct drm_display_mode *adjusted_mode =
4132 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004134 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004139 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004140 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004142 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004144 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146
4147 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004148 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004151 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004154 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 break;
4157 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004158 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 }
4160
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 }
4163
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004164 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004165}
4166
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004173
Daniel Vetterab9412b2013-05-03 11:49:46 +02004174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004175
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004176 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004177
Paulo Zanoni0540e482012-10-31 18:12:40 -02004178 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004180
Paulo Zanoni937bb612012-10-31 18:12:47 -02004181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004182}
4183
Daniel Vettera1520312013-05-03 11:49:50 +02004184static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004185{
4186 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004187 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004188 u32 temp;
4189
4190 temp = I915_READ(dslreg);
4191 udelay(500);
4192 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004193 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004194 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004195 }
4196}
4197
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004198static int
4199skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4200 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4201 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004202{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004203 struct intel_crtc_scaler_state *scaler_state =
4204 &crtc_state->scaler_state;
4205 struct intel_crtc *intel_crtc =
4206 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004207 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004208
4209 need_scaling = intel_rotation_90_or_270(rotation) ?
4210 (src_h != dst_w || src_w != dst_h):
4211 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004212
4213 /*
4214 * if plane is being disabled or scaler is no more required or force detach
4215 * - free scaler binded to this plane/crtc
4216 * - in order to do this, update crtc->scaler_usage
4217 *
4218 * Here scaler state in crtc_state is set free so that
4219 * scaler can be assigned to other user. Actual register
4220 * update to free the scaler is done in plane/panel-fit programming.
4221 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4222 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004223 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004224 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004225 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226 scaler_state->scalers[*scaler_id].in_use = 0;
4227
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004228 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4229 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4230 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231 scaler_state->scaler_users);
4232 *scaler_id = -1;
4233 }
4234 return 0;
4235 }
4236
4237 /* range checks */
4238 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4239 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4240
4241 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4242 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004243 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004244 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004246 return -EINVAL;
4247 }
4248
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249 /* mark this plane as a scaler user in crtc_state */
4250 scaler_state->scaler_users |= (1 << scaler_user);
4251 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4252 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4254 scaler_state->scaler_users);
4255
4256 return 0;
4257}
4258
4259/**
4260 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4261 *
4262 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004263 *
4264 * Return
4265 * 0 - scaler_usage updated successfully
4266 * error - requested scaling cannot be supported or other error condition
4267 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004268int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269{
4270 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004271 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272
4273 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4274 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4275
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004276 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004277 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004278 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004279 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280}
4281
4282/**
4283 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4284 *
4285 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004286 * @plane_state: atomic plane state to update
4287 *
4288 * Return
4289 * 0 - scaler_usage updated successfully
4290 * error - requested scaling cannot be supported or other error condition
4291 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004292static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4293 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004294{
4295
4296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004297 struct intel_plane *intel_plane =
4298 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004299 struct drm_framebuffer *fb = plane_state->base.fb;
4300 int ret;
4301
4302 bool force_detach = !fb || !plane_state->visible;
4303
4304 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4305 intel_plane->base.base.id, intel_crtc->pipe,
4306 drm_plane_index(&intel_plane->base));
4307
4308 ret = skl_update_scaler(crtc_state, force_detach,
4309 drm_plane_index(&intel_plane->base),
4310 &plane_state->scaler_id,
4311 plane_state->base.rotation,
4312 drm_rect_width(&plane_state->src) >> 16,
4313 drm_rect_height(&plane_state->src) >> 16,
4314 drm_rect_width(&plane_state->dst),
4315 drm_rect_height(&plane_state->dst));
4316
4317 if (ret || plane_state->scaler_id < 0)
4318 return ret;
4319
Chandra Kondurua1b22782015-04-07 15:28:45 -07004320 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004321 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004323 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004324 return -EINVAL;
4325 }
4326
4327 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004328 switch (fb->pixel_format) {
4329 case DRM_FORMAT_RGB565:
4330 case DRM_FORMAT_XBGR8888:
4331 case DRM_FORMAT_XRGB8888:
4332 case DRM_FORMAT_ABGR8888:
4333 case DRM_FORMAT_ARGB8888:
4334 case DRM_FORMAT_XRGB2101010:
4335 case DRM_FORMAT_XBGR2101010:
4336 case DRM_FORMAT_YUYV:
4337 case DRM_FORMAT_YVYU:
4338 case DRM_FORMAT_UYVY:
4339 case DRM_FORMAT_VYUY:
4340 break;
4341 default:
4342 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4343 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4344 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004345 }
4346
Chandra Kondurua1b22782015-04-07 15:28:45 -07004347 return 0;
4348}
4349
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004350static void skylake_scaler_disable(struct intel_crtc *crtc)
4351{
4352 int i;
4353
4354 for (i = 0; i < crtc->num_scalers; i++)
4355 skl_detach_scaler(crtc, i);
4356}
4357
4358static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004359{
4360 struct drm_device *dev = crtc->base.dev;
4361 struct drm_i915_private *dev_priv = dev->dev_private;
4362 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004363 struct intel_crtc_scaler_state *scaler_state =
4364 &crtc->config->scaler_state;
4365
4366 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004368 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 int id;
4370
4371 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4372 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4373 return;
4374 }
4375
4376 id = scaler_state->scaler_id;
4377 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4378 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4379 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4380 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4381
4382 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004383 }
4384}
4385
Jesse Barnesb074cec2013-04-25 12:55:02 -07004386static void ironlake_pfit_enable(struct intel_crtc *crtc)
4387{
4388 struct drm_device *dev = crtc->base.dev;
4389 struct drm_i915_private *dev_priv = dev->dev_private;
4390 int pipe = crtc->pipe;
4391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004392 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004393 /* Force use of hard-coded filter coefficients
4394 * as some pre-programmed values are broken,
4395 * e.g. x201.
4396 */
4397 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4398 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4399 PF_PIPE_SEL_IVB(pipe));
4400 else
4401 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004402 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4403 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004404 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004405}
4406
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004407void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004408{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004409 struct drm_device *dev = crtc->base.dev;
4410 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004412 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004413 return;
4414
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004415 /*
4416 * We can only enable IPS after we enable a plane and wait for a vblank
4417 * This function is called from post_plane_update, which is run after
4418 * a vblank wait.
4419 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004420
Paulo Zanonid77e4532013-09-24 13:52:55 -03004421 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004422 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004423 mutex_lock(&dev_priv->rps.hw_lock);
4424 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4425 mutex_unlock(&dev_priv->rps.hw_lock);
4426 /* Quoting Art Runyan: "its not safe to expect any particular
4427 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004428 * mailbox." Moreover, the mailbox may return a bogus state,
4429 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004430 */
4431 } else {
4432 I915_WRITE(IPS_CTL, IPS_ENABLE);
4433 /* The bit only becomes 1 in the next vblank, so this wait here
4434 * is essentially intel_wait_for_vblank. If we don't have this
4435 * and don't wait for vblanks until the end of crtc_enable, then
4436 * the HW state readout code will complain that the expected
4437 * IPS_CTL value is not the one we read. */
4438 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4439 DRM_ERROR("Timed out waiting for IPS enable\n");
4440 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004441}
4442
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004443void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004444{
4445 struct drm_device *dev = crtc->base.dev;
4446 struct drm_i915_private *dev_priv = dev->dev_private;
4447
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004448 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004449 return;
4450
4451 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004452 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004453 mutex_lock(&dev_priv->rps.hw_lock);
4454 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004456 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4457 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4458 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004459 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004460 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004461 POSTING_READ(IPS_CTL);
4462 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004463
4464 /* We need to wait for a vblank before we can disable the plane. */
4465 intel_wait_for_vblank(dev, crtc->pipe);
4466}
4467
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004468static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004469{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004470 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004471 struct drm_device *dev = intel_crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473
4474 mutex_lock(&dev->struct_mutex);
4475 dev_priv->mm.interruptible = false;
4476 (void) intel_overlay_switch_off(intel_crtc->overlay);
4477 dev_priv->mm.interruptible = true;
4478 mutex_unlock(&dev->struct_mutex);
4479 }
4480
4481 /* Let userspace switch the overlay on again. In most cases userspace
4482 * has to recompute where to put it anyway.
4483 */
4484}
4485
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004486/**
4487 * intel_post_enable_primary - Perform operations after enabling primary plane
4488 * @crtc: the CRTC whose primary plane was just enabled
4489 *
4490 * Performs potentially sleeping operations that must be done after the primary
4491 * plane is enabled, such as updating FBC and IPS. Note that this may be
4492 * called due to an explicit primary plane update, or due to an implicit
4493 * re-enable that is caused when a sprite plane is updated to no longer
4494 * completely hide the primary plane.
4495 */
4496static void
4497intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004498{
4499 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004500 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4502 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004503
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004504 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004505 * FIXME IPS should be fine as long as one plane is
4506 * enabled, but in practice it seems to have problems
4507 * when going from primary only to sprite only and vice
4508 * versa.
4509 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004510 hsw_enable_ips(intel_crtc);
4511
Daniel Vetterf99d7062014-06-19 16:01:59 +02004512 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 * Gen2 reports pipe underruns whenever all planes are disabled.
4514 * So don't enable underrun reporting before at least some planes
4515 * are enabled.
4516 * FIXME: Need to fix the logic to work when we turn off all planes
4517 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004518 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004519 if (IS_GEN2(dev))
4520 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4521
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004522 /* Underruns don't always raise interrupts, so check manually. */
4523 intel_check_cpu_fifo_underruns(dev_priv);
4524 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004525}
4526
Ville Syrjälä2622a082016-03-09 19:07:26 +02004527/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004528static void
4529intel_pre_disable_primary(struct drm_crtc *crtc)
4530{
4531 struct drm_device *dev = crtc->dev;
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
4535
4536 /*
4537 * Gen2 reports pipe underruns whenever all planes are disabled.
4538 * So diasble underrun reporting before all the planes get disabled.
4539 * FIXME: Need to fix the logic to work when we turn off all planes
4540 * but leave the pipe running.
4541 */
4542 if (IS_GEN2(dev))
4543 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4544
4545 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004546 * FIXME IPS should be fine as long as one plane is
4547 * enabled, but in practice it seems to have problems
4548 * when going from primary only to sprite only and vice
4549 * versa.
4550 */
4551 hsw_disable_ips(intel_crtc);
4552}
4553
4554/* FIXME get rid of this and use pre_plane_update */
4555static void
4556intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4557{
4558 struct drm_device *dev = crtc->dev;
4559 struct drm_i915_private *dev_priv = dev->dev_private;
4560 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4561 int pipe = intel_crtc->pipe;
4562
4563 intel_pre_disable_primary(crtc);
4564
4565 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004566 * Vblank time updates from the shadow to live plane control register
4567 * are blocked if the memory self-refresh mode is active at that
4568 * moment. So to make sure the plane gets truly disabled, disable
4569 * first the self-refresh mode. The self-refresh enable bit in turn
4570 * will be checked/applied by the HW only at the next frame start
4571 * event which is after the vblank start event, so we need to have a
4572 * wait-for-vblank between disabling the plane and the pipe.
4573 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004574 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004575 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004576 dev_priv->wm.vlv.cxsr = false;
4577 intel_wait_for_vblank(dev, pipe);
4578 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004579}
4580
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004581static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004582{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004583 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4584 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004585 struct intel_crtc_state *pipe_config =
4586 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004587 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004588 struct drm_plane *primary = crtc->base.primary;
4589 struct drm_plane_state *old_pri_state =
4590 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004591
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004592 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004593
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004594 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004595
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004596 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004597 intel_update_watermarks(&crtc->base);
4598
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004599 if (old_pri_state) {
4600 struct intel_plane_state *primary_state =
4601 to_intel_plane_state(primary->state);
4602 struct intel_plane_state *old_primary_state =
4603 to_intel_plane_state(old_pri_state);
4604
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004605 intel_fbc_post_update(crtc);
4606
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004607 if (primary_state->visible &&
4608 (needs_modeset(&pipe_config->base) ||
4609 !old_primary_state->visible))
4610 intel_post_enable_primary(&crtc->base);
4611 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004612}
4613
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004614static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004615{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004616 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004617 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004618 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004619 struct intel_crtc_state *pipe_config =
4620 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004621 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4622 struct drm_plane *primary = crtc->base.primary;
4623 struct drm_plane_state *old_pri_state =
4624 drm_atomic_get_existing_plane_state(old_state, primary);
4625 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004626
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004627 if (old_pri_state) {
4628 struct intel_plane_state *primary_state =
4629 to_intel_plane_state(primary->state);
4630 struct intel_plane_state *old_primary_state =
4631 to_intel_plane_state(old_pri_state);
4632
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004633 intel_fbc_pre_update(crtc);
4634
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004635 if (old_primary_state->visible &&
4636 (modeset || !primary_state->visible))
4637 intel_pre_disable_primary(&crtc->base);
4638 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004639
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004640 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004641 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004642
Ville Syrjälä2622a082016-03-09 19:07:26 +02004643 /*
4644 * Vblank time updates from the shadow to live plane control register
4645 * are blocked if the memory self-refresh mode is active at that
4646 * moment. So to make sure the plane gets truly disabled, disable
4647 * first the self-refresh mode. The self-refresh enable bit in turn
4648 * will be checked/applied by the HW only at the next frame start
4649 * event which is after the vblank start event, so we need to have a
4650 * wait-for-vblank between disabling the plane and the pipe.
4651 */
4652 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004653 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004654 dev_priv->wm.vlv.cxsr = false;
4655 intel_wait_for_vblank(dev, crtc->pipe);
4656 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004657 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004658
Matt Ropered4a6a72016-02-23 17:20:13 -08004659 /*
4660 * IVB workaround: must disable low power watermarks for at least
4661 * one frame before enabling scaling. LP watermarks can be re-enabled
4662 * when scaling is disabled.
4663 *
4664 * WaCxSRDisabledForSpriteScaling:ivb
4665 */
4666 if (pipe_config->disable_lp_wm) {
4667 ilk_disable_lp_wm(dev);
4668 intel_wait_for_vblank(dev, crtc->pipe);
4669 }
4670
4671 /*
4672 * If we're doing a modeset, we're done. No need to do any pre-vblank
4673 * watermark programming here.
4674 */
4675 if (needs_modeset(&pipe_config->base))
4676 return;
4677
4678 /*
4679 * For platforms that support atomic watermarks, program the
4680 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4681 * will be the intermediate values that are safe for both pre- and
4682 * post- vblank; when vblank happens, the 'active' values will be set
4683 * to the final 'target' values and we'll do this again to get the
4684 * optimal watermarks. For gen9+ platforms, the values we program here
4685 * will be the final target values which will get automatically latched
4686 * at vblank time; no further programming will be necessary.
4687 *
4688 * If a platform hasn't been transitioned to atomic watermarks yet,
4689 * we'll continue to update watermarks the old way, if flags tell
4690 * us to.
4691 */
4692 if (dev_priv->display.initial_watermarks != NULL)
4693 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004694 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004695 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004696}
4697
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004698static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004699{
4700 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004702 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004703 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004705 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004706
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004707 drm_for_each_plane_mask(p, dev, plane_mask)
4708 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004709
Daniel Vetterf99d7062014-06-19 16:01:59 +02004710 /*
4711 * FIXME: Once we grow proper nuclear flip support out of this we need
4712 * to compute the mask of flip planes precisely. For the time being
4713 * consider this a flip to a NULL plane.
4714 */
4715 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004716}
4717
Jesse Barnesf67a5592011-01-05 10:31:48 -08004718static void ironlake_crtc_enable(struct drm_crtc *crtc)
4719{
4720 struct drm_device *dev = crtc->dev;
4721 struct drm_i915_private *dev_priv = dev->dev_private;
4722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004723 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004724 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004725
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004726 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004727 return;
4728
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004729 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004730 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4731
4732 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004733 intel_prepare_shared_dpll(intel_crtc);
4734
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004735 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304736 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004737
4738 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004739 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004740
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004741 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004742 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004743 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004744 }
4745
4746 ironlake_set_pipeconf(crtc);
4747
Jesse Barnesf67a5592011-01-05 10:31:48 -08004748 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004749
Daniel Vettera72e4c92014-09-30 10:56:47 +02004750 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004751
Daniel Vetterf6736a12013-06-05 13:34:30 +02004752 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004753 if (encoder->pre_enable)
4754 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004756 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004757 /* Note: FDI PLL enabling _must_ be done before we enable the
4758 * cpu pipes, hence this is separate from all the other fdi/pch
4759 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004760 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004761 } else {
4762 assert_fdi_tx_disabled(dev_priv, pipe);
4763 assert_fdi_rx_disabled(dev_priv, pipe);
4764 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004765
Jesse Barnesb074cec2013-04-25 12:55:02 -07004766 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004767
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004768 /*
4769 * On ILK+ LUT must be loaded before the pipe is running but with
4770 * clocks enabled
4771 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004772 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004773
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004774 if (dev_priv->display.initial_watermarks != NULL)
4775 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004776 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004777
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004778 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004779 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004780
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004781 assert_vblank_disabled(crtc);
4782 drm_crtc_vblank_on(crtc);
4783
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004784 for_each_encoder_on_crtc(dev, crtc, encoder)
4785 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004786
4787 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004788 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004789
4790 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4791 if (intel_crtc->config->has_pch_encoder)
4792 intel_wait_for_vblank(dev, pipe);
4793 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004794}
4795
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004796/* IPS only exists on ULT machines and is tied to pipe A. */
4797static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4798{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004799 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004800}
4801
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004802static void haswell_crtc_enable(struct drm_crtc *crtc)
4803{
4804 struct drm_device *dev = crtc->dev;
4805 struct drm_i915_private *dev_priv = dev->dev_private;
4806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4807 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004808 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004809 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004810 struct intel_crtc_state *pipe_config =
4811 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004812
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004813 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004814 return;
4815
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004816 if (intel_crtc->config->has_pch_encoder)
4817 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4818 false);
4819
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004820 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004821 intel_enable_shared_dpll(intel_crtc);
4822
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004823 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304824 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004825
Jani Nikula4d1de972016-03-18 17:05:42 +02004826 if (!intel_crtc->config->has_dsi_encoder)
4827 intel_set_pipe_timings(intel_crtc);
4828
Jani Nikulabc58be62016-03-18 17:05:39 +02004829 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004830
Jani Nikula4d1de972016-03-18 17:05:42 +02004831 if (cpu_transcoder != TRANSCODER_EDP &&
4832 !transcoder_is_dsi(cpu_transcoder)) {
4833 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004834 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004835 }
4836
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004837 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004838 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004839 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004840 }
4841
Jani Nikula4d1de972016-03-18 17:05:42 +02004842 if (!intel_crtc->config->has_dsi_encoder)
4843 haswell_set_pipeconf(crtc);
4844
Jani Nikula391bf042016-03-18 17:05:40 +02004845 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004846
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004847 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004848
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004849 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004850
Daniel Vetter6b698512015-11-28 11:05:39 +01004851 if (intel_crtc->config->has_pch_encoder)
4852 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4853 else
4854 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4855
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304856 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004857 if (encoder->pre_enable)
4858 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304859 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004860
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004861 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004862 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004863
Jani Nikulaa65347b2015-11-27 12:21:46 +02004864 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304865 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004866
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004867 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004868 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004869 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004870 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004871
4872 /*
4873 * On ILK+ LUT must be loaded before the pipe is running but with
4874 * clocks enabled
4875 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004876 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877
Paulo Zanoni1f544382012-10-24 11:32:00 -02004878 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004879 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304880 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004881
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004882 if (dev_priv->display.initial_watermarks != NULL)
4883 dev_priv->display.initial_watermarks(pipe_config);
4884 else
4885 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004886
4887 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4888 if (!intel_crtc->config->has_dsi_encoder)
4889 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004890
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004891 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004892 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004893
Jani Nikulaa65347b2015-11-27 12:21:46 +02004894 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004895 intel_ddi_set_vc_payload_alloc(crtc, true);
4896
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004897 assert_vblank_disabled(crtc);
4898 drm_crtc_vblank_on(crtc);
4899
Jani Nikula8807e552013-08-30 19:40:32 +03004900 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004902 intel_opregion_notify_encoder(encoder, true);
4903 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004904
Daniel Vetter6b698512015-11-28 11:05:39 +01004905 if (intel_crtc->config->has_pch_encoder) {
4906 intel_wait_for_vblank(dev, pipe);
4907 intel_wait_for_vblank(dev, pipe);
4908 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004909 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4910 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004911 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004912
Paulo Zanonie4916942013-09-20 16:21:19 -03004913 /* If we change the relative order between pipe/planes enabling, we need
4914 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004915 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4916 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4917 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4918 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4919 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920}
4921
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004922static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004923{
4924 struct drm_device *dev = crtc->base.dev;
4925 struct drm_i915_private *dev_priv = dev->dev_private;
4926 int pipe = crtc->pipe;
4927
4928 /* To avoid upsetting the power well on haswell only disable the pfit if
4929 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004930 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004931 I915_WRITE(PF_CTL(pipe), 0);
4932 I915_WRITE(PF_WIN_POS(pipe), 0);
4933 I915_WRITE(PF_WIN_SZ(pipe), 0);
4934 }
4935}
4936
Jesse Barnes6be4a602010-09-10 10:26:01 -07004937static void ironlake_crtc_disable(struct drm_crtc *crtc)
4938{
4939 struct drm_device *dev = crtc->dev;
4940 struct drm_i915_private *dev_priv = dev->dev_private;
4941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004942 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004943 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004944
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004945 if (intel_crtc->config->has_pch_encoder)
4946 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4947
Daniel Vetterea9d7582012-07-10 10:42:52 +02004948 for_each_encoder_on_crtc(dev, crtc, encoder)
4949 encoder->disable(encoder);
4950
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004951 drm_crtc_vblank_off(crtc);
4952 assert_vblank_disabled(crtc);
4953
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004954 /*
4955 * Sometimes spurious CPU pipe underruns happen when the
4956 * pipe is already disabled, but FDI RX/TX is still enabled.
4957 * Happens at least with VGA+HDMI cloning. Suppress them.
4958 */
4959 if (intel_crtc->config->has_pch_encoder)
4960 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4961
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004962 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004963
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004964 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004966 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004967 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004968 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4969 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004970
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004971 for_each_encoder_on_crtc(dev, crtc, encoder)
4972 if (encoder->post_disable)
4973 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004974
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004975 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004976 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004977
Daniel Vetterd925c592013-06-05 13:34:04 +02004978 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004979 i915_reg_t reg;
4980 u32 temp;
4981
Daniel Vetterd925c592013-06-05 13:34:04 +02004982 /* disable TRANS_DP_CTL */
4983 reg = TRANS_DP_CTL(pipe);
4984 temp = I915_READ(reg);
4985 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4986 TRANS_DP_PORT_SEL_MASK);
4987 temp |= TRANS_DP_PORT_SEL_NONE;
4988 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004989
Daniel Vetterd925c592013-06-05 13:34:04 +02004990 /* disable DPLL_SEL */
4991 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004992 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004993 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004994 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004995
Daniel Vetterd925c592013-06-05 13:34:04 +02004996 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004998
4999 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005000}
5001
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005002static void haswell_crtc_disable(struct drm_crtc *crtc)
5003{
5004 struct drm_device *dev = crtc->dev;
5005 struct drm_i915_private *dev_priv = dev->dev_private;
5006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5007 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005008 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005009
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005010 if (intel_crtc->config->has_pch_encoder)
5011 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5012 false);
5013
Jani Nikula8807e552013-08-30 19:40:32 +03005014 for_each_encoder_on_crtc(dev, crtc, encoder) {
5015 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005016 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005017 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005019 drm_crtc_vblank_off(crtc);
5020 assert_vblank_disabled(crtc);
5021
Jani Nikula4d1de972016-03-18 17:05:42 +02005022 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5023 if (!intel_crtc->config->has_dsi_encoder)
5024 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005026 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005027 intel_ddi_set_vc_payload_alloc(crtc, false);
5028
Jani Nikulaa65347b2015-11-27 12:21:46 +02005029 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305030 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005032 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005033 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005034 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005035 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Jani Nikulaa65347b2015-11-27 12:21:46 +02005037 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305038 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039
Imre Deak97b040a2014-06-25 22:01:50 +03005040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 if (encoder->post_disable)
5042 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005043
Ville Syrjälä92966a32015-12-08 16:05:48 +02005044 if (intel_crtc->config->has_pch_encoder) {
5045 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005046 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005047 intel_ddi_fdi_disable(crtc);
5048
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005049 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5050 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005051 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052}
5053
Jesse Barnes2dd24552013-04-25 12:55:01 -07005054static void i9xx_pfit_enable(struct intel_crtc *crtc)
5055{
5056 struct drm_device *dev = crtc->base.dev;
5057 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005058 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005059
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005060 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005061 return;
5062
Daniel Vetterc0b03412013-05-28 12:05:54 +02005063 /*
5064 * The panel fitter should only be adjusted whilst the pipe is disabled,
5065 * according to register description and PRM.
5066 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005067 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5068 assert_pipe_disabled(dev_priv, crtc->pipe);
5069
Jesse Barnesb074cec2013-04-25 12:55:02 -07005070 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5071 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005072
5073 /* Border color in case we don't scale up to the full screen. Black by
5074 * default, change to something else for debugging. */
5075 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005076}
5077
Dave Airlied05410f2014-06-05 13:22:59 +10005078static enum intel_display_power_domain port_to_power_domain(enum port port)
5079{
5080 switch (port) {
5081 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005082 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005083 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005084 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005085 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005086 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005087 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005088 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005089 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005090 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005091 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005092 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005093 return POWER_DOMAIN_PORT_OTHER;
5094 }
5095}
5096
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005097static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5098{
5099 switch (port) {
5100 case PORT_A:
5101 return POWER_DOMAIN_AUX_A;
5102 case PORT_B:
5103 return POWER_DOMAIN_AUX_B;
5104 case PORT_C:
5105 return POWER_DOMAIN_AUX_C;
5106 case PORT_D:
5107 return POWER_DOMAIN_AUX_D;
5108 case PORT_E:
5109 /* FIXME: Check VBT for actual wiring of PORT E */
5110 return POWER_DOMAIN_AUX_D;
5111 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005112 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005113 return POWER_DOMAIN_AUX_A;
5114 }
5115}
5116
Imre Deak319be8a2014-03-04 19:22:57 +02005117enum intel_display_power_domain
5118intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005119{
Imre Deak319be8a2014-03-04 19:22:57 +02005120 struct drm_device *dev = intel_encoder->base.dev;
5121 struct intel_digital_port *intel_dig_port;
5122
5123 switch (intel_encoder->type) {
5124 case INTEL_OUTPUT_UNKNOWN:
5125 /* Only DDI platforms should ever use this output type */
5126 WARN_ON_ONCE(!HAS_DDI(dev));
5127 case INTEL_OUTPUT_DISPLAYPORT:
5128 case INTEL_OUTPUT_HDMI:
5129 case INTEL_OUTPUT_EDP:
5130 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005131 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005132 case INTEL_OUTPUT_DP_MST:
5133 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5134 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005135 case INTEL_OUTPUT_ANALOG:
5136 return POWER_DOMAIN_PORT_CRT;
5137 case INTEL_OUTPUT_DSI:
5138 return POWER_DOMAIN_PORT_DSI;
5139 default:
5140 return POWER_DOMAIN_PORT_OTHER;
5141 }
5142}
5143
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005144enum intel_display_power_domain
5145intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5146{
5147 struct drm_device *dev = intel_encoder->base.dev;
5148 struct intel_digital_port *intel_dig_port;
5149
5150 switch (intel_encoder->type) {
5151 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005152 case INTEL_OUTPUT_HDMI:
5153 /*
5154 * Only DDI platforms should ever use these output types.
5155 * We can get here after the HDMI detect code has already set
5156 * the type of the shared encoder. Since we can't be sure
5157 * what's the status of the given connectors, play safe and
5158 * run the DP detection too.
5159 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005160 WARN_ON_ONCE(!HAS_DDI(dev));
5161 case INTEL_OUTPUT_DISPLAYPORT:
5162 case INTEL_OUTPUT_EDP:
5163 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5164 return port_to_aux_power_domain(intel_dig_port->port);
5165 case INTEL_OUTPUT_DP_MST:
5166 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5167 return port_to_aux_power_domain(intel_dig_port->port);
5168 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005169 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005170 return POWER_DOMAIN_AUX_A;
5171 }
5172}
5173
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005174static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5175 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005176{
5177 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005178 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5180 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005181 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005182 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005183
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005184 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005185 return 0;
5186
Imre Deak77d22dc2014-03-05 16:20:52 +02005187 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5188 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005189 if (crtc_state->pch_pfit.enabled ||
5190 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005191 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5192
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005193 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5194 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5195
Imre Deak319be8a2014-03-04 19:22:57 +02005196 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005197 }
Imre Deak319be8a2014-03-04 19:22:57 +02005198
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005199 if (crtc_state->shared_dpll)
5200 mask |= BIT(POWER_DOMAIN_PLLS);
5201
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 return mask;
5203}
5204
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005205static unsigned long
5206modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5207 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005208{
5209 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5211 enum intel_display_power_domain domain;
5212 unsigned long domains, new_domains, old_domains;
5213
5214 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215 intel_crtc->enabled_power_domains = new_domains =
5216 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005217
5218 domains = new_domains & ~old_domains;
5219
5220 for_each_power_domain(domain, domains)
5221 intel_display_power_get(dev_priv, domain);
5222
5223 return old_domains & ~new_domains;
5224}
5225
5226static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5227 unsigned long domains)
5228{
5229 enum intel_display_power_domain domain;
5230
5231 for_each_power_domain(domain, domains)
5232 intel_display_power_put(dev_priv, domain);
5233}
5234
Mika Kaholaadafdc62015-08-18 14:36:59 +03005235static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5236{
5237 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5238
5239 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5240 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5241 return max_cdclk_freq;
5242 else if (IS_CHERRYVIEW(dev_priv))
5243 return max_cdclk_freq*95/100;
5244 else if (INTEL_INFO(dev_priv)->gen < 4)
5245 return 2*max_cdclk_freq*90/100;
5246 else
5247 return max_cdclk_freq*90/100;
5248}
5249
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005250static void intel_update_max_cdclk(struct drm_device *dev)
5251{
5252 struct drm_i915_private *dev_priv = dev->dev_private;
5253
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005254 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005255 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5256
5257 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5258 dev_priv->max_cdclk_freq = 675000;
5259 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5260 dev_priv->max_cdclk_freq = 540000;
5261 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5262 dev_priv->max_cdclk_freq = 450000;
5263 else
5264 dev_priv->max_cdclk_freq = 337500;
5265 } else if (IS_BROADWELL(dev)) {
5266 /*
5267 * FIXME with extra cooling we can allow
5268 * 540 MHz for ULX and 675 Mhz for ULT.
5269 * How can we know if extra cooling is
5270 * available? PCI ID, VTB, something else?
5271 */
5272 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5273 dev_priv->max_cdclk_freq = 450000;
5274 else if (IS_BDW_ULX(dev))
5275 dev_priv->max_cdclk_freq = 450000;
5276 else if (IS_BDW_ULT(dev))
5277 dev_priv->max_cdclk_freq = 540000;
5278 else
5279 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005280 } else if (IS_CHERRYVIEW(dev)) {
5281 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005282 } else if (IS_VALLEYVIEW(dev)) {
5283 dev_priv->max_cdclk_freq = 400000;
5284 } else {
5285 /* otherwise assume cdclk is fixed */
5286 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5287 }
5288
Mika Kaholaadafdc62015-08-18 14:36:59 +03005289 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5290
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005291 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5292 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005293
5294 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5295 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005296}
5297
5298static void intel_update_cdclk(struct drm_device *dev)
5299{
5300 struct drm_i915_private *dev_priv = dev->dev_private;
5301
5302 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5303 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5304 dev_priv->cdclk_freq);
5305
5306 /*
5307 * Program the gmbus_freq based on the cdclk frequency.
5308 * BSpec erroneously claims we should aim for 4MHz, but
5309 * in fact 1MHz is the correct frequency.
5310 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005311 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005312 /*
5313 * Program the gmbus_freq based on the cdclk frequency.
5314 * BSpec erroneously claims we should aim for 4MHz, but
5315 * in fact 1MHz is the correct frequency.
5316 */
5317 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5318 }
5319
5320 if (dev_priv->max_cdclk_freq == 0)
5321 intel_update_max_cdclk(dev);
5322}
5323
Damien Lespiau70d0c572015-06-04 18:21:29 +01005324static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305325{
5326 struct drm_i915_private *dev_priv = dev->dev_private;
5327 uint32_t divider;
5328 uint32_t ratio;
5329 uint32_t current_freq;
5330 int ret;
5331
5332 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5333 switch (frequency) {
5334 case 144000:
5335 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5336 ratio = BXT_DE_PLL_RATIO(60);
5337 break;
5338 case 288000:
5339 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5340 ratio = BXT_DE_PLL_RATIO(60);
5341 break;
5342 case 384000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 576000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 624000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5352 ratio = BXT_DE_PLL_RATIO(65);
5353 break;
5354 case 19200:
5355 /*
5356 * Bypass frequency with DE PLL disabled. Init ratio, divider
5357 * to suppress GCC warning.
5358 */
5359 ratio = 0;
5360 divider = 0;
5361 break;
5362 default:
5363 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5364
5365 return;
5366 }
5367
5368 mutex_lock(&dev_priv->rps.hw_lock);
5369 /* Inform power controller of upcoming frequency change */
5370 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5371 0x80000000);
5372 mutex_unlock(&dev_priv->rps.hw_lock);
5373
5374 if (ret) {
5375 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5376 ret, frequency);
5377 return;
5378 }
5379
5380 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5381 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5382 current_freq = current_freq * 500 + 1000;
5383
5384 /*
5385 * DE PLL has to be disabled when
5386 * - setting to 19.2MHz (bypass, PLL isn't used)
5387 * - before setting to 624MHz (PLL needs toggling)
5388 * - before setting to any frequency from 624MHz (PLL needs toggling)
5389 */
5390 if (frequency == 19200 || frequency == 624000 ||
5391 current_freq == 624000) {
5392 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5393 /* Timeout 200us */
5394 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5395 1))
5396 DRM_ERROR("timout waiting for DE PLL unlock\n");
5397 }
5398
5399 if (frequency != 19200) {
5400 uint32_t val;
5401
5402 val = I915_READ(BXT_DE_PLL_CTL);
5403 val &= ~BXT_DE_PLL_RATIO_MASK;
5404 val |= ratio;
5405 I915_WRITE(BXT_DE_PLL_CTL, val);
5406
5407 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5408 /* Timeout 200us */
5409 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5410 DRM_ERROR("timeout waiting for DE PLL lock\n");
5411
5412 val = I915_READ(CDCLK_CTL);
5413 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5414 val |= divider;
5415 /*
5416 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5417 * enable otherwise.
5418 */
5419 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5420 if (frequency >= 500000)
5421 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5422
5423 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5424 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5425 val |= (frequency - 1000) / 500;
5426 I915_WRITE(CDCLK_CTL, val);
5427 }
5428
5429 mutex_lock(&dev_priv->rps.hw_lock);
5430 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5431 DIV_ROUND_UP(frequency, 25000));
5432 mutex_unlock(&dev_priv->rps.hw_lock);
5433
5434 if (ret) {
5435 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5436 ret, frequency);
5437 return;
5438 }
5439
Damien Lespiaua47871b2015-06-04 18:21:34 +01005440 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305441}
5442
5443void broxton_init_cdclk(struct drm_device *dev)
5444{
5445 struct drm_i915_private *dev_priv = dev->dev_private;
5446 uint32_t val;
5447
5448 /*
5449 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5450 * or else the reset will hang because there is no PCH to respond.
5451 * Move the handshake programming to initialization sequence.
5452 * Previously was left up to BIOS.
5453 */
5454 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5455 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5456 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5457
5458 /* Enable PG1 for cdclk */
5459 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5460
5461 /* check if cd clock is enabled */
5462 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5463 DRM_DEBUG_KMS("Display already initialized\n");
5464 return;
5465 }
5466
5467 /*
5468 * FIXME:
5469 * - The initial CDCLK needs to be read from VBT.
5470 * Need to make this change after VBT has changes for BXT.
5471 * - check if setting the max (or any) cdclk freq is really necessary
5472 * here, it belongs to modeset time
5473 */
5474 broxton_set_cdclk(dev, 624000);
5475
5476 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005477 POSTING_READ(DBUF_CTL);
5478
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305479 udelay(10);
5480
5481 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5482 DRM_ERROR("DBuf power enable timeout!\n");
5483}
5484
5485void broxton_uninit_cdclk(struct drm_device *dev)
5486{
5487 struct drm_i915_private *dev_priv = dev->dev_private;
5488
5489 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005490 POSTING_READ(DBUF_CTL);
5491
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 udelay(10);
5493
5494 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5495 DRM_ERROR("DBuf power disable timeout!\n");
5496
5497 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5498 broxton_set_cdclk(dev, 19200);
5499
5500 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5501}
5502
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005503static const struct skl_cdclk_entry {
5504 unsigned int freq;
5505 unsigned int vco;
5506} skl_cdclk_frequencies[] = {
5507 { .freq = 308570, .vco = 8640 },
5508 { .freq = 337500, .vco = 8100 },
5509 { .freq = 432000, .vco = 8640 },
5510 { .freq = 450000, .vco = 8100 },
5511 { .freq = 540000, .vco = 8100 },
5512 { .freq = 617140, .vco = 8640 },
5513 { .freq = 675000, .vco = 8100 },
5514};
5515
5516static unsigned int skl_cdclk_decimal(unsigned int freq)
5517{
5518 return (freq - 1000) / 500;
5519}
5520
5521static unsigned int skl_cdclk_get_vco(unsigned int freq)
5522{
5523 unsigned int i;
5524
5525 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5526 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5527
5528 if (e->freq == freq)
5529 return e->vco;
5530 }
5531
5532 return 8100;
5533}
5534
5535static void
5536skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5537{
5538 unsigned int min_freq;
5539 u32 val;
5540
5541 /* select the minimum CDCLK before enabling DPLL 0 */
5542 val = I915_READ(CDCLK_CTL);
5543 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5544 val |= CDCLK_FREQ_337_308;
5545
5546 if (required_vco == 8640)
5547 min_freq = 308570;
5548 else
5549 min_freq = 337500;
5550
5551 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5552
5553 I915_WRITE(CDCLK_CTL, val);
5554 POSTING_READ(CDCLK_CTL);
5555
5556 /*
5557 * We always enable DPLL0 with the lowest link rate possible, but still
5558 * taking into account the VCO required to operate the eDP panel at the
5559 * desired frequency. The usual DP link rates operate with a VCO of
5560 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5561 * The modeset code is responsible for the selection of the exact link
5562 * rate later on, with the constraint of choosing a frequency that
5563 * works with required_vco.
5564 */
5565 val = I915_READ(DPLL_CTRL1);
5566
5567 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5568 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5569 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5570 if (required_vco == 8640)
5571 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5572 SKL_DPLL0);
5573 else
5574 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5575 SKL_DPLL0);
5576
5577 I915_WRITE(DPLL_CTRL1, val);
5578 POSTING_READ(DPLL_CTRL1);
5579
5580 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5581
5582 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5583 DRM_ERROR("DPLL0 not locked\n");
5584}
5585
5586static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5587{
5588 int ret;
5589 u32 val;
5590
5591 /* inform PCU we want to change CDCLK */
5592 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5593 mutex_lock(&dev_priv->rps.hw_lock);
5594 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5595 mutex_unlock(&dev_priv->rps.hw_lock);
5596
5597 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5598}
5599
5600static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5601{
5602 unsigned int i;
5603
5604 for (i = 0; i < 15; i++) {
5605 if (skl_cdclk_pcu_ready(dev_priv))
5606 return true;
5607 udelay(10);
5608 }
5609
5610 return false;
5611}
5612
5613static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5614{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005615 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005616 u32 freq_select, pcu_ack;
5617
5618 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5619
5620 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5621 DRM_ERROR("failed to inform PCU about cdclk change\n");
5622 return;
5623 }
5624
5625 /* set CDCLK_CTL */
5626 switch(freq) {
5627 case 450000:
5628 case 432000:
5629 freq_select = CDCLK_FREQ_450_432;
5630 pcu_ack = 1;
5631 break;
5632 case 540000:
5633 freq_select = CDCLK_FREQ_540;
5634 pcu_ack = 2;
5635 break;
5636 case 308570:
5637 case 337500:
5638 default:
5639 freq_select = CDCLK_FREQ_337_308;
5640 pcu_ack = 0;
5641 break;
5642 case 617140:
5643 case 675000:
5644 freq_select = CDCLK_FREQ_675_617;
5645 pcu_ack = 3;
5646 break;
5647 }
5648
5649 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5650 POSTING_READ(CDCLK_CTL);
5651
5652 /* inform PCU of the change */
5653 mutex_lock(&dev_priv->rps.hw_lock);
5654 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5655 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005656
5657 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005658}
5659
5660void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5661{
5662 /* disable DBUF power */
5663 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5664 POSTING_READ(DBUF_CTL);
5665
5666 udelay(10);
5667
5668 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5669 DRM_ERROR("DBuf power disable timeout\n");
5670
Imre Deakab96c1ee2015-11-04 19:24:18 +02005671 /* disable DPLL0 */
5672 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5673 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5674 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005675}
5676
5677void skl_init_cdclk(struct drm_i915_private *dev_priv)
5678{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005679 unsigned int required_vco;
5680
Gary Wang39d9b852015-08-28 16:40:34 +08005681 /* DPLL0 not enabled (happens on early BIOS versions) */
5682 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5683 /* enable DPLL0 */
5684 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5685 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005686 }
5687
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005688 /* set CDCLK to the frequency the BIOS chose */
5689 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5690
5691 /* enable DBUF power */
5692 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5693 POSTING_READ(DBUF_CTL);
5694
5695 udelay(10);
5696
5697 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5698 DRM_ERROR("DBuf power enable timeout\n");
5699}
5700
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305701int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5702{
5703 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5704 uint32_t cdctl = I915_READ(CDCLK_CTL);
5705 int freq = dev_priv->skl_boot_cdclk;
5706
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305707 /*
5708 * check if the pre-os intialized the display
5709 * There is SWF18 scratchpad register defined which is set by the
5710 * pre-os which can be used by the OS drivers to check the status
5711 */
5712 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5713 goto sanitize;
5714
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305715 /* Is PLL enabled and locked ? */
5716 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5717 goto sanitize;
5718
5719 /* DPLL okay; verify the cdclock
5720 *
5721 * Noticed in some instances that the freq selection is correct but
5722 * decimal part is programmed wrong from BIOS where pre-os does not
5723 * enable display. Verify the same as well.
5724 */
5725 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5726 /* All well; nothing to sanitize */
5727 return false;
5728sanitize:
5729 /*
5730 * As of now initialize with max cdclk till
5731 * we get dynamic cdclk support
5732 * */
5733 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5734 skl_init_cdclk(dev_priv);
5735
5736 /* we did have to sanitize */
5737 return true;
5738}
5739
Jesse Barnes30a970c2013-11-04 13:48:12 -08005740/* Adjust CDclk dividers to allow high res or save power if possible */
5741static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5742{
5743 struct drm_i915_private *dev_priv = dev->dev_private;
5744 u32 val, cmd;
5745
Vandana Kannan164dfd22014-11-24 13:37:41 +05305746 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5747 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005748
Ville Syrjälädfcab172014-06-13 13:37:47 +03005749 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005750 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005751 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005752 cmd = 1;
5753 else
5754 cmd = 0;
5755
5756 mutex_lock(&dev_priv->rps.hw_lock);
5757 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5758 val &= ~DSPFREQGUAR_MASK;
5759 val |= (cmd << DSPFREQGUAR_SHIFT);
5760 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5761 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5762 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5763 50)) {
5764 DRM_ERROR("timed out waiting for CDclk change\n");
5765 }
5766 mutex_unlock(&dev_priv->rps.hw_lock);
5767
Ville Syrjälä54433e92015-05-26 20:42:31 +03005768 mutex_lock(&dev_priv->sb_lock);
5769
Ville Syrjälädfcab172014-06-13 13:37:47 +03005770 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005771 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005772
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005773 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005774
Jesse Barnes30a970c2013-11-04 13:48:12 -08005775 /* adjust cdclk divider */
5776 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005777 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005778 val |= divider;
5779 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005780
5781 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005782 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005783 50))
5784 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005785 }
5786
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787 /* adjust self-refresh exit latency value */
5788 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5789 val &= ~0x7f;
5790
5791 /*
5792 * For high bandwidth configs, we set a higher latency in the bunit
5793 * so that the core display fetch happens in time to avoid underruns.
5794 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005795 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796 val |= 4500 / 250; /* 4.5 usec */
5797 else
5798 val |= 3000 / 250; /* 3.0 usec */
5799 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005800
Ville Syrjäläa5805162015-05-26 20:42:30 +03005801 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005802
Ville Syrjäläb6283052015-06-03 15:45:07 +03005803 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804}
5805
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005806static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5807{
5808 struct drm_i915_private *dev_priv = dev->dev_private;
5809 u32 val, cmd;
5810
Vandana Kannan164dfd22014-11-24 13:37:41 +05305811 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5812 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005813
5814 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005815 case 333333:
5816 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005817 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005818 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005819 break;
5820 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005821 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822 return;
5823 }
5824
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005825 /*
5826 * Specs are full of misinformation, but testing on actual
5827 * hardware has shown that we just need to write the desired
5828 * CCK divider into the Punit register.
5829 */
5830 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5831
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005832 mutex_lock(&dev_priv->rps.hw_lock);
5833 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5834 val &= ~DSPFREQGUAR_MASK_CHV;
5835 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5836 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5837 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5838 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5839 50)) {
5840 DRM_ERROR("timed out waiting for CDclk change\n");
5841 }
5842 mutex_unlock(&dev_priv->rps.hw_lock);
5843
Ville Syrjäläb6283052015-06-03 15:45:07 +03005844 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005845}
5846
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5848 int max_pixclk)
5849{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005850 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005851 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005852
Jesse Barnes30a970c2013-11-04 13:48:12 -08005853 /*
5854 * Really only a few cases to deal with, as only 4 CDclks are supported:
5855 * 200MHz
5856 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005857 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005858 * 400MHz (VLV only)
5859 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5860 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005861 *
5862 * We seem to get an unstable or solid color picture at 200MHz.
5863 * Not sure what's wrong. For now use 200MHz only when all pipes
5864 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005865 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005866 if (!IS_CHERRYVIEW(dev_priv) &&
5867 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005868 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005869 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005870 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005871 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005872 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005873 else
5874 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005875}
5876
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305877static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5878 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005879{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305880 /*
5881 * FIXME:
5882 * - remove the guardband, it's not needed on BXT
5883 * - set 19.2MHz bypass frequency if there are no active pipes
5884 */
5885 if (max_pixclk > 576000*9/10)
5886 return 624000;
5887 else if (max_pixclk > 384000*9/10)
5888 return 576000;
5889 else if (max_pixclk > 288000*9/10)
5890 return 384000;
5891 else if (max_pixclk > 144000*9/10)
5892 return 288000;
5893 else
5894 return 144000;
5895}
5896
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005897/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005898static int intel_mode_max_pixclk(struct drm_device *dev,
5899 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005901 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903 struct drm_crtc *crtc;
5904 struct drm_crtc_state *crtc_state;
5905 unsigned max_pixclk = 0, i;
5906 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005907
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005908 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5909 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005910
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005911 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5912 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005913
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005914 if (crtc_state->enable)
5915 pixclk = crtc_state->adjusted_mode.crtc_clock;
5916
5917 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918 }
5919
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005920 for_each_pipe(dev_priv, pipe)
5921 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5922
Jesse Barnes30a970c2013-11-04 13:48:12 -08005923 return max_pixclk;
5924}
5925
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005926static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005927{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005928 struct drm_device *dev = state->dev;
5929 struct drm_i915_private *dev_priv = dev->dev_private;
5930 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005931 struct intel_atomic_state *intel_state =
5932 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005934 if (max_pixclk < 0)
5935 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005937 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005938 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305939
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005940 if (!intel_state->active_crtcs)
5941 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5942
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005943 return 0;
5944}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005945
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5947{
5948 struct drm_device *dev = state->dev;
5949 struct drm_i915_private *dev_priv = dev->dev_private;
5950 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005951 struct intel_atomic_state *intel_state =
5952 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005953
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005954 if (max_pixclk < 0)
5955 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005956
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005957 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005958 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005959
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005960 if (!intel_state->active_crtcs)
5961 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5962
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005963 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964}
5965
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005966static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5967{
5968 unsigned int credits, default_credits;
5969
5970 if (IS_CHERRYVIEW(dev_priv))
5971 default_credits = PFI_CREDIT(12);
5972 else
5973 default_credits = PFI_CREDIT(8);
5974
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005975 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005976 /* CHV suggested value is 31 or 63 */
5977 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005978 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005979 else
5980 credits = PFI_CREDIT(15);
5981 } else {
5982 credits = default_credits;
5983 }
5984
5985 /*
5986 * WA - write default credits before re-programming
5987 * FIXME: should we also set the resend bit here?
5988 */
5989 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5990 default_credits);
5991
5992 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5993 credits | PFI_CREDIT_RESEND);
5994
5995 /*
5996 * FIXME is this guaranteed to clear
5997 * immediately or should we poll for it?
5998 */
5999 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6000}
6001
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006002static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006003{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006004 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006005 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006006 struct intel_atomic_state *old_intel_state =
6007 to_intel_atomic_state(old_state);
6008 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006009
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006010 /*
6011 * FIXME: We can end up here with all power domains off, yet
6012 * with a CDCLK frequency other than the minimum. To account
6013 * for this take the PIPE-A power domain, which covers the HW
6014 * blocks needed for the following programming. This can be
6015 * removed once it's guaranteed that we get here either with
6016 * the minimum CDCLK set, or the required power domains
6017 * enabled.
6018 */
6019 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006020
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006021 if (IS_CHERRYVIEW(dev))
6022 cherryview_set_cdclk(dev, req_cdclk);
6023 else
6024 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006025
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006026 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006027
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006028 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006029}
6030
Jesse Barnes89b667f2013-04-18 14:51:36 -07006031static void valleyview_crtc_enable(struct drm_crtc *crtc)
6032{
6033 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006034 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6036 struct intel_encoder *encoder;
6037 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006039 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006040 return;
6041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006042 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306043 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006044
6045 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006046 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006047
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006048 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6049 struct drm_i915_private *dev_priv = dev->dev_private;
6050
6051 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6052 I915_WRITE(CHV_CANVAS(pipe), 0);
6053 }
6054
Daniel Vetter5b18e572014-04-24 23:55:06 +02006055 i9xx_set_pipeconf(intel_crtc);
6056
Jesse Barnes89b667f2013-04-18 14:51:36 -07006057 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058
Daniel Vettera72e4c92014-09-30 10:56:47 +02006059 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006060
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061 for_each_encoder_on_crtc(dev, crtc, encoder)
6062 if (encoder->pre_pll_enable)
6063 encoder->pre_pll_enable(encoder);
6064
Jani Nikulaa65347b2015-11-27 12:21:46 +02006065 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006066 if (IS_CHERRYVIEW(dev)) {
6067 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006068 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006069 } else {
6070 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006071 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006072 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006073 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074
6075 for_each_encoder_on_crtc(dev, crtc, encoder)
6076 if (encoder->pre_enable)
6077 encoder->pre_enable(encoder);
6078
Jesse Barnes2dd24552013-04-25 12:55:01 -07006079 i9xx_pfit_enable(intel_crtc);
6080
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006081 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006082
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006083 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006084 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006085
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006086 assert_vblank_disabled(crtc);
6087 drm_crtc_vblank_on(crtc);
6088
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006089 for_each_encoder_on_crtc(dev, crtc, encoder)
6090 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006091}
6092
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006093static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6094{
6095 struct drm_device *dev = crtc->base.dev;
6096 struct drm_i915_private *dev_priv = dev->dev_private;
6097
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006098 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6099 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006100}
6101
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006102static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006103{
6104 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006105 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006107 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006108 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006109
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006110 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006111 return;
6112
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006113 i9xx_set_pll_dividers(intel_crtc);
6114
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006115 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306116 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006117
6118 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006119 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006120
Daniel Vetter5b18e572014-04-24 23:55:06 +02006121 i9xx_set_pipeconf(intel_crtc);
6122
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006123 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006124
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006125 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006126 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006127
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006128 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006129 if (encoder->pre_enable)
6130 encoder->pre_enable(encoder);
6131
Daniel Vetterf6736a12013-06-05 13:34:30 +02006132 i9xx_enable_pll(intel_crtc);
6133
Jesse Barnes2dd24552013-04-25 12:55:01 -07006134 i9xx_pfit_enable(intel_crtc);
6135
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006136 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006137
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006138 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006139 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006140
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006141 assert_vblank_disabled(crtc);
6142 drm_crtc_vblank_on(crtc);
6143
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006146}
6147
Daniel Vetter87476d62013-04-11 16:29:06 +02006148static void i9xx_pfit_disable(struct intel_crtc *crtc)
6149{
6150 struct drm_device *dev = crtc->base.dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006153 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006154 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006155
6156 assert_pipe_disabled(dev_priv, crtc->pipe);
6157
Daniel Vetter328d8e82013-05-08 10:36:31 +02006158 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6159 I915_READ(PFIT_CONTROL));
6160 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006161}
6162
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006163static void i9xx_crtc_disable(struct drm_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006168 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006169 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006170
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006171 /*
6172 * On gen2 planes are double buffered but the pipe isn't, so we must
6173 * wait for planes to fully turn off before disabling the pipe.
6174 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006175 if (IS_GEN2(dev))
6176 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006177
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006178 for_each_encoder_on_crtc(dev, crtc, encoder)
6179 encoder->disable(encoder);
6180
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006181 drm_crtc_vblank_off(crtc);
6182 assert_vblank_disabled(crtc);
6183
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006184 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006185
Daniel Vetter87476d62013-04-11 16:29:06 +02006186 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006187
Jesse Barnes89b667f2013-04-18 14:51:36 -07006188 for_each_encoder_on_crtc(dev, crtc, encoder)
6189 if (encoder->post_disable)
6190 encoder->post_disable(encoder);
6191
Jani Nikulaa65347b2015-11-27 12:21:46 +02006192 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006193 if (IS_CHERRYVIEW(dev))
6194 chv_disable_pll(dev_priv, pipe);
6195 else if (IS_VALLEYVIEW(dev))
6196 vlv_disable_pll(dev_priv, pipe);
6197 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006198 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006199 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006200
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006201 for_each_encoder_on_crtc(dev, crtc, encoder)
6202 if (encoder->post_pll_disable)
6203 encoder->post_pll_disable(encoder);
6204
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006205 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006206 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006207}
6208
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006209static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006210{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006211 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006213 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006214 enum intel_display_power_domain domain;
6215 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006216
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006217 if (!intel_crtc->active)
6218 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006220 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006221 WARN_ON(intel_crtc->unpin_work);
6222
Ville Syrjälä2622a082016-03-09 19:07:26 +02006223 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006224
6225 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6226 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006227 }
6228
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006229 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006230
6231 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6232 crtc->base.id);
6233
6234 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6235 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006236 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006237 crtc->enabled = false;
6238 crtc->state->connector_mask = 0;
6239 crtc->state->encoder_mask = 0;
6240
6241 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6242 encoder->base.crtc = NULL;
6243
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006244 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006245 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006246 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006247
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006248 domains = intel_crtc->enabled_power_domains;
6249 for_each_power_domain(domain, domains)
6250 intel_display_power_put(dev_priv, domain);
6251 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006252
6253 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6254 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006255}
6256
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006257/*
6258 * turn all crtc's off, but do not adjust state
6259 * This has to be paired with a call to intel_modeset_setup_hw_state.
6260 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006261int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006262{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006263 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006264 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006265 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006266
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006267 state = drm_atomic_helper_suspend(dev);
6268 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006269 if (ret)
6270 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006271 else
6272 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006273 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006274}
6275
Chris Wilsonea5b2132010-08-04 13:50:23 +01006276void intel_encoder_destroy(struct drm_encoder *encoder)
6277{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006278 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006279
Chris Wilsonea5b2132010-08-04 13:50:23 +01006280 drm_encoder_cleanup(encoder);
6281 kfree(intel_encoder);
6282}
6283
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006284/* Cross check the actual hw state with our own modeset state tracking (and it's
6285 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006286static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006287{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006288 struct drm_crtc *crtc = connector->base.state->crtc;
6289
6290 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6291 connector->base.base.id,
6292 connector->base.name);
6293
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006294 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006295 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006296 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006297
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006298 I915_STATE_WARN(!crtc,
6299 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006300
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006301 if (!crtc)
6302 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006303
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006304 I915_STATE_WARN(!crtc->state->active,
6305 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006306
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006307 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006308 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006310 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006312
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006313 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006314 "attached encoder crtc differs from connector crtc\n");
6315 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006316 I915_STATE_WARN(crtc && crtc->state->active,
6317 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6319 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006320 }
6321}
6322
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006323int intel_connector_init(struct intel_connector *connector)
6324{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006325 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006326
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006327 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006328 return -ENOMEM;
6329
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006330 return 0;
6331}
6332
6333struct intel_connector *intel_connector_alloc(void)
6334{
6335 struct intel_connector *connector;
6336
6337 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6338 if (!connector)
6339 return NULL;
6340
6341 if (intel_connector_init(connector) < 0) {
6342 kfree(connector);
6343 return NULL;
6344 }
6345
6346 return connector;
6347}
6348
Daniel Vetterf0947c32012-07-02 13:10:34 +02006349/* Simple connector->get_hw_state implementation for encoders that support only
6350 * one connector and no cloning and hence the encoder state determines the state
6351 * of the connector. */
6352bool intel_connector_get_hw_state(struct intel_connector *connector)
6353{
Daniel Vetter24929352012-07-02 20:28:59 +02006354 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006355 struct intel_encoder *encoder = connector->encoder;
6356
6357 return encoder->get_hw_state(encoder, &pipe);
6358}
6359
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006360static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006361{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006362 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6363 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006364
6365 return 0;
6366}
6367
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006368static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006369 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006370{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006371 struct drm_atomic_state *state = pipe_config->base.state;
6372 struct intel_crtc *other_crtc;
6373 struct intel_crtc_state *other_crtc_state;
6374
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006375 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6376 pipe_name(pipe), pipe_config->fdi_lanes);
6377 if (pipe_config->fdi_lanes > 4) {
6378 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6379 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381 }
6382
Paulo Zanonibafb6552013-11-02 21:07:44 -07006383 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006384 if (pipe_config->fdi_lanes > 2) {
6385 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6386 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006390 }
6391 }
6392
6393 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395
6396 /* Ivybridge 3 pipe is really complicated */
6397 switch (pipe) {
6398 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006399 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006400 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 if (pipe_config->fdi_lanes <= 2)
6402 return 0;
6403
6404 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6405 other_crtc_state =
6406 intel_atomic_get_crtc_state(state, other_crtc);
6407 if (IS_ERR(other_crtc_state))
6408 return PTR_ERR(other_crtc_state);
6409
6410 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6412 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006413 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006414 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006417 if (pipe_config->fdi_lanes > 2) {
6418 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006421 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422
6423 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6424 other_crtc_state =
6425 intel_atomic_get_crtc_state(state, other_crtc);
6426 if (IS_ERR(other_crtc_state))
6427 return PTR_ERR(other_crtc_state);
6428
6429 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006431 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434 default:
6435 BUG();
6436 }
6437}
6438
Daniel Vettere29c22c2013-02-21 00:00:16 +01006439#define RETRY 1
6440static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006441 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006442{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006444 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 int lane, link_bw, fdi_dotclock, ret;
6446 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006447
Daniel Vettere29c22c2013-02-21 00:00:16 +01006448retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006449 /* FDI is a binary signal running at ~2.7GHz, encoding
6450 * each output octet as 10 bits. The actual frequency
6451 * is stored as a divider into a 100MHz clock, and the
6452 * mode pixel clock is stored in units of 1KHz.
6453 * Hence the bw of each lane in terms of the mode signal
6454 * is:
6455 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006456 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006457
Damien Lespiau241bfc32013-09-25 16:45:37 +01006458 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006459
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006460 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006461 pipe_config->pipe_bpp);
6462
6463 pipe_config->fdi_lanes = lane;
6464
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006465 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006467
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006468 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006470 pipe_config->pipe_bpp -= 2*3;
6471 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6472 pipe_config->pipe_bpp);
6473 needs_recompute = true;
6474 pipe_config->bw_constrained = true;
6475
6476 goto retry;
6477 }
6478
6479 if (needs_recompute)
6480 return RETRY;
6481
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006482 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006483}
6484
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006485static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6486 struct intel_crtc_state *pipe_config)
6487{
6488 if (pipe_config->pipe_bpp > 24)
6489 return false;
6490
6491 /* HSW can handle pixel rate up to cdclk? */
6492 if (IS_HASWELL(dev_priv->dev))
6493 return true;
6494
6495 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006496 * We compare against max which means we must take
6497 * the increased cdclk requirement into account when
6498 * calculating the new cdclk.
6499 *
6500 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006501 */
6502 return ilk_pipe_pixel_rate(pipe_config) <=
6503 dev_priv->max_cdclk_freq * 95 / 100;
6504}
6505
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006506static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006507 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006508{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006509 struct drm_device *dev = crtc->base.dev;
6510 struct drm_i915_private *dev_priv = dev->dev_private;
6511
Jani Nikulad330a952014-01-21 11:24:25 +02006512 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006513 hsw_crtc_supports_ips(crtc) &&
6514 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006515}
6516
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006517static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6518{
6519 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6520
6521 /* GDG double wide on either pipe, otherwise pipe A only */
6522 return INTEL_INFO(dev_priv)->gen < 4 &&
6523 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6524}
6525
Daniel Vettera43f6e02013-06-07 23:10:32 +02006526static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006527 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006528{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006529 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006530 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006531 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006532
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006533 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006534 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006535 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006536
6537 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006538 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006539 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006540 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006541 if (intel_crtc_supports_double_wide(crtc) &&
6542 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006543 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006544 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006545 }
6546
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006547 if (adjusted_mode->crtc_clock > clock_limit) {
6548 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6549 adjusted_mode->crtc_clock, clock_limit,
6550 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006551 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006552 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006553 }
Chris Wilson89749352010-09-12 18:25:19 +01006554
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006555 /*
6556 * Pipe horizontal size must be even in:
6557 * - DVO ganged mode
6558 * - LVDS dual channel mode
6559 * - Double wide pipe
6560 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006561 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006562 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6563 pipe_config->pipe_src_w &= ~1;
6564
Damien Lespiau8693a822013-05-03 18:48:11 +01006565 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6566 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006567 */
6568 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006569 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006570 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006571
Damien Lespiauf5adf942013-06-24 18:29:34 +01006572 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006573 hsw_compute_ips_config(crtc, pipe_config);
6574
Daniel Vetter877d48d2013-04-19 11:24:43 +02006575 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006576 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006577
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006578 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006579}
6580
Ville Syrjälä1652d192015-03-31 14:12:01 +03006581static int skylake_get_display_clock_speed(struct drm_device *dev)
6582{
6583 struct drm_i915_private *dev_priv = to_i915(dev);
6584 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6585 uint32_t cdctl = I915_READ(CDCLK_CTL);
6586 uint32_t linkrate;
6587
Damien Lespiau414355a2015-06-04 18:21:31 +01006588 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006589 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006590
6591 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6592 return 540000;
6593
6594 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006595 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006596
Damien Lespiau71cd8422015-04-30 16:39:17 +01006597 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6598 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006599 /* vco 8640 */
6600 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6601 case CDCLK_FREQ_450_432:
6602 return 432000;
6603 case CDCLK_FREQ_337_308:
6604 return 308570;
6605 case CDCLK_FREQ_675_617:
6606 return 617140;
6607 default:
6608 WARN(1, "Unknown cd freq selection\n");
6609 }
6610 } else {
6611 /* vco 8100 */
6612 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6613 case CDCLK_FREQ_450_432:
6614 return 450000;
6615 case CDCLK_FREQ_337_308:
6616 return 337500;
6617 case CDCLK_FREQ_675_617:
6618 return 675000;
6619 default:
6620 WARN(1, "Unknown cd freq selection\n");
6621 }
6622 }
6623
6624 /* error case, do as if DPLL0 isn't enabled */
6625 return 24000;
6626}
6627
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006628static int broxton_get_display_clock_speed(struct drm_device *dev)
6629{
6630 struct drm_i915_private *dev_priv = to_i915(dev);
6631 uint32_t cdctl = I915_READ(CDCLK_CTL);
6632 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6633 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6634 int cdclk;
6635
6636 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6637 return 19200;
6638
6639 cdclk = 19200 * pll_ratio / 2;
6640
6641 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6642 case BXT_CDCLK_CD2X_DIV_SEL_1:
6643 return cdclk; /* 576MHz or 624MHz */
6644 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6645 return cdclk * 2 / 3; /* 384MHz */
6646 case BXT_CDCLK_CD2X_DIV_SEL_2:
6647 return cdclk / 2; /* 288MHz */
6648 case BXT_CDCLK_CD2X_DIV_SEL_4:
6649 return cdclk / 4; /* 144MHz */
6650 }
6651
6652 /* error case, do as if DE PLL isn't enabled */
6653 return 19200;
6654}
6655
Ville Syrjälä1652d192015-03-31 14:12:01 +03006656static int broadwell_get_display_clock_speed(struct drm_device *dev)
6657{
6658 struct drm_i915_private *dev_priv = dev->dev_private;
6659 uint32_t lcpll = I915_READ(LCPLL_CTL);
6660 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6661
6662 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6663 return 800000;
6664 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6665 return 450000;
6666 else if (freq == LCPLL_CLK_FREQ_450)
6667 return 450000;
6668 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6669 return 540000;
6670 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6671 return 337500;
6672 else
6673 return 675000;
6674}
6675
6676static int haswell_get_display_clock_speed(struct drm_device *dev)
6677{
6678 struct drm_i915_private *dev_priv = dev->dev_private;
6679 uint32_t lcpll = I915_READ(LCPLL_CTL);
6680 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6681
6682 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6683 return 800000;
6684 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6685 return 450000;
6686 else if (freq == LCPLL_CLK_FREQ_450)
6687 return 450000;
6688 else if (IS_HSW_ULT(dev))
6689 return 337500;
6690 else
6691 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006692}
6693
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006694static int valleyview_get_display_clock_speed(struct drm_device *dev)
6695{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006696 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6697 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006698}
6699
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006700static int ilk_get_display_clock_speed(struct drm_device *dev)
6701{
6702 return 450000;
6703}
6704
Jesse Barnese70236a2009-09-21 10:42:27 -07006705static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006706{
Jesse Barnese70236a2009-09-21 10:42:27 -07006707 return 400000;
6708}
Jesse Barnes79e53942008-11-07 14:24:08 -08006709
Jesse Barnese70236a2009-09-21 10:42:27 -07006710static int i915_get_display_clock_speed(struct drm_device *dev)
6711{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006712 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006713}
Jesse Barnes79e53942008-11-07 14:24:08 -08006714
Jesse Barnese70236a2009-09-21 10:42:27 -07006715static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6716{
6717 return 200000;
6718}
Jesse Barnes79e53942008-11-07 14:24:08 -08006719
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006720static int pnv_get_display_clock_speed(struct drm_device *dev)
6721{
6722 u16 gcfgc = 0;
6723
6724 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6725
6726 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6727 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006728 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006729 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006730 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006731 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006732 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006733 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6734 return 200000;
6735 default:
6736 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6737 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006738 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006739 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006740 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006741 }
6742}
6743
Jesse Barnese70236a2009-09-21 10:42:27 -07006744static int i915gm_get_display_clock_speed(struct drm_device *dev)
6745{
6746 u16 gcfgc = 0;
6747
6748 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6749
6750 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006751 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006752 else {
6753 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6754 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006755 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006756 default:
6757 case GC_DISPLAY_CLOCK_190_200_MHZ:
6758 return 190000;
6759 }
6760 }
6761}
Jesse Barnes79e53942008-11-07 14:24:08 -08006762
Jesse Barnese70236a2009-09-21 10:42:27 -07006763static int i865_get_display_clock_speed(struct drm_device *dev)
6764{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006765 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006766}
6767
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006768static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006769{
6770 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006771
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006772 /*
6773 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6774 * encoding is different :(
6775 * FIXME is this the right way to detect 852GM/852GMV?
6776 */
6777 if (dev->pdev->revision == 0x1)
6778 return 133333;
6779
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006780 pci_bus_read_config_word(dev->pdev->bus,
6781 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6782
Jesse Barnese70236a2009-09-21 10:42:27 -07006783 /* Assume that the hardware is in the high speed state. This
6784 * should be the default.
6785 */
6786 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6787 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006788 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006789 case GC_CLOCK_100_200:
6790 return 200000;
6791 case GC_CLOCK_166_250:
6792 return 250000;
6793 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006794 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006795 case GC_CLOCK_133_266:
6796 case GC_CLOCK_133_266_2:
6797 case GC_CLOCK_166_266:
6798 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006799 }
6800
6801 /* Shouldn't happen */
6802 return 0;
6803}
6804
6805static int i830_get_display_clock_speed(struct drm_device *dev)
6806{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006807 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006808}
6809
Ville Syrjälä34edce22015-05-22 11:22:33 +03006810static unsigned int intel_hpll_vco(struct drm_device *dev)
6811{
6812 struct drm_i915_private *dev_priv = dev->dev_private;
6813 static const unsigned int blb_vco[8] = {
6814 [0] = 3200000,
6815 [1] = 4000000,
6816 [2] = 5333333,
6817 [3] = 4800000,
6818 [4] = 6400000,
6819 };
6820 static const unsigned int pnv_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 4800000,
6825 [4] = 2666667,
6826 };
6827 static const unsigned int cl_vco[8] = {
6828 [0] = 3200000,
6829 [1] = 4000000,
6830 [2] = 5333333,
6831 [3] = 6400000,
6832 [4] = 3333333,
6833 [5] = 3566667,
6834 [6] = 4266667,
6835 };
6836 static const unsigned int elk_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 };
6842 static const unsigned int ctg_vco[8] = {
6843 [0] = 3200000,
6844 [1] = 4000000,
6845 [2] = 5333333,
6846 [3] = 6400000,
6847 [4] = 2666667,
6848 [5] = 4266667,
6849 };
6850 const unsigned int *vco_table;
6851 unsigned int vco;
6852 uint8_t tmp = 0;
6853
6854 /* FIXME other chipsets? */
6855 if (IS_GM45(dev))
6856 vco_table = ctg_vco;
6857 else if (IS_G4X(dev))
6858 vco_table = elk_vco;
6859 else if (IS_CRESTLINE(dev))
6860 vco_table = cl_vco;
6861 else if (IS_PINEVIEW(dev))
6862 vco_table = pnv_vco;
6863 else if (IS_G33(dev))
6864 vco_table = blb_vco;
6865 else
6866 return 0;
6867
6868 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6869
6870 vco = vco_table[tmp & 0x7];
6871 if (vco == 0)
6872 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6873 else
6874 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6875
6876 return vco;
6877}
6878
6879static int gm45_get_display_clock_speed(struct drm_device *dev)
6880{
6881 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6882 uint16_t tmp = 0;
6883
6884 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6885
6886 cdclk_sel = (tmp >> 12) & 0x1;
6887
6888 switch (vco) {
6889 case 2666667:
6890 case 4000000:
6891 case 5333333:
6892 return cdclk_sel ? 333333 : 222222;
6893 case 3200000:
6894 return cdclk_sel ? 320000 : 228571;
6895 default:
6896 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6897 return 222222;
6898 }
6899}
6900
6901static int i965gm_get_display_clock_speed(struct drm_device *dev)
6902{
6903 static const uint8_t div_3200[] = { 16, 10, 8 };
6904 static const uint8_t div_4000[] = { 20, 12, 10 };
6905 static const uint8_t div_5333[] = { 24, 16, 14 };
6906 const uint8_t *div_table;
6907 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6908 uint16_t tmp = 0;
6909
6910 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6911
6912 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6913
6914 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6915 goto fail;
6916
6917 switch (vco) {
6918 case 3200000:
6919 div_table = div_3200;
6920 break;
6921 case 4000000:
6922 div_table = div_4000;
6923 break;
6924 case 5333333:
6925 div_table = div_5333;
6926 break;
6927 default:
6928 goto fail;
6929 }
6930
6931 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6932
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006933fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006934 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6935 return 200000;
6936}
6937
6938static int g33_get_display_clock_speed(struct drm_device *dev)
6939{
6940 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6941 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6942 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6943 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6944 const uint8_t *div_table;
6945 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6946 uint16_t tmp = 0;
6947
6948 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6949
6950 cdclk_sel = (tmp >> 4) & 0x7;
6951
6952 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6953 goto fail;
6954
6955 switch (vco) {
6956 case 3200000:
6957 div_table = div_3200;
6958 break;
6959 case 4000000:
6960 div_table = div_4000;
6961 break;
6962 case 4800000:
6963 div_table = div_4800;
6964 break;
6965 case 5333333:
6966 div_table = div_5333;
6967 break;
6968 default:
6969 goto fail;
6970 }
6971
6972 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6973
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006974fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006975 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6976 return 190476;
6977}
6978
Zhenyu Wang2c072452009-06-05 15:38:42 +08006979static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006980intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006981{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006982 while (*num > DATA_LINK_M_N_MASK ||
6983 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006984 *num >>= 1;
6985 *den >>= 1;
6986 }
6987}
6988
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006989static void compute_m_n(unsigned int m, unsigned int n,
6990 uint32_t *ret_m, uint32_t *ret_n)
6991{
6992 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6993 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6994 intel_reduce_m_n_ratio(ret_m, ret_n);
6995}
6996
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006997void
6998intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6999 int pixel_clock, int link_clock,
7000 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007001{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007002 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007003
7004 compute_m_n(bits_per_pixel * pixel_clock,
7005 link_clock * nlanes * 8,
7006 &m_n->gmch_m, &m_n->gmch_n);
7007
7008 compute_m_n(pixel_clock, link_clock,
7009 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007010}
7011
Chris Wilsona7615032011-01-12 17:04:08 +00007012static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7013{
Jani Nikulad330a952014-01-21 11:24:25 +02007014 if (i915.panel_use_ssc >= 0)
7015 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007016 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007017 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007018}
7019
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007020static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007021{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007022 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007023}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007024
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007025static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7026{
7027 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007028}
7029
Daniel Vetterf47709a2013-03-28 10:42:02 +01007030static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007031 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007032 intel_clock_t *reduced_clock)
7033{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007034 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007035 u32 fp, fp2 = 0;
7036
7037 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007038 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007039 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007040 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007041 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007042 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007043 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007044 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007045 }
7046
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007047 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048
Daniel Vetterf47709a2013-03-28 10:42:02 +01007049 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007050 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007051 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007052 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007053 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007054 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007055 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007056 }
7057}
7058
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007059static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7060 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007061{
7062 u32 reg_val;
7063
7064 /*
7065 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7066 * and set it to a reasonable value instead.
7067 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007068 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007069 reg_val &= 0xffffff00;
7070 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007071 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007072
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007073 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007074 reg_val &= 0x8cffffff;
7075 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007076 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007077
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007078 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007079 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007080 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007081
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007082 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007083 reg_val &= 0x00ffffff;
7084 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086}
7087
Daniel Vetterb5518422013-05-03 11:49:48 +02007088static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7089 struct intel_link_m_n *m_n)
7090{
7091 struct drm_device *dev = crtc->base.dev;
7092 struct drm_i915_private *dev_priv = dev->dev_private;
7093 int pipe = crtc->pipe;
7094
Daniel Vettere3b95f12013-05-03 11:49:49 +02007095 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7096 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7097 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7098 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007099}
7100
7101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007102 struct intel_link_m_n *m_n,
7103 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007104{
7105 struct drm_device *dev = crtc->base.dev;
7106 struct drm_i915_private *dev_priv = dev->dev_private;
7107 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007108 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007109
7110 if (INTEL_INFO(dev)->gen >= 5) {
7111 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7112 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7113 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7114 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007115 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7116 * for gen < 8) and if DRRS is supported (to make sure the
7117 * registers are not unnecessarily accessed).
7118 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307119 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007120 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007121 I915_WRITE(PIPE_DATA_M2(transcoder),
7122 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7123 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7124 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7125 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7126 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007127 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007128 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7129 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7130 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7131 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007132 }
7133}
7134
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307135void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007136{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307137 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7138
7139 if (m_n == M1_N1) {
7140 dp_m_n = &crtc->config->dp_m_n;
7141 dp_m2_n2 = &crtc->config->dp_m2_n2;
7142 } else if (m_n == M2_N2) {
7143
7144 /*
7145 * M2_N2 registers are not supported. Hence m2_n2 divider value
7146 * needs to be programmed into M1_N1.
7147 */
7148 dp_m_n = &crtc->config->dp_m2_n2;
7149 } else {
7150 DRM_ERROR("Unsupported divider value\n");
7151 return;
7152 }
7153
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007154 if (crtc->config->has_pch_encoder)
7155 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007156 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307157 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007158}
7159
Daniel Vetter251ac862015-06-18 10:30:24 +02007160static void vlv_compute_dpll(struct intel_crtc *crtc,
7161 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007162{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007163 u32 dpll, dpll_md;
7164
7165 /*
7166 * Enable DPIO clock input. We should never disable the reference
7167 * clock for pipe B, since VGA hotplug / manual detection depends
7168 * on it.
7169 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007170 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7171 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007172 /* We should never disable this, set it here for state tracking */
7173 if (crtc->pipe == PIPE_B)
7174 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7175 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007176 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007177
Ville Syrjäläd288f652014-10-28 13:20:22 +02007178 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007179 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007180 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007181}
7182
Ville Syrjäläd288f652014-10-28 13:20:22 +02007183static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007184 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007185{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007186 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007187 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007188 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007189 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007190 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007191 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007192
Ville Syrjäläa5805162015-05-26 20:42:30 +03007193 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007194
Ville Syrjäläd288f652014-10-28 13:20:22 +02007195 bestn = pipe_config->dpll.n;
7196 bestm1 = pipe_config->dpll.m1;
7197 bestm2 = pipe_config->dpll.m2;
7198 bestp1 = pipe_config->dpll.p1;
7199 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007200
Jesse Barnes89b667f2013-04-18 14:51:36 -07007201 /* See eDP HDMI DPIO driver vbios notes doc */
7202
7203 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007204 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007205 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007206
7207 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007208 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007209
7210 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007211 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007212 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007213 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007214
7215 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007216 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007217
7218 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007219 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7220 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7221 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007222 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007223
7224 /*
7225 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7226 * but we don't support that).
7227 * Note: don't use the DAC post divider as it seems unstable.
7228 */
7229 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007230 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007231
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007232 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007234
Jesse Barnes89b667f2013-04-18 14:51:36 -07007235 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007236 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007237 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7238 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007240 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007241 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007244
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007245 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007246 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007247 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007249 0x0df40000);
7250 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007252 0x0df70000);
7253 } else { /* HDMI or VGA */
7254 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007255 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007257 0x0df70000);
7258 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007260 0x0df40000);
7261 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007262
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007263 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007265 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7266 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007269
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007270 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007271 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272}
7273
Daniel Vetter251ac862015-06-18 10:30:24 +02007274static void chv_compute_dpll(struct intel_crtc *crtc,
7275 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007276{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007277 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7278 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007279 DPLL_VCO_ENABLE;
7280 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007281 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007282
Ville Syrjäläd288f652014-10-28 13:20:22 +02007283 pipe_config->dpll_hw_state.dpll_md =
7284 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007285}
7286
Ville Syrjäläd288f652014-10-28 13:20:22 +02007287static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007288 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007289{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007290 struct drm_device *dev = crtc->base.dev;
7291 struct drm_i915_private *dev_priv = dev->dev_private;
7292 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007293 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007294 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307295 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007296 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307297 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307298 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007299
Ville Syrjäläd288f652014-10-28 13:20:22 +02007300 bestn = pipe_config->dpll.n;
7301 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7302 bestm1 = pipe_config->dpll.m1;
7303 bestm2 = pipe_config->dpll.m2 >> 22;
7304 bestp1 = pipe_config->dpll.p1;
7305 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307306 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307307 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307308 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007309
7310 /*
7311 * Enable Refclk and SSC
7312 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007313 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007314 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007315
Ville Syrjäläa5805162015-05-26 20:42:30 +03007316 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007317
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007318 /* p1 and p2 divider */
7319 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7320 5 << DPIO_CHV_S1_DIV_SHIFT |
7321 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7322 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7323 1 << DPIO_CHV_K_DIV_SHIFT);
7324
7325 /* Feedback post-divider - m2 */
7326 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7327
7328 /* Feedback refclk divider - n and m1 */
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7330 DPIO_CHV_M1_DIV_BY_2 |
7331 1 << DPIO_CHV_N_DIV_SHIFT);
7332
7333 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007334 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007335
7336 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307337 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7338 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7339 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7340 if (bestm2_frac)
7341 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7342 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007343
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307344 /* Program digital lock detect threshold */
7345 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7346 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7347 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7348 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7349 if (!bestm2_frac)
7350 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7351 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7352
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007353 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307354 if (vco == 5400000) {
7355 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7356 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7357 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7358 tribuf_calcntr = 0x9;
7359 } else if (vco <= 6200000) {
7360 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7361 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7362 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7363 tribuf_calcntr = 0x9;
7364 } else if (vco <= 6480000) {
7365 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7366 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7367 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7368 tribuf_calcntr = 0x8;
7369 } else {
7370 /* Not supported. Apply the same limits as in the max case */
7371 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7372 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7373 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7374 tribuf_calcntr = 0;
7375 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7377
Ville Syrjälä968040b2015-03-11 22:52:08 +02007378 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307379 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7380 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7382
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007383 /* AFC Recal */
7384 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7385 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7386 DPIO_AFC_RECAL);
7387
Ville Syrjäläa5805162015-05-26 20:42:30 +03007388 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007389}
7390
Ville Syrjäläd288f652014-10-28 13:20:22 +02007391/**
7392 * vlv_force_pll_on - forcibly enable just the PLL
7393 * @dev_priv: i915 private structure
7394 * @pipe: pipe PLL to enable
7395 * @dpll: PLL configuration
7396 *
7397 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7398 * in cases where we need the PLL enabled even when @pipe is not going to
7399 * be enabled.
7400 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007401int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7402 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007403{
7404 struct intel_crtc *crtc =
7405 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007406 struct intel_crtc_state *pipe_config;
7407
7408 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7409 if (!pipe_config)
7410 return -ENOMEM;
7411
7412 pipe_config->base.crtc = &crtc->base;
7413 pipe_config->pixel_multiplier = 1;
7414 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007415
7416 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007417 chv_compute_dpll(crtc, pipe_config);
7418 chv_prepare_pll(crtc, pipe_config);
7419 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007420 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007421 vlv_compute_dpll(crtc, pipe_config);
7422 vlv_prepare_pll(crtc, pipe_config);
7423 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007424 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007425
7426 kfree(pipe_config);
7427
7428 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007429}
7430
7431/**
7432 * vlv_force_pll_off - forcibly disable just the PLL
7433 * @dev_priv: i915 private structure
7434 * @pipe: pipe PLL to disable
7435 *
7436 * Disable the PLL for @pipe. To be used in cases where we need
7437 * the PLL enabled even when @pipe is not going to be enabled.
7438 */
7439void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7440{
7441 if (IS_CHERRYVIEW(dev))
7442 chv_disable_pll(to_i915(dev), pipe);
7443 else
7444 vlv_disable_pll(to_i915(dev), pipe);
7445}
7446
Daniel Vetter251ac862015-06-18 10:30:24 +02007447static void i9xx_compute_dpll(struct intel_crtc *crtc,
7448 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007449 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007450{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007451 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007452 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453 u32 dpll;
7454 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007455 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007456
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007457 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307458
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007459 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7460 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007461
7462 dpll = DPLL_VGA_MODE_DIS;
7463
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007464 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007465 dpll |= DPLLB_MODE_LVDS;
7466 else
7467 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007468
Daniel Vetteref1b4602013-06-01 17:17:04 +02007469 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007470 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007471 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007472 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007473
7474 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007475 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007476
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007477 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007478 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007479
7480 /* compute bitmask from p1 value */
7481 if (IS_PINEVIEW(dev))
7482 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7483 else {
7484 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7485 if (IS_G4X(dev) && reduced_clock)
7486 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7487 }
7488 switch (clock->p2) {
7489 case 5:
7490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7491 break;
7492 case 7:
7493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7494 break;
7495 case 10:
7496 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7497 break;
7498 case 14:
7499 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7500 break;
7501 }
7502 if (INTEL_INFO(dev)->gen >= 4)
7503 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7504
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007505 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007506 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007507 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007508 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7510 else
7511 dpll |= PLL_REF_INPUT_DREFCLK;
7512
7513 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007514 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007515
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007516 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007517 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007518 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007519 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007520 }
7521}
7522
Daniel Vetter251ac862015-06-18 10:30:24 +02007523static void i8xx_compute_dpll(struct intel_crtc *crtc,
7524 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007525 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007527 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007531
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307533
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534 dpll = DPLL_VGA_MODE_DIS;
7535
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007536 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7538 } else {
7539 if (clock->p1 == 2)
7540 dpll |= PLL_P1_DIVIDE_BY_TWO;
7541 else
7542 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7543 if (clock->p2 == 4)
7544 dpll |= PLL_P2_DIVIDE_BY_4;
7545 }
7546
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007547 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007548 dpll |= DPLL_DVO_2X_MODE;
7549
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007550 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007551 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007552 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7553 else
7554 dpll |= PLL_REF_INPUT_DREFCLK;
7555
7556 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007557 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007558}
7559
Daniel Vetter8a654f32013-06-01 17:16:22 +02007560static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007561{
7562 struct drm_device *dev = intel_crtc->base.dev;
7563 struct drm_i915_private *dev_priv = dev->dev_private;
7564 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007565 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007566 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007567 uint32_t crtc_vtotal, crtc_vblank_end;
7568 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007569
7570 /* We need to be careful not to changed the adjusted mode, for otherwise
7571 * the hw state checker will get angry at the mismatch. */
7572 crtc_vtotal = adjusted_mode->crtc_vtotal;
7573 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007574
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007575 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007576 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007577 crtc_vtotal -= 1;
7578 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007579
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007580 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007581 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7582 else
7583 vsyncshift = adjusted_mode->crtc_hsync_start -
7584 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007585 if (vsyncshift < 0)
7586 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007587 }
7588
7589 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007590 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007591
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007592 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007593 (adjusted_mode->crtc_hdisplay - 1) |
7594 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007595 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007596 (adjusted_mode->crtc_hblank_start - 1) |
7597 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007598 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007599 (adjusted_mode->crtc_hsync_start - 1) |
7600 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7601
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007602 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007603 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007604 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007605 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007606 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007607 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007608 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007609 (adjusted_mode->crtc_vsync_start - 1) |
7610 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7611
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007612 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7613 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7614 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7615 * bits. */
7616 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7617 (pipe == PIPE_B || pipe == PIPE_C))
7618 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7619
Jani Nikulabc58be62016-03-18 17:05:39 +02007620}
7621
7622static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7623{
7624 struct drm_device *dev = intel_crtc->base.dev;
7625 struct drm_i915_private *dev_priv = dev->dev_private;
7626 enum pipe pipe = intel_crtc->pipe;
7627
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007628 /* pipesrc controls the size that is scaled from, which should
7629 * always be the user's requested size.
7630 */
7631 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007632 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7633 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007634}
7635
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007636static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007637 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007638{
7639 struct drm_device *dev = crtc->base.dev;
7640 struct drm_i915_private *dev_priv = dev->dev_private;
7641 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7642 uint32_t tmp;
7643
7644 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007645 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7646 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007647 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007648 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7649 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007651 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7652 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007653
7654 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007655 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7656 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007657 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007658 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7659 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007660 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007661 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7662 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007663
7664 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007665 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7666 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7667 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007668 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007669}
7670
7671static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7672 struct intel_crtc_state *pipe_config)
7673{
7674 struct drm_device *dev = crtc->base.dev;
7675 struct drm_i915_private *dev_priv = dev->dev_private;
7676 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677
7678 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007679 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7680 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7681
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007682 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7683 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007684}
7685
Daniel Vetterf6a83282014-02-11 15:28:57 -08007686void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007687 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007688{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007689 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7690 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7691 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7692 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007693
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007694 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7695 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7696 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7697 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007698
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007699 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007700 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007701
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007702 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7703 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007704
7705 mode->hsync = drm_mode_hsync(mode);
7706 mode->vrefresh = drm_mode_vrefresh(mode);
7707 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007708}
7709
Daniel Vetter84b046f2013-02-19 18:48:54 +01007710static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7711{
7712 struct drm_device *dev = intel_crtc->base.dev;
7713 struct drm_i915_private *dev_priv = dev->dev_private;
7714 uint32_t pipeconf;
7715
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007716 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007717
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007718 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7719 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7720 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007721
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007722 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007723 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007724
Daniel Vetterff9ce462013-04-24 14:57:17 +02007725 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007726 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007727 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007728 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007729 pipeconf |= PIPECONF_DITHER_EN |
7730 PIPECONF_DITHER_TYPE_SP;
7731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007732 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007733 case 18:
7734 pipeconf |= PIPECONF_6BPC;
7735 break;
7736 case 24:
7737 pipeconf |= PIPECONF_8BPC;
7738 break;
7739 case 30:
7740 pipeconf |= PIPECONF_10BPC;
7741 break;
7742 default:
7743 /* Case prevented by intel_choose_pipe_bpp_dither. */
7744 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007745 }
7746 }
7747
7748 if (HAS_PIPE_CXSR(dev)) {
7749 if (intel_crtc->lowfreq_avail) {
7750 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7751 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7752 } else {
7753 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007754 }
7755 }
7756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007757 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007758 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007759 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007760 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7761 else
7762 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7763 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007764 pipeconf |= PIPECONF_PROGRESSIVE;
7765
Wayne Boyer666a4532015-12-09 12:29:35 -08007766 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7767 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007768 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007769
Daniel Vetter84b046f2013-02-19 18:48:54 +01007770 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7771 POSTING_READ(PIPECONF(intel_crtc->pipe));
7772}
7773
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007774static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7775 struct intel_crtc_state *crtc_state)
7776{
7777 struct drm_device *dev = crtc->base.dev;
7778 struct drm_i915_private *dev_priv = dev->dev_private;
7779 const intel_limit_t *limit;
7780 int refclk = 48000;
7781
7782 memset(&crtc_state->dpll_hw_state, 0,
7783 sizeof(crtc_state->dpll_hw_state));
7784
7785 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7786 if (intel_panel_use_ssc(dev_priv)) {
7787 refclk = dev_priv->vbt.lvds_ssc_freq;
7788 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7789 }
7790
7791 limit = &intel_limits_i8xx_lvds;
7792 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7793 limit = &intel_limits_i8xx_dvo;
7794 } else {
7795 limit = &intel_limits_i8xx_dac;
7796 }
7797
7798 if (!crtc_state->clock_set &&
7799 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7800 refclk, NULL, &crtc_state->dpll)) {
7801 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7802 return -EINVAL;
7803 }
7804
7805 i8xx_compute_dpll(crtc, crtc_state, NULL);
7806
7807 return 0;
7808}
7809
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007810static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7811 struct intel_crtc_state *crtc_state)
7812{
7813 struct drm_device *dev = crtc->base.dev;
7814 struct drm_i915_private *dev_priv = dev->dev_private;
7815 const intel_limit_t *limit;
7816 int refclk = 96000;
7817
7818 memset(&crtc_state->dpll_hw_state, 0,
7819 sizeof(crtc_state->dpll_hw_state));
7820
7821 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7822 if (intel_panel_use_ssc(dev_priv)) {
7823 refclk = dev_priv->vbt.lvds_ssc_freq;
7824 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7825 }
7826
7827 if (intel_is_dual_link_lvds(dev))
7828 limit = &intel_limits_g4x_dual_channel_lvds;
7829 else
7830 limit = &intel_limits_g4x_single_channel_lvds;
7831 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7832 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7833 limit = &intel_limits_g4x_hdmi;
7834 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7835 limit = &intel_limits_g4x_sdvo;
7836 } else {
7837 /* The option is for other outputs */
7838 limit = &intel_limits_i9xx_sdvo;
7839 }
7840
7841 if (!crtc_state->clock_set &&
7842 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7843 refclk, NULL, &crtc_state->dpll)) {
7844 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7845 return -EINVAL;
7846 }
7847
7848 i9xx_compute_dpll(crtc, crtc_state, NULL);
7849
7850 return 0;
7851}
7852
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007853static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7854 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007855{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007856 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007857 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007858 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007859 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007860
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007861 memset(&crtc_state->dpll_hw_state, 0,
7862 sizeof(crtc_state->dpll_hw_state));
7863
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007864 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7865 if (intel_panel_use_ssc(dev_priv)) {
7866 refclk = dev_priv->vbt.lvds_ssc_freq;
7867 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7868 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007869
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007870 limit = &intel_limits_pineview_lvds;
7871 } else {
7872 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007873 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007874
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007875 if (!crtc_state->clock_set &&
7876 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7877 refclk, NULL, &crtc_state->dpll)) {
7878 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7879 return -EINVAL;
7880 }
7881
7882 i9xx_compute_dpll(crtc, crtc_state, NULL);
7883
7884 return 0;
7885}
7886
7887static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7888 struct intel_crtc_state *crtc_state)
7889{
7890 struct drm_device *dev = crtc->base.dev;
7891 struct drm_i915_private *dev_priv = dev->dev_private;
7892 const intel_limit_t *limit;
7893 int refclk = 96000;
7894
7895 memset(&crtc_state->dpll_hw_state, 0,
7896 sizeof(crtc_state->dpll_hw_state));
7897
7898 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7899 if (intel_panel_use_ssc(dev_priv)) {
7900 refclk = dev_priv->vbt.lvds_ssc_freq;
7901 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007902 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007903
7904 limit = &intel_limits_i9xx_lvds;
7905 } else {
7906 limit = &intel_limits_i9xx_sdvo;
7907 }
7908
7909 if (!crtc_state->clock_set &&
7910 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7911 refclk, NULL, &crtc_state->dpll)) {
7912 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7913 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007914 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007915
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007916 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007917
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007918 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007919}
7920
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007921static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7922 struct intel_crtc_state *crtc_state)
7923{
7924 int refclk = 100000;
7925 const intel_limit_t *limit = &intel_limits_chv;
7926
7927 memset(&crtc_state->dpll_hw_state, 0,
7928 sizeof(crtc_state->dpll_hw_state));
7929
7930 if (crtc_state->has_dsi_encoder)
7931 return 0;
7932
7933 if (!crtc_state->clock_set &&
7934 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7935 refclk, NULL, &crtc_state->dpll)) {
7936 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7937 return -EINVAL;
7938 }
7939
7940 chv_compute_dpll(crtc, crtc_state);
7941
7942 return 0;
7943}
7944
7945static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7946 struct intel_crtc_state *crtc_state)
7947{
7948 int refclk = 100000;
7949 const intel_limit_t *limit = &intel_limits_vlv;
7950
7951 memset(&crtc_state->dpll_hw_state, 0,
7952 sizeof(crtc_state->dpll_hw_state));
7953
7954 if (crtc_state->has_dsi_encoder)
7955 return 0;
7956
7957 if (!crtc_state->clock_set &&
7958 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7959 refclk, NULL, &crtc_state->dpll)) {
7960 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7961 return -EINVAL;
7962 }
7963
7964 vlv_compute_dpll(crtc, crtc_state);
7965
7966 return 0;
7967}
7968
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007969static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007970 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007971{
7972 struct drm_device *dev = crtc->base.dev;
7973 struct drm_i915_private *dev_priv = dev->dev_private;
7974 uint32_t tmp;
7975
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007976 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7977 return;
7978
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007979 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007980 if (!(tmp & PFIT_ENABLE))
7981 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007982
Daniel Vetter06922822013-07-11 13:35:40 +02007983 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984 if (INTEL_INFO(dev)->gen < 4) {
7985 if (crtc->pipe != PIPE_B)
7986 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007987 } else {
7988 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7989 return;
7990 }
7991
Daniel Vetter06922822013-07-11 13:35:40 +02007992 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007993 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7994 if (INTEL_INFO(dev)->gen < 5)
7995 pipe_config->gmch_pfit.lvds_border_bits =
7996 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7997}
7998
Jesse Barnesacbec812013-09-20 11:29:32 -07007999static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008000 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008001{
8002 struct drm_device *dev = crtc->base.dev;
8003 struct drm_i915_private *dev_priv = dev->dev_private;
8004 int pipe = pipe_config->cpu_transcoder;
8005 intel_clock_t clock;
8006 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008007 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008008
Shobhit Kumarf573de52014-07-30 20:32:37 +05308009 /* In case of MIPI DPLL will not even be used */
8010 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8011 return;
8012
Ville Syrjäläa5805162015-05-26 20:42:30 +03008013 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008014 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008015 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008016
8017 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8018 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8019 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8020 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8021 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8022
Imre Deakdccbea32015-06-22 23:35:51 +03008023 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008024}
8025
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008026static void
8027i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8028 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008029{
8030 struct drm_device *dev = crtc->base.dev;
8031 struct drm_i915_private *dev_priv = dev->dev_private;
8032 u32 val, base, offset;
8033 int pipe = crtc->pipe, plane = crtc->plane;
8034 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008035 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008036 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008037 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008038
Damien Lespiau42a7b082015-02-05 19:35:13 +00008039 val = I915_READ(DSPCNTR(plane));
8040 if (!(val & DISPLAY_PLANE_ENABLE))
8041 return;
8042
Damien Lespiaud9806c92015-01-21 14:07:19 +00008043 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008044 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008045 DRM_DEBUG_KMS("failed to alloc fb\n");
8046 return;
8047 }
8048
Damien Lespiau1b842c82015-01-21 13:50:54 +00008049 fb = &intel_fb->base;
8050
Daniel Vetter18c52472015-02-10 17:16:09 +00008051 if (INTEL_INFO(dev)->gen >= 4) {
8052 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008053 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008054 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8055 }
8056 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008057
8058 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008059 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008060 fb->pixel_format = fourcc;
8061 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008062
8063 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008064 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008065 offset = I915_READ(DSPTILEOFF(plane));
8066 else
8067 offset = I915_READ(DSPLINOFF(plane));
8068 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8069 } else {
8070 base = I915_READ(DSPADDR(plane));
8071 }
8072 plane_config->base = base;
8073
8074 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008075 fb->width = ((val >> 16) & 0xfff) + 1;
8076 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008077
8078 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008079 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008080
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008081 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008082 fb->pixel_format,
8083 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008084
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008085 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008086
Damien Lespiau2844a922015-01-20 12:51:48 +00008087 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8088 pipe_name(pipe), plane, fb->width, fb->height,
8089 fb->bits_per_pixel, base, fb->pitches[0],
8090 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008091
Damien Lespiau2d140302015-02-05 17:22:18 +00008092 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008093}
8094
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008095static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008096 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008097{
8098 struct drm_device *dev = crtc->base.dev;
8099 struct drm_i915_private *dev_priv = dev->dev_private;
8100 int pipe = pipe_config->cpu_transcoder;
8101 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8102 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008103 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008104 int refclk = 100000;
8105
Ville Syrjäläa5805162015-05-26 20:42:30 +03008106 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008107 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8108 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8109 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8110 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008111 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008112 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008113
8114 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008115 clock.m2 = (pll_dw0 & 0xff) << 22;
8116 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8117 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008118 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8119 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8120 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8121
Imre Deakdccbea32015-06-22 23:35:51 +03008122 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008123}
8124
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008125static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008126 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008127{
8128 struct drm_device *dev = crtc->base.dev;
8129 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008130 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008131 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008132 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008133
Imre Deak17290502016-02-12 18:55:11 +02008134 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8135 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008136 return false;
8137
Daniel Vettere143a212013-07-04 12:01:15 +02008138 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008139 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008140
Imre Deak17290502016-02-12 18:55:11 +02008141 ret = false;
8142
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008143 tmp = I915_READ(PIPECONF(crtc->pipe));
8144 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008145 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008146
Wayne Boyer666a4532015-12-09 12:29:35 -08008147 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008148 switch (tmp & PIPECONF_BPC_MASK) {
8149 case PIPECONF_6BPC:
8150 pipe_config->pipe_bpp = 18;
8151 break;
8152 case PIPECONF_8BPC:
8153 pipe_config->pipe_bpp = 24;
8154 break;
8155 case PIPECONF_10BPC:
8156 pipe_config->pipe_bpp = 30;
8157 break;
8158 default:
8159 break;
8160 }
8161 }
8162
Wayne Boyer666a4532015-12-09 12:29:35 -08008163 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8164 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008165 pipe_config->limited_color_range = true;
8166
Ville Syrjälä282740f2013-09-04 18:30:03 +03008167 if (INTEL_INFO(dev)->gen < 4)
8168 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8169
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008170 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008171 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008172
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008173 i9xx_get_pfit_config(crtc, pipe_config);
8174
Daniel Vetter6c49f242013-06-06 12:45:25 +02008175 if (INTEL_INFO(dev)->gen >= 4) {
8176 tmp = I915_READ(DPLL_MD(crtc->pipe));
8177 pipe_config->pixel_multiplier =
8178 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8179 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008180 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008181 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8182 tmp = I915_READ(DPLL(crtc->pipe));
8183 pipe_config->pixel_multiplier =
8184 ((tmp & SDVO_MULTIPLIER_MASK)
8185 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8186 } else {
8187 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8188 * port and will be fixed up in the encoder->get_config
8189 * function. */
8190 pipe_config->pixel_multiplier = 1;
8191 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008192 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008193 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008194 /*
8195 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8196 * on 830. Filter it out here so that we don't
8197 * report errors due to that.
8198 */
8199 if (IS_I830(dev))
8200 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8201
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008202 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8203 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008204 } else {
8205 /* Mask out read-only status bits. */
8206 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8207 DPLL_PORTC_READY_MASK |
8208 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008209 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008210
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008211 if (IS_CHERRYVIEW(dev))
8212 chv_crtc_clock_get(crtc, pipe_config);
8213 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008214 vlv_crtc_clock_get(crtc, pipe_config);
8215 else
8216 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008217
Ville Syrjälä0f646142015-08-26 19:39:18 +03008218 /*
8219 * Normally the dotclock is filled in by the encoder .get_config()
8220 * but in case the pipe is enabled w/o any ports we need a sane
8221 * default.
8222 */
8223 pipe_config->base.adjusted_mode.crtc_clock =
8224 pipe_config->port_clock / pipe_config->pixel_multiplier;
8225
Imre Deak17290502016-02-12 18:55:11 +02008226 ret = true;
8227
8228out:
8229 intel_display_power_put(dev_priv, power_domain);
8230
8231 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008232}
8233
Paulo Zanonidde86e22012-12-01 12:04:25 -02008234static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008235{
8236 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008237 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008238 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008239 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008240 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008241 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008242 bool has_ck505 = false;
8243 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008244
8245 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008246 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008247 switch (encoder->type) {
8248 case INTEL_OUTPUT_LVDS:
8249 has_panel = true;
8250 has_lvds = true;
8251 break;
8252 case INTEL_OUTPUT_EDP:
8253 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008254 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008255 has_cpu_edp = true;
8256 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008257 default:
8258 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008259 }
8260 }
8261
Keith Packard99eb6a02011-09-26 14:29:12 -07008262 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008263 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008264 can_ssc = has_ck505;
8265 } else {
8266 has_ck505 = false;
8267 can_ssc = true;
8268 }
8269
Imre Deak2de69052013-05-08 13:14:04 +03008270 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8271 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008272
8273 /* Ironlake: try to setup display ref clock before DPLL
8274 * enabling. This is only under driver's control after
8275 * PCH B stepping, previous chipset stepping should be
8276 * ignoring this setting.
8277 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008279
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008280 /* As we must carefully and slowly disable/enable each source in turn,
8281 * compute the final state we want first and check if we need to
8282 * make any changes at all.
8283 */
8284 final = val;
8285 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008286 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008288 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008289 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8290
8291 final &= ~DREF_SSC_SOURCE_MASK;
8292 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8293 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008294
Keith Packard199e5d72011-09-22 12:01:57 -07008295 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008296 final |= DREF_SSC_SOURCE_ENABLE;
8297
8298 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8299 final |= DREF_SSC1_ENABLE;
8300
8301 if (has_cpu_edp) {
8302 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8303 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8304 else
8305 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8306 } else
8307 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8308 } else {
8309 final |= DREF_SSC_SOURCE_DISABLE;
8310 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8311 }
8312
8313 if (final == val)
8314 return;
8315
8316 /* Always enable nonspread source */
8317 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8318
8319 if (has_ck505)
8320 val |= DREF_NONSPREAD_CK505_ENABLE;
8321 else
8322 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8323
8324 if (has_panel) {
8325 val &= ~DREF_SSC_SOURCE_MASK;
8326 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008327
Keith Packard199e5d72011-09-22 12:01:57 -07008328 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008329 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008330 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008331 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008332 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008333 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008334
8335 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008336 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008337 POSTING_READ(PCH_DREF_CONTROL);
8338 udelay(200);
8339
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008340 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008341
8342 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008343 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008344 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008345 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008346 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008347 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008348 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008349 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008350 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008351
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008352 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008353 POSTING_READ(PCH_DREF_CONTROL);
8354 udelay(200);
8355 } else {
8356 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8357
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008358 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008359
8360 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008361 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008362
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008363 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008364 POSTING_READ(PCH_DREF_CONTROL);
8365 udelay(200);
8366
8367 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008368 val &= ~DREF_SSC_SOURCE_MASK;
8369 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008370
8371 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008372 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008373
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008374 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008375 POSTING_READ(PCH_DREF_CONTROL);
8376 udelay(200);
8377 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008378
8379 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008380}
8381
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008382static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008383{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008384 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008385
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008386 tmp = I915_READ(SOUTH_CHICKEN2);
8387 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8388 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008389
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008390 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8391 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8392 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008393
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008394 tmp = I915_READ(SOUTH_CHICKEN2);
8395 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8396 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008397
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008398 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8399 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8400 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008401}
8402
8403/* WaMPhyProgramming:hsw */
8404static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8405{
8406 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008407
8408 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8409 tmp &= ~(0xFF << 24);
8410 tmp |= (0x12 << 24);
8411 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8412
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8414 tmp |= (1 << 11);
8415 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8416
8417 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8418 tmp |= (1 << 11);
8419 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8420
Paulo Zanonidde86e22012-12-01 12:04:25 -02008421 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8422 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8423 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8424
8425 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8426 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8427 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8428
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008429 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8430 tmp &= ~(7 << 13);
8431 tmp |= (5 << 13);
8432 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008433
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008434 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8435 tmp &= ~(7 << 13);
8436 tmp |= (5 << 13);
8437 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008438
8439 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8440 tmp &= ~0xFF;
8441 tmp |= 0x1C;
8442 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8443
8444 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8445 tmp &= ~0xFF;
8446 tmp |= 0x1C;
8447 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8448
8449 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8450 tmp &= ~(0xFF << 16);
8451 tmp |= (0x1C << 16);
8452 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8453
8454 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8455 tmp &= ~(0xFF << 16);
8456 tmp |= (0x1C << 16);
8457 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8458
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008459 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8460 tmp |= (1 << 27);
8461 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008462
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008463 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8464 tmp |= (1 << 27);
8465 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008466
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008467 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8468 tmp &= ~(0xF << 28);
8469 tmp |= (4 << 28);
8470 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008472 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8473 tmp &= ~(0xF << 28);
8474 tmp |= (4 << 28);
8475 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008476}
8477
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008478/* Implements 3 different sequences from BSpec chapter "Display iCLK
8479 * Programming" based on the parameters passed:
8480 * - Sequence to enable CLKOUT_DP
8481 * - Sequence to enable CLKOUT_DP without spread
8482 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8483 */
8484static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8485 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008486{
8487 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008488 uint32_t reg, tmp;
8489
8490 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8491 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008492 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008493 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008494
Ville Syrjäläa5805162015-05-26 20:42:30 +03008495 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008496
8497 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8498 tmp &= ~SBI_SSCCTL_DISABLE;
8499 tmp |= SBI_SSCCTL_PATHALT;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8501
8502 udelay(24);
8503
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008504 if (with_spread) {
8505 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8506 tmp &= ~SBI_SSCCTL_PATHALT;
8507 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008508
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008509 if (with_fdi) {
8510 lpt_reset_fdi_mphy(dev_priv);
8511 lpt_program_fdi_mphy(dev_priv);
8512 }
8513 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514
Ville Syrjäläc2699522015-08-27 23:55:59 +03008515 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008516 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8517 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8518 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008519
Ville Syrjäläa5805162015-05-26 20:42:30 +03008520 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008521}
8522
Paulo Zanoni47701c32013-07-23 11:19:25 -03008523/* Sequence to disable CLKOUT_DP */
8524static void lpt_disable_clkout_dp(struct drm_device *dev)
8525{
8526 struct drm_i915_private *dev_priv = dev->dev_private;
8527 uint32_t reg, tmp;
8528
Ville Syrjäläa5805162015-05-26 20:42:30 +03008529 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008530
Ville Syrjäläc2699522015-08-27 23:55:59 +03008531 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008532 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8533 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8534 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8535
8536 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8537 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8538 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8539 tmp |= SBI_SSCCTL_PATHALT;
8540 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8541 udelay(32);
8542 }
8543 tmp |= SBI_SSCCTL_DISABLE;
8544 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8545 }
8546
Ville Syrjäläa5805162015-05-26 20:42:30 +03008547 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008548}
8549
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008550#define BEND_IDX(steps) ((50 + (steps)) / 5)
8551
8552static const uint16_t sscdivintphase[] = {
8553 [BEND_IDX( 50)] = 0x3B23,
8554 [BEND_IDX( 45)] = 0x3B23,
8555 [BEND_IDX( 40)] = 0x3C23,
8556 [BEND_IDX( 35)] = 0x3C23,
8557 [BEND_IDX( 30)] = 0x3D23,
8558 [BEND_IDX( 25)] = 0x3D23,
8559 [BEND_IDX( 20)] = 0x3E23,
8560 [BEND_IDX( 15)] = 0x3E23,
8561 [BEND_IDX( 10)] = 0x3F23,
8562 [BEND_IDX( 5)] = 0x3F23,
8563 [BEND_IDX( 0)] = 0x0025,
8564 [BEND_IDX( -5)] = 0x0025,
8565 [BEND_IDX(-10)] = 0x0125,
8566 [BEND_IDX(-15)] = 0x0125,
8567 [BEND_IDX(-20)] = 0x0225,
8568 [BEND_IDX(-25)] = 0x0225,
8569 [BEND_IDX(-30)] = 0x0325,
8570 [BEND_IDX(-35)] = 0x0325,
8571 [BEND_IDX(-40)] = 0x0425,
8572 [BEND_IDX(-45)] = 0x0425,
8573 [BEND_IDX(-50)] = 0x0525,
8574};
8575
8576/*
8577 * Bend CLKOUT_DP
8578 * steps -50 to 50 inclusive, in steps of 5
8579 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8580 * change in clock period = -(steps / 10) * 5.787 ps
8581 */
8582static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8583{
8584 uint32_t tmp;
8585 int idx = BEND_IDX(steps);
8586
8587 if (WARN_ON(steps % 5 != 0))
8588 return;
8589
8590 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8591 return;
8592
8593 mutex_lock(&dev_priv->sb_lock);
8594
8595 if (steps % 10 != 0)
8596 tmp = 0xAAAAAAAB;
8597 else
8598 tmp = 0x00000000;
8599 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8600
8601 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8602 tmp &= 0xffff0000;
8603 tmp |= sscdivintphase[idx];
8604 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8605
8606 mutex_unlock(&dev_priv->sb_lock);
8607}
8608
8609#undef BEND_IDX
8610
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008611static void lpt_init_pch_refclk(struct drm_device *dev)
8612{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008613 struct intel_encoder *encoder;
8614 bool has_vga = false;
8615
Damien Lespiaub2784e12014-08-05 11:29:37 +01008616 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008617 switch (encoder->type) {
8618 case INTEL_OUTPUT_ANALOG:
8619 has_vga = true;
8620 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008621 default:
8622 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008623 }
8624 }
8625
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008626 if (has_vga) {
8627 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008628 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008629 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008630 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008631 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008632}
8633
Paulo Zanonidde86e22012-12-01 12:04:25 -02008634/*
8635 * Initialize reference clocks when the driver loads
8636 */
8637void intel_init_pch_refclk(struct drm_device *dev)
8638{
8639 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8640 ironlake_init_pch_refclk(dev);
8641 else if (HAS_PCH_LPT(dev))
8642 lpt_init_pch_refclk(dev);
8643}
8644
Daniel Vetter6ff93602013-04-19 11:24:36 +02008645static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008646{
8647 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8649 int pipe = intel_crtc->pipe;
8650 uint32_t val;
8651
Daniel Vetter78114072013-06-13 00:54:57 +02008652 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008653
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008654 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008655 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008656 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008657 break;
8658 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008659 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008660 break;
8661 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008662 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008663 break;
8664 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008665 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008666 break;
8667 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008668 /* Case prevented by intel_choose_pipe_bpp_dither. */
8669 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008670 }
8671
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008672 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008673 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8674
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008675 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008676 val |= PIPECONF_INTERLACED_ILK;
8677 else
8678 val |= PIPECONF_PROGRESSIVE;
8679
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008680 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008681 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008682
Paulo Zanonic8203562012-09-12 10:06:29 -03008683 I915_WRITE(PIPECONF(pipe), val);
8684 POSTING_READ(PIPECONF(pipe));
8685}
8686
Daniel Vetter6ff93602013-04-19 11:24:36 +02008687static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008688{
Jani Nikula391bf042016-03-18 17:05:40 +02008689 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008691 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008692 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008693
Jani Nikula391bf042016-03-18 17:05:40 +02008694 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008695 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8696
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008697 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008698 val |= PIPECONF_INTERLACED_ILK;
8699 else
8700 val |= PIPECONF_PROGRESSIVE;
8701
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008702 I915_WRITE(PIPECONF(cpu_transcoder), val);
8703 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008704}
8705
Jani Nikula391bf042016-03-18 17:05:40 +02008706static void haswell_set_pipemisc(struct drm_crtc *crtc)
8707{
8708 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8710
8711 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8712 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008713
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008714 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008715 case 18:
8716 val |= PIPEMISC_DITHER_6_BPC;
8717 break;
8718 case 24:
8719 val |= PIPEMISC_DITHER_8_BPC;
8720 break;
8721 case 30:
8722 val |= PIPEMISC_DITHER_10_BPC;
8723 break;
8724 case 36:
8725 val |= PIPEMISC_DITHER_12_BPC;
8726 break;
8727 default:
8728 /* Case prevented by pipe_config_set_bpp. */
8729 BUG();
8730 }
8731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008732 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008733 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8734
Jani Nikula391bf042016-03-18 17:05:40 +02008735 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008736 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008737}
8738
Paulo Zanonid4b19312012-11-29 11:29:32 -02008739int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8740{
8741 /*
8742 * Account for spread spectrum to avoid
8743 * oversubscribing the link. Max center spread
8744 * is 2.5%; use 5% for safety's sake.
8745 */
8746 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008747 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008748}
8749
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008750static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008751{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008752 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008753}
8754
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008755static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8756 struct intel_crtc_state *crtc_state,
8757 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008758{
8759 struct drm_crtc *crtc = &intel_crtc->base;
8760 struct drm_device *dev = crtc->dev;
8761 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008762 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008763 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008764 struct drm_connector_state *connector_state;
8765 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008766 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008767 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008768 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008769
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008770 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008771 if (connector_state->crtc != crtc_state->base.crtc)
8772 continue;
8773
8774 encoder = to_intel_encoder(connector_state->best_encoder);
8775
8776 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008777 case INTEL_OUTPUT_LVDS:
8778 is_lvds = true;
8779 break;
8780 case INTEL_OUTPUT_SDVO:
8781 case INTEL_OUTPUT_HDMI:
8782 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008783 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008784 default:
8785 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008786 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008787 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008788
Chris Wilsonc1858122010-12-03 21:35:48 +00008789 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008790 factor = 21;
8791 if (is_lvds) {
8792 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008793 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008794 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008795 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008796 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008797 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008798
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008799 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008800
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008801 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8802 fp |= FP_CB_TUNE;
8803
8804 if (reduced_clock) {
8805 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8806
8807 if (reduced_clock->m < factor * reduced_clock->n)
8808 fp2 |= FP_CB_TUNE;
8809 } else {
8810 fp2 = fp;
8811 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008812
Chris Wilson5eddb702010-09-11 13:48:45 +01008813 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008814
Eric Anholta07d6782011-03-30 13:01:08 -07008815 if (is_lvds)
8816 dpll |= DPLLB_MODE_LVDS;
8817 else
8818 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008819
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008820 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008821 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008822
8823 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008824 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008825 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008826 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008827
Eric Anholta07d6782011-03-30 13:01:08 -07008828 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008829 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008830 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008831 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008832
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008833 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008834 case 5:
8835 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8836 break;
8837 case 7:
8838 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8839 break;
8840 case 10:
8841 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8842 break;
8843 case 14:
8844 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8845 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846 }
8847
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008848 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008849 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008850 else
8851 dpll |= PLL_REF_INPUT_DREFCLK;
8852
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008853 dpll |= DPLL_VCO_ENABLE;
8854
8855 crtc_state->dpll_hw_state.dpll = dpll;
8856 crtc_state->dpll_hw_state.fp0 = fp;
8857 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008858}
8859
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008860static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8861 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008862{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008863 struct drm_device *dev = crtc->base.dev;
8864 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008865 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008866 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008867 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008868 const intel_limit_t *limit;
8869 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008870
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008871 memset(&crtc_state->dpll_hw_state, 0,
8872 sizeof(crtc_state->dpll_hw_state));
8873
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008874 crtc->lowfreq_avail = false;
8875
8876 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8877 if (!crtc_state->has_pch_encoder)
8878 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008879
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008880 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8881 if (intel_panel_use_ssc(dev_priv)) {
8882 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8883 dev_priv->vbt.lvds_ssc_freq);
8884 refclk = dev_priv->vbt.lvds_ssc_freq;
8885 }
8886
8887 if (intel_is_dual_link_lvds(dev)) {
8888 if (refclk == 100000)
8889 limit = &intel_limits_ironlake_dual_lvds_100m;
8890 else
8891 limit = &intel_limits_ironlake_dual_lvds;
8892 } else {
8893 if (refclk == 100000)
8894 limit = &intel_limits_ironlake_single_lvds_100m;
8895 else
8896 limit = &intel_limits_ironlake_single_lvds;
8897 }
8898 } else {
8899 limit = &intel_limits_ironlake_dac;
8900 }
8901
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008902 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008903 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8904 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008905 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8906 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008907 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008908
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008909 ironlake_compute_dpll(crtc, crtc_state,
8910 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008911
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008912 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8913 if (pll == NULL) {
8914 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8915 pipe_name(crtc->pipe));
8916 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008917 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008918
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008919 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8920 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008921 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008922
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008923 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008924}
8925
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008926static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8927 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008928{
8929 struct drm_device *dev = crtc->base.dev;
8930 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008931 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008932
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008933 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8934 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8935 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8936 & ~TU_SIZE_MASK;
8937 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8938 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8939 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8940}
8941
8942static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8943 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008944 struct intel_link_m_n *m_n,
8945 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008946{
8947 struct drm_device *dev = crtc->base.dev;
8948 struct drm_i915_private *dev_priv = dev->dev_private;
8949 enum pipe pipe = crtc->pipe;
8950
8951 if (INTEL_INFO(dev)->gen >= 5) {
8952 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8953 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8954 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8955 & ~TU_SIZE_MASK;
8956 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8957 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8958 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008959 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8960 * gen < 8) and if DRRS is supported (to make sure the
8961 * registers are not unnecessarily read).
8962 */
8963 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008964 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008965 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8966 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8967 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8968 & ~TU_SIZE_MASK;
8969 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8970 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8971 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8972 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008973 } else {
8974 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8975 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8976 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8977 & ~TU_SIZE_MASK;
8978 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8979 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8980 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8981 }
8982}
8983
8984void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008985 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008986{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008987 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008988 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8989 else
8990 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008991 &pipe_config->dp_m_n,
8992 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008993}
8994
Daniel Vetter72419202013-04-04 13:28:53 +02008995static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008996 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008997{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008998 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008999 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009000}
9001
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009002static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009003 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009004{
9005 struct drm_device *dev = crtc->base.dev;
9006 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009007 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9008 uint32_t ps_ctrl = 0;
9009 int id = -1;
9010 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009011
Chandra Kondurua1b22782015-04-07 15:28:45 -07009012 /* find scaler attached to this pipe */
9013 for (i = 0; i < crtc->num_scalers; i++) {
9014 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9015 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9016 id = i;
9017 pipe_config->pch_pfit.enabled = true;
9018 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9019 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9020 break;
9021 }
9022 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009023
Chandra Kondurua1b22782015-04-07 15:28:45 -07009024 scaler_state->scaler_id = id;
9025 if (id >= 0) {
9026 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9027 } else {
9028 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009029 }
9030}
9031
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009032static void
9033skylake_get_initial_plane_config(struct intel_crtc *crtc,
9034 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009035{
9036 struct drm_device *dev = crtc->base.dev;
9037 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009038 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009039 int pipe = crtc->pipe;
9040 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009041 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009042 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009043 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009044
Damien Lespiaud9806c92015-01-21 14:07:19 +00009045 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009046 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009047 DRM_DEBUG_KMS("failed to alloc fb\n");
9048 return;
9049 }
9050
Damien Lespiau1b842c82015-01-21 13:50:54 +00009051 fb = &intel_fb->base;
9052
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009053 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009054 if (!(val & PLANE_CTL_ENABLE))
9055 goto error;
9056
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009057 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9058 fourcc = skl_format_to_fourcc(pixel_format,
9059 val & PLANE_CTL_ORDER_RGBX,
9060 val & PLANE_CTL_ALPHA_MASK);
9061 fb->pixel_format = fourcc;
9062 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9063
Damien Lespiau40f46282015-02-27 11:15:21 +00009064 tiling = val & PLANE_CTL_TILED_MASK;
9065 switch (tiling) {
9066 case PLANE_CTL_TILED_LINEAR:
9067 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9068 break;
9069 case PLANE_CTL_TILED_X:
9070 plane_config->tiling = I915_TILING_X;
9071 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9072 break;
9073 case PLANE_CTL_TILED_Y:
9074 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9075 break;
9076 case PLANE_CTL_TILED_YF:
9077 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9078 break;
9079 default:
9080 MISSING_CASE(tiling);
9081 goto error;
9082 }
9083
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009084 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9085 plane_config->base = base;
9086
9087 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9088
9089 val = I915_READ(PLANE_SIZE(pipe, 0));
9090 fb->height = ((val >> 16) & 0xfff) + 1;
9091 fb->width = ((val >> 0) & 0x1fff) + 1;
9092
9093 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009094 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009095 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009096 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9097
9098 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009099 fb->pixel_format,
9100 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009101
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009102 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009103
9104 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9105 pipe_name(pipe), fb->width, fb->height,
9106 fb->bits_per_pixel, base, fb->pitches[0],
9107 plane_config->size);
9108
Damien Lespiau2d140302015-02-05 17:22:18 +00009109 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009110 return;
9111
9112error:
9113 kfree(fb);
9114}
9115
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009116static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009117 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009118{
9119 struct drm_device *dev = crtc->base.dev;
9120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 uint32_t tmp;
9122
9123 tmp = I915_READ(PF_CTL(crtc->pipe));
9124
9125 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009126 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009127 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9128 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009129
9130 /* We currently do not free assignements of panel fitters on
9131 * ivb/hsw (since we don't use the higher upscaling modes which
9132 * differentiates them) so just WARN about this case for now. */
9133 if (IS_GEN7(dev)) {
9134 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9135 PF_PIPE_SEL_IVB(crtc->pipe));
9136 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009137 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009138}
9139
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009140static void
9141ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9142 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009143{
9144 struct drm_device *dev = crtc->base.dev;
9145 struct drm_i915_private *dev_priv = dev->dev_private;
9146 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009147 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009148 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009149 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009150 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009151 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009152
Damien Lespiau42a7b082015-02-05 19:35:13 +00009153 val = I915_READ(DSPCNTR(pipe));
9154 if (!(val & DISPLAY_PLANE_ENABLE))
9155 return;
9156
Damien Lespiaud9806c92015-01-21 14:07:19 +00009157 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009158 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159 DRM_DEBUG_KMS("failed to alloc fb\n");
9160 return;
9161 }
9162
Damien Lespiau1b842c82015-01-21 13:50:54 +00009163 fb = &intel_fb->base;
9164
Daniel Vetter18c52472015-02-10 17:16:09 +00009165 if (INTEL_INFO(dev)->gen >= 4) {
9166 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009167 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009168 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9169 }
9170 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009171
9172 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009173 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009174 fb->pixel_format = fourcc;
9175 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009176
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009177 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009178 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009179 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009180 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009181 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009182 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009183 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009184 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009185 }
9186 plane_config->base = base;
9187
9188 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009189 fb->width = ((val >> 16) & 0xfff) + 1;
9190 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191
9192 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009193 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009194
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009195 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009196 fb->pixel_format,
9197 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009198
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009199 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
Damien Lespiau2844a922015-01-20 12:51:48 +00009201 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9202 pipe_name(pipe), fb->width, fb->height,
9203 fb->bits_per_pixel, base, fb->pitches[0],
9204 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009205
Damien Lespiau2d140302015-02-05 17:22:18 +00009206 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207}
9208
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009209static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009210 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009211{
9212 struct drm_device *dev = crtc->base.dev;
9213 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009214 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009215 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009216 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009217
Imre Deak17290502016-02-12 18:55:11 +02009218 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9219 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009220 return false;
9221
Daniel Vettere143a212013-07-04 12:01:15 +02009222 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009223 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009224
Imre Deak17290502016-02-12 18:55:11 +02009225 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009226 tmp = I915_READ(PIPECONF(crtc->pipe));
9227 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009228 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009229
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009230 switch (tmp & PIPECONF_BPC_MASK) {
9231 case PIPECONF_6BPC:
9232 pipe_config->pipe_bpp = 18;
9233 break;
9234 case PIPECONF_8BPC:
9235 pipe_config->pipe_bpp = 24;
9236 break;
9237 case PIPECONF_10BPC:
9238 pipe_config->pipe_bpp = 30;
9239 break;
9240 case PIPECONF_12BPC:
9241 pipe_config->pipe_bpp = 36;
9242 break;
9243 default:
9244 break;
9245 }
9246
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009247 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9248 pipe_config->limited_color_range = true;
9249
Daniel Vetterab9412b2013-05-03 11:49:46 +02009250 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009251 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009252 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009253
Daniel Vetter88adfff2013-03-28 10:42:01 +01009254 pipe_config->has_pch_encoder = true;
9255
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009256 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9257 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9258 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009259
9260 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009261
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009262 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009263 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009264 } else {
9265 tmp = I915_READ(PCH_DPLL_SEL);
9266 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009267 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009268 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009269 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009270 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009271
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009272 pipe_config->shared_dpll =
9273 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9274 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009275
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009276 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9277 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009278
9279 tmp = pipe_config->dpll_hw_state.dpll;
9280 pipe_config->pixel_multiplier =
9281 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9282 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009283
9284 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009285 } else {
9286 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009287 }
9288
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009289 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009290 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009291
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009292 ironlake_get_pfit_config(crtc, pipe_config);
9293
Imre Deak17290502016-02-12 18:55:11 +02009294 ret = true;
9295
9296out:
9297 intel_display_power_put(dev_priv, power_domain);
9298
9299 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009300}
9301
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009302static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9303{
9304 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009305 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009306
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009307 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009308 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009309 pipe_name(crtc->pipe));
9310
Rob Clarke2c719b2014-12-15 13:56:32 -05009311 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9312 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009313 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9314 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009315 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9316 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009317 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009318 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009319 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009320 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009321 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009322 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009323 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009324 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009325 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009327 /*
9328 * In theory we can still leave IRQs enabled, as long as only the HPD
9329 * interrupts remain enabled. We used to check for that, but since it's
9330 * gen-specific and since we only disable LCPLL after we fully disable
9331 * the interrupts, the check below should be enough.
9332 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009333 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009334}
9335
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009336static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9337{
9338 struct drm_device *dev = dev_priv->dev;
9339
9340 if (IS_HASWELL(dev))
9341 return I915_READ(D_COMP_HSW);
9342 else
9343 return I915_READ(D_COMP_BDW);
9344}
9345
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009346static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9347{
9348 struct drm_device *dev = dev_priv->dev;
9349
9350 if (IS_HASWELL(dev)) {
9351 mutex_lock(&dev_priv->rps.hw_lock);
9352 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9353 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009354 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009355 mutex_unlock(&dev_priv->rps.hw_lock);
9356 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009357 I915_WRITE(D_COMP_BDW, val);
9358 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009359 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009360}
9361
9362/*
9363 * This function implements pieces of two sequences from BSpec:
9364 * - Sequence for display software to disable LCPLL
9365 * - Sequence for display software to allow package C8+
9366 * The steps implemented here are just the steps that actually touch the LCPLL
9367 * register. Callers should take care of disabling all the display engine
9368 * functions, doing the mode unset, fixing interrupts, etc.
9369 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009370static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9371 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009372{
9373 uint32_t val;
9374
9375 assert_can_disable_lcpll(dev_priv);
9376
9377 val = I915_READ(LCPLL_CTL);
9378
9379 if (switch_to_fclk) {
9380 val |= LCPLL_CD_SOURCE_FCLK;
9381 I915_WRITE(LCPLL_CTL, val);
9382
9383 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9384 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9385 DRM_ERROR("Switching to FCLK failed\n");
9386
9387 val = I915_READ(LCPLL_CTL);
9388 }
9389
9390 val |= LCPLL_PLL_DISABLE;
9391 I915_WRITE(LCPLL_CTL, val);
9392 POSTING_READ(LCPLL_CTL);
9393
9394 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9395 DRM_ERROR("LCPLL still locked\n");
9396
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009397 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009398 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 ndelay(100);
9401
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009402 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9403 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 DRM_ERROR("D_COMP RCOMP still in progress\n");
9405
9406 if (allow_power_down) {
9407 val = I915_READ(LCPLL_CTL);
9408 val |= LCPLL_POWER_DOWN_ALLOW;
9409 I915_WRITE(LCPLL_CTL, val);
9410 POSTING_READ(LCPLL_CTL);
9411 }
9412}
9413
9414/*
9415 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9416 * source.
9417 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009418static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009419{
9420 uint32_t val;
9421
9422 val = I915_READ(LCPLL_CTL);
9423
9424 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9425 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9426 return;
9427
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009428 /*
9429 * Make sure we're not on PC8 state before disabling PC8, otherwise
9430 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009431 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009432 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009433
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009434 if (val & LCPLL_POWER_DOWN_ALLOW) {
9435 val &= ~LCPLL_POWER_DOWN_ALLOW;
9436 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009437 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009438 }
9439
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009440 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441 val |= D_COMP_COMP_FORCE;
9442 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009443 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009444
9445 val = I915_READ(LCPLL_CTL);
9446 val &= ~LCPLL_PLL_DISABLE;
9447 I915_WRITE(LCPLL_CTL, val);
9448
9449 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9450 DRM_ERROR("LCPLL not locked yet\n");
9451
9452 if (val & LCPLL_CD_SOURCE_FCLK) {
9453 val = I915_READ(LCPLL_CTL);
9454 val &= ~LCPLL_CD_SOURCE_FCLK;
9455 I915_WRITE(LCPLL_CTL, val);
9456
9457 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9458 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9459 DRM_ERROR("Switching back to LCPLL failed\n");
9460 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009461
Mika Kuoppala59bad942015-01-16 11:34:40 +02009462 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009463 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009464}
9465
Paulo Zanoni765dab672014-03-07 20:08:18 -03009466/*
9467 * Package states C8 and deeper are really deep PC states that can only be
9468 * reached when all the devices on the system allow it, so even if the graphics
9469 * device allows PC8+, it doesn't mean the system will actually get to these
9470 * states. Our driver only allows PC8+ when going into runtime PM.
9471 *
9472 * The requirements for PC8+ are that all the outputs are disabled, the power
9473 * well is disabled and most interrupts are disabled, and these are also
9474 * requirements for runtime PM. When these conditions are met, we manually do
9475 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9476 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9477 * hang the machine.
9478 *
9479 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9480 * the state of some registers, so when we come back from PC8+ we need to
9481 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9482 * need to take care of the registers kept by RC6. Notice that this happens even
9483 * if we don't put the device in PCI D3 state (which is what currently happens
9484 * because of the runtime PM support).
9485 *
9486 * For more, read "Display Sequences for Package C8" on the hardware
9487 * documentation.
9488 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009489void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009490{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491 struct drm_device *dev = dev_priv->dev;
9492 uint32_t val;
9493
Paulo Zanonic67a4702013-08-19 13:18:09 -03009494 DRM_DEBUG_KMS("Enabling package C8+\n");
9495
Ville Syrjäläc2699522015-08-27 23:55:59 +03009496 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009497 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9498 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9499 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9500 }
9501
9502 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009503 hsw_disable_lcpll(dev_priv, true, true);
9504}
9505
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009506void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009507{
9508 struct drm_device *dev = dev_priv->dev;
9509 uint32_t val;
9510
Paulo Zanonic67a4702013-08-19 13:18:09 -03009511 DRM_DEBUG_KMS("Disabling package C8+\n");
9512
9513 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009514 lpt_init_pch_refclk(dev);
9515
Ville Syrjäläc2699522015-08-27 23:55:59 +03009516 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009517 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9518 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9519 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9520 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009521}
9522
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009523static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309524{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009525 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009526 struct intel_atomic_state *old_intel_state =
9527 to_intel_atomic_state(old_state);
9528 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309529
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009530 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309531}
9532
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009533/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009534static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009535{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009536 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9537 struct drm_i915_private *dev_priv = state->dev->dev_private;
9538 struct drm_crtc *crtc;
9539 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009540 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009541 unsigned max_pixel_rate = 0, i;
9542 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009543
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009544 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9545 sizeof(intel_state->min_pixclk));
9546
9547 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009548 int pixel_rate;
9549
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009550 crtc_state = to_intel_crtc_state(cstate);
9551 if (!crtc_state->base.enable) {
9552 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009553 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009554 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009555
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009556 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009557
9558 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009559 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9561
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009562 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009563 }
9564
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009565 for_each_pipe(dev_priv, pipe)
9566 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9567
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568 return max_pixel_rate;
9569}
9570
9571static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9572{
9573 struct drm_i915_private *dev_priv = dev->dev_private;
9574 uint32_t val, data;
9575 int ret;
9576
9577 if (WARN((I915_READ(LCPLL_CTL) &
9578 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9579 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9580 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9581 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9582 "trying to change cdclk frequency with cdclk not enabled\n"))
9583 return;
9584
9585 mutex_lock(&dev_priv->rps.hw_lock);
9586 ret = sandybridge_pcode_write(dev_priv,
9587 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9588 mutex_unlock(&dev_priv->rps.hw_lock);
9589 if (ret) {
9590 DRM_ERROR("failed to inform pcode about cdclk change\n");
9591 return;
9592 }
9593
9594 val = I915_READ(LCPLL_CTL);
9595 val |= LCPLL_CD_SOURCE_FCLK;
9596 I915_WRITE(LCPLL_CTL, val);
9597
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009598 if (wait_for_us(I915_READ(LCPLL_CTL) &
9599 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009600 DRM_ERROR("Switching to FCLK failed\n");
9601
9602 val = I915_READ(LCPLL_CTL);
9603 val &= ~LCPLL_CLK_FREQ_MASK;
9604
9605 switch (cdclk) {
9606 case 450000:
9607 val |= LCPLL_CLK_FREQ_450;
9608 data = 0;
9609 break;
9610 case 540000:
9611 val |= LCPLL_CLK_FREQ_54O_BDW;
9612 data = 1;
9613 break;
9614 case 337500:
9615 val |= LCPLL_CLK_FREQ_337_5_BDW;
9616 data = 2;
9617 break;
9618 case 675000:
9619 val |= LCPLL_CLK_FREQ_675_BDW;
9620 data = 3;
9621 break;
9622 default:
9623 WARN(1, "invalid cdclk frequency\n");
9624 return;
9625 }
9626
9627 I915_WRITE(LCPLL_CTL, val);
9628
9629 val = I915_READ(LCPLL_CTL);
9630 val &= ~LCPLL_CD_SOURCE_FCLK;
9631 I915_WRITE(LCPLL_CTL, val);
9632
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009633 if (wait_for_us((I915_READ(LCPLL_CTL) &
9634 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009635 DRM_ERROR("Switching back to LCPLL failed\n");
9636
9637 mutex_lock(&dev_priv->rps.hw_lock);
9638 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9639 mutex_unlock(&dev_priv->rps.hw_lock);
9640
9641 intel_update_cdclk(dev);
9642
9643 WARN(cdclk != dev_priv->cdclk_freq,
9644 "cdclk requested %d kHz but got %d kHz\n",
9645 cdclk, dev_priv->cdclk_freq);
9646}
9647
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009648static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009649{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009650 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009651 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009652 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009653 int cdclk;
9654
9655 /*
9656 * FIXME should also account for plane ratio
9657 * once 64bpp pixel formats are supported.
9658 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009659 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009660 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009661 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009662 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009663 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009664 cdclk = 450000;
9665 else
9666 cdclk = 337500;
9667
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009668 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009669 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9670 cdclk, dev_priv->max_cdclk_freq);
9671 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009672 }
9673
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009674 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9675 if (!intel_state->active_crtcs)
9676 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009677
9678 return 0;
9679}
9680
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009681static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009683 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009684 struct intel_atomic_state *old_intel_state =
9685 to_intel_atomic_state(old_state);
9686 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009688 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009689}
9690
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009691static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9692 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009693{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009694 struct intel_encoder *intel_encoder =
9695 intel_ddi_get_crtc_new_encoder(crtc_state);
9696
9697 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9698 if (!intel_ddi_pll_select(crtc, crtc_state))
9699 return -EINVAL;
9700 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009701
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009702 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009703
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009704 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009705}
9706
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309707static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9708 enum port port,
9709 struct intel_crtc_state *pipe_config)
9710{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009711 enum intel_dpll_id id;
9712
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309713 switch (port) {
9714 case PORT_A:
9715 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009716 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309717 break;
9718 case PORT_B:
9719 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009720 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309721 break;
9722 case PORT_C:
9723 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009724 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309725 break;
9726 default:
9727 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009728 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309729 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009730
9731 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309732}
9733
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009734static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9735 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009736 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009737{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009738 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009739 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009740
9741 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9742 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9743
9744 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009745 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009746 id = DPLL_ID_SKL_DPLL0;
9747 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009748 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009749 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009750 break;
9751 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009752 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009753 break;
9754 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009755 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009756 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009757 default:
9758 MISSING_CASE(pipe_config->ddi_pll_sel);
9759 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009760 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009761
9762 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009763}
9764
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009765static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9766 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009767 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009768{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009769 enum intel_dpll_id id;
9770
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009771 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9772
9773 switch (pipe_config->ddi_pll_sel) {
9774 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009775 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009776 break;
9777 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009778 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009779 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009780 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009781 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009782 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009783 case PORT_CLK_SEL_LCPLL_810:
9784 id = DPLL_ID_LCPLL_810;
9785 break;
9786 case PORT_CLK_SEL_LCPLL_1350:
9787 id = DPLL_ID_LCPLL_1350;
9788 break;
9789 case PORT_CLK_SEL_LCPLL_2700:
9790 id = DPLL_ID_LCPLL_2700;
9791 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009792 default:
9793 MISSING_CASE(pipe_config->ddi_pll_sel);
9794 /* fall through */
9795 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009796 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009797 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009798
9799 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009800}
9801
Jani Nikulacf304292016-03-18 17:05:41 +02009802static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9803 struct intel_crtc_state *pipe_config,
9804 unsigned long *power_domain_mask)
9805{
9806 struct drm_device *dev = crtc->base.dev;
9807 struct drm_i915_private *dev_priv = dev->dev_private;
9808 enum intel_display_power_domain power_domain;
9809 u32 tmp;
9810
9811 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9812
9813 /*
9814 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9815 * consistency and less surprising code; it's in always on power).
9816 */
9817 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9818 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9819 enum pipe trans_edp_pipe;
9820 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9821 default:
9822 WARN(1, "unknown pipe linked to edp transcoder\n");
9823 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9824 case TRANS_DDI_EDP_INPUT_A_ON:
9825 trans_edp_pipe = PIPE_A;
9826 break;
9827 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9828 trans_edp_pipe = PIPE_B;
9829 break;
9830 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9831 trans_edp_pipe = PIPE_C;
9832 break;
9833 }
9834
9835 if (trans_edp_pipe == crtc->pipe)
9836 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9837 }
9838
9839 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9840 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9841 return false;
9842 *power_domain_mask |= BIT(power_domain);
9843
9844 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9845
9846 return tmp & PIPECONF_ENABLE;
9847}
9848
Jani Nikula4d1de972016-03-18 17:05:42 +02009849static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9850 struct intel_crtc_state *pipe_config,
9851 unsigned long *power_domain_mask)
9852{
9853 struct drm_device *dev = crtc->base.dev;
9854 struct drm_i915_private *dev_priv = dev->dev_private;
9855 enum intel_display_power_domain power_domain;
9856 enum port port;
9857 enum transcoder cpu_transcoder;
9858 u32 tmp;
9859
9860 pipe_config->has_dsi_encoder = false;
9861
9862 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9863 if (port == PORT_A)
9864 cpu_transcoder = TRANSCODER_DSI_A;
9865 else
9866 cpu_transcoder = TRANSCODER_DSI_C;
9867
9868 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9869 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9870 continue;
9871 *power_domain_mask |= BIT(power_domain);
9872
9873 /* XXX: this works for video mode only */
9874 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9875 if (!(tmp & DPI_ENABLE))
9876 continue;
9877
9878 tmp = I915_READ(MIPI_CTRL(port));
9879 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9880 continue;
9881
9882 pipe_config->cpu_transcoder = cpu_transcoder;
9883 pipe_config->has_dsi_encoder = true;
9884 break;
9885 }
9886
9887 return pipe_config->has_dsi_encoder;
9888}
9889
Daniel Vetter26804af2014-06-25 22:01:55 +03009890static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009891 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009892{
9893 struct drm_device *dev = crtc->base.dev;
9894 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009895 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009896 enum port port;
9897 uint32_t tmp;
9898
9899 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9900
9901 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9902
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009903 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009904 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309905 else if (IS_BROXTON(dev))
9906 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009907 else
9908 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009909
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009910 pll = pipe_config->shared_dpll;
9911 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009912 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9913 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009914 }
9915
Daniel Vetter26804af2014-06-25 22:01:55 +03009916 /*
9917 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9918 * DDI E. So just check whether this pipe is wired to DDI E and whether
9919 * the PCH transcoder is on.
9920 */
Damien Lespiauca370452013-12-03 13:56:24 +00009921 if (INTEL_INFO(dev)->gen < 9 &&
9922 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009923 pipe_config->has_pch_encoder = true;
9924
9925 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9926 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9927 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9928
9929 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9930 }
9931}
9932
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009933static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009934 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009935{
9936 struct drm_device *dev = crtc->base.dev;
9937 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009938 enum intel_display_power_domain power_domain;
9939 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009940 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009941
Imre Deak17290502016-02-12 18:55:11 +02009942 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9943 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009944 return false;
Imre Deak17290502016-02-12 18:55:11 +02009945 power_domain_mask = BIT(power_domain);
9946
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009947 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009948
Jani Nikulacf304292016-03-18 17:05:41 +02009949 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009950
Jani Nikula4d1de972016-03-18 17:05:42 +02009951 if (IS_BROXTON(dev_priv)) {
9952 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9953 &power_domain_mask);
9954 WARN_ON(active && pipe_config->has_dsi_encoder);
9955 if (pipe_config->has_dsi_encoder)
9956 active = true;
9957 }
9958
Jani Nikulacf304292016-03-18 17:05:41 +02009959 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009960 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009961
Jani Nikula4d1de972016-03-18 17:05:42 +02009962 if (!pipe_config->has_dsi_encoder) {
9963 haswell_get_ddi_port_state(crtc, pipe_config);
9964 intel_get_pipe_timings(crtc, pipe_config);
9965 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009966
Jani Nikulabc58be62016-03-18 17:05:39 +02009967 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009968
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009969 pipe_config->gamma_mode =
9970 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9971
Chandra Kondurua1b22782015-04-07 15:28:45 -07009972 if (INTEL_INFO(dev)->gen >= 9) {
9973 skl_init_scalers(dev, crtc, pipe_config);
9974 }
9975
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009976 if (INTEL_INFO(dev)->gen >= 9) {
9977 pipe_config->scaler_state.scaler_id = -1;
9978 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9979 }
9980
Imre Deak17290502016-02-12 18:55:11 +02009981 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9982 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9983 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009984 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009985 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009986 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009987 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009988 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009989
Jesse Barnese59150d2014-01-07 13:30:45 -08009990 if (IS_HASWELL(dev))
9991 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9992 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009993
Jani Nikula4d1de972016-03-18 17:05:42 +02009994 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9995 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009996 pipe_config->pixel_multiplier =
9997 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9998 } else {
9999 pipe_config->pixel_multiplier = 1;
10000 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010001
Imre Deak17290502016-02-12 18:55:11 +020010002out:
10003 for_each_power_domain(power_domain, power_domain_mask)
10004 intel_display_power_put(dev_priv, power_domain);
10005
Jani Nikulacf304292016-03-18 17:05:41 +020010006 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010007}
10008
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010009static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10010 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010011{
10012 struct drm_device *dev = crtc->dev;
10013 struct drm_i915_private *dev_priv = dev->dev_private;
10014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010015 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010016
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010017 if (plane_state && plane_state->visible) {
10018 unsigned int width = plane_state->base.crtc_w;
10019 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010020 unsigned int stride = roundup_pow_of_two(width) * 4;
10021
10022 switch (stride) {
10023 default:
10024 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10025 width, stride);
10026 stride = 256;
10027 /* fallthrough */
10028 case 256:
10029 case 512:
10030 case 1024:
10031 case 2048:
10032 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010033 }
10034
Ville Syrjälädc41c152014-08-13 11:57:05 +030010035 cntl |= CURSOR_ENABLE |
10036 CURSOR_GAMMA_ENABLE |
10037 CURSOR_FORMAT_ARGB |
10038 CURSOR_STRIDE(stride);
10039
10040 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010041 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010042
Ville Syrjälädc41c152014-08-13 11:57:05 +030010043 if (intel_crtc->cursor_cntl != 0 &&
10044 (intel_crtc->cursor_base != base ||
10045 intel_crtc->cursor_size != size ||
10046 intel_crtc->cursor_cntl != cntl)) {
10047 /* On these chipsets we can only modify the base/size/stride
10048 * whilst the cursor is disabled.
10049 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010050 I915_WRITE(CURCNTR(PIPE_A), 0);
10051 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010052 intel_crtc->cursor_cntl = 0;
10053 }
10054
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010055 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010056 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010057 intel_crtc->cursor_base = base;
10058 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010059
10060 if (intel_crtc->cursor_size != size) {
10061 I915_WRITE(CURSIZE, size);
10062 intel_crtc->cursor_size = size;
10063 }
10064
Chris Wilson4b0e3332014-05-30 16:35:26 +030010065 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010066 I915_WRITE(CURCNTR(PIPE_A), cntl);
10067 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010068 intel_crtc->cursor_cntl = cntl;
10069 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010070}
10071
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010072static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10073 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010074{
10075 struct drm_device *dev = crtc->dev;
10076 struct drm_i915_private *dev_priv = dev->dev_private;
10077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10078 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010079 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010080
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010081 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010082 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010083 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010084 case 64:
10085 cntl |= CURSOR_MODE_64_ARGB_AX;
10086 break;
10087 case 128:
10088 cntl |= CURSOR_MODE_128_ARGB_AX;
10089 break;
10090 case 256:
10091 cntl |= CURSOR_MODE_256_ARGB_AX;
10092 break;
10093 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010094 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010095 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010096 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010097 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010098
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010099 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010100 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010101
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010102 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10103 cntl |= CURSOR_ROTATE_180;
10104 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010105
Chris Wilson4b0e3332014-05-30 16:35:26 +030010106 if (intel_crtc->cursor_cntl != cntl) {
10107 I915_WRITE(CURCNTR(pipe), cntl);
10108 POSTING_READ(CURCNTR(pipe));
10109 intel_crtc->cursor_cntl = cntl;
10110 }
10111
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010112 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010113 I915_WRITE(CURBASE(pipe), base);
10114 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010115
10116 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010117}
10118
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010119/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010120static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010121 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010122{
10123 struct drm_device *dev = crtc->dev;
10124 struct drm_i915_private *dev_priv = dev->dev_private;
10125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10126 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010127 u32 base = intel_crtc->cursor_addr;
10128 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010129
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010130 if (plane_state) {
10131 int x = plane_state->base.crtc_x;
10132 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010133
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010134 if (x < 0) {
10135 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10136 x = -x;
10137 }
10138 pos |= x << CURSOR_X_SHIFT;
10139
10140 if (y < 0) {
10141 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10142 y = -y;
10143 }
10144 pos |= y << CURSOR_Y_SHIFT;
10145
10146 /* ILK+ do this automagically */
10147 if (HAS_GMCH_DISPLAY(dev) &&
10148 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10149 base += (plane_state->base.crtc_h *
10150 plane_state->base.crtc_w - 1) * 4;
10151 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010152 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010153
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010154 I915_WRITE(CURPOS(pipe), pos);
10155
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010156 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010157 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010158 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010159 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010160}
10161
Ville Syrjälädc41c152014-08-13 11:57:05 +030010162static bool cursor_size_ok(struct drm_device *dev,
10163 uint32_t width, uint32_t height)
10164{
10165 if (width == 0 || height == 0)
10166 return false;
10167
10168 /*
10169 * 845g/865g are special in that they are only limited by
10170 * the width of their cursors, the height is arbitrary up to
10171 * the precision of the register. Everything else requires
10172 * square cursors, limited to a few power-of-two sizes.
10173 */
10174 if (IS_845G(dev) || IS_I865G(dev)) {
10175 if ((width & 63) != 0)
10176 return false;
10177
10178 if (width > (IS_845G(dev) ? 64 : 512))
10179 return false;
10180
10181 if (height > 1023)
10182 return false;
10183 } else {
10184 switch (width | height) {
10185 case 256:
10186 case 128:
10187 if (IS_GEN2(dev))
10188 return false;
10189 case 64:
10190 break;
10191 default:
10192 return false;
10193 }
10194 }
10195
10196 return true;
10197}
10198
Jesse Barnes79e53942008-11-07 14:24:08 -080010199/* VESA 640x480x72Hz mode to set on the pipe */
10200static struct drm_display_mode load_detect_mode = {
10201 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10202 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10203};
10204
Daniel Vettera8bb6812014-02-10 18:00:39 +010010205struct drm_framebuffer *
10206__intel_framebuffer_create(struct drm_device *dev,
10207 struct drm_mode_fb_cmd2 *mode_cmd,
10208 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010209{
10210 struct intel_framebuffer *intel_fb;
10211 int ret;
10212
10213 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010214 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010215 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010216
10217 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010218 if (ret)
10219 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010220
10221 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010222
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010223err:
10224 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010225 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010226}
10227
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010228static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010229intel_framebuffer_create(struct drm_device *dev,
10230 struct drm_mode_fb_cmd2 *mode_cmd,
10231 struct drm_i915_gem_object *obj)
10232{
10233 struct drm_framebuffer *fb;
10234 int ret;
10235
10236 ret = i915_mutex_lock_interruptible(dev);
10237 if (ret)
10238 return ERR_PTR(ret);
10239 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10240 mutex_unlock(&dev->struct_mutex);
10241
10242 return fb;
10243}
10244
Chris Wilsond2dff872011-04-19 08:36:26 +010010245static u32
10246intel_framebuffer_pitch_for_width(int width, int bpp)
10247{
10248 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10249 return ALIGN(pitch, 64);
10250}
10251
10252static u32
10253intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10254{
10255 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010256 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010257}
10258
10259static struct drm_framebuffer *
10260intel_framebuffer_create_for_mode(struct drm_device *dev,
10261 struct drm_display_mode *mode,
10262 int depth, int bpp)
10263{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010264 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010265 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010266 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010267
10268 obj = i915_gem_alloc_object(dev,
10269 intel_framebuffer_size_for_mode(mode, bpp));
10270 if (obj == NULL)
10271 return ERR_PTR(-ENOMEM);
10272
10273 mode_cmd.width = mode->hdisplay;
10274 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010275 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10276 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010277 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010278
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010279 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10280 if (IS_ERR(fb))
10281 drm_gem_object_unreference_unlocked(&obj->base);
10282
10283 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010284}
10285
10286static struct drm_framebuffer *
10287mode_fits_in_fbdev(struct drm_device *dev,
10288 struct drm_display_mode *mode)
10289{
Daniel Vetter06957262015-08-10 13:34:08 +020010290#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010291 struct drm_i915_private *dev_priv = dev->dev_private;
10292 struct drm_i915_gem_object *obj;
10293 struct drm_framebuffer *fb;
10294
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010295 if (!dev_priv->fbdev)
10296 return NULL;
10297
10298 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010299 return NULL;
10300
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010301 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010302 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010303
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010304 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010305 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10306 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010307 return NULL;
10308
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010309 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010310 return NULL;
10311
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010312 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010313 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010314#else
10315 return NULL;
10316#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010317}
10318
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010319static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10320 struct drm_crtc *crtc,
10321 struct drm_display_mode *mode,
10322 struct drm_framebuffer *fb,
10323 int x, int y)
10324{
10325 struct drm_plane_state *plane_state;
10326 int hdisplay, vdisplay;
10327 int ret;
10328
10329 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10330 if (IS_ERR(plane_state))
10331 return PTR_ERR(plane_state);
10332
10333 if (mode)
10334 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10335 else
10336 hdisplay = vdisplay = 0;
10337
10338 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10339 if (ret)
10340 return ret;
10341 drm_atomic_set_fb_for_plane(plane_state, fb);
10342 plane_state->crtc_x = 0;
10343 plane_state->crtc_y = 0;
10344 plane_state->crtc_w = hdisplay;
10345 plane_state->crtc_h = vdisplay;
10346 plane_state->src_x = x << 16;
10347 plane_state->src_y = y << 16;
10348 plane_state->src_w = hdisplay << 16;
10349 plane_state->src_h = vdisplay << 16;
10350
10351 return 0;
10352}
10353
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010354bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010355 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010356 struct intel_load_detect_pipe *old,
10357 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010358{
10359 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010360 struct intel_encoder *intel_encoder =
10361 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010362 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010363 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010364 struct drm_crtc *crtc = NULL;
10365 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010366 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010367 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010368 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010369 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010370 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010371 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010372
Chris Wilsond2dff872011-04-19 08:36:26 +010010373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010374 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010375 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010376
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010377 old->restore_state = NULL;
10378
Rob Clark51fd3712013-11-19 12:10:12 -050010379retry:
10380 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10381 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010382 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010383
Jesse Barnes79e53942008-11-07 14:24:08 -080010384 /*
10385 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010386 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010387 * - if the connector already has an assigned crtc, use it (but make
10388 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010389 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010390 * - try to find the first unused crtc that can drive this connector,
10391 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010392 */
10393
10394 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010395 if (connector->state->crtc) {
10396 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010397
Rob Clark51fd3712013-11-19 12:10:12 -050010398 ret = drm_modeset_lock(&crtc->mutex, ctx);
10399 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010400 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010401
10402 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010403 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 }
10405
10406 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010407 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010408 i++;
10409 if (!(encoder->possible_crtcs & (1 << i)))
10410 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010411
10412 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10413 if (ret)
10414 goto fail;
10415
10416 if (possible_crtc->state->enable) {
10417 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010418 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010419 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010420
10421 crtc = possible_crtc;
10422 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010423 }
10424
10425 /*
10426 * If we didn't find an unused CRTC, don't use any.
10427 */
10428 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010429 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010430 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010431 }
10432
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010433found:
10434 intel_crtc = to_intel_crtc(crtc);
10435
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010436 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10437 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010438 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010439
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010440 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010441 restore_state = drm_atomic_state_alloc(dev);
10442 if (!state || !restore_state) {
10443 ret = -ENOMEM;
10444 goto fail;
10445 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010446
10447 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010448 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010449
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010450 connector_state = drm_atomic_get_connector_state(state, connector);
10451 if (IS_ERR(connector_state)) {
10452 ret = PTR_ERR(connector_state);
10453 goto fail;
10454 }
10455
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010456 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10457 if (ret)
10458 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010459
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010460 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10461 if (IS_ERR(crtc_state)) {
10462 ret = PTR_ERR(crtc_state);
10463 goto fail;
10464 }
10465
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010466 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010467
Chris Wilson64927112011-04-20 07:25:26 +010010468 if (!mode)
10469 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010470
Chris Wilsond2dff872011-04-19 08:36:26 +010010471 /* We need a framebuffer large enough to accommodate all accesses
10472 * that the plane may generate whilst we perform load detection.
10473 * We can not rely on the fbcon either being present (we get called
10474 * during its initialisation to detect all boot displays, or it may
10475 * not even exist) or that it is large enough to satisfy the
10476 * requested mode.
10477 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010478 fb = mode_fits_in_fbdev(dev, mode);
10479 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010480 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010481 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010482 } else
10483 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010484 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010485 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010486 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010488
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010489 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10490 if (ret)
10491 goto fail;
10492
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010493 drm_framebuffer_unreference(fb);
10494
10495 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10496 if (ret)
10497 goto fail;
10498
10499 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10500 if (!ret)
10501 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10502 if (!ret)
10503 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10504 if (ret) {
10505 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10506 goto fail;
10507 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010508
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010509 ret = drm_atomic_commit(state);
10510 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010511 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010512 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010513 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010514
10515 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010516
Jesse Barnes79e53942008-11-07 14:24:08 -080010517 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010518 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010519 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010520
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010521fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010522 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010523 drm_atomic_state_free(restore_state);
10524 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010525
Rob Clark51fd3712013-11-19 12:10:12 -050010526 if (ret == -EDEADLK) {
10527 drm_modeset_backoff(ctx);
10528 goto retry;
10529 }
10530
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010531 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010532}
10533
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010534void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010535 struct intel_load_detect_pipe *old,
10536 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010537{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010538 struct intel_encoder *intel_encoder =
10539 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010540 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010541 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010542 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010543
Chris Wilsond2dff872011-04-19 08:36:26 +010010544 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010545 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010546 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010547
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010548 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010549 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010550
10551 ret = drm_atomic_commit(state);
10552 if (ret) {
10553 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10554 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010556}
10557
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010558static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010559 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010560{
10561 struct drm_i915_private *dev_priv = dev->dev_private;
10562 u32 dpll = pipe_config->dpll_hw_state.dpll;
10563
10564 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010565 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010566 else if (HAS_PCH_SPLIT(dev))
10567 return 120000;
10568 else if (!IS_GEN2(dev))
10569 return 96000;
10570 else
10571 return 48000;
10572}
10573
Jesse Barnes79e53942008-11-07 14:24:08 -080010574/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010575static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010576 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010577{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010578 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010579 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010580 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010581 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010582 u32 fp;
10583 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010584 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010585 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010586
10587 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010588 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010589 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010590 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010591
10592 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010593 if (IS_PINEVIEW(dev)) {
10594 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10595 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010596 } else {
10597 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10598 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10599 }
10600
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010601 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010602 if (IS_PINEVIEW(dev))
10603 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10604 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010605 else
10606 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010607 DPLL_FPA01_P1_POST_DIV_SHIFT);
10608
10609 switch (dpll & DPLL_MODE_MASK) {
10610 case DPLLB_MODE_DAC_SERIAL:
10611 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10612 5 : 10;
10613 break;
10614 case DPLLB_MODE_LVDS:
10615 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10616 7 : 14;
10617 break;
10618 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010619 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010620 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010621 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010622 }
10623
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010624 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010625 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010626 else
Imre Deakdccbea32015-06-22 23:35:51 +030010627 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010628 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010629 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010630 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010631
10632 if (is_lvds) {
10633 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10634 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010635
10636 if (lvds & LVDS_CLKB_POWER_UP)
10637 clock.p2 = 7;
10638 else
10639 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010640 } else {
10641 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10642 clock.p1 = 2;
10643 else {
10644 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10645 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10646 }
10647 if (dpll & PLL_P2_DIVIDE_BY_4)
10648 clock.p2 = 4;
10649 else
10650 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010651 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010652
Imre Deakdccbea32015-06-22 23:35:51 +030010653 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010654 }
10655
Ville Syrjälä18442d02013-09-13 16:00:08 +030010656 /*
10657 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010658 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010659 * encoder's get_config() function.
10660 */
Imre Deakdccbea32015-06-22 23:35:51 +030010661 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010662}
10663
Ville Syrjälä6878da02013-09-13 15:59:11 +030010664int intel_dotclock_calculate(int link_freq,
10665 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010666{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010667 /*
10668 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010669 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010670 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010671 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010672 *
10673 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010674 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010675 */
10676
Ville Syrjälä6878da02013-09-13 15:59:11 +030010677 if (!m_n->link_n)
10678 return 0;
10679
10680 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10681}
10682
Ville Syrjälä18442d02013-09-13 16:00:08 +030010683static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010684 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010685{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010686 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010687
10688 /* read out port_clock from the DPLL */
10689 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010690
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010691 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010692 * In case there is an active pipe without active ports,
10693 * we may need some idea for the dotclock anyway.
10694 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010695 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010696 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010697 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010698 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010699}
10700
10701/** Returns the currently programmed mode of the given pipe. */
10702struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10703 struct drm_crtc *crtc)
10704{
Jesse Barnes548f2452011-02-17 10:40:53 -080010705 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010707 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010708 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010709 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010710 int htot = I915_READ(HTOTAL(cpu_transcoder));
10711 int hsync = I915_READ(HSYNC(cpu_transcoder));
10712 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10713 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010714 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010715
10716 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10717 if (!mode)
10718 return NULL;
10719
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010720 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10721 if (!pipe_config) {
10722 kfree(mode);
10723 return NULL;
10724 }
10725
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010726 /*
10727 * Construct a pipe_config sufficient for getting the clock info
10728 * back out of crtc_clock_get.
10729 *
10730 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10731 * to use a real value here instead.
10732 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010733 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10734 pipe_config->pixel_multiplier = 1;
10735 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10736 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10737 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10738 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010739
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010740 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010741 mode->hdisplay = (htot & 0xffff) + 1;
10742 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10743 mode->hsync_start = (hsync & 0xffff) + 1;
10744 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10745 mode->vdisplay = (vtot & 0xffff) + 1;
10746 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10747 mode->vsync_start = (vsync & 0xffff) + 1;
10748 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10749
10750 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010751
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010752 kfree(pipe_config);
10753
Jesse Barnes79e53942008-11-07 14:24:08 -080010754 return mode;
10755}
10756
Chris Wilsonf047e392012-07-21 12:31:41 +010010757void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010758{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010759 struct drm_i915_private *dev_priv = dev->dev_private;
10760
Chris Wilsonf62a0072014-02-21 17:55:39 +000010761 if (dev_priv->mm.busy)
10762 return;
10763
Paulo Zanoni43694d62014-03-07 20:08:08 -030010764 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010765 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010766 if (INTEL_INFO(dev)->gen >= 6)
10767 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010768 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010769}
10770
10771void intel_mark_idle(struct drm_device *dev)
10772{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010773 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010774
Chris Wilsonf62a0072014-02-21 17:55:39 +000010775 if (!dev_priv->mm.busy)
10776 return;
10777
10778 dev_priv->mm.busy = false;
10779
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010780 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010781 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010782
Paulo Zanoni43694d62014-03-07 20:08:08 -030010783 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010784}
10785
Jesse Barnes79e53942008-11-07 14:24:08 -080010786static void intel_crtc_destroy(struct drm_crtc *crtc)
10787{
10788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010789 struct drm_device *dev = crtc->dev;
10790 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010791
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010792 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010793 work = intel_crtc->unpin_work;
10794 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010795 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010796
10797 if (work) {
10798 cancel_work_sync(&work->work);
10799 kfree(work);
10800 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010801
10802 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010803
Jesse Barnes79e53942008-11-07 14:24:08 -080010804 kfree(intel_crtc);
10805}
10806
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010807static void intel_unpin_work_fn(struct work_struct *__work)
10808{
10809 struct intel_unpin_work *work =
10810 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010811 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10812 struct drm_device *dev = crtc->base.dev;
10813 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010814
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010815 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010816 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010817 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010818
John Harrisonf06cc1b2014-11-24 18:49:37 +000010819 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010820 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010821 mutex_unlock(&dev->struct_mutex);
10822
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010823 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010824 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010825 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010826
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010827 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10828 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010829
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010830 kfree(work);
10831}
10832
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010833static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010834 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010835{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10837 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010838 unsigned long flags;
10839
10840 /* Ignore early vblank irqs */
10841 if (intel_crtc == NULL)
10842 return;
10843
Daniel Vetterf3260382014-09-15 14:55:23 +020010844 /*
10845 * This is called both by irq handlers and the reset code (to complete
10846 * lost pageflips) so needs the full irqsave spinlocks.
10847 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010848 spin_lock_irqsave(&dev->event_lock, flags);
10849 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010850
10851 /* Ensure we don't miss a work->pending update ... */
10852 smp_rmb();
10853
10854 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010855 spin_unlock_irqrestore(&dev->event_lock, flags);
10856 return;
10857 }
10858
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010859 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010860
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010861 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010862}
10863
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010864void intel_finish_page_flip(struct drm_device *dev, int pipe)
10865{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010866 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010867 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10868
Mario Kleiner49b14a52010-12-09 07:00:07 +010010869 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010870}
10871
10872void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10873{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010874 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010875 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10876
Mario Kleiner49b14a52010-12-09 07:00:07 +010010877 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010878}
10879
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010880/* Is 'a' after or equal to 'b'? */
10881static bool g4x_flip_count_after_eq(u32 a, u32 b)
10882{
10883 return !((a - b) & 0x80000000);
10884}
10885
10886static bool page_flip_finished(struct intel_crtc *crtc)
10887{
10888 struct drm_device *dev = crtc->base.dev;
10889 struct drm_i915_private *dev_priv = dev->dev_private;
10890
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010891 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10892 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10893 return true;
10894
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010895 /*
10896 * The relevant registers doen't exist on pre-ctg.
10897 * As the flip done interrupt doesn't trigger for mmio
10898 * flips on gmch platforms, a flip count check isn't
10899 * really needed there. But since ctg has the registers,
10900 * include it in the check anyway.
10901 */
10902 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10903 return true;
10904
10905 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010906 * BDW signals flip done immediately if the plane
10907 * is disabled, even if the plane enable is already
10908 * armed to occur at the next vblank :(
10909 */
10910
10911 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010912 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10913 * used the same base address. In that case the mmio flip might
10914 * have completed, but the CS hasn't even executed the flip yet.
10915 *
10916 * A flip count check isn't enough as the CS might have updated
10917 * the base address just after start of vblank, but before we
10918 * managed to process the interrupt. This means we'd complete the
10919 * CS flip too soon.
10920 *
10921 * Combining both checks should get us a good enough result. It may
10922 * still happen that the CS flip has been executed, but has not
10923 * yet actually completed. But in case the base address is the same
10924 * anyway, we don't really care.
10925 */
10926 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10927 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010928 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010929 crtc->unpin_work->flip_count);
10930}
10931
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010932void intel_prepare_page_flip(struct drm_device *dev, int plane)
10933{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010934 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010935 struct intel_crtc *intel_crtc =
10936 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10937 unsigned long flags;
10938
Daniel Vetterf3260382014-09-15 14:55:23 +020010939
10940 /*
10941 * This is called both by irq handlers and the reset code (to complete
10942 * lost pageflips) so needs the full irqsave spinlocks.
10943 *
10944 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010945 * generate a page-flip completion irq, i.e. every modeset
10946 * is also accompanied by a spurious intel_prepare_page_flip().
10947 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010948 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010949 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010950 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 spin_unlock_irqrestore(&dev->event_lock, flags);
10952}
10953
Chris Wilson60426392015-10-10 10:44:32 +010010954static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010955{
10956 /* Ensure that the work item is consistent when activating it ... */
10957 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010958 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010959 /* and that it is marked active as soon as the irq could fire. */
10960 smp_wmb();
10961}
10962
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010963static int intel_gen2_queue_flip(struct drm_device *dev,
10964 struct drm_crtc *crtc,
10965 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010966 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010967 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010968 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010969{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010970 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010972 u32 flip_mask;
10973 int ret;
10974
John Harrison5fb9de12015-05-29 17:44:07 +010010975 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010976 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010977 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010978
10979 /* Can't queue multiple flips, so wait for the previous
10980 * one to finish before executing the next.
10981 */
10982 if (intel_crtc->plane)
10983 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10984 else
10985 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010986 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10987 intel_ring_emit(engine, MI_NOOP);
10988 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010989 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010990 intel_ring_emit(engine, fb->pitches[0]);
10991 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10992 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010993
Chris Wilson60426392015-10-10 10:44:32 +010010994 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010995 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010996}
10997
10998static int intel_gen3_queue_flip(struct drm_device *dev,
10999 struct drm_crtc *crtc,
11000 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011001 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011002 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011003 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011004{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011005 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011007 u32 flip_mask;
11008 int ret;
11009
John Harrison5fb9de12015-05-29 17:44:07 +010011010 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011012 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011013
11014 if (intel_crtc->plane)
11015 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11016 else
11017 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011018 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11019 intel_ring_emit(engine, MI_NOOP);
11020 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011021 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011022 intel_ring_emit(engine, fb->pitches[0]);
11023 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11024 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011025
Chris Wilson60426392015-10-10 10:44:32 +010011026 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011027 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011028}
11029
11030static int intel_gen4_queue_flip(struct drm_device *dev,
11031 struct drm_crtc *crtc,
11032 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011033 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011034 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011035 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011037 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011038 struct drm_i915_private *dev_priv = dev->dev_private;
11039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11040 uint32_t pf, pipesrc;
11041 int ret;
11042
John Harrison5fb9de12015-05-29 17:44:07 +010011043 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011044 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011045 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011046
11047 /* i965+ uses the linear or tiled offsets from the
11048 * Display Registers (which do not change across a page-flip)
11049 * so we need only reprogram the base address.
11050 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011051 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011052 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011053 intel_ring_emit(engine, fb->pitches[0]);
11054 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011055 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011056
11057 /* XXX Enabling the panel-fitter across page-flip is so far
11058 * untested on non-native modes, so ignore it for now.
11059 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11060 */
11061 pf = 0;
11062 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011063 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011064
Chris Wilson60426392015-10-10 10:44:32 +010011065 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011066 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011067}
11068
11069static int intel_gen6_queue_flip(struct drm_device *dev,
11070 struct drm_crtc *crtc,
11071 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011072 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011073 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011074 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011075{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011076 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011077 struct drm_i915_private *dev_priv = dev->dev_private;
11078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11079 uint32_t pf, pipesrc;
11080 int ret;
11081
John Harrison5fb9de12015-05-29 17:44:07 +010011082 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011083 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011084 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011085
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011086 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011087 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011088 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11089 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011090
Chris Wilson99d9acd2012-04-17 20:37:00 +010011091 /* Contrary to the suggestions in the documentation,
11092 * "Enable Panel Fitter" does not seem to be required when page
11093 * flipping with a non-native mode, and worse causes a normal
11094 * modeset to fail.
11095 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11096 */
11097 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011098 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011099 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011100
Chris Wilson60426392015-10-10 10:44:32 +010011101 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011102 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011103}
11104
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011105static int intel_gen7_queue_flip(struct drm_device *dev,
11106 struct drm_crtc *crtc,
11107 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011108 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011109 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011110 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011111{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011112 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011114 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011115 int len, ret;
11116
Robin Schroereba905b2014-05-18 02:24:50 +020011117 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011118 case PLANE_A:
11119 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11120 break;
11121 case PLANE_B:
11122 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11123 break;
11124 case PLANE_C:
11125 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11126 break;
11127 default:
11128 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011129 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011130 }
11131
Chris Wilsonffe74d72013-08-26 20:58:12 +010011132 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011133 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011134 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011135 /*
11136 * On Gen 8, SRM is now taking an extra dword to accommodate
11137 * 48bits addresses, and we need a NOOP for the batch size to
11138 * stay even.
11139 */
11140 if (IS_GEN8(dev))
11141 len += 2;
11142 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011143
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011144 /*
11145 * BSpec MI_DISPLAY_FLIP for IVB:
11146 * "The full packet must be contained within the same cache line."
11147 *
11148 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11149 * cacheline, if we ever start emitting more commands before
11150 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11151 * then do the cacheline alignment, and finally emit the
11152 * MI_DISPLAY_FLIP.
11153 */
John Harrisonbba09b12015-05-29 17:44:06 +010011154 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011155 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011156 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011157
John Harrison5fb9de12015-05-29 17:44:07 +010011158 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011159 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011160 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011161
Chris Wilsonffe74d72013-08-26 20:58:12 +010011162 /* Unmask the flip-done completion message. Note that the bspec says that
11163 * we should do this for both the BCS and RCS, and that we must not unmask
11164 * more than one flip event at any time (or ensure that one flip message
11165 * can be sent by waiting for flip-done prior to queueing new flips).
11166 * Experimentation says that BCS works despite DERRMR masking all
11167 * flip-done completion events and that unmasking all planes at once
11168 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11169 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11170 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011171 if (engine->id == RCS) {
11172 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11173 intel_ring_emit_reg(engine, DERRMR);
11174 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11175 DERRMR_PIPEB_PRI_FLIP_DONE |
11176 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011177 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011178 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011179 MI_SRM_LRM_GLOBAL_GTT);
11180 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011181 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011182 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011183 intel_ring_emit_reg(engine, DERRMR);
11184 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011185 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011186 intel_ring_emit(engine, 0);
11187 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011188 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011189 }
11190
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011191 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11192 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11193 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11194 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011195
Chris Wilson60426392015-10-10 10:44:32 +010011196 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011197 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011198}
11199
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011200static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011201 struct drm_i915_gem_object *obj)
11202{
11203 /*
11204 * This is not being used for older platforms, because
11205 * non-availability of flip done interrupt forces us to use
11206 * CS flips. Older platforms derive flip done using some clever
11207 * tricks involving the flip_pending status bits and vblank irqs.
11208 * So using MMIO flips there would disrupt this mechanism.
11209 */
11210
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011211 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011212 return true;
11213
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011214 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011215 return false;
11216
11217 if (i915.use_mmio_flip < 0)
11218 return false;
11219 else if (i915.use_mmio_flip > 0)
11220 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011221 else if (i915.enable_execlists)
11222 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011223 else if (obj->base.dma_buf &&
11224 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11225 false))
11226 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011227 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011228 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011229}
11230
Chris Wilson60426392015-10-10 10:44:32 +010011231static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011232 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011233 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011234{
11235 struct drm_device *dev = intel_crtc->base.dev;
11236 struct drm_i915_private *dev_priv = dev->dev_private;
11237 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011238 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011239 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011240
11241 ctl = I915_READ(PLANE_CTL(pipe, 0));
11242 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011243 switch (fb->modifier[0]) {
11244 case DRM_FORMAT_MOD_NONE:
11245 break;
11246 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011247 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011248 break;
11249 case I915_FORMAT_MOD_Y_TILED:
11250 ctl |= PLANE_CTL_TILED_Y;
11251 break;
11252 case I915_FORMAT_MOD_Yf_TILED:
11253 ctl |= PLANE_CTL_TILED_YF;
11254 break;
11255 default:
11256 MISSING_CASE(fb->modifier[0]);
11257 }
Damien Lespiauff944562014-11-20 14:58:16 +000011258
11259 /*
11260 * The stride is either expressed as a multiple of 64 bytes chunks for
11261 * linear buffers or in number of tiles for tiled buffers.
11262 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011263 if (intel_rotation_90_or_270(rotation)) {
11264 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011265 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011266 stride = DIV_ROUND_UP(fb->height, tile_height);
11267 } else {
11268 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011269 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11270 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011271 }
Damien Lespiauff944562014-11-20 14:58:16 +000011272
11273 /*
11274 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11275 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11276 */
11277 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11278 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11279
Chris Wilson60426392015-10-10 10:44:32 +010011280 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011281 POSTING_READ(PLANE_SURF(pipe, 0));
11282}
11283
Chris Wilson60426392015-10-10 10:44:32 +010011284static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11285 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011286{
11287 struct drm_device *dev = intel_crtc->base.dev;
11288 struct drm_i915_private *dev_priv = dev->dev_private;
11289 struct intel_framebuffer *intel_fb =
11290 to_intel_framebuffer(intel_crtc->base.primary->fb);
11291 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011292 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011293 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011294
Sourab Gupta84c33a62014-06-02 16:47:17 +053011295 dspcntr = I915_READ(reg);
11296
Damien Lespiauc5d97472014-10-25 00:11:11 +010011297 if (obj->tiling_mode != I915_TILING_NONE)
11298 dspcntr |= DISPPLANE_TILED;
11299 else
11300 dspcntr &= ~DISPPLANE_TILED;
11301
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302 I915_WRITE(reg, dspcntr);
11303
Chris Wilson60426392015-10-10 10:44:32 +010011304 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011305 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011306}
11307
11308/*
11309 * XXX: This is the temporary way to update the plane registers until we get
11310 * around to using the usual plane update functions for MMIO flips
11311 */
Chris Wilson60426392015-10-10 10:44:32 +010011312static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011313{
Chris Wilson60426392015-10-10 10:44:32 +010011314 struct intel_crtc *crtc = mmio_flip->crtc;
11315 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011316
Chris Wilson60426392015-10-10 10:44:32 +010011317 spin_lock_irq(&crtc->base.dev->event_lock);
11318 work = crtc->unpin_work;
11319 spin_unlock_irq(&crtc->base.dev->event_lock);
11320 if (work == NULL)
11321 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011322
Chris Wilson60426392015-10-10 10:44:32 +010011323 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011324
Chris Wilson60426392015-10-10 10:44:32 +010011325 intel_pipe_update_start(crtc);
11326
11327 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011328 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011329 else
11330 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011331 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011332
Chris Wilson60426392015-10-10 10:44:32 +010011333 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011334}
11335
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011336static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011337{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011338 struct intel_mmio_flip *mmio_flip =
11339 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011340 struct intel_framebuffer *intel_fb =
11341 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11342 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011343
Chris Wilson60426392015-10-10 10:44:32 +010011344 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011345 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011346 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011347 false, NULL,
11348 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011349 i915_gem_request_unreference__unlocked(mmio_flip->req);
11350 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011351
Alex Goinsfd8e0582015-11-25 18:43:38 -080011352 /* For framebuffer backed by dmabuf, wait for fence */
11353 if (obj->base.dma_buf)
11354 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11355 false, false,
11356 MAX_SCHEDULE_TIMEOUT) < 0);
11357
Chris Wilson60426392015-10-10 10:44:32 +010011358 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011359 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011360}
11361
11362static int intel_queue_mmio_flip(struct drm_device *dev,
11363 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011364 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011365{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011366 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011367
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011368 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11369 if (mmio_flip == NULL)
11370 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011371
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011372 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011373 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011374 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011375 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011376
11377 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11378 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011379
Sourab Gupta84c33a62014-06-02 16:47:17 +053011380 return 0;
11381}
11382
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011383static int intel_default_queue_flip(struct drm_device *dev,
11384 struct drm_crtc *crtc,
11385 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011386 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011387 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011388 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011389{
11390 return -ENODEV;
11391}
11392
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011393static bool __intel_pageflip_stall_check(struct drm_device *dev,
11394 struct drm_crtc *crtc)
11395{
11396 struct drm_i915_private *dev_priv = dev->dev_private;
11397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11398 struct intel_unpin_work *work = intel_crtc->unpin_work;
11399 u32 addr;
11400
11401 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11402 return true;
11403
Chris Wilson908565c2015-08-12 13:08:22 +010011404 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11405 return false;
11406
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011407 if (!work->enable_stall_check)
11408 return false;
11409
11410 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011411 if (work->flip_queued_req &&
11412 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011413 return false;
11414
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011415 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011416 }
11417
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011418 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011419 return false;
11420
11421 /* Potential stall - if we see that the flip has happened,
11422 * assume a missed interrupt. */
11423 if (INTEL_INFO(dev)->gen >= 4)
11424 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11425 else
11426 addr = I915_READ(DSPADDR(intel_crtc->plane));
11427
11428 /* There is a potential issue here with a false positive after a flip
11429 * to the same address. We could address this by checking for a
11430 * non-incrementing frame counter.
11431 */
11432 return addr == work->gtt_offset;
11433}
11434
11435void intel_check_page_flip(struct drm_device *dev, int pipe)
11436{
11437 struct drm_i915_private *dev_priv = dev->dev_private;
11438 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011440 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011441
Dave Gordon6c51d462015-03-06 15:34:26 +000011442 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011443
11444 if (crtc == NULL)
11445 return;
11446
Daniel Vetterf3260382014-09-15 14:55:23 +020011447 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011448 work = intel_crtc->unpin_work;
11449 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011450 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011451 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011452 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011453 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011454 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011455 if (work != NULL &&
11456 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11457 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011458 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011459}
11460
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011461static int intel_crtc_page_flip(struct drm_crtc *crtc,
11462 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011463 struct drm_pending_vblank_event *event,
11464 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465{
11466 struct drm_device *dev = crtc->dev;
11467 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011468 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011469 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011471 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011472 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011473 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011474 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011475 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011476 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011477 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011478
Matt Roper2ff8fde2014-07-08 07:50:07 -070011479 /*
11480 * drm_mode_page_flip_ioctl() should already catch this, but double
11481 * check to be safe. In the future we may enable pageflipping from
11482 * a disabled primary plane.
11483 */
11484 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11485 return -EBUSY;
11486
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011487 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011488 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011489 return -EINVAL;
11490
11491 /*
11492 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11493 * Note that pitch changes could also affect these register.
11494 */
11495 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011496 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11497 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011498 return -EINVAL;
11499
Chris Wilsonf900db42014-02-20 09:26:13 +000011500 if (i915_terminally_wedged(&dev_priv->gpu_error))
11501 goto out_hang;
11502
Daniel Vetterb14c5672013-09-19 12:18:32 +020011503 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011504 if (work == NULL)
11505 return -ENOMEM;
11506
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011507 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011508 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011509 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011510 INIT_WORK(&work->work, intel_unpin_work_fn);
11511
Daniel Vetter87b6b102014-05-15 15:33:46 +020011512 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011513 if (ret)
11514 goto free_work;
11515
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011516 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011517 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011518 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011519 /* Before declaring the flip queue wedged, check if
11520 * the hardware completed the operation behind our backs.
11521 */
11522 if (__intel_pageflip_stall_check(dev, crtc)) {
11523 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11524 page_flip_completed(intel_crtc);
11525 } else {
11526 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011527 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011528
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011529 drm_crtc_vblank_put(crtc);
11530 kfree(work);
11531 return -EBUSY;
11532 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011533 }
11534 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011535 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011536
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011537 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11538 flush_workqueue(dev_priv->wq);
11539
Jesse Barnes75dfca82010-02-10 15:09:44 -080011540 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011541 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011542 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011543
Matt Roperf4510a22014-04-01 15:22:40 -070011544 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011545 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011546 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011547
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011548 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011549
Chris Wilson89ed88b2015-02-16 14:31:49 +000011550 ret = i915_mutex_lock_interruptible(dev);
11551 if (ret)
11552 goto cleanup;
11553
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011554 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011555 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011556
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011557 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011558 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011559
Wayne Boyer666a4532015-12-09 12:29:35 -080011560 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011561 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011562 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011563 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011564 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011565 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011566 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011567 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011568 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011569 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011570 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011571 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011572 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011573 }
11574
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011575 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011576
11577 /* When using CS flips, we want to emit semaphores between rings.
11578 * However, when using mmio flips we will create a task to do the
11579 * synchronisation, so all we want here is to pin the framebuffer
11580 * into the display plane and skip any waits.
11581 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011582 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011583 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011584 if (ret)
11585 goto cleanup_pending;
11586 }
11587
Ville Syrjälä3465c582016-02-15 22:54:43 +020011588 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011589 if (ret)
11590 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011591
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011592 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11593 obj, 0);
11594 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011595
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011596 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011597 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011598 if (ret)
11599 goto cleanup_unpin;
11600
John Harrisonf06cc1b2014-11-24 18:49:37 +000011601 i915_gem_request_assign(&work->flip_queued_req,
11602 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011603 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011604 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011605 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011606 if (IS_ERR(request)) {
11607 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011608 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011609 }
John Harrison6258fbe2015-05-29 17:43:48 +010011610 }
11611
11612 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011613 page_flip_flags);
11614 if (ret)
11615 goto cleanup_unpin;
11616
John Harrison6258fbe2015-05-29 17:43:48 +010011617 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011618 }
11619
John Harrison91af1272015-06-18 13:14:56 +010011620 if (request)
John Harrison75289872015-05-29 17:43:49 +010011621 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011622
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011623 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011624 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011625
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011626 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011627 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011628 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011629
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011630 intel_frontbuffer_flip_prepare(dev,
11631 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011632
Jesse Barnese5510fa2010-07-01 16:48:37 -070011633 trace_i915_flip_request(intel_crtc->plane, obj);
11634
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011635 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011636
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011637cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011638 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011639cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011640 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011641 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011642 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011643 mutex_unlock(&dev->struct_mutex);
11644cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011645 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011646 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011647
Chris Wilson89ed88b2015-02-16 14:31:49 +000011648 drm_gem_object_unreference_unlocked(&obj->base);
11649 drm_framebuffer_unreference(work->old_fb);
11650
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011651 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011652 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011653 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011654
Daniel Vetter87b6b102014-05-15 15:33:46 +020011655 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011656free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011657 kfree(work);
11658
Chris Wilsonf900db42014-02-20 09:26:13 +000011659 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011660 struct drm_atomic_state *state;
11661 struct drm_plane_state *plane_state;
11662
Chris Wilsonf900db42014-02-20 09:26:13 +000011663out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011664 state = drm_atomic_state_alloc(dev);
11665 if (!state)
11666 return -ENOMEM;
11667 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11668
11669retry:
11670 plane_state = drm_atomic_get_plane_state(state, primary);
11671 ret = PTR_ERR_OR_ZERO(plane_state);
11672 if (!ret) {
11673 drm_atomic_set_fb_for_plane(plane_state, fb);
11674
11675 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11676 if (!ret)
11677 ret = drm_atomic_commit(state);
11678 }
11679
11680 if (ret == -EDEADLK) {
11681 drm_modeset_backoff(state->acquire_ctx);
11682 drm_atomic_state_clear(state);
11683 goto retry;
11684 }
11685
11686 if (ret)
11687 drm_atomic_state_free(state);
11688
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011689 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011690 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011691 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011692 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011693 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011694 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011695 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011696}
11697
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011698
11699/**
11700 * intel_wm_need_update - Check whether watermarks need updating
11701 * @plane: drm plane
11702 * @state: new plane state
11703 *
11704 * Check current plane state versus the new one to determine whether
11705 * watermarks need to be recalculated.
11706 *
11707 * Returns true or false.
11708 */
11709static bool intel_wm_need_update(struct drm_plane *plane,
11710 struct drm_plane_state *state)
11711{
Matt Roperd21fbe82015-09-24 15:53:12 -070011712 struct intel_plane_state *new = to_intel_plane_state(state);
11713 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11714
11715 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011716 if (new->visible != cur->visible)
11717 return true;
11718
11719 if (!cur->base.fb || !new->base.fb)
11720 return false;
11721
11722 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11723 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011724 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11725 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11726 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11727 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011728 return true;
11729
11730 return false;
11731}
11732
Matt Roperd21fbe82015-09-24 15:53:12 -070011733static bool needs_scaling(struct intel_plane_state *state)
11734{
11735 int src_w = drm_rect_width(&state->src) >> 16;
11736 int src_h = drm_rect_height(&state->src) >> 16;
11737 int dst_w = drm_rect_width(&state->dst);
11738 int dst_h = drm_rect_height(&state->dst);
11739
11740 return (src_w != dst_w || src_h != dst_h);
11741}
11742
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011743int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11744 struct drm_plane_state *plane_state)
11745{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011746 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011747 struct drm_crtc *crtc = crtc_state->crtc;
11748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11749 struct drm_plane *plane = plane_state->plane;
11750 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011751 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011752 struct intel_plane_state *old_plane_state =
11753 to_intel_plane_state(plane->state);
11754 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011755 bool mode_changed = needs_modeset(crtc_state);
11756 bool was_crtc_enabled = crtc->state->active;
11757 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011758 bool turn_off, turn_on, visible, was_visible;
11759 struct drm_framebuffer *fb = plane_state->fb;
11760
11761 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11762 plane->type != DRM_PLANE_TYPE_CURSOR) {
11763 ret = skl_update_scaler_plane(
11764 to_intel_crtc_state(crtc_state),
11765 to_intel_plane_state(plane_state));
11766 if (ret)
11767 return ret;
11768 }
11769
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011770 was_visible = old_plane_state->visible;
11771 visible = to_intel_plane_state(plane_state)->visible;
11772
11773 if (!was_crtc_enabled && WARN_ON(was_visible))
11774 was_visible = false;
11775
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011776 /*
11777 * Visibility is calculated as if the crtc was on, but
11778 * after scaler setup everything depends on it being off
11779 * when the crtc isn't active.
11780 */
11781 if (!is_crtc_enabled)
11782 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011783
11784 if (!was_visible && !visible)
11785 return 0;
11786
Maarten Lankhorste8861672016-02-24 11:24:26 +010011787 if (fb != old_plane_state->base.fb)
11788 pipe_config->fb_changed = true;
11789
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011790 turn_off = was_visible && (!visible || mode_changed);
11791 turn_on = visible && (!was_visible || mode_changed);
11792
11793 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11794 plane->base.id, fb ? fb->base.id : -1);
11795
11796 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11797 plane->base.id, was_visible, visible,
11798 turn_off, turn_on, mode_changed);
11799
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011800 if (turn_on) {
11801 pipe_config->update_wm_pre = true;
11802
11803 /* must disable cxsr around plane enable/disable */
11804 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11805 pipe_config->disable_cxsr = true;
11806 } else if (turn_off) {
11807 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011808
Ville Syrjälä852eb002015-06-24 22:00:07 +030011809 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011810 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011811 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011812 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011813 /* FIXME bollocks */
11814 pipe_config->update_wm_pre = true;
11815 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011816 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011817
Matt Ropered4a6a72016-02-23 17:20:13 -080011818 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011819 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11820 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011821 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11822
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011823 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011824 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011825
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011826 /*
11827 * WaCxSRDisabledForSpriteScaling:ivb
11828 *
11829 * cstate->update_wm was already set above, so this flag will
11830 * take effect when we commit and program watermarks.
11831 */
11832 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11833 needs_scaling(to_intel_plane_state(plane_state)) &&
11834 !needs_scaling(old_plane_state))
11835 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011836
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011837 return 0;
11838}
11839
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011840static bool encoders_cloneable(const struct intel_encoder *a,
11841 const struct intel_encoder *b)
11842{
11843 /* masks could be asymmetric, so check both ways */
11844 return a == b || (a->cloneable & (1 << b->type) &&
11845 b->cloneable & (1 << a->type));
11846}
11847
11848static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11849 struct intel_crtc *crtc,
11850 struct intel_encoder *encoder)
11851{
11852 struct intel_encoder *source_encoder;
11853 struct drm_connector *connector;
11854 struct drm_connector_state *connector_state;
11855 int i;
11856
11857 for_each_connector_in_state(state, connector, connector_state, i) {
11858 if (connector_state->crtc != &crtc->base)
11859 continue;
11860
11861 source_encoder =
11862 to_intel_encoder(connector_state->best_encoder);
11863 if (!encoders_cloneable(encoder, source_encoder))
11864 return false;
11865 }
11866
11867 return true;
11868}
11869
11870static bool check_encoder_cloning(struct drm_atomic_state *state,
11871 struct intel_crtc *crtc)
11872{
11873 struct intel_encoder *encoder;
11874 struct drm_connector *connector;
11875 struct drm_connector_state *connector_state;
11876 int i;
11877
11878 for_each_connector_in_state(state, connector, connector_state, i) {
11879 if (connector_state->crtc != &crtc->base)
11880 continue;
11881
11882 encoder = to_intel_encoder(connector_state->best_encoder);
11883 if (!check_single_encoder_cloning(state, crtc, encoder))
11884 return false;
11885 }
11886
11887 return true;
11888}
11889
11890static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11891 struct drm_crtc_state *crtc_state)
11892{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011893 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011894 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011896 struct intel_crtc_state *pipe_config =
11897 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011898 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011899 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011900 bool mode_changed = needs_modeset(crtc_state);
11901
11902 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11903 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11904 return -EINVAL;
11905 }
11906
Ville Syrjälä852eb002015-06-24 22:00:07 +030011907 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011908 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011909
Maarten Lankhorstad421372015-06-15 12:33:42 +020011910 if (mode_changed && crtc_state->enable &&
11911 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011912 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011913 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11914 pipe_config);
11915 if (ret)
11916 return ret;
11917 }
11918
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011919 if (crtc_state->color_mgmt_changed) {
11920 ret = intel_color_check(crtc, crtc_state);
11921 if (ret)
11922 return ret;
11923 }
11924
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011925 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011926 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011927 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011928 if (ret) {
11929 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011930 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011931 }
11932 }
11933
11934 if (dev_priv->display.compute_intermediate_wm &&
11935 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11936 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11937 return 0;
11938
11939 /*
11940 * Calculate 'intermediate' watermarks that satisfy both the
11941 * old state and the new state. We can program these
11942 * immediately.
11943 */
11944 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11945 intel_crtc,
11946 pipe_config);
11947 if (ret) {
11948 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11949 return ret;
11950 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011951 }
11952
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011953 if (INTEL_INFO(dev)->gen >= 9) {
11954 if (mode_changed)
11955 ret = skl_update_scaler_crtc(pipe_config);
11956
11957 if (!ret)
11958 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11959 pipe_config);
11960 }
11961
11962 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011963}
11964
Jani Nikula65b38e02015-04-13 11:26:56 +030011965static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011966 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011967 .atomic_begin = intel_begin_crtc_commit,
11968 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011969 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011970};
11971
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011972static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11973{
11974 struct intel_connector *connector;
11975
11976 for_each_intel_connector(dev, connector) {
11977 if (connector->base.encoder) {
11978 connector->base.state->best_encoder =
11979 connector->base.encoder;
11980 connector->base.state->crtc =
11981 connector->base.encoder->crtc;
11982 } else {
11983 connector->base.state->best_encoder = NULL;
11984 connector->base.state->crtc = NULL;
11985 }
11986 }
11987}
11988
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011989static void
Robin Schroereba905b2014-05-18 02:24:50 +020011990connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011991 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011992{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011993 int bpp = pipe_config->pipe_bpp;
11994
11995 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11996 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011997 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011998
11999 /* Don't use an invalid EDID bpc value */
12000 if (connector->base.display_info.bpc &&
12001 connector->base.display_info.bpc * 3 < bpp) {
12002 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12003 bpp, connector->base.display_info.bpc*3);
12004 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12005 }
12006
Jani Nikula013dd9e2016-01-13 16:35:20 +020012007 /* Clamp bpp to default limit on screens without EDID 1.4 */
12008 if (connector->base.display_info.bpc == 0) {
12009 int type = connector->base.connector_type;
12010 int clamp_bpp = 24;
12011
12012 /* Fall back to 18 bpp when DP sink capability is unknown. */
12013 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12014 type == DRM_MODE_CONNECTOR_eDP)
12015 clamp_bpp = 18;
12016
12017 if (bpp > clamp_bpp) {
12018 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12019 bpp, clamp_bpp);
12020 pipe_config->pipe_bpp = clamp_bpp;
12021 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012022 }
12023}
12024
12025static int
12026compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012027 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012028{
12029 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012030 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012031 struct drm_connector *connector;
12032 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012033 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012034
Wayne Boyer666a4532015-12-09 12:29:35 -080012035 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012036 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012037 else if (INTEL_INFO(dev)->gen >= 5)
12038 bpp = 12*3;
12039 else
12040 bpp = 8*3;
12041
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012042
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012043 pipe_config->pipe_bpp = bpp;
12044
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012045 state = pipe_config->base.state;
12046
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012047 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012048 for_each_connector_in_state(state, connector, connector_state, i) {
12049 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012050 continue;
12051
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012052 connected_sink_compute_bpp(to_intel_connector(connector),
12053 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012054 }
12055
12056 return bpp;
12057}
12058
Daniel Vetter644db712013-09-19 14:53:58 +020012059static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12060{
12061 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12062 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012063 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012064 mode->crtc_hdisplay, mode->crtc_hsync_start,
12065 mode->crtc_hsync_end, mode->crtc_htotal,
12066 mode->crtc_vdisplay, mode->crtc_vsync_start,
12067 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12068}
12069
Daniel Vetterc0b03412013-05-28 12:05:54 +020012070static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012071 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012072 const char *context)
12073{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012074 struct drm_device *dev = crtc->base.dev;
12075 struct drm_plane *plane;
12076 struct intel_plane *intel_plane;
12077 struct intel_plane_state *state;
12078 struct drm_framebuffer *fb;
12079
12080 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12081 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012082
Jani Nikulada205632016-03-15 21:51:10 +020012083 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012084 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12085 pipe_config->pipe_bpp, pipe_config->dither);
12086 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12087 pipe_config->has_pch_encoder,
12088 pipe_config->fdi_lanes,
12089 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12090 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12091 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012092 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 +030012093 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012094 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012095 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12096 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12097 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012098
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012099 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 -070012100 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012101 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012102 pipe_config->dp_m2_n2.gmch_m,
12103 pipe_config->dp_m2_n2.gmch_n,
12104 pipe_config->dp_m2_n2.link_m,
12105 pipe_config->dp_m2_n2.link_n,
12106 pipe_config->dp_m2_n2.tu);
12107
Daniel Vetter55072d12014-11-20 16:10:28 +010012108 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12109 pipe_config->has_audio,
12110 pipe_config->has_infoframe);
12111
Daniel Vetterc0b03412013-05-28 12:05:54 +020012112 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012113 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012114 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012115 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12116 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012117 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012118 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12119 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012120 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12121 crtc->num_scalers,
12122 pipe_config->scaler_state.scaler_users,
12123 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012124 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12125 pipe_config->gmch_pfit.control,
12126 pipe_config->gmch_pfit.pgm_ratios,
12127 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012128 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012129 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012130 pipe_config->pch_pfit.size,
12131 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012132 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012133 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012134
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012135 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012136 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012137 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012138 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012139 pipe_config->ddi_pll_sel,
12140 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012141 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012142 pipe_config->dpll_hw_state.pll0,
12143 pipe_config->dpll_hw_state.pll1,
12144 pipe_config->dpll_hw_state.pll2,
12145 pipe_config->dpll_hw_state.pll3,
12146 pipe_config->dpll_hw_state.pll6,
12147 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012148 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012149 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012150 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012151 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012152 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12153 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12154 pipe_config->ddi_pll_sel,
12155 pipe_config->dpll_hw_state.ctrl1,
12156 pipe_config->dpll_hw_state.cfgcr1,
12157 pipe_config->dpll_hw_state.cfgcr2);
12158 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012159 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 +010012160 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012161 pipe_config->dpll_hw_state.wrpll,
12162 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012163 } else {
12164 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12165 "fp0: 0x%x, fp1: 0x%x\n",
12166 pipe_config->dpll_hw_state.dpll,
12167 pipe_config->dpll_hw_state.dpll_md,
12168 pipe_config->dpll_hw_state.fp0,
12169 pipe_config->dpll_hw_state.fp1);
12170 }
12171
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012172 DRM_DEBUG_KMS("planes on this crtc\n");
12173 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12174 intel_plane = to_intel_plane(plane);
12175 if (intel_plane->pipe != crtc->pipe)
12176 continue;
12177
12178 state = to_intel_plane_state(plane->state);
12179 fb = state->base.fb;
12180 if (!fb) {
12181 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12182 "disabled, scaler_id = %d\n",
12183 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12184 plane->base.id, intel_plane->pipe,
12185 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12186 drm_plane_index(plane), state->scaler_id);
12187 continue;
12188 }
12189
12190 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12191 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12192 plane->base.id, intel_plane->pipe,
12193 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12194 drm_plane_index(plane));
12195 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12196 fb->base.id, fb->width, fb->height, fb->pixel_format);
12197 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12198 state->scaler_id,
12199 state->src.x1 >> 16, state->src.y1 >> 16,
12200 drm_rect_width(&state->src) >> 16,
12201 drm_rect_height(&state->src) >> 16,
12202 state->dst.x1, state->dst.y1,
12203 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12204 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012205}
12206
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012207static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012208{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012209 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012210 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012211 unsigned int used_ports = 0;
12212
12213 /*
12214 * Walk the connector list instead of the encoder
12215 * list to detect the problem on ddi platforms
12216 * where there's just one encoder per digital port.
12217 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012218 drm_for_each_connector(connector, dev) {
12219 struct drm_connector_state *connector_state;
12220 struct intel_encoder *encoder;
12221
12222 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12223 if (!connector_state)
12224 connector_state = connector->state;
12225
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012226 if (!connector_state->best_encoder)
12227 continue;
12228
12229 encoder = to_intel_encoder(connector_state->best_encoder);
12230
12231 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012232
12233 switch (encoder->type) {
12234 unsigned int port_mask;
12235 case INTEL_OUTPUT_UNKNOWN:
12236 if (WARN_ON(!HAS_DDI(dev)))
12237 break;
12238 case INTEL_OUTPUT_DISPLAYPORT:
12239 case INTEL_OUTPUT_HDMI:
12240 case INTEL_OUTPUT_EDP:
12241 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12242
12243 /* the same port mustn't appear more than once */
12244 if (used_ports & port_mask)
12245 return false;
12246
12247 used_ports |= port_mask;
12248 default:
12249 break;
12250 }
12251 }
12252
12253 return true;
12254}
12255
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012256static void
12257clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12258{
12259 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012260 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012261 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012262 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012263 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012264 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012265
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012266 /* FIXME: before the switch to atomic started, a new pipe_config was
12267 * kzalloc'd. Code that depends on any field being zero should be
12268 * fixed, so that the crtc_state can be safely duplicated. For now,
12269 * only fields that are know to not cause problems are preserved. */
12270
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012271 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012272 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012273 shared_dpll = crtc_state->shared_dpll;
12274 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012275 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012276 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012277
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012278 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012279
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012280 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012281 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012282 crtc_state->shared_dpll = shared_dpll;
12283 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012284 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012285 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012286}
12287
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012288static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012289intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012290 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012291{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012292 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012293 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012294 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012295 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012296 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012297 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012298 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012299
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012300 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012301
Daniel Vettere143a212013-07-04 12:01:15 +020012302 pipe_config->cpu_transcoder =
12303 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012304
Imre Deak2960bc92013-07-30 13:36:32 +030012305 /*
12306 * Sanitize sync polarity flags based on requested ones. If neither
12307 * positive or negative polarity is requested, treat this as meaning
12308 * negative polarity.
12309 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012310 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012311 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012312 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012313
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012314 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012315 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012316 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012317
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012318 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12319 pipe_config);
12320 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012321 goto fail;
12322
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012323 /*
12324 * Determine the real pipe dimensions. Note that stereo modes can
12325 * increase the actual pipe size due to the frame doubling and
12326 * insertion of additional space for blanks between the frame. This
12327 * is stored in the crtc timings. We use the requested mode to do this
12328 * computation to clearly distinguish it from the adjusted mode, which
12329 * can be changed by the connectors in the below retry loop.
12330 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012331 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012332 &pipe_config->pipe_src_w,
12333 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012334
Daniel Vettere29c22c2013-02-21 00:00:16 +010012335encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012336 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012337 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012338 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012339
Daniel Vetter135c81b2013-07-21 21:37:09 +020012340 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012341 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12342 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012343
Daniel Vetter7758a112012-07-08 19:40:39 +020012344 /* Pass our mode to the connectors and the CRTC to give them a chance to
12345 * adjust it according to limitations or connector properties, and also
12346 * a chance to reject the mode entirely.
12347 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012348 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012349 if (connector_state->crtc != crtc)
12350 continue;
12351
12352 encoder = to_intel_encoder(connector_state->best_encoder);
12353
Daniel Vetterefea6e82013-07-21 21:36:59 +020012354 if (!(encoder->compute_config(encoder, pipe_config))) {
12355 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012356 goto fail;
12357 }
12358 }
12359
Daniel Vetterff9a6752013-06-01 17:16:21 +020012360 /* Set default port clock if not overwritten by the encoder. Needs to be
12361 * done afterwards in case the encoder adjusts the mode. */
12362 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012363 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012364 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012365
Daniel Vettera43f6e02013-06-07 23:10:32 +020012366 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012367 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012368 DRM_DEBUG_KMS("CRTC fixup failed\n");
12369 goto fail;
12370 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012371
12372 if (ret == RETRY) {
12373 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12374 ret = -EINVAL;
12375 goto fail;
12376 }
12377
12378 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12379 retry = false;
12380 goto encoder_retry;
12381 }
12382
Daniel Vettere8fa4272015-08-12 11:43:34 +020012383 /* Dithering seems to not pass-through bits correctly when it should, so
12384 * only enable it on 6bpc panels. */
12385 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012386 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012387 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012388
Daniel Vetter7758a112012-07-08 19:40:39 +020012389fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012390 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012391}
12392
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012393static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012394intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012395{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012396 struct drm_crtc *crtc;
12397 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012398 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012399
Ville Syrjälä76688512014-01-10 11:28:06 +020012400 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012401 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012402 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012403
12404 /* Update hwmode for vblank functions */
12405 if (crtc->state->active)
12406 crtc->hwmode = crtc->state->adjusted_mode;
12407 else
12408 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012409
12410 /*
12411 * Update legacy state to satisfy fbc code. This can
12412 * be removed when fbc uses the atomic state.
12413 */
12414 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12415 struct drm_plane_state *plane_state = crtc->primary->state;
12416
12417 crtc->primary->fb = plane_state->fb;
12418 crtc->x = plane_state->src_x >> 16;
12419 crtc->y = plane_state->src_y >> 16;
12420 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012421 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012422}
12423
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012424static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012425{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012426 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012427
12428 if (clock1 == clock2)
12429 return true;
12430
12431 if (!clock1 || !clock2)
12432 return false;
12433
12434 diff = abs(clock1 - clock2);
12435
12436 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12437 return true;
12438
12439 return false;
12440}
12441
Daniel Vetter25c5b262012-07-08 22:08:04 +020012442#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12443 list_for_each_entry((intel_crtc), \
12444 &(dev)->mode_config.crtc_list, \
12445 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012446 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012447
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012448static bool
12449intel_compare_m_n(unsigned int m, unsigned int n,
12450 unsigned int m2, unsigned int n2,
12451 bool exact)
12452{
12453 if (m == m2 && n == n2)
12454 return true;
12455
12456 if (exact || !m || !n || !m2 || !n2)
12457 return false;
12458
12459 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12460
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012461 if (n > n2) {
12462 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012463 m2 <<= 1;
12464 n2 <<= 1;
12465 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012466 } else if (n < n2) {
12467 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012468 m <<= 1;
12469 n <<= 1;
12470 }
12471 }
12472
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012473 if (n != n2)
12474 return false;
12475
12476 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012477}
12478
12479static bool
12480intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12481 struct intel_link_m_n *m2_n2,
12482 bool adjust)
12483{
12484 if (m_n->tu == m2_n2->tu &&
12485 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12486 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12487 intel_compare_m_n(m_n->link_m, m_n->link_n,
12488 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12489 if (adjust)
12490 *m2_n2 = *m_n;
12491
12492 return true;
12493 }
12494
12495 return false;
12496}
12497
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012498static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012499intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012500 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012501 struct intel_crtc_state *pipe_config,
12502 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012503{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012504 bool ret = true;
12505
12506#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12507 do { \
12508 if (!adjust) \
12509 DRM_ERROR(fmt, ##__VA_ARGS__); \
12510 else \
12511 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12512 } while (0)
12513
Daniel Vetter66e985c2013-06-05 13:34:20 +020012514#define PIPE_CONF_CHECK_X(name) \
12515 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012516 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012517 "(expected 0x%08x, found 0x%08x)\n", \
12518 current_config->name, \
12519 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012520 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012521 }
12522
Daniel Vetter08a24032013-04-19 11:25:34 +020012523#define PIPE_CONF_CHECK_I(name) \
12524 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012525 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012526 "(expected %i, found %i)\n", \
12527 current_config->name, \
12528 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012529 ret = false; \
12530 }
12531
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012532#define PIPE_CONF_CHECK_P(name) \
12533 if (current_config->name != pipe_config->name) { \
12534 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12535 "(expected %p, found %p)\n", \
12536 current_config->name, \
12537 pipe_config->name); \
12538 ret = false; \
12539 }
12540
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012541#define PIPE_CONF_CHECK_M_N(name) \
12542 if (!intel_compare_link_m_n(&current_config->name, \
12543 &pipe_config->name,\
12544 adjust)) { \
12545 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12546 "(expected tu %i gmch %i/%i link %i/%i, " \
12547 "found tu %i, gmch %i/%i link %i/%i)\n", \
12548 current_config->name.tu, \
12549 current_config->name.gmch_m, \
12550 current_config->name.gmch_n, \
12551 current_config->name.link_m, \
12552 current_config->name.link_n, \
12553 pipe_config->name.tu, \
12554 pipe_config->name.gmch_m, \
12555 pipe_config->name.gmch_n, \
12556 pipe_config->name.link_m, \
12557 pipe_config->name.link_n); \
12558 ret = false; \
12559 }
12560
12561#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12562 if (!intel_compare_link_m_n(&current_config->name, \
12563 &pipe_config->name, adjust) && \
12564 !intel_compare_link_m_n(&current_config->alt_name, \
12565 &pipe_config->name, adjust)) { \
12566 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12567 "(expected tu %i gmch %i/%i link %i/%i, " \
12568 "or tu %i gmch %i/%i link %i/%i, " \
12569 "found tu %i, gmch %i/%i link %i/%i)\n", \
12570 current_config->name.tu, \
12571 current_config->name.gmch_m, \
12572 current_config->name.gmch_n, \
12573 current_config->name.link_m, \
12574 current_config->name.link_n, \
12575 current_config->alt_name.tu, \
12576 current_config->alt_name.gmch_m, \
12577 current_config->alt_name.gmch_n, \
12578 current_config->alt_name.link_m, \
12579 current_config->alt_name.link_n, \
12580 pipe_config->name.tu, \
12581 pipe_config->name.gmch_m, \
12582 pipe_config->name.gmch_n, \
12583 pipe_config->name.link_m, \
12584 pipe_config->name.link_n); \
12585 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012586 }
12587
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012588/* This is required for BDW+ where there is only one set of registers for
12589 * switching between high and low RR.
12590 * This macro can be used whenever a comparison has to be made between one
12591 * hw state and multiple sw state variables.
12592 */
12593#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12594 if ((current_config->name != pipe_config->name) && \
12595 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012596 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012597 "(expected %i or %i, found %i)\n", \
12598 current_config->name, \
12599 current_config->alt_name, \
12600 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012601 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012602 }
12603
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012606 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012607 "(expected %i, found %i)\n", \
12608 current_config->name & (mask), \
12609 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012610 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012611 }
12612
Ville Syrjälä5e550652013-09-06 23:29:07 +030012613#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12614 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012616 "(expected %i, found %i)\n", \
12617 current_config->name, \
12618 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012619 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012620 }
12621
Daniel Vetterbb760062013-06-06 14:55:52 +020012622#define PIPE_CONF_QUIRK(quirk) \
12623 ((current_config->quirks | pipe_config->quirks) & (quirk))
12624
Daniel Vettereccb1402013-05-22 00:50:22 +020012625 PIPE_CONF_CHECK_I(cpu_transcoder);
12626
Daniel Vetter08a24032013-04-19 11:25:34 +020012627 PIPE_CONF_CHECK_I(has_pch_encoder);
12628 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012630
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012631 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012632 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012633
12634 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012635 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012636
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012637 if (current_config->has_drrs)
12638 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12639 } else
12640 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012641
Jani Nikulaa65347b2015-11-27 12:21:46 +020012642 PIPE_CONF_CHECK_I(has_dsi_encoder);
12643
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012650
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012657
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012658 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012659 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012660 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012661 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012662 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012663 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012664
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012665 PIPE_CONF_CHECK_I(has_audio);
12666
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012668 DRM_MODE_FLAG_INTERLACE);
12669
Daniel Vetterbb760062013-06-06 14:55:52 +020012670 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012671 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012672 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012673 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012674 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012675 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012676 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012677 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012678 DRM_MODE_FLAG_NVSYNC);
12679 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012680
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012681 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012682 /* pfit ratios are autocomputed by the hw on gen4+ */
12683 if (INTEL_INFO(dev)->gen < 4)
12684 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012685 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012686
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012687 if (!adjust) {
12688 PIPE_CONF_CHECK_I(pipe_src_w);
12689 PIPE_CONF_CHECK_I(pipe_src_h);
12690
12691 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12692 if (current_config->pch_pfit.enabled) {
12693 PIPE_CONF_CHECK_X(pch_pfit.pos);
12694 PIPE_CONF_CHECK_X(pch_pfit.size);
12695 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012696
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012697 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12698 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012699
Jesse Barnese59150d2014-01-07 13:30:45 -080012700 /* BDW+ don't expose a synchronous way to read the state */
12701 if (IS_HASWELL(dev))
12702 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012703
Ville Syrjälä282740f2013-09-04 18:30:03 +030012704 PIPE_CONF_CHECK_I(double_wide);
12705
Daniel Vetter26804af2014-06-25 22:01:55 +030012706 PIPE_CONF_CHECK_X(ddi_pll_sel);
12707
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012708 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012709 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012710 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012711 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12712 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012713 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012714 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012715 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12716 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12717 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012718
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012719 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12720 PIPE_CONF_CHECK_I(pipe_bpp);
12721
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012722 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012723 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012724
Daniel Vetter66e985c2013-06-05 13:34:20 +020012725#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012726#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012727#undef PIPE_CONF_CHECK_P
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012728#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012729#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012730#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012731#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012732#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012733
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012734 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012735}
12736
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012737static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12738 const struct intel_crtc_state *pipe_config)
12739{
12740 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012741 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012742 &pipe_config->fdi_m_n);
12743 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12744
12745 /*
12746 * FDI already provided one idea for the dotclock.
12747 * Yell if the encoder disagrees.
12748 */
12749 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12750 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12751 fdi_dotclock, dotclock);
12752 }
12753}
12754
Damien Lespiau08db6652014-11-04 17:06:52 +000012755static void check_wm_state(struct drm_device *dev)
12756{
12757 struct drm_i915_private *dev_priv = dev->dev_private;
12758 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12759 struct intel_crtc *intel_crtc;
12760 int plane;
12761
12762 if (INTEL_INFO(dev)->gen < 9)
12763 return;
12764
12765 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12766 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12767
12768 for_each_intel_crtc(dev, intel_crtc) {
12769 struct skl_ddb_entry *hw_entry, *sw_entry;
12770 const enum pipe pipe = intel_crtc->pipe;
12771
12772 if (!intel_crtc->active)
12773 continue;
12774
12775 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012776 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012777 hw_entry = &hw_ddb.plane[pipe][plane];
12778 sw_entry = &sw_ddb->plane[pipe][plane];
12779
12780 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12781 continue;
12782
12783 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12784 "(expected (%u,%u), found (%u,%u))\n",
12785 pipe_name(pipe), plane + 1,
12786 sw_entry->start, sw_entry->end,
12787 hw_entry->start, hw_entry->end);
12788 }
12789
12790 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012791 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12792 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012793
12794 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12795 continue;
12796
12797 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12798 "(expected (%u,%u), found (%u,%u))\n",
12799 pipe_name(pipe),
12800 sw_entry->start, sw_entry->end,
12801 hw_entry->start, hw_entry->end);
12802 }
12803}
12804
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012805static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012806check_connector_state(struct drm_device *dev,
12807 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012808{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012809 struct drm_connector_state *old_conn_state;
12810 struct drm_connector *connector;
12811 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012812
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012813 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12814 struct drm_encoder *encoder = connector->encoder;
12815 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012816
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012817 /* This also checks the encoder/connector hw state with the
12818 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012819 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012820
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012821 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012822 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012823 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012824}
12825
12826static void
12827check_encoder_state(struct drm_device *dev)
12828{
12829 struct intel_encoder *encoder;
12830 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012831
Damien Lespiaub2784e12014-08-05 11:29:37 +010012832 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012833 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012834 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012835
12836 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12837 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012838 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012839
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012840 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012841 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012842 continue;
12843 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012844
12845 I915_STATE_WARN(connector->base.state->crtc !=
12846 encoder->base.crtc,
12847 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012848 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012849
Rob Clarke2c719b2014-12-15 13:56:32 -050012850 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012851 "encoder's enabled state mismatch "
12852 "(expected %i, found %i)\n",
12853 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012854
12855 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012856 bool active;
12857
12858 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012859 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012860 "encoder detached but still enabled on pipe %c.\n",
12861 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012862 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012863 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012864}
12865
12866static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012867check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012868{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012869 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012870 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012871 struct drm_crtc_state *old_crtc_state;
12872 struct drm_crtc *crtc;
12873 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012874
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012875 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12877 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012878 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012879
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012880 if (!needs_modeset(crtc->state) &&
12881 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012882 continue;
12883
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012884 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12885 pipe_config = to_intel_crtc_state(old_crtc_state);
12886 memset(pipe_config, 0, sizeof(*pipe_config));
12887 pipe_config->base.crtc = crtc;
12888 pipe_config->base.state = old_state;
12889
12890 DRM_DEBUG_KMS("[CRTC:%d]\n",
12891 crtc->base.id);
12892
12893 active = dev_priv->display.get_pipe_config(intel_crtc,
12894 pipe_config);
12895
12896 /* hw state is inconsistent with the pipe quirk */
12897 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12898 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12899 active = crtc->state->active;
12900
12901 I915_STATE_WARN(crtc->state->active != active,
12902 "crtc active state doesn't match with hw state "
12903 "(expected %i, found %i)\n", crtc->state->active, active);
12904
12905 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12906 "transitional active state does not match atomic hw state "
12907 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12908
12909 for_each_encoder_on_crtc(dev, crtc, encoder) {
12910 enum pipe pipe;
12911
12912 active = encoder->get_hw_state(encoder, &pipe);
12913 I915_STATE_WARN(active != crtc->state->active,
12914 "[ENCODER:%i] active %i with crtc active %i\n",
12915 encoder->base.base.id, active, crtc->state->active);
12916
12917 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12918 "Encoder connected to wrong pipe %c\n",
12919 pipe_name(pipe));
12920
12921 if (active)
12922 encoder->get_config(encoder, pipe_config);
12923 }
12924
12925 if (!crtc->state->active)
12926 continue;
12927
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012928 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12929
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012930 sw_config = to_intel_crtc_state(crtc->state);
12931 if (!intel_pipe_config_compare(dev, sw_config,
12932 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012933 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012934 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012935 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012936 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012937 "[sw state]");
12938 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012939 }
12940}
12941
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012942static void
12943check_shared_dpll_state(struct drm_device *dev)
12944{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012945 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012946 struct intel_crtc *crtc;
12947 struct intel_dpll_hw_state dpll_hw_state;
12948 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012949
12950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012951 struct intel_shared_dpll *pll =
12952 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012953 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012954 bool active;
12955
12956 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12957
12958 DRM_DEBUG_KMS("%s\n", pll->name);
12959
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012960 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012961
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012962 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12963 "more active pll users than references: %x vs %x\n",
12964 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012965
12966 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012967 I915_STATE_WARN(!pll->on && pll->active_mask,
12968 "pll in active use but not on in sw tracking\n");
12969 I915_STATE_WARN(pll->on && !pll->active_mask,
12970 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012971 I915_STATE_WARN(pll->on != active,
12972 "pll on state mismatch (expected %i, found %i)\n",
12973 pll->on, active);
12974 }
Daniel Vetter53589012013-06-05 13:34:16 +020012975
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012976 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012977 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012978 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12979 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12980 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012981 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012982
12983 I915_STATE_WARN(pll->active_mask != active_crtcs,
12984 "pll active crtcs mismatch (expected %x, found %x)\n",
12985 pll->active_mask, active_crtcs);
12986 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12987 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12988 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012989
Rob Clarke2c719b2014-12-15 13:56:32 -050012990 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012991 sizeof(dpll_hw_state)),
12992 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012993 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012994}
12995
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012996static void
12997intel_modeset_check_state(struct drm_device *dev,
12998 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012999{
Damien Lespiau08db6652014-11-04 17:06:52 +000013000 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013001 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013002 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013003 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013004 check_shared_dpll_state(dev);
13005}
13006
Ville Syrjälä80715b22014-05-15 20:23:23 +030013007static void update_scanline_offset(struct intel_crtc *crtc)
13008{
13009 struct drm_device *dev = crtc->base.dev;
13010
13011 /*
13012 * The scanline counter increments at the leading edge of hsync.
13013 *
13014 * On most platforms it starts counting from vtotal-1 on the
13015 * first active line. That means the scanline counter value is
13016 * always one less than what we would expect. Ie. just after
13017 * start of vblank, which also occurs at start of hsync (on the
13018 * last active line), the scanline counter will read vblank_start-1.
13019 *
13020 * On gen2 the scanline counter starts counting from 1 instead
13021 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13022 * to keep the value positive), instead of adding one.
13023 *
13024 * On HSW+ the behaviour of the scanline counter depends on the output
13025 * type. For DP ports it behaves like most other platforms, but on HDMI
13026 * there's an extra 1 line difference. So we need to add two instead of
13027 * one to the value.
13028 */
13029 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013030 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013031 int vtotal;
13032
Ville Syrjälä124abe02015-09-08 13:40:45 +030013033 vtotal = adjusted_mode->crtc_vtotal;
13034 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013035 vtotal /= 2;
13036
13037 crtc->scanline_offset = vtotal - 1;
13038 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013039 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013040 crtc->scanline_offset = 2;
13041 } else
13042 crtc->scanline_offset = 1;
13043}
13044
Maarten Lankhorstad421372015-06-15 12:33:42 +020013045static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013046{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013047 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013048 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013049 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013050 struct drm_crtc *crtc;
13051 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013052 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013053
13054 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013055 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013056
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013057 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013059 struct intel_shared_dpll *old_dpll =
13060 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013061
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013062 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013063 continue;
13064
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013065 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013066
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013067 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013068 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013069
Maarten Lankhorstad421372015-06-15 12:33:42 +020013070 if (!shared_dpll)
13071 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13072
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013073 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013074 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013075}
13076
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013077/*
13078 * This implements the workaround described in the "notes" section of the mode
13079 * set sequence documentation. When going from no pipes or single pipe to
13080 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13081 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13082 */
13083static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13084{
13085 struct drm_crtc_state *crtc_state;
13086 struct intel_crtc *intel_crtc;
13087 struct drm_crtc *crtc;
13088 struct intel_crtc_state *first_crtc_state = NULL;
13089 struct intel_crtc_state *other_crtc_state = NULL;
13090 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13091 int i;
13092
13093 /* look at all crtc's that are going to be enabled in during modeset */
13094 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13095 intel_crtc = to_intel_crtc(crtc);
13096
13097 if (!crtc_state->active || !needs_modeset(crtc_state))
13098 continue;
13099
13100 if (first_crtc_state) {
13101 other_crtc_state = to_intel_crtc_state(crtc_state);
13102 break;
13103 } else {
13104 first_crtc_state = to_intel_crtc_state(crtc_state);
13105 first_pipe = intel_crtc->pipe;
13106 }
13107 }
13108
13109 /* No workaround needed? */
13110 if (!first_crtc_state)
13111 return 0;
13112
13113 /* w/a possibly needed, check how many crtc's are already enabled. */
13114 for_each_intel_crtc(state->dev, intel_crtc) {
13115 struct intel_crtc_state *pipe_config;
13116
13117 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13118 if (IS_ERR(pipe_config))
13119 return PTR_ERR(pipe_config);
13120
13121 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13122
13123 if (!pipe_config->base.active ||
13124 needs_modeset(&pipe_config->base))
13125 continue;
13126
13127 /* 2 or more enabled crtcs means no need for w/a */
13128 if (enabled_pipe != INVALID_PIPE)
13129 return 0;
13130
13131 enabled_pipe = intel_crtc->pipe;
13132 }
13133
13134 if (enabled_pipe != INVALID_PIPE)
13135 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13136 else if (other_crtc_state)
13137 other_crtc_state->hsw_workaround_pipe = first_pipe;
13138
13139 return 0;
13140}
13141
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013142static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13143{
13144 struct drm_crtc *crtc;
13145 struct drm_crtc_state *crtc_state;
13146 int ret = 0;
13147
13148 /* add all active pipes to the state */
13149 for_each_crtc(state->dev, crtc) {
13150 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13151 if (IS_ERR(crtc_state))
13152 return PTR_ERR(crtc_state);
13153
13154 if (!crtc_state->active || needs_modeset(crtc_state))
13155 continue;
13156
13157 crtc_state->mode_changed = true;
13158
13159 ret = drm_atomic_add_affected_connectors(state, crtc);
13160 if (ret)
13161 break;
13162
13163 ret = drm_atomic_add_affected_planes(state, crtc);
13164 if (ret)
13165 break;
13166 }
13167
13168 return ret;
13169}
13170
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013171static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013172{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013173 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13174 struct drm_i915_private *dev_priv = state->dev->dev_private;
13175 struct drm_crtc *crtc;
13176 struct drm_crtc_state *crtc_state;
13177 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013178
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013179 if (!check_digital_port_conflicts(state)) {
13180 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13181 return -EINVAL;
13182 }
13183
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013184 intel_state->modeset = true;
13185 intel_state->active_crtcs = dev_priv->active_crtcs;
13186
13187 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13188 if (crtc_state->active)
13189 intel_state->active_crtcs |= 1 << i;
13190 else
13191 intel_state->active_crtcs &= ~(1 << i);
13192 }
13193
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013194 /*
13195 * See if the config requires any additional preparation, e.g.
13196 * to adjust global state with pipes off. We need to do this
13197 * here so we can get the modeset_pipe updated config for the new
13198 * mode set on this crtc. For other crtcs we need to use the
13199 * adjusted_mode bits in the crtc directly.
13200 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013201 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013202 ret = dev_priv->display.modeset_calc_cdclk(state);
13203
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013204 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013205 ret = intel_modeset_all_pipes(state);
13206
13207 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013208 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013209
13210 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13211 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013212 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013213 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013214
Maarten Lankhorstad421372015-06-15 12:33:42 +020013215 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013216
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013217 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013218 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013219
Maarten Lankhorstad421372015-06-15 12:33:42 +020013220 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013221}
13222
Matt Roperaa363132015-09-24 15:53:18 -070013223/*
13224 * Handle calculation of various watermark data at the end of the atomic check
13225 * phase. The code here should be run after the per-crtc and per-plane 'check'
13226 * handlers to ensure that all derived state has been updated.
13227 */
13228static void calc_watermark_data(struct drm_atomic_state *state)
13229{
13230 struct drm_device *dev = state->dev;
13231 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13232 struct drm_crtc *crtc;
13233 struct drm_crtc_state *cstate;
13234 struct drm_plane *plane;
13235 struct drm_plane_state *pstate;
13236
13237 /*
13238 * Calculate watermark configuration details now that derived
13239 * plane/crtc state is all properly updated.
13240 */
13241 drm_for_each_crtc(crtc, dev) {
13242 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13243 crtc->state;
13244
13245 if (cstate->active)
13246 intel_state->wm_config.num_pipes_active++;
13247 }
13248 drm_for_each_legacy_plane(plane, dev) {
13249 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13250 plane->state;
13251
13252 if (!to_intel_plane_state(pstate)->visible)
13253 continue;
13254
13255 intel_state->wm_config.sprites_enabled = true;
13256 if (pstate->crtc_w != pstate->src_w >> 16 ||
13257 pstate->crtc_h != pstate->src_h >> 16)
13258 intel_state->wm_config.sprites_scaled = true;
13259 }
13260}
13261
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013262/**
13263 * intel_atomic_check - validate state object
13264 * @dev: drm device
13265 * @state: state to validate
13266 */
13267static int intel_atomic_check(struct drm_device *dev,
13268 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013269{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013270 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013271 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013272 struct drm_crtc *crtc;
13273 struct drm_crtc_state *crtc_state;
13274 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013275 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013276
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013277 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013278 if (ret)
13279 return ret;
13280
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013281 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013282 struct intel_crtc_state *pipe_config =
13283 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013284
13285 /* Catch I915_MODE_FLAG_INHERITED */
13286 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13287 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013288
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013289 if (!crtc_state->enable) {
13290 if (needs_modeset(crtc_state))
13291 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013292 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013293 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013294
Daniel Vetter26495482015-07-15 14:15:52 +020013295 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013296 continue;
13297
Daniel Vetter26495482015-07-15 14:15:52 +020013298 /* FIXME: For only active_changed we shouldn't need to do any
13299 * state recomputation at all. */
13300
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013301 ret = drm_atomic_add_affected_connectors(state, crtc);
13302 if (ret)
13303 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013304
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013305 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013306 if (ret)
13307 return ret;
13308
Jani Nikula73831232015-11-19 10:26:30 +020013309 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013310 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013311 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013312 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013313 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013314 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013315 }
13316
13317 if (needs_modeset(crtc_state)) {
13318 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013319
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013320 ret = drm_atomic_add_affected_planes(state, crtc);
13321 if (ret)
13322 return ret;
13323 }
13324
Daniel Vetter26495482015-07-15 14:15:52 +020013325 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13326 needs_modeset(crtc_state) ?
13327 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013328 }
13329
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013330 if (any_ms) {
13331 ret = intel_modeset_checks(state);
13332
13333 if (ret)
13334 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013335 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013336 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013337
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013338 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013339 if (ret)
13340 return ret;
13341
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013342 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013343 calc_watermark_data(state);
13344
13345 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013346}
13347
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013348static int intel_atomic_prepare_commit(struct drm_device *dev,
13349 struct drm_atomic_state *state,
13350 bool async)
13351{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013352 struct drm_i915_private *dev_priv = dev->dev_private;
13353 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013354 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013355 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013356 struct drm_crtc *crtc;
13357 int i, ret;
13358
13359 if (async) {
13360 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13361 return -EINVAL;
13362 }
13363
13364 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13365 ret = intel_crtc_wait_for_pending_flips(crtc);
13366 if (ret)
13367 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013368
13369 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13370 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013371 }
13372
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013373 ret = mutex_lock_interruptible(&dev->struct_mutex);
13374 if (ret)
13375 return ret;
13376
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013377 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013378 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13379 u32 reset_counter;
13380
13381 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13382 mutex_unlock(&dev->struct_mutex);
13383
13384 for_each_plane_in_state(state, plane, plane_state, i) {
13385 struct intel_plane_state *intel_plane_state =
13386 to_intel_plane_state(plane_state);
13387
13388 if (!intel_plane_state->wait_req)
13389 continue;
13390
13391 ret = __i915_wait_request(intel_plane_state->wait_req,
13392 reset_counter, true,
13393 NULL, NULL);
13394
13395 /* Swallow -EIO errors to allow updates during hw lockup. */
13396 if (ret == -EIO)
13397 ret = 0;
13398
13399 if (ret)
13400 break;
13401 }
13402
13403 if (!ret)
13404 return 0;
13405
13406 mutex_lock(&dev->struct_mutex);
13407 drm_atomic_helper_cleanup_planes(dev, state);
13408 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013409
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013410 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013411 return ret;
13412}
13413
Maarten Lankhorste8861672016-02-24 11:24:26 +010013414static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13415 struct drm_i915_private *dev_priv,
13416 unsigned crtc_mask)
13417{
13418 unsigned last_vblank_count[I915_MAX_PIPES];
13419 enum pipe pipe;
13420 int ret;
13421
13422 if (!crtc_mask)
13423 return;
13424
13425 for_each_pipe(dev_priv, pipe) {
13426 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13427
13428 if (!((1 << pipe) & crtc_mask))
13429 continue;
13430
13431 ret = drm_crtc_vblank_get(crtc);
13432 if (WARN_ON(ret != 0)) {
13433 crtc_mask &= ~(1 << pipe);
13434 continue;
13435 }
13436
13437 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13438 }
13439
13440 for_each_pipe(dev_priv, pipe) {
13441 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13442 long lret;
13443
13444 if (!((1 << pipe) & crtc_mask))
13445 continue;
13446
13447 lret = wait_event_timeout(dev->vblank[pipe].queue,
13448 last_vblank_count[pipe] !=
13449 drm_crtc_vblank_count(crtc),
13450 msecs_to_jiffies(50));
13451
13452 WARN_ON(!lret);
13453
13454 drm_crtc_vblank_put(crtc);
13455 }
13456}
13457
13458static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13459{
13460 /* fb updated, need to unpin old fb */
13461 if (crtc_state->fb_changed)
13462 return true;
13463
13464 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013465 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013466 return true;
13467
13468 /*
13469 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013470 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013471 * but added for clarity.
13472 */
13473 if (crtc_state->disable_cxsr)
13474 return true;
13475
13476 return false;
13477}
13478
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013479/**
13480 * intel_atomic_commit - commit validated state object
13481 * @dev: DRM device
13482 * @state: the top-level driver state object
13483 * @async: asynchronous commit
13484 *
13485 * This function commits a top-level state object that has been validated
13486 * with drm_atomic_helper_check().
13487 *
13488 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13489 * we can only handle plane-related operations and do not yet support
13490 * asynchronous commit.
13491 *
13492 * RETURNS
13493 * Zero for success or -errno.
13494 */
13495static int intel_atomic_commit(struct drm_device *dev,
13496 struct drm_atomic_state *state,
13497 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013498{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013499 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013500 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013501 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013502 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013503 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013504 int ret = 0, i;
13505 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013506 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013507 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013508
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013509 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013510 if (ret) {
13511 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013512 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013513 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013514
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013515 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013516 dev_priv->wm.config = intel_state->wm_config;
13517 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013518
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013519 if (intel_state->modeset) {
13520 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13521 sizeof(intel_state->min_pixclk));
13522 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013523 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013524
13525 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013526 }
13527
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013528 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13530
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013531 if (needs_modeset(crtc->state) ||
13532 to_intel_crtc_state(crtc->state)->update_pipe) {
13533 hw_check = true;
13534
13535 put_domains[to_intel_crtc(crtc)->pipe] =
13536 modeset_get_crtc_power_domains(crtc,
13537 to_intel_crtc_state(crtc->state));
13538 }
13539
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013540 if (!needs_modeset(crtc->state))
13541 continue;
13542
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013543 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013544
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013545 if (old_crtc_state->active) {
13546 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013547 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013548 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013549 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013550 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013551
13552 /*
13553 * Underruns don't always raise
13554 * interrupts, so check manually.
13555 */
13556 intel_check_cpu_fifo_underruns(dev_priv);
13557 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013558
13559 if (!crtc->state->active)
13560 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013561 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013562 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013563
Daniel Vetterea9d7582012-07-10 10:42:52 +020013564 /* Only after disabling all output pipelines that will be changed can we
13565 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013566 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013567
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013568 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013569 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013570
13571 if (dev_priv->display.modeset_commit_cdclk &&
13572 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13573 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013574 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013575
Daniel Vettera6778b32012-07-02 09:56:42 +020013576 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013577 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13579 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013580 struct intel_crtc_state *pipe_config =
13581 to_intel_crtc_state(crtc->state);
13582 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013583
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013584 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013585 update_scanline_offset(to_intel_crtc(crtc));
13586 dev_priv->display.crtc_enable(crtc);
13587 }
13588
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013589 if (!modeset &&
13590 crtc->state->active &&
13591 crtc->state->color_mgmt_changed) {
13592 /*
13593 * Only update color management when not doing
13594 * a modeset as this will be done by
13595 * crtc_enable already.
13596 */
13597 intel_color_set_csc(crtc);
13598 intel_color_load_luts(crtc);
13599 }
13600
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013601 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013602 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013603
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013604 if (crtc->state->active &&
13605 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013606 intel_fbc_enable(intel_crtc);
13607
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013608 if (crtc->state->active &&
13609 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013610 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013611
Maarten Lankhorste8861672016-02-24 11:24:26 +010013612 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13613 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013614 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013615
Daniel Vettera6778b32012-07-02 09:56:42 +020013616 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013617
Maarten Lankhorste8861672016-02-24 11:24:26 +010013618 if (!state->legacy_cursor_update)
13619 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013620
Matt Ropered4a6a72016-02-23 17:20:13 -080013621 /*
13622 * Now that the vblank has passed, we can go ahead and program the
13623 * optimal watermarks on platforms that need two-step watermark
13624 * programming.
13625 *
13626 * TODO: Move this (and other cleanup) to an async worker eventually.
13627 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013628 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013629 intel_cstate = to_intel_crtc_state(crtc->state);
13630
13631 if (dev_priv->display.optimize_watermarks)
13632 dev_priv->display.optimize_watermarks(intel_cstate);
13633 }
13634
Matt Roper177246a2016-03-04 15:59:39 -080013635 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13636 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13637
13638 if (put_domains[i])
13639 modeset_put_power_domains(dev_priv, put_domains[i]);
13640 }
13641
13642 if (intel_state->modeset)
13643 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13644
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013645 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013646 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013647 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013648
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013649 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013650 intel_modeset_check_state(dev, state);
13651
13652 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013653
Mika Kuoppala75714942015-12-16 09:26:48 +020013654 /* As one of the primary mmio accessors, KMS has a high likelihood
13655 * of triggering bugs in unclaimed access. After we finish
13656 * modesetting, see if an error has been flagged, and if so
13657 * enable debugging for the next modeset - and hope we catch
13658 * the culprit.
13659 *
13660 * XXX note that we assume display power is on at this point.
13661 * This might hold true now but we need to add pm helper to check
13662 * unclaimed only when the hardware is on, as atomic commits
13663 * can happen also when the device is completely off.
13664 */
13665 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13666
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013667 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013668}
13669
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013670void intel_crtc_restore_mode(struct drm_crtc *crtc)
13671{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013672 struct drm_device *dev = crtc->dev;
13673 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013674 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013675 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013676
13677 state = drm_atomic_state_alloc(dev);
13678 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013679 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013680 crtc->base.id);
13681 return;
13682 }
13683
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013684 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013685
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013686retry:
13687 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13688 ret = PTR_ERR_OR_ZERO(crtc_state);
13689 if (!ret) {
13690 if (!crtc_state->active)
13691 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013692
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013693 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013694 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013695 }
13696
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013697 if (ret == -EDEADLK) {
13698 drm_atomic_state_clear(state);
13699 drm_modeset_backoff(state->acquire_ctx);
13700 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013701 }
13702
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013703 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013704out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013705 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013706}
13707
Daniel Vetter25c5b262012-07-08 22:08:04 +020013708#undef for_each_intel_crtc_masked
13709
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013710static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013711 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013712 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013713 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013714 .destroy = intel_crtc_destroy,
13715 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013716 .atomic_duplicate_state = intel_crtc_duplicate_state,
13717 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013718};
13719
Matt Roper6beb8c232014-12-01 15:40:14 -080013720/**
13721 * intel_prepare_plane_fb - Prepare fb for usage on plane
13722 * @plane: drm plane to prepare for
13723 * @fb: framebuffer to prepare for presentation
13724 *
13725 * Prepares a framebuffer for usage on a display plane. Generally this
13726 * involves pinning the underlying object and updating the frontbuffer tracking
13727 * bits. Some older platforms need special physical address handling for
13728 * cursor planes.
13729 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013730 * Must be called with struct_mutex held.
13731 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013732 * Returns 0 on success, negative error code on failure.
13733 */
13734int
13735intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013736 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013737{
13738 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013739 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013740 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013741 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013742 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013743 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013744
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013745 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013746 return 0;
13747
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013748 if (old_obj) {
13749 struct drm_crtc_state *crtc_state =
13750 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13751
13752 /* Big Hammer, we also need to ensure that any pending
13753 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13754 * current scanout is retired before unpinning the old
13755 * framebuffer. Note that we rely on userspace rendering
13756 * into the buffer attached to the pipe they are waiting
13757 * on. If not, userspace generates a GPU hang with IPEHR
13758 * point to the MI_WAIT_FOR_EVENT.
13759 *
13760 * This should only fail upon a hung GPU, in which case we
13761 * can safely continue.
13762 */
13763 if (needs_modeset(crtc_state))
13764 ret = i915_gem_object_wait_rendering(old_obj, true);
13765
13766 /* Swallow -EIO errors to allow updates during hw lockup. */
13767 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013768 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013769 }
13770
Alex Goins3c28ff22015-11-25 18:43:39 -080013771 /* For framebuffer backed by dmabuf, wait for fence */
13772 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013773 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013774
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013775 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13776 false, true,
13777 MAX_SCHEDULE_TIMEOUT);
13778 if (lret == -ERESTARTSYS)
13779 return lret;
13780
13781 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013782 }
13783
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013784 if (!obj) {
13785 ret = 0;
13786 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013787 INTEL_INFO(dev)->cursor_needs_physical) {
13788 int align = IS_I830(dev) ? 16 * 1024 : 256;
13789 ret = i915_gem_object_attach_phys(obj, align);
13790 if (ret)
13791 DRM_DEBUG_KMS("failed to attach phys object\n");
13792 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013793 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013794 }
13795
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013796 if (ret == 0) {
13797 if (obj) {
13798 struct intel_plane_state *plane_state =
13799 to_intel_plane_state(new_state);
13800
13801 i915_gem_request_assign(&plane_state->wait_req,
13802 obj->last_write_req);
13803 }
13804
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013805 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013806 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013807
Matt Roper6beb8c232014-12-01 15:40:14 -080013808 return ret;
13809}
13810
Matt Roper38f3ce32014-12-02 07:45:25 -080013811/**
13812 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13813 * @plane: drm plane to clean up for
13814 * @fb: old framebuffer that was on plane
13815 *
13816 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013817 *
13818 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013819 */
13820void
13821intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013822 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013823{
13824 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013825 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013826 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013827 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13828 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013829
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013830 old_intel_state = to_intel_plane_state(old_state);
13831
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013832 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013833 return;
13834
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013835 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13836 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013837 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013838
13839 /* prepare_fb aborted? */
13840 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13841 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13842 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013843
13844 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013845}
13846
Chandra Konduru6156a452015-04-27 13:48:39 -070013847int
13848skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13849{
13850 int max_scale;
13851 struct drm_device *dev;
13852 struct drm_i915_private *dev_priv;
13853 int crtc_clock, cdclk;
13854
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013855 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013856 return DRM_PLANE_HELPER_NO_SCALING;
13857
13858 dev = intel_crtc->base.dev;
13859 dev_priv = dev->dev_private;
13860 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013861 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013862
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013863 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013864 return DRM_PLANE_HELPER_NO_SCALING;
13865
13866 /*
13867 * skl max scale is lower of:
13868 * close to 3 but not 3, -1 is for that purpose
13869 * or
13870 * cdclk/crtc_clock
13871 */
13872 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13873
13874 return max_scale;
13875}
13876
Matt Roper465c1202014-05-29 08:06:54 -070013877static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013878intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013879 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013880 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013881{
Matt Roper2b875c22014-12-01 15:40:13 -080013882 struct drm_crtc *crtc = state->base.crtc;
13883 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013884 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013885 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13886 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013887
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013888 if (INTEL_INFO(plane->dev)->gen >= 9) {
13889 /* use scaler when colorkey is not required */
13890 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13891 min_scale = 1;
13892 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13893 }
Sonika Jindald8106362015-04-10 14:37:28 +053013894 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013895 }
Sonika Jindald8106362015-04-10 14:37:28 +053013896
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013897 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13898 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013899 min_scale, max_scale,
13900 can_position, true,
13901 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013902}
13903
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013904static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13905 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013906{
13907 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013909 struct intel_crtc_state *old_intel_state =
13910 to_intel_crtc_state(old_crtc_state);
13911 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013912
Matt Roperc34c9ee2014-12-23 10:41:50 -080013913 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013914 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013915
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013916 if (modeset)
13917 return;
13918
13919 if (to_intel_crtc_state(crtc->state)->update_pipe)
13920 intel_update_pipe_config(intel_crtc, old_intel_state);
13921 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013922 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013923}
13924
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013925static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13926 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013927{
Matt Roper32b7eee2014-12-24 07:59:06 -080013928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013929
Maarten Lankhorst62852622015-09-23 16:29:38 +020013930 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013931}
13932
Matt Ropercf4c7c12014-12-04 10:27:42 -080013933/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013934 * intel_plane_destroy - destroy a plane
13935 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013936 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013937 * Common destruction function for all types of planes (primary, cursor,
13938 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013939 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013940void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013941{
13942 struct intel_plane *intel_plane = to_intel_plane(plane);
13943 drm_plane_cleanup(plane);
13944 kfree(intel_plane);
13945}
13946
Matt Roper65a3fea2015-01-21 16:35:42 -080013947const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013948 .update_plane = drm_atomic_helper_update_plane,
13949 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013950 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013951 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013952 .atomic_get_property = intel_plane_atomic_get_property,
13953 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013954 .atomic_duplicate_state = intel_plane_duplicate_state,
13955 .atomic_destroy_state = intel_plane_destroy_state,
13956
Matt Roper465c1202014-05-29 08:06:54 -070013957};
13958
13959static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13960 int pipe)
13961{
13962 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013963 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013964 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013965 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013966
13967 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13968 if (primary == NULL)
13969 return NULL;
13970
Matt Roper8e7d6882015-01-21 16:35:41 -080013971 state = intel_create_plane_state(&primary->base);
13972 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013973 kfree(primary);
13974 return NULL;
13975 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013976 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013977
Matt Roper465c1202014-05-29 08:06:54 -070013978 primary->can_scale = false;
13979 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013980 if (INTEL_INFO(dev)->gen >= 9) {
13981 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013982 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013983 }
Matt Roper465c1202014-05-29 08:06:54 -070013984 primary->pipe = pipe;
13985 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013986 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013987 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013988 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13989 primary->plane = !pipe;
13990
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013991 if (INTEL_INFO(dev)->gen >= 9) {
13992 intel_primary_formats = skl_primary_formats;
13993 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013994
13995 primary->update_plane = skylake_update_primary_plane;
13996 primary->disable_plane = skylake_disable_primary_plane;
13997 } else if (HAS_PCH_SPLIT(dev)) {
13998 intel_primary_formats = i965_primary_formats;
13999 num_formats = ARRAY_SIZE(i965_primary_formats);
14000
14001 primary->update_plane = ironlake_update_primary_plane;
14002 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014003 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014004 intel_primary_formats = i965_primary_formats;
14005 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014006
14007 primary->update_plane = i9xx_update_primary_plane;
14008 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014009 } else {
14010 intel_primary_formats = i8xx_primary_formats;
14011 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014012
14013 primary->update_plane = i9xx_update_primary_plane;
14014 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014015 }
14016
14017 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014018 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014019 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014020 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014021
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014022 if (INTEL_INFO(dev)->gen >= 4)
14023 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014024
Matt Roperea2c67b2014-12-23 10:41:52 -080014025 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14026
Matt Roper465c1202014-05-29 08:06:54 -070014027 return &primary->base;
14028}
14029
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014030void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14031{
14032 if (!dev->mode_config.rotation_property) {
14033 unsigned long flags = BIT(DRM_ROTATE_0) |
14034 BIT(DRM_ROTATE_180);
14035
14036 if (INTEL_INFO(dev)->gen >= 9)
14037 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14038
14039 dev->mode_config.rotation_property =
14040 drm_mode_create_rotation_property(dev, flags);
14041 }
14042 if (dev->mode_config.rotation_property)
14043 drm_object_attach_property(&plane->base.base,
14044 dev->mode_config.rotation_property,
14045 plane->base.state->rotation);
14046}
14047
Matt Roper3d7d6512014-06-10 08:28:13 -070014048static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014049intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014050 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014051 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014052{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014053 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014054 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014055 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014056 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014057 unsigned stride;
14058 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014059
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014060 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14061 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014062 DRM_PLANE_HELPER_NO_SCALING,
14063 DRM_PLANE_HELPER_NO_SCALING,
14064 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014065 if (ret)
14066 return ret;
14067
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014068 /* if we want to turn off the cursor ignore width and height */
14069 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014070 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014071
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014072 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014073 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014074 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14075 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014076 return -EINVAL;
14077 }
14078
Matt Roperea2c67b2014-12-23 10:41:52 -080014079 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14080 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014081 DRM_DEBUG_KMS("buffer is too small\n");
14082 return -ENOMEM;
14083 }
14084
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014085 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014086 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014087 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014088 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014089
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014090 /*
14091 * There's something wrong with the cursor on CHV pipe C.
14092 * If it straddles the left edge of the screen then
14093 * moving it away from the edge or disabling it often
14094 * results in a pipe underrun, and often that can lead to
14095 * dead pipe (constant underrun reported, and it scans
14096 * out just a solid color). To recover from that, the
14097 * display power well must be turned off and on again.
14098 * Refuse the put the cursor into that compromised position.
14099 */
14100 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14101 state->visible && state->base.crtc_x < 0) {
14102 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14103 return -EINVAL;
14104 }
14105
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014106 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014107}
14108
Matt Roperf4a2cf22014-12-01 15:40:12 -080014109static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014110intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014111 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014112{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14114
14115 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014116 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014117}
14118
14119static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014120intel_update_cursor_plane(struct drm_plane *plane,
14121 const struct intel_crtc_state *crtc_state,
14122 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014123{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014124 struct drm_crtc *crtc = crtc_state->base.crtc;
14125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014126 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014127 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014128 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014129
Matt Roperf4a2cf22014-12-01 15:40:12 -080014130 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014131 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014132 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014133 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014134 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014135 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014136
Gustavo Padovana912f122014-12-01 15:40:10 -080014137 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014138 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014139}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014140
Matt Roper3d7d6512014-06-10 08:28:13 -070014141static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14142 int pipe)
14143{
14144 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014145 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014146
14147 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14148 if (cursor == NULL)
14149 return NULL;
14150
Matt Roper8e7d6882015-01-21 16:35:41 -080014151 state = intel_create_plane_state(&cursor->base);
14152 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014153 kfree(cursor);
14154 return NULL;
14155 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014156 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014157
Matt Roper3d7d6512014-06-10 08:28:13 -070014158 cursor->can_scale = false;
14159 cursor->max_downscale = 1;
14160 cursor->pipe = pipe;
14161 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014162 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014163 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014164 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014165 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014166
14167 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014168 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014169 intel_cursor_formats,
14170 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014171 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014172
14173 if (INTEL_INFO(dev)->gen >= 4) {
14174 if (!dev->mode_config.rotation_property)
14175 dev->mode_config.rotation_property =
14176 drm_mode_create_rotation_property(dev,
14177 BIT(DRM_ROTATE_0) |
14178 BIT(DRM_ROTATE_180));
14179 if (dev->mode_config.rotation_property)
14180 drm_object_attach_property(&cursor->base.base,
14181 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014182 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014183 }
14184
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014185 if (INTEL_INFO(dev)->gen >=9)
14186 state->scaler_id = -1;
14187
Matt Roperea2c67b2014-12-23 10:41:52 -080014188 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14189
Matt Roper3d7d6512014-06-10 08:28:13 -070014190 return &cursor->base;
14191}
14192
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014193static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14194 struct intel_crtc_state *crtc_state)
14195{
14196 int i;
14197 struct intel_scaler *intel_scaler;
14198 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14199
14200 for (i = 0; i < intel_crtc->num_scalers; i++) {
14201 intel_scaler = &scaler_state->scalers[i];
14202 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014203 intel_scaler->mode = PS_SCALER_MODE_DYN;
14204 }
14205
14206 scaler_state->scaler_id = -1;
14207}
14208
Hannes Ederb358d0a2008-12-18 21:18:47 +010014209static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014210{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014211 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014212 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014213 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014214 struct drm_plane *primary = NULL;
14215 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014216 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014217
Daniel Vetter955382f2013-09-19 14:05:45 +020014218 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014219 if (intel_crtc == NULL)
14220 return;
14221
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014222 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14223 if (!crtc_state)
14224 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014225 intel_crtc->config = crtc_state;
14226 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014227 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014228
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014229 /* initialize shared scalers */
14230 if (INTEL_INFO(dev)->gen >= 9) {
14231 if (pipe == PIPE_C)
14232 intel_crtc->num_scalers = 1;
14233 else
14234 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14235
14236 skl_init_scalers(dev, intel_crtc, crtc_state);
14237 }
14238
Matt Roper465c1202014-05-29 08:06:54 -070014239 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014240 if (!primary)
14241 goto fail;
14242
14243 cursor = intel_cursor_plane_create(dev, pipe);
14244 if (!cursor)
14245 goto fail;
14246
Matt Roper465c1202014-05-29 08:06:54 -070014247 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014248 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014249 if (ret)
14250 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014251
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014252 /*
14253 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014254 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014255 */
Jesse Barnes80824002009-09-10 15:28:06 -070014256 intel_crtc->pipe = pipe;
14257 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014258 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014259 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014260 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014261 }
14262
Chris Wilson4b0e3332014-05-30 16:35:26 +030014263 intel_crtc->cursor_base = ~0;
14264 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014265 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014266
Ville Syrjälä852eb002015-06-24 22:00:07 +030014267 intel_crtc->wm.cxsr_allowed = true;
14268
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014269 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14270 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14271 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14272 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14273
Jesse Barnes79e53942008-11-07 14:24:08 -080014274 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014275
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014276 intel_color_init(&intel_crtc->base);
14277
Daniel Vetter87b6b102014-05-15 15:33:46 +020014278 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014279 return;
14280
14281fail:
14282 if (primary)
14283 drm_plane_cleanup(primary);
14284 if (cursor)
14285 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014286 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014287 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014288}
14289
Jesse Barnes752aa882013-10-31 18:55:49 +020014290enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14291{
14292 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014293 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014294
Rob Clark51fd3712013-11-19 12:10:12 -050014295 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014296
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014297 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014298 return INVALID_PIPE;
14299
14300 return to_intel_crtc(encoder->crtc)->pipe;
14301}
14302
Carl Worth08d7b3d2009-04-29 14:43:54 -070014303int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014304 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014305{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014306 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014307 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014308 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014309
Rob Clark7707e652014-07-17 23:30:04 -040014310 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014311
Rob Clark7707e652014-07-17 23:30:04 -040014312 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014313 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014314 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014315 }
14316
Rob Clark7707e652014-07-17 23:30:04 -040014317 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014318 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014319
Daniel Vetterc05422d2009-08-11 16:05:30 +020014320 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014321}
14322
Daniel Vetter66a92782012-07-12 20:08:18 +020014323static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014324{
Daniel Vetter66a92782012-07-12 20:08:18 +020014325 struct drm_device *dev = encoder->base.dev;
14326 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014327 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014328 int entry = 0;
14329
Damien Lespiaub2784e12014-08-05 11:29:37 +010014330 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014331 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014332 index_mask |= (1 << entry);
14333
Jesse Barnes79e53942008-11-07 14:24:08 -080014334 entry++;
14335 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014336
Jesse Barnes79e53942008-11-07 14:24:08 -080014337 return index_mask;
14338}
14339
Chris Wilson4d302442010-12-14 19:21:29 +000014340static bool has_edp_a(struct drm_device *dev)
14341{
14342 struct drm_i915_private *dev_priv = dev->dev_private;
14343
14344 if (!IS_MOBILE(dev))
14345 return false;
14346
14347 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14348 return false;
14349
Damien Lespiaue3589902014-02-07 19:12:50 +000014350 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014351 return false;
14352
14353 return true;
14354}
14355
Jesse Barnes84b4e042014-06-25 08:24:29 -070014356static bool intel_crt_present(struct drm_device *dev)
14357{
14358 struct drm_i915_private *dev_priv = dev->dev_private;
14359
Damien Lespiau884497e2013-12-03 13:56:23 +000014360 if (INTEL_INFO(dev)->gen >= 9)
14361 return false;
14362
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014363 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014364 return false;
14365
14366 if (IS_CHERRYVIEW(dev))
14367 return false;
14368
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014369 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14370 return false;
14371
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014372 /* DDI E can't be used if DDI A requires 4 lanes */
14373 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14374 return false;
14375
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014376 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014377 return false;
14378
14379 return true;
14380}
14381
Jesse Barnes79e53942008-11-07 14:24:08 -080014382static void intel_setup_outputs(struct drm_device *dev)
14383{
Eric Anholt725e30a2009-01-22 13:01:02 -080014384 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014385 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014386 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014387
Daniel Vetterc9093352013-06-06 22:22:47 +020014388 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014389
Jesse Barnes84b4e042014-06-25 08:24:29 -070014390 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014391 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014392
Vandana Kannanc776eb22014-08-19 12:05:01 +053014393 if (IS_BROXTON(dev)) {
14394 /*
14395 * FIXME: Broxton doesn't support port detection via the
14396 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14397 * detect the ports.
14398 */
14399 intel_ddi_init(dev, PORT_A);
14400 intel_ddi_init(dev, PORT_B);
14401 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014402
14403 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014404 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014405 int found;
14406
Jesse Barnesde31fac2015-03-06 15:53:32 -080014407 /*
14408 * Haswell uses DDI functions to detect digital outputs.
14409 * On SKL pre-D0 the strap isn't connected, so we assume
14410 * it's there.
14411 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014412 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014413 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014414 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014415 intel_ddi_init(dev, PORT_A);
14416
14417 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14418 * register */
14419 found = I915_READ(SFUSE_STRAP);
14420
14421 if (found & SFUSE_STRAP_DDIB_DETECTED)
14422 intel_ddi_init(dev, PORT_B);
14423 if (found & SFUSE_STRAP_DDIC_DETECTED)
14424 intel_ddi_init(dev, PORT_C);
14425 if (found & SFUSE_STRAP_DDID_DETECTED)
14426 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014427 /*
14428 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14429 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014430 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014431 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14432 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14433 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14434 intel_ddi_init(dev, PORT_E);
14435
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014436 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014437 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014438 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014439
14440 if (has_edp_a(dev))
14441 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014442
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014443 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014444 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014445 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014446 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014447 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014448 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014449 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014450 }
14451
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014452 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014453 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014454
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014455 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014456 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014457
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014458 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014459 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014460
Daniel Vetter270b3042012-10-27 15:52:05 +020014461 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014462 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014463 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014464 /*
14465 * The DP_DETECTED bit is the latched state of the DDC
14466 * SDA pin at boot. However since eDP doesn't require DDC
14467 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14468 * eDP ports may have been muxed to an alternate function.
14469 * Thus we can't rely on the DP_DETECTED bit alone to detect
14470 * eDP ports. Consult the VBT as well as DP_DETECTED to
14471 * detect eDP ports.
14472 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014473 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014474 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014475 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14476 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014477 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014478 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014479
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014480 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014481 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014482 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14483 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014484 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014485 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014486
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014487 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014488 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014489 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14490 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14491 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14492 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014493 }
14494
Jani Nikula3cfca972013-08-27 15:12:26 +030014495 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014496 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014497 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014498
Paulo Zanonie2debe92013-02-18 19:00:27 -030014499 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014500 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014501 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014502 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014503 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014504 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014505 }
Ma Ling27185ae2009-08-24 13:50:23 +080014506
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014507 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014508 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014509 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014510
14511 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014512
Paulo Zanonie2debe92013-02-18 19:00:27 -030014513 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014514 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014515 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014516 }
Ma Ling27185ae2009-08-24 13:50:23 +080014517
Paulo Zanonie2debe92013-02-18 19:00:27 -030014518 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014519
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014520 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014521 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014522 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014523 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014524 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014525 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014526 }
Ma Ling27185ae2009-08-24 13:50:23 +080014527
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014528 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014529 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014530 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014531 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014532 intel_dvo_init(dev);
14533
Zhenyu Wang103a1962009-11-27 11:44:36 +080014534 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014535 intel_tv_init(dev);
14536
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014537 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014538
Damien Lespiaub2784e12014-08-05 11:29:37 +010014539 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014540 encoder->base.possible_crtcs = encoder->crtc_mask;
14541 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014542 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014543 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014544
Paulo Zanonidde86e22012-12-01 12:04:25 -020014545 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014546
14547 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014548}
14549
14550static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14551{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014552 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014553 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014554
Daniel Vetteref2d6332014-02-10 18:00:38 +010014555 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014556 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014557 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014558 drm_gem_object_unreference(&intel_fb->obj->base);
14559 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014560 kfree(intel_fb);
14561}
14562
14563static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014564 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014565 unsigned int *handle)
14566{
14567 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014568 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014569
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014570 if (obj->userptr.mm) {
14571 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14572 return -EINVAL;
14573 }
14574
Chris Wilson05394f32010-11-08 19:18:58 +000014575 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014576}
14577
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014578static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14579 struct drm_file *file,
14580 unsigned flags, unsigned color,
14581 struct drm_clip_rect *clips,
14582 unsigned num_clips)
14583{
14584 struct drm_device *dev = fb->dev;
14585 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14586 struct drm_i915_gem_object *obj = intel_fb->obj;
14587
14588 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014589 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014590 mutex_unlock(&dev->struct_mutex);
14591
14592 return 0;
14593}
14594
Jesse Barnes79e53942008-11-07 14:24:08 -080014595static const struct drm_framebuffer_funcs intel_fb_funcs = {
14596 .destroy = intel_user_framebuffer_destroy,
14597 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014598 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014599};
14600
Damien Lespiaub3218032015-02-27 11:15:18 +000014601static
14602u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14603 uint32_t pixel_format)
14604{
14605 u32 gen = INTEL_INFO(dev)->gen;
14606
14607 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014608 int cpp = drm_format_plane_cpp(pixel_format, 0);
14609
Damien Lespiaub3218032015-02-27 11:15:18 +000014610 /* "The stride in bytes must not exceed the of the size of 8K
14611 * pixels and 32K bytes."
14612 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014613 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014614 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014615 return 32*1024;
14616 } else if (gen >= 4) {
14617 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14618 return 16*1024;
14619 else
14620 return 32*1024;
14621 } else if (gen >= 3) {
14622 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14623 return 8*1024;
14624 else
14625 return 16*1024;
14626 } else {
14627 /* XXX DSPC is limited to 4k tiled */
14628 return 8*1024;
14629 }
14630}
14631
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014632static int intel_framebuffer_init(struct drm_device *dev,
14633 struct intel_framebuffer *intel_fb,
14634 struct drm_mode_fb_cmd2 *mode_cmd,
14635 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014636{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014637 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014638 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014639 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014640 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014641
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014642 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14643
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014644 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14645 /* Enforce that fb modifier and tiling mode match, but only for
14646 * X-tiled. This is needed for FBC. */
14647 if (!!(obj->tiling_mode == I915_TILING_X) !=
14648 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14649 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14650 return -EINVAL;
14651 }
14652 } else {
14653 if (obj->tiling_mode == I915_TILING_X)
14654 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14655 else if (obj->tiling_mode == I915_TILING_Y) {
14656 DRM_DEBUG("No Y tiling for legacy addfb\n");
14657 return -EINVAL;
14658 }
14659 }
14660
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014661 /* Passed in modifier sanity checking. */
14662 switch (mode_cmd->modifier[0]) {
14663 case I915_FORMAT_MOD_Y_TILED:
14664 case I915_FORMAT_MOD_Yf_TILED:
14665 if (INTEL_INFO(dev)->gen < 9) {
14666 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14667 mode_cmd->modifier[0]);
14668 return -EINVAL;
14669 }
14670 case DRM_FORMAT_MOD_NONE:
14671 case I915_FORMAT_MOD_X_TILED:
14672 break;
14673 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014674 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14675 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014676 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014677 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014678
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014679 stride_alignment = intel_fb_stride_alignment(dev_priv,
14680 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014681 mode_cmd->pixel_format);
14682 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14683 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14684 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014685 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014686 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014687
Damien Lespiaub3218032015-02-27 11:15:18 +000014688 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14689 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014690 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014691 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14692 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014693 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014694 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014695 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014696 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014697
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014698 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014699 mode_cmd->pitches[0] != obj->stride) {
14700 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14701 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014702 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014703 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014704
Ville Syrjälä57779d02012-10-31 17:50:14 +020014705 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014706 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014707 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014708 case DRM_FORMAT_RGB565:
14709 case DRM_FORMAT_XRGB8888:
14710 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014711 break;
14712 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014713 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014714 DRM_DEBUG("unsupported pixel format: %s\n",
14715 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014716 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014717 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014718 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014719 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014720 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14721 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014722 DRM_DEBUG("unsupported pixel format: %s\n",
14723 drm_get_format_name(mode_cmd->pixel_format));
14724 return -EINVAL;
14725 }
14726 break;
14727 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014728 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014729 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014730 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014731 DRM_DEBUG("unsupported pixel format: %s\n",
14732 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014733 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014734 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014735 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014736 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014737 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014738 DRM_DEBUG("unsupported pixel format: %s\n",
14739 drm_get_format_name(mode_cmd->pixel_format));
14740 return -EINVAL;
14741 }
14742 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014743 case DRM_FORMAT_YUYV:
14744 case DRM_FORMAT_UYVY:
14745 case DRM_FORMAT_YVYU:
14746 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014747 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014748 DRM_DEBUG("unsupported pixel format: %s\n",
14749 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014750 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014751 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014752 break;
14753 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014754 DRM_DEBUG("unsupported pixel format: %s\n",
14755 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014756 return -EINVAL;
14757 }
14758
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014759 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14760 if (mode_cmd->offsets[0] != 0)
14761 return -EINVAL;
14762
Damien Lespiauec2c9812015-01-20 12:51:45 +000014763 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014764 mode_cmd->pixel_format,
14765 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014766 /* FIXME drm helper for size checks (especially planar formats)? */
14767 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14768 return -EINVAL;
14769
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014770 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14771 intel_fb->obj = obj;
14772
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014773 intel_fill_fb_info(dev_priv, &intel_fb->base);
14774
Jesse Barnes79e53942008-11-07 14:24:08 -080014775 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14776 if (ret) {
14777 DRM_ERROR("framebuffer init failed %d\n", ret);
14778 return ret;
14779 }
14780
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014781 intel_fb->obj->framebuffer_references++;
14782
Jesse Barnes79e53942008-11-07 14:24:08 -080014783 return 0;
14784}
14785
Jesse Barnes79e53942008-11-07 14:24:08 -080014786static struct drm_framebuffer *
14787intel_user_framebuffer_create(struct drm_device *dev,
14788 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014789 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014790{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014791 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014792 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014793 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014794
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014795 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014796 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014797 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014798 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014799
Daniel Vetter92907cb2015-11-23 09:04:05 +010014800 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014801 if (IS_ERR(fb))
14802 drm_gem_object_unreference_unlocked(&obj->base);
14803
14804 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014805}
14806
Daniel Vetter06957262015-08-10 13:34:08 +020014807#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014808static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014809{
14810}
14811#endif
14812
Jesse Barnes79e53942008-11-07 14:24:08 -080014813static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014814 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014815 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014816 .atomic_check = intel_atomic_check,
14817 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014818 .atomic_state_alloc = intel_atomic_state_alloc,
14819 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014820};
14821
Imre Deak88212942016-03-16 13:38:53 +020014822/**
14823 * intel_init_display_hooks - initialize the display modesetting hooks
14824 * @dev_priv: device private
14825 */
14826void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014827{
Imre Deak88212942016-03-16 13:38:53 +020014828 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014829 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014830 dev_priv->display.get_initial_plane_config =
14831 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014832 dev_priv->display.crtc_compute_clock =
14833 haswell_crtc_compute_clock;
14834 dev_priv->display.crtc_enable = haswell_crtc_enable;
14835 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014836 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014837 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014838 dev_priv->display.get_initial_plane_config =
14839 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014840 dev_priv->display.crtc_compute_clock =
14841 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014842 dev_priv->display.crtc_enable = haswell_crtc_enable;
14843 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014844 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014845 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014846 dev_priv->display.get_initial_plane_config =
14847 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014848 dev_priv->display.crtc_compute_clock =
14849 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014850 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14851 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014852 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014853 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014854 dev_priv->display.get_initial_plane_config =
14855 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014856 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14857 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14858 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14859 } else if (IS_VALLEYVIEW(dev_priv)) {
14860 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14861 dev_priv->display.get_initial_plane_config =
14862 i9xx_get_initial_plane_config;
14863 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014864 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14865 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014866 } else if (IS_G4X(dev_priv)) {
14867 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14868 dev_priv->display.get_initial_plane_config =
14869 i9xx_get_initial_plane_config;
14870 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14871 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14872 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014873 } else if (IS_PINEVIEW(dev_priv)) {
14874 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14875 dev_priv->display.get_initial_plane_config =
14876 i9xx_get_initial_plane_config;
14877 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14878 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14879 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014880 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014881 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014882 dev_priv->display.get_initial_plane_config =
14883 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014884 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014885 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14886 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014887 } else {
14888 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14889 dev_priv->display.get_initial_plane_config =
14890 i9xx_get_initial_plane_config;
14891 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14892 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14893 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014894 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014895
Jesse Barnese70236a2009-09-21 10:42:27 -070014896 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014897 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014898 dev_priv->display.get_display_clock_speed =
14899 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014900 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014901 dev_priv->display.get_display_clock_speed =
14902 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014903 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014904 dev_priv->display.get_display_clock_speed =
14905 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014906 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014907 dev_priv->display.get_display_clock_speed =
14908 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014909 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014910 dev_priv->display.get_display_clock_speed =
14911 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014912 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014913 dev_priv->display.get_display_clock_speed =
14914 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014915 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14916 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014917 dev_priv->display.get_display_clock_speed =
14918 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014919 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014920 dev_priv->display.get_display_clock_speed =
14921 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014922 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014923 dev_priv->display.get_display_clock_speed =
14924 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014925 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014926 dev_priv->display.get_display_clock_speed =
14927 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014928 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014929 dev_priv->display.get_display_clock_speed =
14930 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014931 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014932 dev_priv->display.get_display_clock_speed =
14933 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014934 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014935 dev_priv->display.get_display_clock_speed =
14936 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014937 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014938 dev_priv->display.get_display_clock_speed =
14939 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014940 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014941 dev_priv->display.get_display_clock_speed =
14942 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014943 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014944 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014945 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014946 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014947 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014948 dev_priv->display.get_display_clock_speed =
14949 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014950 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014951
Imre Deak88212942016-03-16 13:38:53 +020014952 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014953 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014954 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014955 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014956 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014957 /* FIXME: detect B0+ stepping and use auto training */
14958 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014959 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014960 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014961 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014962 dev_priv->display.modeset_commit_cdclk =
14963 broadwell_modeset_commit_cdclk;
14964 dev_priv->display.modeset_calc_cdclk =
14965 broadwell_modeset_calc_cdclk;
14966 }
Imre Deak88212942016-03-16 13:38:53 +020014967 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014968 dev_priv->display.modeset_commit_cdclk =
14969 valleyview_modeset_commit_cdclk;
14970 dev_priv->display.modeset_calc_cdclk =
14971 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014972 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014973 dev_priv->display.modeset_commit_cdclk =
14974 broxton_modeset_commit_cdclk;
14975 dev_priv->display.modeset_calc_cdclk =
14976 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014977 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014978
Imre Deak88212942016-03-16 13:38:53 +020014979 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014980 case 2:
14981 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14982 break;
14983
14984 case 3:
14985 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14986 break;
14987
14988 case 4:
14989 case 5:
14990 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14991 break;
14992
14993 case 6:
14994 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14995 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014996 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014997 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014998 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14999 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015000 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015001 /* Drop through - unsupported since execlist only. */
15002 default:
15003 /* Default just returns -ENODEV to indicate unsupported */
15004 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015005 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015006}
15007
Jesse Barnesb690e962010-07-19 13:53:12 -070015008/*
15009 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15010 * resume, or other times. This quirk makes sure that's the case for
15011 * affected systems.
15012 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015013static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015014{
15015 struct drm_i915_private *dev_priv = dev->dev_private;
15016
15017 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015018 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015019}
15020
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015021static void quirk_pipeb_force(struct drm_device *dev)
15022{
15023 struct drm_i915_private *dev_priv = dev->dev_private;
15024
15025 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15026 DRM_INFO("applying pipe b force quirk\n");
15027}
15028
Keith Packard435793d2011-07-12 14:56:22 -070015029/*
15030 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15031 */
15032static void quirk_ssc_force_disable(struct drm_device *dev)
15033{
15034 struct drm_i915_private *dev_priv = dev->dev_private;
15035 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015036 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015037}
15038
Carsten Emde4dca20e2012-03-15 15:56:26 +010015039/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015040 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15041 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015042 */
15043static void quirk_invert_brightness(struct drm_device *dev)
15044{
15045 struct drm_i915_private *dev_priv = dev->dev_private;
15046 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015047 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015048}
15049
Scot Doyle9c72cc62014-07-03 23:27:50 +000015050/* Some VBT's incorrectly indicate no backlight is present */
15051static void quirk_backlight_present(struct drm_device *dev)
15052{
15053 struct drm_i915_private *dev_priv = dev->dev_private;
15054 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15055 DRM_INFO("applying backlight present quirk\n");
15056}
15057
Jesse Barnesb690e962010-07-19 13:53:12 -070015058struct intel_quirk {
15059 int device;
15060 int subsystem_vendor;
15061 int subsystem_device;
15062 void (*hook)(struct drm_device *dev);
15063};
15064
Egbert Eich5f85f172012-10-14 15:46:38 +020015065/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15066struct intel_dmi_quirk {
15067 void (*hook)(struct drm_device *dev);
15068 const struct dmi_system_id (*dmi_id_list)[];
15069};
15070
15071static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15072{
15073 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15074 return 1;
15075}
15076
15077static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15078 {
15079 .dmi_id_list = &(const struct dmi_system_id[]) {
15080 {
15081 .callback = intel_dmi_reverse_brightness,
15082 .ident = "NCR Corporation",
15083 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15084 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15085 },
15086 },
15087 { } /* terminating entry */
15088 },
15089 .hook = quirk_invert_brightness,
15090 },
15091};
15092
Ben Widawskyc43b5632012-04-16 14:07:40 -070015093static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015094 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15095 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15096
Jesse Barnesb690e962010-07-19 13:53:12 -070015097 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15098 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15099
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015100 /* 830 needs to leave pipe A & dpll A up */
15101 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15102
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015103 /* 830 needs to leave pipe B & dpll B up */
15104 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15105
Keith Packard435793d2011-07-12 14:56:22 -070015106 /* Lenovo U160 cannot use SSC on LVDS */
15107 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015108
15109 /* Sony Vaio Y cannot use SSC on LVDS */
15110 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015111
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015112 /* Acer Aspire 5734Z must invert backlight brightness */
15113 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15114
15115 /* Acer/eMachines G725 */
15116 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15117
15118 /* Acer/eMachines e725 */
15119 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15120
15121 /* Acer/Packard Bell NCL20 */
15122 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15123
15124 /* Acer Aspire 4736Z */
15125 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015126
15127 /* Acer Aspire 5336 */
15128 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015129
15130 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15131 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015132
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015133 /* Acer C720 Chromebook (Core i3 4005U) */
15134 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15135
jens steinb2a96012014-10-28 20:25:53 +010015136 /* Apple Macbook 2,1 (Core 2 T7400) */
15137 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15138
Jani Nikula1b9448b2015-11-05 11:49:59 +020015139 /* Apple Macbook 4,1 */
15140 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15141
Scot Doyled4967d82014-07-03 23:27:52 +000015142 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15143 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015144
15145 /* HP Chromebook 14 (Celeron 2955U) */
15146 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015147
15148 /* Dell Chromebook 11 */
15149 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015150
15151 /* Dell Chromebook 11 (2015 version) */
15152 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015153};
15154
15155static void intel_init_quirks(struct drm_device *dev)
15156{
15157 struct pci_dev *d = dev->pdev;
15158 int i;
15159
15160 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15161 struct intel_quirk *q = &intel_quirks[i];
15162
15163 if (d->device == q->device &&
15164 (d->subsystem_vendor == q->subsystem_vendor ||
15165 q->subsystem_vendor == PCI_ANY_ID) &&
15166 (d->subsystem_device == q->subsystem_device ||
15167 q->subsystem_device == PCI_ANY_ID))
15168 q->hook(dev);
15169 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015170 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15171 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15172 intel_dmi_quirks[i].hook(dev);
15173 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015174}
15175
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015176/* Disable the VGA plane that we never use */
15177static void i915_disable_vga(struct drm_device *dev)
15178{
15179 struct drm_i915_private *dev_priv = dev->dev_private;
15180 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015181 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015182
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015183 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015184 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015185 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015186 sr1 = inb(VGA_SR_DATA);
15187 outb(sr1 | 1<<5, VGA_SR_DATA);
15188 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15189 udelay(300);
15190
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015191 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015192 POSTING_READ(vga_reg);
15193}
15194
Daniel Vetterf8175862012-04-10 15:50:11 +020015195void intel_modeset_init_hw(struct drm_device *dev)
15196{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015197 struct drm_i915_private *dev_priv = dev->dev_private;
15198
Ville Syrjäläb6283052015-06-03 15:45:07 +030015199 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015200
15201 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15202
Daniel Vetterf8175862012-04-10 15:50:11 +020015203 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015204 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015205}
15206
Matt Roperd93c0372015-12-03 11:37:41 -080015207/*
15208 * Calculate what we think the watermarks should be for the state we've read
15209 * out of the hardware and then immediately program those watermarks so that
15210 * we ensure the hardware settings match our internal state.
15211 *
15212 * We can calculate what we think WM's should be by creating a duplicate of the
15213 * current state (which was constructed during hardware readout) and running it
15214 * through the atomic check code to calculate new watermark values in the
15215 * state object.
15216 */
15217static void sanitize_watermarks(struct drm_device *dev)
15218{
15219 struct drm_i915_private *dev_priv = to_i915(dev);
15220 struct drm_atomic_state *state;
15221 struct drm_crtc *crtc;
15222 struct drm_crtc_state *cstate;
15223 struct drm_modeset_acquire_ctx ctx;
15224 int ret;
15225 int i;
15226
15227 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015228 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015229 return;
15230
15231 /*
15232 * We need to hold connection_mutex before calling duplicate_state so
15233 * that the connector loop is protected.
15234 */
15235 drm_modeset_acquire_init(&ctx, 0);
15236retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015237 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015238 if (ret == -EDEADLK) {
15239 drm_modeset_backoff(&ctx);
15240 goto retry;
15241 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015242 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015243 }
15244
15245 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15246 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015247 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015248
Matt Ropered4a6a72016-02-23 17:20:13 -080015249 /*
15250 * Hardware readout is the only time we don't want to calculate
15251 * intermediate watermarks (since we don't trust the current
15252 * watermarks).
15253 */
15254 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15255
Matt Roperd93c0372015-12-03 11:37:41 -080015256 ret = intel_atomic_check(dev, state);
15257 if (ret) {
15258 /*
15259 * If we fail here, it means that the hardware appears to be
15260 * programmed in a way that shouldn't be possible, given our
15261 * understanding of watermark requirements. This might mean a
15262 * mistake in the hardware readout code or a mistake in the
15263 * watermark calculations for a given platform. Raise a WARN
15264 * so that this is noticeable.
15265 *
15266 * If this actually happens, we'll have to just leave the
15267 * BIOS-programmed watermarks untouched and hope for the best.
15268 */
15269 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015270 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015271 }
15272
15273 /* Write calculated watermark values back */
15274 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15275 for_each_crtc_in_state(state, crtc, cstate, i) {
15276 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15277
Matt Ropered4a6a72016-02-23 17:20:13 -080015278 cs->wm.need_postvbl_update = true;
15279 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015280 }
15281
15282 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015283fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015284 drm_modeset_drop_locks(&ctx);
15285 drm_modeset_acquire_fini(&ctx);
15286}
15287
Jesse Barnes79e53942008-11-07 14:24:08 -080015288void intel_modeset_init(struct drm_device *dev)
15289{
Jesse Barnes652c3932009-08-17 13:31:43 -070015290 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015291 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015292 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015293 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015294
15295 drm_mode_config_init(dev);
15296
15297 dev->mode_config.min_width = 0;
15298 dev->mode_config.min_height = 0;
15299
Dave Airlie019d96c2011-09-29 16:20:42 +010015300 dev->mode_config.preferred_depth = 24;
15301 dev->mode_config.prefer_shadow = 1;
15302
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015303 dev->mode_config.allow_fb_modifiers = true;
15304
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015305 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015306
Jesse Barnesb690e962010-07-19 13:53:12 -070015307 intel_init_quirks(dev);
15308
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015309 intel_init_pm(dev);
15310
Ben Widawskye3c74752013-04-05 13:12:39 -070015311 if (INTEL_INFO(dev)->num_pipes == 0)
15312 return;
15313
Lukas Wunner69f92f62015-07-15 13:57:35 +020015314 /*
15315 * There may be no VBT; and if the BIOS enabled SSC we can
15316 * just keep using it to avoid unnecessary flicker. Whereas if the
15317 * BIOS isn't using it, don't assume it will work even if the VBT
15318 * indicates as much.
15319 */
15320 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15321 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15322 DREF_SSC1_ENABLE);
15323
15324 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15325 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15326 bios_lvds_use_ssc ? "en" : "dis",
15327 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15328 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15329 }
15330 }
15331
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015332 if (IS_GEN2(dev)) {
15333 dev->mode_config.max_width = 2048;
15334 dev->mode_config.max_height = 2048;
15335 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015336 dev->mode_config.max_width = 4096;
15337 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015338 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015339 dev->mode_config.max_width = 8192;
15340 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015341 }
Damien Lespiau068be562014-03-28 14:17:49 +000015342
Ville Syrjälädc41c152014-08-13 11:57:05 +030015343 if (IS_845G(dev) || IS_I865G(dev)) {
15344 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15345 dev->mode_config.cursor_height = 1023;
15346 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015347 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15348 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15349 } else {
15350 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15351 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15352 }
15353
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015354 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015355
Zhao Yakui28c97732009-10-09 11:39:41 +080015356 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015357 INTEL_INFO(dev)->num_pipes,
15358 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015359
Damien Lespiau055e3932014-08-18 13:49:10 +010015360 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015361 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015362 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015363 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015364 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015365 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015366 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015367 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015368 }
15369
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015370 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015371 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015372 intel_update_cdclk(dev);
15373
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015374 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015375
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015376 /* Just disable it once at startup */
15377 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015378 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015379
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015380 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015381 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015382 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015383
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015384 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015385 struct intel_initial_plane_config plane_config = {};
15386
Jesse Barnes46f297f2014-03-07 08:57:48 -080015387 if (!crtc->active)
15388 continue;
15389
Jesse Barnes46f297f2014-03-07 08:57:48 -080015390 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015391 * Note that reserving the BIOS fb up front prevents us
15392 * from stuffing other stolen allocations like the ring
15393 * on top. This prevents some ugliness at boot time, and
15394 * can even allow for smooth boot transitions if the BIOS
15395 * fb is large enough for the active pipe configuration.
15396 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015397 dev_priv->display.get_initial_plane_config(crtc,
15398 &plane_config);
15399
15400 /*
15401 * If the fb is shared between multiple heads, we'll
15402 * just get the first one.
15403 */
15404 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015405 }
Matt Roperd93c0372015-12-03 11:37:41 -080015406
15407 /*
15408 * Make sure hardware watermarks really match the state we read out.
15409 * Note that we need to do this after reconstructing the BIOS fb's
15410 * since the watermark calculation done here will use pstate->fb.
15411 */
15412 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015413}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015414
Daniel Vetter7fad7982012-07-04 17:51:47 +020015415static void intel_enable_pipe_a(struct drm_device *dev)
15416{
15417 struct intel_connector *connector;
15418 struct drm_connector *crt = NULL;
15419 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015420 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015421
15422 /* We can't just switch on the pipe A, we need to set things up with a
15423 * proper mode and output configuration. As a gross hack, enable pipe A
15424 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015425 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015426 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15427 crt = &connector->base;
15428 break;
15429 }
15430 }
15431
15432 if (!crt)
15433 return;
15434
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015435 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015436 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015437}
15438
Daniel Vetterfa555832012-10-10 23:14:00 +020015439static bool
15440intel_check_plane_mapping(struct intel_crtc *crtc)
15441{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015442 struct drm_device *dev = crtc->base.dev;
15443 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015444 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015445
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015446 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015447 return true;
15448
Ville Syrjälä649636e2015-09-22 19:50:01 +030015449 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015450
15451 if ((val & DISPLAY_PLANE_ENABLE) &&
15452 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15453 return false;
15454
15455 return true;
15456}
15457
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015458static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15459{
15460 struct drm_device *dev = crtc->base.dev;
15461 struct intel_encoder *encoder;
15462
15463 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15464 return true;
15465
15466 return false;
15467}
15468
Ville Syrjälädd756192016-02-17 21:28:45 +020015469static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15470{
15471 struct drm_device *dev = encoder->base.dev;
15472 struct intel_connector *connector;
15473
15474 for_each_connector_on_encoder(dev, &encoder->base, connector)
15475 return true;
15476
15477 return false;
15478}
15479
Daniel Vetter24929352012-07-02 20:28:59 +020015480static void intel_sanitize_crtc(struct intel_crtc *crtc)
15481{
15482 struct drm_device *dev = crtc->base.dev;
15483 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015484 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015485
Daniel Vetter24929352012-07-02 20:28:59 +020015486 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015487 if (!transcoder_is_dsi(cpu_transcoder)) {
15488 i915_reg_t reg = PIPECONF(cpu_transcoder);
15489
15490 I915_WRITE(reg,
15491 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15492 }
Daniel Vetter24929352012-07-02 20:28:59 +020015493
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015494 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015495 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015496 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015497 struct intel_plane *plane;
15498
Daniel Vetter96256042015-02-13 21:03:42 +010015499 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015500
15501 /* Disable everything but the primary plane */
15502 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15503 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15504 continue;
15505
15506 plane->disable_plane(&plane->base, &crtc->base);
15507 }
Daniel Vetter96256042015-02-13 21:03:42 +010015508 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015509
Daniel Vetter24929352012-07-02 20:28:59 +020015510 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015511 * disable the crtc (and hence change the state) if it is wrong. Note
15512 * that gen4+ has a fixed plane -> pipe mapping. */
15513 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015514 bool plane;
15515
Daniel Vetter24929352012-07-02 20:28:59 +020015516 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15517 crtc->base.base.id);
15518
15519 /* Pipe has the wrong plane attached and the plane is active.
15520 * Temporarily change the plane mapping and disable everything
15521 * ... */
15522 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015523 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015524 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015525 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015526 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015527 }
Daniel Vetter24929352012-07-02 20:28:59 +020015528
Daniel Vetter7fad7982012-07-04 17:51:47 +020015529 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15530 crtc->pipe == PIPE_A && !crtc->active) {
15531 /* BIOS forgot to enable pipe A, this mostly happens after
15532 * resume. Force-enable the pipe to fix this, the update_dpms
15533 * call below we restore the pipe to the right state, but leave
15534 * the required bits on. */
15535 intel_enable_pipe_a(dev);
15536 }
15537
Daniel Vetter24929352012-07-02 20:28:59 +020015538 /* Adjust the state of the output pipe according to whether we
15539 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015540 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015541 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015542
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015543 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015544 /*
15545 * We start out with underrun reporting disabled to avoid races.
15546 * For correct bookkeeping mark this on active crtcs.
15547 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015548 * Also on gmch platforms we dont have any hardware bits to
15549 * disable the underrun reporting. Which means we need to start
15550 * out with underrun reporting disabled also on inactive pipes,
15551 * since otherwise we'll complain about the garbage we read when
15552 * e.g. coming up after runtime pm.
15553 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015554 * No protection against concurrent access is required - at
15555 * worst a fifo underrun happens which also sets this to false.
15556 */
15557 crtc->cpu_fifo_underrun_disabled = true;
15558 crtc->pch_fifo_underrun_disabled = true;
15559 }
Daniel Vetter24929352012-07-02 20:28:59 +020015560}
15561
15562static void intel_sanitize_encoder(struct intel_encoder *encoder)
15563{
15564 struct intel_connector *connector;
15565 struct drm_device *dev = encoder->base.dev;
15566
15567 /* We need to check both for a crtc link (meaning that the
15568 * encoder is active and trying to read from a pipe) and the
15569 * pipe itself being active. */
15570 bool has_active_crtc = encoder->base.crtc &&
15571 to_intel_crtc(encoder->base.crtc)->active;
15572
Ville Syrjälädd756192016-02-17 21:28:45 +020015573 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015574 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15575 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015576 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015577
15578 /* Connector is active, but has no active pipe. This is
15579 * fallout from our resume register restoring. Disable
15580 * the encoder manually again. */
15581 if (encoder->base.crtc) {
15582 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15583 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015584 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015585 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015586 if (encoder->post_disable)
15587 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015588 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015589 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015590
15591 /* Inconsistent output/port/pipe state happens presumably due to
15592 * a bug in one of the get_hw_state functions. Or someplace else
15593 * in our code, like the register restore mess on resume. Clamp
15594 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015595 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015596 if (connector->encoder != encoder)
15597 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015598 connector->base.dpms = DRM_MODE_DPMS_OFF;
15599 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015600 }
15601 }
15602 /* Enabled encoders without active connectors will be fixed in
15603 * the crtc fixup. */
15604}
15605
Imre Deak04098752014-02-18 00:02:16 +020015606void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015607{
15608 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015609 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015610
Imre Deak04098752014-02-18 00:02:16 +020015611 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15612 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15613 i915_disable_vga(dev);
15614 }
15615}
15616
15617void i915_redisable_vga(struct drm_device *dev)
15618{
15619 struct drm_i915_private *dev_priv = dev->dev_private;
15620
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015621 /* This function can be called both from intel_modeset_setup_hw_state or
15622 * at a very early point in our resume sequence, where the power well
15623 * structures are not yet restored. Since this function is at a very
15624 * paranoid "someone might have enabled VGA while we were not looking"
15625 * level, just check if the power well is enabled instead of trying to
15626 * follow the "don't touch the power well if we don't need it" policy
15627 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015628 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015629 return;
15630
Imre Deak04098752014-02-18 00:02:16 +020015631 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015632
15633 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015634}
15635
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015636static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015637{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015638 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015639
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015640 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015641}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015642
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015643/* FIXME read out full plane state for all planes */
15644static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015645{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015646 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015647 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015648 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015649
Matt Roper19b8d382015-09-24 15:53:17 -070015650 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015651 primary_get_hw_state(to_intel_plane(primary));
15652
15653 if (plane_state->visible)
15654 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015655}
15656
Daniel Vetter30e984d2013-06-05 13:34:17 +020015657static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015658{
15659 struct drm_i915_private *dev_priv = dev->dev_private;
15660 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015661 struct intel_crtc *crtc;
15662 struct intel_encoder *encoder;
15663 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015664 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015665
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015666 dev_priv->active_crtcs = 0;
15667
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015668 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015669 struct intel_crtc_state *crtc_state = crtc->config;
15670 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015671
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015672 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15673 memset(crtc_state, 0, sizeof(*crtc_state));
15674 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015675
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015676 crtc_state->base.active = crtc_state->base.enable =
15677 dev_priv->display.get_pipe_config(crtc, crtc_state);
15678
15679 crtc->base.enabled = crtc_state->base.enable;
15680 crtc->active = crtc_state->base.active;
15681
15682 if (crtc_state->base.active) {
15683 dev_priv->active_crtcs |= 1 << crtc->pipe;
15684
15685 if (IS_BROADWELL(dev_priv)) {
15686 pixclk = ilk_pipe_pixel_rate(crtc_state);
15687
15688 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15689 if (crtc_state->ips_enabled)
15690 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15691 } else if (IS_VALLEYVIEW(dev_priv) ||
15692 IS_CHERRYVIEW(dev_priv) ||
15693 IS_BROXTON(dev_priv))
15694 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15695 else
15696 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15697 }
15698
15699 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015700
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015701 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015702
15703 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15704 crtc->base.base.id,
15705 crtc->active ? "enabled" : "disabled");
15706 }
15707
Daniel Vetter53589012013-06-05 13:34:16 +020015708 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15709 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15710
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015711 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15712 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015713 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015714 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015715 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015716 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015717 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015718 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015719
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015720 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015721 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015722 }
15723
Damien Lespiaub2784e12014-08-05 11:29:37 +010015724 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015725 pipe = 0;
15726
15727 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015728 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15729 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015730 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015731 } else {
15732 encoder->base.crtc = NULL;
15733 }
15734
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015735 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015736 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015737 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015738 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015739 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015740 }
15741
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015742 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015743 if (connector->get_hw_state(connector)) {
15744 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015745
15746 encoder = connector->encoder;
15747 connector->base.encoder = &encoder->base;
15748
15749 if (encoder->base.crtc &&
15750 encoder->base.crtc->state->active) {
15751 /*
15752 * This has to be done during hardware readout
15753 * because anything calling .crtc_disable may
15754 * rely on the connector_mask being accurate.
15755 */
15756 encoder->base.crtc->state->connector_mask |=
15757 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015758 encoder->base.crtc->state->encoder_mask |=
15759 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015760 }
15761
Daniel Vetter24929352012-07-02 20:28:59 +020015762 } else {
15763 connector->base.dpms = DRM_MODE_DPMS_OFF;
15764 connector->base.encoder = NULL;
15765 }
15766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15767 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015768 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015769 connector->base.encoder ? "enabled" : "disabled");
15770 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015771
15772 for_each_intel_crtc(dev, crtc) {
15773 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15774
15775 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15776 if (crtc->base.state->active) {
15777 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15778 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15779 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15780
15781 /*
15782 * The initial mode needs to be set in order to keep
15783 * the atomic core happy. It wants a valid mode if the
15784 * crtc's enabled, so we do the above call.
15785 *
15786 * At this point some state updated by the connectors
15787 * in their ->detect() callback has not run yet, so
15788 * no recalculation can be done yet.
15789 *
15790 * Even if we could do a recalculation and modeset
15791 * right now it would cause a double modeset if
15792 * fbdev or userspace chooses a different initial mode.
15793 *
15794 * If that happens, someone indicated they wanted a
15795 * mode change, which means it's safe to do a full
15796 * recalculation.
15797 */
15798 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015799
15800 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15801 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015802 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015803
15804 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015805 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015806}
15807
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015808/* Scan out the current hw modeset state,
15809 * and sanitizes it to the current state
15810 */
15811static void
15812intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015813{
15814 struct drm_i915_private *dev_priv = dev->dev_private;
15815 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015816 struct intel_crtc *crtc;
15817 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015818 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015819
15820 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015821
15822 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015823 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015824 intel_sanitize_encoder(encoder);
15825 }
15826
Damien Lespiau055e3932014-08-18 13:49:10 +010015827 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015828 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15829 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015830 intel_dump_pipe_config(crtc, crtc->config,
15831 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015832 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015833
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015834 intel_modeset_update_connector_atomic_state(dev);
15835
Daniel Vetter35c95372013-07-17 06:55:04 +020015836 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15837 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15838
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015839 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015840 continue;
15841
15842 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15843
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015844 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015845 pll->on = false;
15846 }
15847
Wayne Boyer666a4532015-12-09 12:29:35 -080015848 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015849 vlv_wm_get_hw_state(dev);
15850 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015851 skl_wm_get_hw_state(dev);
15852 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015853 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015854
15855 for_each_intel_crtc(dev, crtc) {
15856 unsigned long put_domains;
15857
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015858 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015859 if (WARN_ON(put_domains))
15860 modeset_put_power_domains(dev_priv, put_domains);
15861 }
15862 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015863
15864 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015865}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015866
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015867void intel_display_resume(struct drm_device *dev)
15868{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015869 struct drm_i915_private *dev_priv = to_i915(dev);
15870 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15871 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015872 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015873 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015874
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015875 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015876
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015877 /*
15878 * This is a cludge because with real atomic modeset mode_config.mutex
15879 * won't be taken. Unfortunately some probed state like
15880 * audio_codec_enable is still protected by mode_config.mutex, so lock
15881 * it here for now.
15882 */
15883 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015884 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015885
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015886retry:
15887 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015888
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015889 if (ret == 0 && !setup) {
15890 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015891
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015892 intel_modeset_setup_hw_state(dev);
15893 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015894 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015895
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015896 if (ret == 0 && state) {
15897 struct drm_crtc_state *crtc_state;
15898 struct drm_crtc *crtc;
15899 int i;
15900
15901 state->acquire_ctx = &ctx;
15902
15903 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15904 /*
15905 * Force recalculation even if we restore
15906 * current state. With fast modeset this may not result
15907 * in a modeset when the state is compatible.
15908 */
15909 crtc_state->mode_changed = true;
15910 }
15911
15912 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015913 }
15914
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015915 if (ret == -EDEADLK) {
15916 drm_modeset_backoff(&ctx);
15917 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015918 }
15919
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015920 drm_modeset_drop_locks(&ctx);
15921 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015922 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015923
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015924 if (ret) {
15925 DRM_ERROR("Restoring old state failed with %i\n", ret);
15926 drm_atomic_state_free(state);
15927 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015928}
15929
15930void intel_modeset_gem_init(struct drm_device *dev)
15931{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015932 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015933 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015934 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015935
Imre Deakae484342014-03-31 15:10:44 +030015936 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015937
Chris Wilson1833b132012-05-09 11:56:28 +010015938 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015939
15940 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015941
15942 /*
15943 * Make sure any fbs we allocated at startup are properly
15944 * pinned & fenced. When we do the allocation it's too early
15945 * for this.
15946 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015947 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015948 obj = intel_fb_obj(c->primary->fb);
15949 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015950 continue;
15951
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015952 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015953 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15954 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015955 mutex_unlock(&dev->struct_mutex);
15956 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015957 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15958 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015959 drm_framebuffer_unreference(c->primary->fb);
15960 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015961 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015962 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015963 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015964 }
15965 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015966
15967 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015968}
15969
Imre Deak4932e2c2014-02-11 17:12:48 +020015970void intel_connector_unregister(struct intel_connector *intel_connector)
15971{
15972 struct drm_connector *connector = &intel_connector->base;
15973
15974 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015975 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015976}
15977
Jesse Barnes79e53942008-11-07 14:24:08 -080015978void intel_modeset_cleanup(struct drm_device *dev)
15979{
Jesse Barnes652c3932009-08-17 13:31:43 -070015980 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015981 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015982
Imre Deak2eb52522014-11-19 15:30:05 +020015983 intel_disable_gt_powersave(dev);
15984
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015985 intel_backlight_unregister(dev);
15986
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015987 /*
15988 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015989 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015990 * experience fancy races otherwise.
15991 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015992 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015993
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015994 /*
15995 * Due to the hpd irq storm handling the hotplug work can re-arm the
15996 * poll handlers. Hence disable polling after hpd handling is shut down.
15997 */
Keith Packardf87ea762010-10-03 19:36:26 -070015998 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015999
Jesse Barnes723bfd72010-10-07 16:01:13 -070016000 intel_unregister_dsm_handler();
16001
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016002 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016003
Chris Wilson1630fe72011-07-08 12:22:42 +010016004 /* flush any delayed tasks or pending work */
16005 flush_scheduled_work();
16006
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016007 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016008 for_each_intel_connector(dev, connector)
16009 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016010
Jesse Barnes79e53942008-11-07 14:24:08 -080016011 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016012
16013 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016014
Imre Deakae484342014-03-31 15:10:44 +030016015 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016016
16017 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016018}
16019
Dave Airlie28d52042009-09-21 14:33:58 +100016020/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016021 * Return which encoder is currently attached for connector.
16022 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016023struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016024{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016025 return &intel_attached_encoder(connector)->base;
16026}
Jesse Barnes79e53942008-11-07 14:24:08 -080016027
Chris Wilsondf0e9242010-09-09 16:20:55 +010016028void intel_connector_attach_encoder(struct intel_connector *connector,
16029 struct intel_encoder *encoder)
16030{
16031 connector->encoder = encoder;
16032 drm_mode_connector_attach_encoder(&connector->base,
16033 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016034}
Dave Airlie28d52042009-09-21 14:33:58 +100016035
16036/*
16037 * set vga decode state - true == enable VGA decode
16038 */
16039int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16040{
16041 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016042 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016043 u16 gmch_ctrl;
16044
Chris Wilson75fa0412014-02-07 18:37:02 -020016045 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16046 DRM_ERROR("failed to read control word\n");
16047 return -EIO;
16048 }
16049
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016050 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16051 return 0;
16052
Dave Airlie28d52042009-09-21 14:33:58 +100016053 if (state)
16054 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16055 else
16056 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016057
16058 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16059 DRM_ERROR("failed to write control word\n");
16060 return -EIO;
16061 }
16062
Dave Airlie28d52042009-09-21 14:33:58 +100016063 return 0;
16064}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016065
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016066struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016067
16068 u32 power_well_driver;
16069
Chris Wilson63b66e52013-08-08 15:12:06 +020016070 int num_transcoders;
16071
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016072 struct intel_cursor_error_state {
16073 u32 control;
16074 u32 position;
16075 u32 base;
16076 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016077 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016078
16079 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016080 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016081 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016082 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016083 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016084
16085 struct intel_plane_error_state {
16086 u32 control;
16087 u32 stride;
16088 u32 size;
16089 u32 pos;
16090 u32 addr;
16091 u32 surface;
16092 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016093 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016094
16095 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016096 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016097 enum transcoder cpu_transcoder;
16098
16099 u32 conf;
16100
16101 u32 htotal;
16102 u32 hblank;
16103 u32 hsync;
16104 u32 vtotal;
16105 u32 vblank;
16106 u32 vsync;
16107 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016108};
16109
16110struct intel_display_error_state *
16111intel_display_capture_error_state(struct drm_device *dev)
16112{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016113 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016114 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016115 int transcoders[] = {
16116 TRANSCODER_A,
16117 TRANSCODER_B,
16118 TRANSCODER_C,
16119 TRANSCODER_EDP,
16120 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016121 int i;
16122
Chris Wilson63b66e52013-08-08 15:12:06 +020016123 if (INTEL_INFO(dev)->num_pipes == 0)
16124 return NULL;
16125
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016126 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016127 if (error == NULL)
16128 return NULL;
16129
Imre Deak190be112013-11-25 17:15:31 +020016130 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016131 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16132
Damien Lespiau055e3932014-08-18 13:49:10 +010016133 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016134 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016135 __intel_display_power_is_enabled(dev_priv,
16136 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016137 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016138 continue;
16139
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016140 error->cursor[i].control = I915_READ(CURCNTR(i));
16141 error->cursor[i].position = I915_READ(CURPOS(i));
16142 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016143
16144 error->plane[i].control = I915_READ(DSPCNTR(i));
16145 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016146 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016147 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016148 error->plane[i].pos = I915_READ(DSPPOS(i));
16149 }
Paulo Zanonica291362013-03-06 20:03:14 -030016150 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16151 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016152 if (INTEL_INFO(dev)->gen >= 4) {
16153 error->plane[i].surface = I915_READ(DSPSURF(i));
16154 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16155 }
16156
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016157 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016158
Sonika Jindal3abfce72014-07-21 15:23:43 +053016159 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016160 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016161 }
16162
Jani Nikula4d1de972016-03-18 17:05:42 +020016163 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016164 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16165 if (HAS_DDI(dev_priv->dev))
16166 error->num_transcoders++; /* Account for eDP. */
16167
16168 for (i = 0; i < error->num_transcoders; i++) {
16169 enum transcoder cpu_transcoder = transcoders[i];
16170
Imre Deakddf9c532013-11-27 22:02:02 +020016171 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016172 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016173 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016174 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016175 continue;
16176
Chris Wilson63b66e52013-08-08 15:12:06 +020016177 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16178
16179 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16180 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16181 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16182 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16183 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16184 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16185 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016186 }
16187
16188 return error;
16189}
16190
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016191#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16192
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016193void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016194intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016195 struct drm_device *dev,
16196 struct intel_display_error_state *error)
16197{
Damien Lespiau055e3932014-08-18 13:49:10 +010016198 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016199 int i;
16200
Chris Wilson63b66e52013-08-08 15:12:06 +020016201 if (!error)
16202 return;
16203
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016204 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016205 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016206 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016207 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016208 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016209 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016210 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016211 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016212 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016213 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016214
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016215 err_printf(m, "Plane [%d]:\n", i);
16216 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16217 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016218 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016219 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16220 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016221 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016222 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016223 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016224 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016225 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16226 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016227 }
16228
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016229 err_printf(m, "Cursor [%d]:\n", i);
16230 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16231 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16232 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016233 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016234
16235 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016236 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016237 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016238 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016239 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016240 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16241 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16242 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16243 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16244 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16245 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16246 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16247 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016248}