blob: dc0d1b61dae4f960c28f591c66bba8692de31268 [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"
Chris Wilson5d723d72016-08-04 16:32:35 +010037#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010040#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020041#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070042#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080043#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080044#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010045#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070047#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080049#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080050#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080051
Daniel Vetter5a21b662016-05-24 17:13:53 +020052static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
Matt Roper465c1202014-05-29 08:06:54 -070057/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010058static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010059 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010062 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063};
64
65/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010066static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010067 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070070 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010071 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
79 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010080 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070081 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070083 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053084 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070088};
89
Matt Roper3d7d6512014-06-10 08:28:13 -070090/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
Jesse Barnesf1f644d2013-06-27 00:39:25 +030095static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020096 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030097static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020098 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030099
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200111static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200112static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200113static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200114 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200115static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200116 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
120 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200124static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300127static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100128
Ma Lingd4906092009-03-18 20:13:27 +0800129struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800138};
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156{
157 u32 val;
158 int divider;
159
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300181}
182
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200185{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200187}
188
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300191{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300192 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195}
196
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
199{
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 uint32_t clkcfg;
201
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200212 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300213 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200216 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300221 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200222 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300223 }
224}
225
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300226void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
Wayne Boyer666a4532015-12-09 12:29:35 -0800242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
Chris Wilson021357a2010-09-07 20:54:59 +0100251static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100254{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200259 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200260 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100261}
262
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300263static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400264 .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 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400267 .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 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300276static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200277 .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 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200280 .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 = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300289static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200291 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200292 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
Eric Anholt273e27c2011-03-30 13:01:10 -0700301
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300302static const struct intel_limit intel_limits_i9xx_sdvo = {
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 Jakobsson4f7dfb62013-02-13 22:20:22 +0100307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300315static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700326};
327
Eric Anholt273e27c2011-03-30 13:01:10 -0700328
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300329static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800341 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300344static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700355};
356
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800368 },
Keith Packarde4b36692009-06-05 19:22:17 -0700369};
370
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800382 },
Keith Packarde4b36692009-06-05 19:22:17 -0700383};
384
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300385static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700388 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700398};
399
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300400static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700411};
412
Eric Anholt273e27c2011-03-30 13:01:10 -0700413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300418static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300431static const struct intel_limit intel_limits_ironlake_single_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 = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800455};
456
Eric Anholt273e27c2011-03-30 13:01:10 -0700457/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300458static const struct intel_limit intel_limits_ironlake_single_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 = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469};
470
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400479 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800482};
483
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300484static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200492 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300496 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700498};
499
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300500static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200508 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300516static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530519 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200531 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200532}
533
Imre Deakdccbea32015-06-22 23:35:51 +0300534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
Shaohua Li21778322009-02-23 15:19:16 +0800545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200547 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300548 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300551
552 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800553}
554
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800561{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200562 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800563 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300565 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300568
569 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800570}
571
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300577 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300580
581 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300582}
583
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300584int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300593
594 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300595}
596
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
Chris Wilson1b894b52010-12-14 20:04:54 +0000603static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300604 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615
Wayne Boyer666a4532015-12-09 12:29:35 -0800616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
617 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Wayne Boyer666a4532015-12-09 12:29:35 -0800621 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635
636 return true;
637}
638
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300639static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300640i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300641 const struct intel_crtc_state *crtc_state,
642 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300644 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800645
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100652 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300653 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300655 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 } else {
657 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300658 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300662}
663
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300674static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300675i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300676 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300681 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300682 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800683
Akshay Joshi0206e352011-08-16 15:34:10 -0400684 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
Zhao Yakui42158662009-11-20 11:24:18 +0800688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200692 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 int this_err;
699
Imre Deakdccbea32015-06-22 23:35:51 +0300700 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800703 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
730 */
Ma Lingd4906092009-03-18 20:13:27 +0800731static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300732pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200733 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200736{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300737 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300738 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200739 int err = target;
740
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 memset(best_clock, 0, sizeof(*best_clock));
742
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300743 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
744
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200745 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
746 clock.m1++) {
747 for (clock.m2 = limit->m2.min;
748 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200749 for (clock.n = limit->n.min;
750 clock.n <= limit->n.max; clock.n++) {
751 for (clock.p1 = limit->p1.min;
752 clock.p1 <= limit->p1.max; clock.p1++) {
753 int this_err;
754
Imre Deakdccbea32015-06-22 23:35:51 +0300755 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800756 if (!intel_PLL_is_valid(dev, limit,
757 &clock))
758 continue;
759 if (match_clock &&
760 clock.p != match_clock->p)
761 continue;
762
763 this_err = abs(clock.dot - target);
764 if (this_err < err) {
765 *best_clock = clock;
766 err = this_err;
767 }
768 }
769 }
770 }
771 }
772
773 return (err != target);
774}
775
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200776/*
777 * Returns a set of divisors for the desired target clock with the given
778 * refclk, or FALSE. The returned values represent the clock equation:
779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200780 *
781 * Target and reference clocks are specified in kHz.
782 *
783 * If match_clock is provided, then best_clock P divider must match the P
784 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200785 */
Ma Lingd4906092009-03-18 20:13:27 +0800786static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300787g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200788 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300789 int target, int refclk, struct dpll *match_clock,
790 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800791{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300792 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300793 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800794 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300795 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400796 /* approximately equals target * 0.00585 */
797 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800798
799 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300800
801 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
802
Ma Lingd4906092009-03-18 20:13:27 +0800803 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200804 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800805 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200806 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800807 for (clock.m1 = limit->m1.max;
808 clock.m1 >= limit->m1.min; clock.m1--) {
809 for (clock.m2 = limit->m2.max;
810 clock.m2 >= limit->m2.min; clock.m2--) {
811 for (clock.p1 = limit->p1.max;
812 clock.p1 >= limit->p1.min; clock.p1--) {
813 int this_err;
814
Imre Deakdccbea32015-06-22 23:35:51 +0300815 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000816 if (!intel_PLL_is_valid(dev, limit,
817 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800818 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000819
820 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800821 if (this_err < err_most) {
822 *best_clock = clock;
823 err_most = this_err;
824 max_n = clock.n;
825 found = true;
826 }
827 }
828 }
829 }
830 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800831 return found;
832}
Ma Lingd4906092009-03-18 20:13:27 +0800833
Imre Deakd5dd62b2015-03-17 11:40:03 +0200834/*
835 * Check if the calculated PLL configuration is more optimal compared to the
836 * best configuration and error found so far. Return the calculated error.
837 */
838static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300839 const struct dpll *calculated_clock,
840 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200841 unsigned int best_error_ppm,
842 unsigned int *error_ppm)
843{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200844 /*
845 * For CHV ignore the error and consider only the P value.
846 * Prefer a bigger P value based on HW requirements.
847 */
848 if (IS_CHERRYVIEW(dev)) {
849 *error_ppm = 0;
850
851 return calculated_clock->p > best_clock->p;
852 }
853
Imre Deak24be4e42015-03-17 11:40:04 +0200854 if (WARN_ON_ONCE(!target_freq))
855 return false;
856
Imre Deakd5dd62b2015-03-17 11:40:03 +0200857 *error_ppm = div_u64(1000000ULL *
858 abs(target_freq - calculated_clock->dot),
859 target_freq);
860 /*
861 * Prefer a better P value over a better (smaller) error if the error
862 * is small. Ensure this preference for future configurations too by
863 * setting the error to 0.
864 */
865 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
866 *error_ppm = 0;
867
868 return true;
869 }
870
871 return *error_ppm + 10 < best_error_ppm;
872}
873
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200874/*
875 * Returns a set of divisors for the desired target clock with the given
876 * refclk, or FALSE. The returned values represent the clock equation:
877 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
878 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800879static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300880vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200881 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300882 int target, int refclk, struct dpll *match_clock,
883 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700884{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300886 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300887 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300888 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300889 /* min update 19.2 MHz */
890 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300891 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 target *= 5; /* fast clock */
894
895 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896
897 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300898 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300899 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300900 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300901 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300902 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700903 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300904 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200905 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300906
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
908 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300909
Imre Deakdccbea32015-06-22 23:35:51 +0300910 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300911
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300912 if (!intel_PLL_is_valid(dev, limit,
913 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300914 continue;
915
Imre Deakd5dd62b2015-03-17 11:40:03 +0200916 if (!vlv_PLL_is_optimal(dev, target,
917 &clock,
918 best_clock,
919 bestppm, &ppm))
920 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300921
Imre Deakd5dd62b2015-03-17 11:40:03 +0200922 *best_clock = clock;
923 bestppm = ppm;
924 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700925 }
926 }
927 }
928 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300930 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700931}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700932
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300938static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300939chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200940 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300941 int target, int refclk, struct dpll *match_clock,
942 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300943{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300945 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300947 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948 uint64_t m2;
949 int found = false;
950
951 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200952 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953
954 /*
955 * Based on hardware doc, the n always set to 1, and m1 always
956 * set to 2. If requires to support 200Mhz refclk, we need to
957 * revisit this because n may not 1 anymore.
958 */
959 clock.n = 1, clock.m1 = 2;
960 target *= 5; /* fast clock */
961
962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
963 for (clock.p2 = limit->p2.p2_fast;
964 clock.p2 >= limit->p2.p2_slow;
965 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200966 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300967
968 clock.p = clock.p1 * clock.p2;
969
970 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
971 clock.n) << 22, refclk * clock.m1);
972
973 if (m2 > INT_MAX/clock.m1)
974 continue;
975
976 clock.m2 = m2;
977
Imre Deakdccbea32015-06-22 23:35:51 +0300978 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300979
980 if (!intel_PLL_is_valid(dev, limit, &clock))
981 continue;
982
Imre Deak9ca3ba02015-03-17 11:40:05 +0200983 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
984 best_error_ppm, &error_ppm))
985 continue;
986
987 *best_clock = clock;
988 best_error_ppm = error_ppm;
989 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300990 }
991 }
992
993 return found;
994}
995
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200996bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300997 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200998{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200999 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001000 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001001
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001002 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001003 target_clock, refclk, NULL, best_clock);
1004}
1005
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001006bool intel_crtc_active(struct drm_crtc *crtc)
1007{
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
1010 /* Be paranoid as we can arrive here with only partial
1011 * state retrieved from the hardware during setup.
1012 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001013 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001014 * as Haswell has gained clock readout/fastboot support.
1015 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001016 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001017 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001018 *
1019 * FIXME: The intel_crtc->active here should be switched to
1020 * crtc->state->active once we have proper CRTC states wired up
1021 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001022 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001023 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001024 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001025}
1026
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001027enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1032
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001033 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001034}
1035
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001036static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1037{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001038 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001039 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001040 u32 line1, line2;
1041 u32 line_mask;
1042
1043 if (IS_GEN2(dev))
1044 line_mask = DSL_LINEMASK_GEN2;
1045 else
1046 line_mask = DSL_LINEMASK_GEN3;
1047
1048 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001049 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001050 line2 = I915_READ(reg) & line_mask;
1051
1052 return line1 == line2;
1053}
1054
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055/*
1056 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001057 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001058 *
1059 * After disabling a pipe, we can't wait for vblank in the usual way,
1060 * spinning on the vblank interrupt status bit, since we won't actually
1061 * see an interrupt when the pipe is disabled.
1062 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 * On Gen4 and above:
1064 * wait for the pipe register state bit to turn off
1065 *
1066 * Otherwise:
1067 * wait for the display line value to settle (it usually
1068 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001069 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001070 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001071static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001072{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001073 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001074 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001076 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001077
Keith Packardab7ad7f2010-10-03 00:33:06 -07001078 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001079 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001080
Keith Packardab7ad7f2010-10-03 00:33:06 -07001081 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001085 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001087 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001088 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001089 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001091}
1092
Jesse Barnesb24e7172011-01-04 15:09:30 -08001093/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097 u32 val;
1098 bool cur_state;
1099
Ville Syrjälä649636e2015-09-22 19:50:01 +03001100 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001102 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001104 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001106
Jani Nikula23538ef2013-08-27 15:12:22 +03001107/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001109{
1110 u32 val;
1111 bool cur_state;
1112
Ville Syrjäläa5805162015-05-26 20:42:30 +03001113 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001115 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001116
1117 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001118 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001119 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001120 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001121}
Jani Nikula23538ef2013-08-27 15:12:22 +03001122
Jesse Barnes040484a2011-01-03 12:14:26 -08001123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
Jesse Barnes040484a2011-01-03 12:14:26 -08001126 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001130 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001131 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001134 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001135 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001138 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001139 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001140 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
Jesse Barnes040484a2011-01-03 12:14:26 -08001148 u32 val;
1149 bool cur_state;
1150
Ville Syrjälä649636e2015-09-22 19:50:01 +03001151 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001152 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001153 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001154 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001155 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
Jesse Barnes040484a2011-01-03 12:14:26 -08001163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001166 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001167 return;
1168
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001170 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 return;
1172
Ville Syrjälä649636e2015-09-22 19:50:01 +03001173 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 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 -08001175}
1176
Daniel Vetter55607e82013-06-16 21:42:39 +02001177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001179{
Jesse Barnes040484a2011-01-03 12:14:26 -08001180 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001181 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001182
Ville Syrjälä649636e2015-09-22 19:50:01 +03001183 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001186 "FDI RX PLL 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
Daniel Vetterb680c372014-09-19 18:27:27 +02001190void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1191 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001192{
Chris Wilson91c8a322016-07-05 10:40:23 +01001193 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001194 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001197 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198
Jani Nikulabedd4db2014-08-22 15:04:13 +03001199 if (WARN_ON(HAS_DDI(dev)))
1200 return;
1201
1202 if (HAS_PCH_SPLIT(dev)) {
1203 u32 port_sel;
1204
Imre Deak44cb7342016-08-10 14:07:29 +03001205 pp_reg = PP_CONTROL(0);
1206 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001212 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001213 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001214 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001215 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001216 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001217 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225 locked = false;
1226
Rob Clarke2c719b2014-12-15 13:56:32 -05001227 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001229 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230}
1231
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
Chris Wilson91c8a322016-07-05 10:40:23 +01001235 struct drm_device *dev = &dev_priv->drm;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001236 bool cur_state;
1237
Paulo Zanonid9d82082014-02-27 16:30:56 -03001238 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001239 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001240 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001241 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001242
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001244 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001245 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001246}
1247#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1248#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1249
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001250void assert_pipe(struct drm_i915_private *dev_priv,
1251 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001252{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001253 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001254 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1255 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001256 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001257
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001258 /* if we need the pipe quirk it must be always on */
1259 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1260 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001261 state = true;
1262
Imre Deak4feed0e2016-02-12 18:55:14 +02001263 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1264 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001265 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001266 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001267
1268 intel_display_power_put(dev_priv, power_domain);
1269 } else {
1270 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001271 }
1272
Rob Clarke2c719b2014-12-15 13:56:32 -05001273 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001274 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001275 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001276}
1277
Chris Wilson931872f2012-01-16 23:01:13 +00001278static void assert_plane(struct drm_i915_private *dev_priv,
1279 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001281 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001282 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001283
Ville Syrjälä649636e2015-09-22 19:50:01 +03001284 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001285 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001286 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001287 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001288 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001289}
1290
Chris Wilson931872f2012-01-16 23:01:13 +00001291#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1292#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1293
Jesse Barnesb24e7172011-01-04 15:09:30 -08001294static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
Chris Wilson91c8a322016-07-05 10:40:23 +01001297 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001298 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001299
Ville Syrjälä653e1022013-06-04 13:49:05 +03001300 /* Primary planes are fixed to pipes on gen4+ */
1301 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001302 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001303 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001304 "plane %c assertion failure, should be disabled but not\n",
1305 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001306 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001307 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001308
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001310 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001311 u32 val = I915_READ(DSPCNTR(i));
1312 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001315 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1316 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001317 }
1318}
1319
Jesse Barnes19332d72013-03-28 09:55:38 -07001320static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe)
1322{
Chris Wilson91c8a322016-07-05 10:40:23 +01001323 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001324 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001325
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001326 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001327 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001328 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001330 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1331 sprite, pipe_name(pipe));
1332 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001333 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001334 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001335 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001336 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001337 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001338 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001339 }
1340 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001344 plane_name(pipe), pipe_name(pipe));
1345 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001346 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001348 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1349 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001350 }
1351}
1352
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001353static void assert_vblank_disabled(struct drm_crtc *crtc)
1354{
Rob Clarke2c719b2014-12-15 13:56:32 -05001355 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001356 drm_crtc_vblank_put(crtc);
1357}
1358
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001359void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001361{
Jesse Barnes92f25842011-01-04 15:09:34 -08001362 u32 val;
1363 bool enabled;
1364
Ville Syrjälä649636e2015-09-22 19:50:01 +03001365 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001366 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001367 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001368 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1369 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001370}
1371
Keith Packard4e634382011-08-06 10:39:45 -07001372static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001374{
1375 if ((val & DP_PORT_EN) == 0)
1376 return false;
1377
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001378 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001379 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001380 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1381 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001382 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001383 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1384 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001385 } else {
1386 if ((val & DP_PIPE_MASK) != (pipe << 30))
1387 return false;
1388 }
1389 return true;
1390}
1391
Keith Packard1519b992011-08-06 10:35:34 -07001392static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001395 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001396 return false;
1397
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001398 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001399 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001400 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001401 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001402 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1403 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001404 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001405 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001406 return false;
1407 }
1408 return true;
1409}
1410
1411static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 val)
1413{
1414 if ((val & LVDS_PORT_EN) == 0)
1415 return false;
1416
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001417 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001418 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1419 return false;
1420 } else {
1421 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, u32 val)
1429{
1430 if ((val & ADPA_DAC_ENABLE) == 0)
1431 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001432 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001433 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1434 return false;
1435 } else {
1436 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1437 return false;
1438 }
1439 return true;
1440}
1441
Jesse Barnes291906f2011-02-02 12:28:03 -08001442static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001443 enum pipe pipe, i915_reg_t reg,
1444 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001445{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001446 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001448 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001449 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001450
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001451 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001452 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001453 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001454}
1455
1456static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001457 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001458{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001459 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001460 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001461 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001462 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001463
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001464 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001465 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001466 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001467}
1468
1469static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
Jesse Barnes291906f2011-02-02 12:28:03 -08001472 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001473
Keith Packardf0575e92011-07-25 22:12:43 -07001474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001477
Ville Syrjälä649636e2015-09-22 19:50:01 +03001478 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001479 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001480 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001481 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001482
Ville Syrjälä649636e2015-09-22 19:50:01 +03001483 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001484 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001485 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001486 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001487
Paulo Zanonie2debe92013-02-18 19:00:27 -03001488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001491}
1492
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001493static void _vlv_enable_pll(struct intel_crtc *crtc,
1494 const struct intel_crtc_state *pipe_config)
1495{
1496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497 enum pipe pipe = crtc->pipe;
1498
1499 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1500 POSTING_READ(DPLL(pipe));
1501 udelay(150);
1502
Chris Wilson2c30b432016-06-30 15:32:54 +01001503 if (intel_wait_for_register(dev_priv,
1504 DPLL(pipe),
1505 DPLL_LOCK_VLV,
1506 DPLL_LOCK_VLV,
1507 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001508 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1509}
1510
Ville Syrjäläd288f652014-10-28 13:20:22 +02001511static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001512 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001513{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001515 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001516
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001517 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001518
Daniel Vetter87442f72013-06-06 00:52:17 +02001519 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001520 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001521
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001522 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1523 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001524
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001525 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1526 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001527}
1528
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001529
1530static void _chv_enable_pll(struct intel_crtc *crtc,
1531 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001532{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001533 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001534 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001536 u32 tmp;
1537
Ville Syrjäläa5805162015-05-26 20:42:30 +03001538 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001539
1540 /* Enable back the 10bit clock to display controller */
1541 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1542 tmp |= DPIO_DCLKP_EN;
1543 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1544
Ville Syrjälä54433e92015-05-26 20:42:31 +03001545 mutex_unlock(&dev_priv->sb_lock);
1546
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001553 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001554
1555 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001556 if (intel_wait_for_register(dev_priv,
1557 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1558 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001559 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001560}
1561
1562static void chv_enable_pll(struct intel_crtc *crtc,
1563 const struct intel_crtc_state *pipe_config)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567
1568 assert_pipe_disabled(dev_priv, pipe);
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 assert_panel_unlocked(dev_priv, pipe);
1572
1573 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1574 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575
Ville Syrjäläc2317752016-03-15 16:39:56 +02001576 if (pipe != PIPE_A) {
1577 /*
1578 * WaPixelRepeatModeFixForC0:chv
1579 *
1580 * DPLLCMD is AWOL. Use chicken bits to propagate
1581 * the value from DPLLBMD to either pipe B or C.
1582 */
1583 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1584 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1585 I915_WRITE(CBR4_VLV, 0);
1586 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1587
1588 /*
1589 * DPLLB VGA mode also seems to cause problems.
1590 * We should always have it disabled.
1591 */
1592 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1593 } else {
1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1596 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597}
1598
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001604 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001605 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001606 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1607 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001608
1609 return count;
1610}
1611
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001612static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001613{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001614 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001615 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001616 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001617 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001618
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001619 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001620
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001621 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001624
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001625 /* Enable DVO 2x clock on both PLLs if necessary */
1626 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1627 /*
1628 * It appears to be important that we don't enable this
1629 * for the current pipe before otherwise configuring the
1630 * PLL. No idea how this should be handled if multiple
1631 * DVO outputs are enabled simultaneosly.
1632 */
1633 dpll |= DPLL_DVO_2X_MODE;
1634 I915_WRITE(DPLL(!crtc->pipe),
1635 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1636 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001637
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001638 /*
1639 * Apparently we need to have VGA mode enabled prior to changing
1640 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1641 * dividers, even though the register value does change.
1642 */
1643 I915_WRITE(reg, 0);
1644
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001645 I915_WRITE(reg, dpll);
1646
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 /* Wait for the clocks to stabilize. */
1648 POSTING_READ(reg);
1649 udelay(150);
1650
1651 if (INTEL_INFO(dev)->gen >= 4) {
1652 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001653 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001654 } else {
1655 /* The pixel multiplier can only be updated once the
1656 * DPLL is enabled and the clocks are stable.
1657 *
1658 * So write it again.
1659 */
1660 I915_WRITE(reg, dpll);
1661 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001662
1663 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001664 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001667 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001668 POSTING_READ(reg);
1669 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001670 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671 POSTING_READ(reg);
1672 udelay(150); /* wait for warmup */
1673}
1674
1675/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001676 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001677 * @dev_priv: i915 private structure
1678 * @pipe: pipe PLL to disable
1679 *
1680 * Disable the PLL for @pipe, making sure the pipe is off first.
1681 *
1682 * Note! This is for pre-ILK only.
1683 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001684static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001685{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001686 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001687 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688 enum pipe pipe = crtc->pipe;
1689
1690 /* Disable DVO 2x clock on both PLLs if necessary */
1691 if (IS_I830(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001692 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001693 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001694 I915_WRITE(DPLL(PIPE_B),
1695 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1696 I915_WRITE(DPLL(PIPE_A),
1697 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1698 }
1699
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001700 /* Don't disable pipe or pipe PLLs if needed */
1701 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1702 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703 return;
1704
1705 /* Make sure the pipe isn't still relying on us */
1706 assert_pipe_disabled(dev_priv, pipe);
1707
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001708 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001709 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710}
1711
Jesse Barnesf6071162013-10-01 10:41:38 -07001712static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001714 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001715
1716 /* Make sure the pipe isn't still relying on us */
1717 assert_pipe_disabled(dev_priv, pipe);
1718
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001719 val = DPLL_INTEGRATED_REF_CLK_VLV |
1720 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1721 if (pipe != PIPE_A)
1722 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1723
Jesse Barnesf6071162013-10-01 10:41:38 -07001724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001726}
1727
1728static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001731 u32 val;
1732
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001733 /* Make sure the pipe isn't still relying on us */
1734 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001735
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001736 val = DPLL_SSC_REF_CLK_CHV |
1737 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001738 if (pipe != PIPE_A)
1739 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001740
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001741 I915_WRITE(DPLL(pipe), val);
1742 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001743
Ville Syrjäläa5805162015-05-26 20:42:30 +03001744 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001745
1746 /* Disable 10bit clock to display controller */
1747 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1748 val &= ~DPIO_DCLKP_EN;
1749 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1750
Ville Syrjäläa5805162015-05-26 20:42:30 +03001751 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001752}
1753
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001754void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001755 struct intel_digital_port *dport,
1756 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001757{
1758 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001759 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001760
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001761 switch (dport->port) {
1762 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001763 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001764 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001765 break;
1766 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001767 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001768 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001769 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001774 break;
1775 default:
1776 BUG();
1777 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778
Chris Wilson370004d2016-06-30 15:32:56 +01001779 if (intel_wait_for_register(dev_priv,
1780 dpll_reg, port_mask, expected_mask,
1781 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001782 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1783 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001784}
1785
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001786static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001788{
Chris Wilson91c8a322016-07-05 10:40:23 +01001789 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001794
Jesse Barnes040484a2011-01-03 12:14:26 -08001795 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
Daniel Vetter23670b322012-11-01 09:15:30 +01001802 if (HAS_PCH_CPT(dev)) {
1803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001809 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001810
Daniel Vetterab9412b2013-05-03 11:49:46 +02001811 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001812 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001813 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001814
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001815 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001816 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001820 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001821 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001826 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001830 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001835 else
1836 val |= TRANS_PROGRESSIVE;
1837
Jesse Barnes040484a2011-01-03 12:14:26 -08001838 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001843}
1844
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001846 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001847{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001850 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001854 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001858
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001859 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001861
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001864 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001865 else
1866 val |= TRANS_PROGRESSIVE;
1867
Daniel Vetterab9412b2013-05-03 11:49:46 +02001868 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001874 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875}
1876
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001879{
Chris Wilson91c8a322016-07-05 10:40:23 +01001880 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001881 i915_reg_t reg;
1882 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
Jesse Barnes291906f2011-02-02 12:28:03 -08001888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
Daniel Vetterab9412b2013-05-03 11:49:46 +02001891 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 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 */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001918 if (intel_wait_for_register(dev_priv,
1919 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1920 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001921 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001922
1923 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001924 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001925 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001926 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001927}
1928
1929/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001930 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001931 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001933 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001935 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001936static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001937{
Paulo Zanoni03722642014-01-17 13:51:09 -02001938 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001939 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001940 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001941 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001942 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001943 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001944 u32 val;
1945
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001946 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1947
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001948 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001949 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001950 assert_sprites_disabled(dev_priv, pipe);
1951
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001952 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001953 pch_transcoder = TRANSCODER_A;
1954 else
1955 pch_transcoder = pipe;
1956
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957 /*
1958 * A pipe without a PLL won't actually be able to drive bits from
1959 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1960 * need the check.
1961 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001962 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001963 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001964 assert_dsi_pll_enabled(dev_priv);
1965 else
1966 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001967 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001968 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001969 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001970 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001971 assert_fdi_tx_pll_enabled(dev_priv,
1972 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 }
1974 /* FIXME: assert CPU port conditions for SNB+ */
1975 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001977 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001979 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001980 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1981 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001982 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001983 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001984
1985 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001986 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001987
1988 /*
1989 * Until the pipe starts DSL will read as 0, which would cause
1990 * an apparent vblank timestamp jump, which messes up also the
1991 * frame count when it's derived from the timestamps. So let's
1992 * wait for the pipe to start properly before we call
1993 * drm_crtc_vblank_on()
1994 */
1995 if (dev->max_vblank_count == 0 &&
1996 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1997 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001998}
1999
2000/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002001 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002002 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002004 * Disable the pipe of @crtc, making sure that various hardware
2005 * specific requirements are met, if applicable, e.g. plane
2006 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 *
2008 * Will wait until the pipe has shut down before returning.
2009 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002010static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002014 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002015 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002016 u32 val;
2017
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002018 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2019
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 /*
2021 * Make sure planes won't keep trying to pump pixels to us,
2022 * or we might hang the display.
2023 */
2024 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002025 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002026 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002028 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002030 if ((val & PIPECONF_ENABLE) == 0)
2031 return;
2032
Ville Syrjälä67adc642014-08-15 01:21:57 +03002033 /*
2034 * Double wide has implications for planes
2035 * so best keep it disabled when not needed.
2036 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002037 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002038 val &= ~PIPECONF_DOUBLE_WIDE;
2039
2040 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002041 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2042 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002043 val &= ~PIPECONF_ENABLE;
2044
2045 I915_WRITE(reg, val);
2046 if ((val & PIPECONF_ENABLE) == 0)
2047 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002048}
2049
Ville Syrjälä832be822016-01-12 21:08:33 +02002050static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2051{
2052 return IS_GEN2(dev_priv) ? 2048 : 4096;
2053}
2054
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002055static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2056 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002057{
2058 switch (fb_modifier) {
2059 case DRM_FORMAT_MOD_NONE:
2060 return cpp;
2061 case I915_FORMAT_MOD_X_TILED:
2062 if (IS_GEN2(dev_priv))
2063 return 128;
2064 else
2065 return 512;
2066 case I915_FORMAT_MOD_Y_TILED:
2067 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2068 return 128;
2069 else
2070 return 512;
2071 case I915_FORMAT_MOD_Yf_TILED:
2072 switch (cpp) {
2073 case 1:
2074 return 64;
2075 case 2:
2076 case 4:
2077 return 128;
2078 case 8:
2079 case 16:
2080 return 256;
2081 default:
2082 MISSING_CASE(cpp);
2083 return cpp;
2084 }
2085 break;
2086 default:
2087 MISSING_CASE(fb_modifier);
2088 return cpp;
2089 }
2090}
2091
Ville Syrjälä832be822016-01-12 21:08:33 +02002092unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2093 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002094{
Ville Syrjälä832be822016-01-12 21:08:33 +02002095 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2096 return 1;
2097 else
2098 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002099 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002100}
2101
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002102/* Return the tile dimensions in pixel units */
2103static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2104 unsigned int *tile_width,
2105 unsigned int *tile_height,
2106 uint64_t fb_modifier,
2107 unsigned int cpp)
2108{
2109 unsigned int tile_width_bytes =
2110 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2111
2112 *tile_width = tile_width_bytes / cpp;
2113 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2114}
2115
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002116unsigned int
2117intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002118 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002119{
Ville Syrjälä832be822016-01-12 21:08:33 +02002120 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2121 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2122
2123 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002124}
2125
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
Daniel Vetter75c82a52015-10-14 16:51:04 +02002137static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002138intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2139 const struct drm_framebuffer *fb,
2140 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002141{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002142 if (intel_rotation_90_or_270(rotation)) {
2143 *view = i915_ggtt_view_rotated;
2144 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2145 } else {
2146 *view = i915_ggtt_view_normal;
2147 }
2148}
2149
2150static void
2151intel_fill_fb_info(struct drm_i915_private *dev_priv,
2152 struct drm_framebuffer *fb)
2153{
2154 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002155 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002156
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002157 tile_size = intel_tile_size(dev_priv);
2158
2159 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002160 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2161 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002162
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002163 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2164 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002165
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002166 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002167 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002171 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002172 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2173 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002174 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002175}
2176
Ville Syrjälä603525d2016-01-12 21:08:37 +02002177static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002178{
2179 if (INTEL_INFO(dev_priv)->gen >= 9)
2180 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002181 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002182 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002183 return 128 * 1024;
2184 else if (INTEL_INFO(dev_priv)->gen >= 4)
2185 return 4 * 1024;
2186 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002187 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002188}
2189
Ville Syrjälä603525d2016-01-12 21:08:37 +02002190static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2191 uint64_t fb_modifier)
2192{
2193 switch (fb_modifier) {
2194 case DRM_FORMAT_MOD_NONE:
2195 return intel_linear_alignment(dev_priv);
2196 case I915_FORMAT_MOD_X_TILED:
2197 if (INTEL_INFO(dev_priv)->gen >= 9)
2198 return 256 * 1024;
2199 return 0;
2200 case I915_FORMAT_MOD_Y_TILED:
2201 case I915_FORMAT_MOD_Yf_TILED:
2202 return 1 * 1024 * 1024;
2203 default:
2204 MISSING_CASE(fb_modifier);
2205 return 0;
2206 }
2207}
2208
Chris Wilson127bd2a2010-07-23 23:32:05 +01002209int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002210intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2211 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002212{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002213 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002214 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002215 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002216 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002217 u32 alignment;
2218 int ret;
2219
Matt Roperebcdd392014-07-09 16:22:11 -07002220 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2221
Ville Syrjälä603525d2016-01-12 21:08:37 +02002222 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002223
Ville Syrjälä3465c582016-02-15 22:54:43 +02002224 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002225
Chris Wilson693db182013-03-05 14:52:39 +00002226 /* Note that the w/a also requires 64 PTE of padding following the
2227 * bo. We currently fill all unused PTE with the shadow page and so
2228 * we should always have valid PTE following the scanout preventing
2229 * the VT-d warning.
2230 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002231 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002232 alignment = 256 * 1024;
2233
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002234 /*
2235 * Global gtt pte registers are special registers which actually forward
2236 * writes to a chunk of system memory. Which means that there is no risk
2237 * that the register values disappear as soon as we call
2238 * intel_runtime_pm_put(), so it is correct to wrap only the
2239 * pin/unpin/fence and not more.
2240 */
2241 intel_runtime_pm_get(dev_priv);
2242
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002243 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2244 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002245 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002246 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247
2248 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2249 * fence, whereas 965+ only requires a fence if using
2250 * framebuffer compression. For simplicity, we always install
2251 * a fence as the cost is not that onerous.
2252 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002253 if (view.type == I915_GGTT_VIEW_NORMAL) {
2254 ret = i915_gem_object_get_fence(obj);
2255 if (ret == -EDEADLK) {
2256 /*
2257 * -EDEADLK means there are no free fences
2258 * no pending flips.
2259 *
2260 * This is propagated to atomic, but it uses
2261 * -EDEADLK to force a locking recovery, so
2262 * change the returned error to -EBUSY.
2263 */
2264 ret = -EBUSY;
2265 goto err_unpin;
2266 } else if (ret)
2267 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002268
Vivek Kasireddy98072162015-10-29 18:54:38 -07002269 i915_gem_object_pin_fence(obj);
2270 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002271
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002272 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002273 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002274
2275err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002276 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002277err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002278 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002279 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002280}
2281
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002282void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002283{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002284 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002285 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002286
Matt Roperebcdd392014-07-09 16:22:11 -07002287 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2288
Ville Syrjälä3465c582016-02-15 22:54:43 +02002289 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002290
Vivek Kasireddy98072162015-10-29 18:54:38 -07002291 if (view.type == I915_GGTT_VIEW_NORMAL)
2292 i915_gem_object_unpin_fence(obj);
2293
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002294 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002295}
2296
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002297/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002298 * Adjust the tile offset by moving the difference into
2299 * the x/y offsets.
2300 *
2301 * Input tile dimensions and pitch must already be
2302 * rotated to match x and y, and in pixel units.
2303 */
2304static u32 intel_adjust_tile_offset(int *x, int *y,
2305 unsigned int tile_width,
2306 unsigned int tile_height,
2307 unsigned int tile_size,
2308 unsigned int pitch_tiles,
2309 u32 old_offset,
2310 u32 new_offset)
2311{
2312 unsigned int tiles;
2313
2314 WARN_ON(old_offset & (tile_size - 1));
2315 WARN_ON(new_offset & (tile_size - 1));
2316 WARN_ON(new_offset > old_offset);
2317
2318 tiles = (old_offset - new_offset) / tile_size;
2319
2320 *y += tiles / pitch_tiles * tile_height;
2321 *x += tiles % pitch_tiles * tile_width;
2322
2323 return new_offset;
2324}
2325
2326/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002327 * Computes the linear offset to the base tile and adjusts
2328 * x, y. bytes per pixel is assumed to be a power-of-two.
2329 *
2330 * In the 90/270 rotated case, x and y are assumed
2331 * to be already rotated to match the rotated GTT view, and
2332 * pitch is the tile_height aligned framebuffer height.
2333 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002334u32 intel_compute_tile_offset(int *x, int *y,
2335 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002336 unsigned int pitch,
2337 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002338{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002339 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2340 uint64_t fb_modifier = fb->modifier[plane];
2341 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002342 u32 offset, offset_aligned, alignment;
2343
2344 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2345 if (alignment)
2346 alignment--;
2347
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002348 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002349 unsigned int tile_size, tile_width, tile_height;
2350 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002351
Ville Syrjäläd8433102016-01-12 21:08:35 +02002352 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002353 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2354 fb_modifier, cpp);
2355
2356 if (intel_rotation_90_or_270(rotation)) {
2357 pitch_tiles = pitch / tile_height;
2358 swap(tile_width, tile_height);
2359 } else {
2360 pitch_tiles = pitch / (tile_width * cpp);
2361 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002362
Ville Syrjäläd8433102016-01-12 21:08:35 +02002363 tile_rows = *y / tile_height;
2364 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002365
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002366 tiles = *x / tile_width;
2367 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002368
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002369 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2370 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002371
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002372 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2373 tile_size, pitch_tiles,
2374 offset, offset_aligned);
2375 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002376 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002377 offset_aligned = offset & ~alignment;
2378
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002379 *y = (offset & alignment) / pitch;
2380 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002381 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002382
2383 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002384}
2385
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002386static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002387{
2388 switch (format) {
2389 case DISPPLANE_8BPP:
2390 return DRM_FORMAT_C8;
2391 case DISPPLANE_BGRX555:
2392 return DRM_FORMAT_XRGB1555;
2393 case DISPPLANE_BGRX565:
2394 return DRM_FORMAT_RGB565;
2395 default:
2396 case DISPPLANE_BGRX888:
2397 return DRM_FORMAT_XRGB8888;
2398 case DISPPLANE_RGBX888:
2399 return DRM_FORMAT_XBGR8888;
2400 case DISPPLANE_BGRX101010:
2401 return DRM_FORMAT_XRGB2101010;
2402 case DISPPLANE_RGBX101010:
2403 return DRM_FORMAT_XBGR2101010;
2404 }
2405}
2406
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002407static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2408{
2409 switch (format) {
2410 case PLANE_CTL_FORMAT_RGB_565:
2411 return DRM_FORMAT_RGB565;
2412 default:
2413 case PLANE_CTL_FORMAT_XRGB_8888:
2414 if (rgb_order) {
2415 if (alpha)
2416 return DRM_FORMAT_ABGR8888;
2417 else
2418 return DRM_FORMAT_XBGR8888;
2419 } else {
2420 if (alpha)
2421 return DRM_FORMAT_ARGB8888;
2422 else
2423 return DRM_FORMAT_XRGB8888;
2424 }
2425 case PLANE_CTL_FORMAT_XRGB_2101010:
2426 if (rgb_order)
2427 return DRM_FORMAT_XBGR2101010;
2428 else
2429 return DRM_FORMAT_XRGB2101010;
2430 }
2431}
2432
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002433static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002434intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2435 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002436{
2437 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002438 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002440 struct drm_i915_gem_object *obj = NULL;
2441 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002442 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002443 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2444 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2445 PAGE_SIZE);
2446
2447 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002448
Chris Wilsonff2652e2014-03-10 08:07:02 +00002449 if (plane_config->size == 0)
2450 return false;
2451
Paulo Zanoni3badb492015-09-23 12:52:23 -03002452 /* If the FB is too big, just don't use it since fbdev is not very
2453 * important and we should probably use that space with FBC or other
2454 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002455 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002456 return false;
2457
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002458 mutex_lock(&dev->struct_mutex);
2459
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002460 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2461 base_aligned,
2462 base_aligned,
2463 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002464 if (!obj) {
2465 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002466 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002467 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002468
Chris Wilson3e510a82016-08-05 10:14:23 +01002469 if (plane_config->tiling == I915_TILING_X)
2470 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002480 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002484
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002486
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
2490out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002491 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493 return false;
2494}
2495
Daniel Vetter5a21b662016-05-24 17:13:53 +02002496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513{
2514 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002515 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 struct drm_crtc *c;
2517 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002518 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002519 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002520 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002525 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526
Damien Lespiau2d140302015-02-05 17:22:18 +00002527 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return;
2529
Daniel Vetterf6936e22015-03-26 12:17:05 +01002530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002531 fb = &plane_config->fb->base;
2532 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002533 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534
Damien Lespiau2d140302015-02-05 17:22:18 +00002535 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002541 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
Matt Roper2ff8fde2014-07-08 07:50:07 -07002547 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 continue;
2549
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 fb = c->primary->fb;
2551 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002552 continue;
2553
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 }
2559 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560
Matt Roper200757f2015-12-03 11:37:36 -08002561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
Daniel Vetter88595ac2015-03-26 12:42:24 +01002573 return;
2574
2575valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
Matt Roper0a8d8a82015-12-03 11:37:38 -08002586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002596 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002597 dev_priv->preserve_bios_swizzle = true;
2598
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002601 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002603 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2604 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002605}
2606
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002607static void i9xx_update_primary_plane(struct drm_plane *primary,
2608 const struct intel_crtc_state *crtc_state,
2609 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002610{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002611 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002612 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2614 struct drm_framebuffer *fb = plane_state->base.fb;
2615 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002616 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002617 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002618 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002619 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002620 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002621 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002622 int x = plane_state->src.x1 >> 16;
2623 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002624
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002625 dspcntr = DISPPLANE_GAMMA_ENABLE;
2626
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002627 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002628
2629 if (INTEL_INFO(dev)->gen < 4) {
2630 if (intel_crtc->pipe == PIPE_B)
2631 dspcntr |= DISPPLANE_SEL_PIPE_B;
2632
2633 /* pipesrc and dspsize control the size that is scaled from,
2634 * which should always be the user's requested size.
2635 */
2636 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002637 ((crtc_state->pipe_src_h - 1) << 16) |
2638 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002639 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002640 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2641 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002642 ((crtc_state->pipe_src_h - 1) << 16) |
2643 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002644 I915_WRITE(PRIMPOS(plane), 0);
2645 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002646 }
2647
Ville Syrjälä57779d02012-10-31 17:50:14 +02002648 switch (fb->pixel_format) {
2649 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002650 dspcntr |= DISPPLANE_8BPP;
2651 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002652 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002653 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002654 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002655 case DRM_FORMAT_RGB565:
2656 dspcntr |= DISPPLANE_BGRX565;
2657 break;
2658 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 dspcntr |= DISPPLANE_BGRX888;
2660 break;
2661 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002662 dspcntr |= DISPPLANE_RGBX888;
2663 break;
2664 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 dspcntr |= DISPPLANE_BGRX101010;
2666 break;
2667 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002669 break;
2670 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002671 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002672 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002673
Chris Wilson3e510a82016-08-05 10:14:23 +01002674 if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002675 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
Ville Syrjäläac484962016-01-20 21:05:26 +02002680 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002681
Daniel Vetterc2c75132012-07-05 12:17:30 +02002682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002684 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002685 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002688 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002690
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002691 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302692 dspcntr |= DISPPLANE_ROTATE_180;
2693
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002701 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302702 }
2703
Paulo Zanoni2db33662015-09-14 15:20:03 -03002704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
Sonika Jindal48404c12014-08-22 14:06:04 +05302707 I915_WRITE(reg, dspcntr);
2708
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002710 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002715 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002718}
2719
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002722{
2723 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002724 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002726 int plane = intel_crtc->plane;
2727
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
2730 I915_WRITE(DSPSURF(plane), 0);
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
2735
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002741 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002745 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002746 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002747 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002748 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002749 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002753
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002754 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002755 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2759
Ville Syrjälä57779d02012-10-31 17:50:14 +02002760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002762 dspcntr |= DISPPLANE_8BPP;
2763 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002766 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002777 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 break;
2779 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002780 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 }
2782
Chris Wilson3e510a82016-08-05 10:14:23 +01002783 if (i915_gem_object_is_tiled(obj))
Jesse Barnes17638cd2011-06-24 12:19:23 -07002784 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788
Ville Syrjäläac484962016-01-20 21:05:26 +02002789 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002790 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002791 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002792 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002793 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002794 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302806 }
2807 }
2808
Paulo Zanoni2db33662015-09-14 15:20:03 -03002809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
Sonika Jindal48404c12014-08-22 14:06:04 +05302812 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824}
2825
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002828{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2830 return 64;
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002833
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002835 }
2836}
2837
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002841{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002842 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002843 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002844 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002845
Ville Syrjäläe7941292016-01-19 18:23:17 +02002846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002847 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002851 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002852 return -1;
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002855
2856 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002857 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002858 PAGE_SIZE;
2859 }
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864}
2865
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002869 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874}
2875
Chandra Kondurua1b22782015-04-07 15:28:45 -07002876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002880{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890 }
2891}
2892
Chandra Konduru6156a452015-04-27 13:48:39 -07002893u32 skl_plane_ctl_format(uint32_t pixel_format)
2894{
Chandra Konduru6156a452015-04-27 13:48:39 -07002895 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002896 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002897 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002898 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002899 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002900 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002903 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
2909 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002916 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002917 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002928 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002930
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932}
2933
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 switch (fb_modifier) {
2937 case DRM_FORMAT_MOD_NONE:
2938 break;
2939 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 default:
2946 MISSING_CASE(fb_modifier);
2947 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002948
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950}
2951
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 switch (rotation) {
2955 case BIT(DRM_ROTATE_0):
2956 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302962 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302966 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972}
2973
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002977{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002978 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002979 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002983 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002986 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302987 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002988 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002998
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003009 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303011
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003013
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303014 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303023 } else {
3024 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025 x_offset = src_x;
3026 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 }
3029 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003030
Paulo Zanoni2db33662015-09-14 15:20:03 -03003031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
3061{
3062 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003063 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003064 int pipe = to_intel_crtc(crtc)->pipe;
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
3070
Jesse Barnes17638cd2011-06-24 12:19:23 -07003071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003079 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003080}
3081
Daniel Vetter5a21b662016-05-24 17:13:53 +02003082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
Chris Wilson91c8a322016-07-05 10:40:23 +01003086 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
Ville Syrjälä75147472014-11-24 18:28:11 +02003090static void intel_update_primary_planes(struct drm_device *dev)
3091{
Ville Syrjälä75147472014-11-24 18:28:11 +02003092 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003093
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003094 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003095 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003096 struct intel_plane_state *plane_state =
3097 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003098
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003099 if (plane_state->visible)
3100 plane->update_plane(&plane->base,
3101 to_intel_crtc_state(crtc->state),
3102 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003103 }
3104}
3105
Maarten Lankhorst73974892016-08-05 23:28:27 +03003106static int
3107__intel_display_resume(struct drm_device *dev,
3108 struct drm_atomic_state *state)
3109{
3110 struct drm_crtc_state *crtc_state;
3111 struct drm_crtc *crtc;
3112 int i, ret;
3113
3114 intel_modeset_setup_hw_state(dev);
3115 i915_redisable_vga(dev);
3116
3117 if (!state)
3118 return 0;
3119
3120 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3121 /*
3122 * Force recalculation even if we restore
3123 * current state. With fast modeset this may not result
3124 * in a modeset when the state is compatible.
3125 */
3126 crtc_state->mode_changed = true;
3127 }
3128
3129 /* ignore any reset values/BIOS leftovers in the WM registers */
3130 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3131
3132 ret = drm_atomic_commit(state);
3133
3134 WARN_ON(ret == -EDEADLK);
3135 return ret;
3136}
3137
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003138static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3139{
Ville Syrjäläae981042016-08-05 23:28:30 +03003140 return intel_has_gpu_reset(dev_priv) &&
3141 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003142}
3143
Chris Wilsonc0336662016-05-06 15:40:21 +01003144void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003145{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003146 struct drm_device *dev = &dev_priv->drm;
3147 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3148 struct drm_atomic_state *state;
3149 int ret;
3150
Maarten Lankhorst73974892016-08-05 23:28:27 +03003151 /*
3152 * Need mode_config.mutex so that we don't
3153 * trample ongoing ->detect() and whatnot.
3154 */
3155 mutex_lock(&dev->mode_config.mutex);
3156 drm_modeset_acquire_init(ctx, 0);
3157 while (1) {
3158 ret = drm_modeset_lock_all_ctx(dev, ctx);
3159 if (ret != -EDEADLK)
3160 break;
3161
3162 drm_modeset_backoff(ctx);
3163 }
3164
3165 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003166 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003167 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003168 return;
3169
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003170 /*
3171 * Disabling the crtcs gracefully seems nicer. Also the
3172 * g33 docs say we should at least disable all the planes.
3173 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003174 state = drm_atomic_helper_duplicate_state(dev, ctx);
3175 if (IS_ERR(state)) {
3176 ret = PTR_ERR(state);
3177 state = NULL;
3178 DRM_ERROR("Duplicating state failed with %i\n", ret);
3179 goto err;
3180 }
3181
3182 ret = drm_atomic_helper_disable_all(dev, ctx);
3183 if (ret) {
3184 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3185 goto err;
3186 }
3187
3188 dev_priv->modeset_restore_state = state;
3189 state->acquire_ctx = ctx;
3190 return;
3191
3192err:
3193 drm_atomic_state_free(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003194}
3195
Chris Wilsonc0336662016-05-06 15:40:21 +01003196void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003197{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003198 struct drm_device *dev = &dev_priv->drm;
3199 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3200 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3201 int ret;
3202
Daniel Vetter5a21b662016-05-24 17:13:53 +02003203 /*
3204 * Flips in the rings will be nuked by the reset,
3205 * so complete all pending flips so that user space
3206 * will get its events and not get stuck.
3207 */
3208 intel_complete_page_flips(dev_priv);
3209
Maarten Lankhorst73974892016-08-05 23:28:27 +03003210 dev_priv->modeset_restore_state = NULL;
3211
Ville Syrjälä75147472014-11-24 18:28:11 +02003212 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003213 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003214 if (!state) {
3215 /*
3216 * Flips in the rings have been nuked by the reset,
3217 * so update the base address of all primary
3218 * planes to the the last fb to make sure we're
3219 * showing the correct fb after a reset.
3220 *
3221 * FIXME: Atomic will make this obsolete since we won't schedule
3222 * CS-based flips (which might get lost in gpu resets) any more.
3223 */
3224 intel_update_primary_planes(dev);
3225 } else {
3226 ret = __intel_display_resume(dev, state);
3227 if (ret)
3228 DRM_ERROR("Restoring old state failed with %i\n", ret);
3229 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003230 } else {
3231 /*
3232 * The display has been reset as well,
3233 * so need a full re-initialization.
3234 */
3235 intel_runtime_pm_disable_interrupts(dev_priv);
3236 intel_runtime_pm_enable_interrupts(dev_priv);
3237
3238 intel_modeset_init_hw(dev);
3239
3240 spin_lock_irq(&dev_priv->irq_lock);
3241 if (dev_priv->display.hpd_irq_setup)
3242 dev_priv->display.hpd_irq_setup(dev_priv);
3243 spin_unlock_irq(&dev_priv->irq_lock);
3244
3245 ret = __intel_display_resume(dev, state);
3246 if (ret)
3247 DRM_ERROR("Restoring old state failed with %i\n", ret);
3248
3249 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003250 }
3251
Maarten Lankhorst73974892016-08-05 23:28:27 +03003252 drm_modeset_drop_locks(ctx);
3253 drm_modeset_acquire_fini(ctx);
3254 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003255}
3256
Chris Wilson7d5e3792014-03-04 13:15:08 +00003257static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3258{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003259 struct drm_device *dev = crtc->dev;
3260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3261 unsigned reset_counter;
3262 bool pending;
3263
3264 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3265 if (intel_crtc->reset_counter != reset_counter)
3266 return false;
3267
3268 spin_lock_irq(&dev->event_lock);
3269 pending = to_intel_crtc(crtc)->flip_work != NULL;
3270 spin_unlock_irq(&dev->event_lock);
3271
3272 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003273}
3274
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003275static void intel_update_pipe_config(struct intel_crtc *crtc,
3276 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003277{
3278 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003279 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003280 struct intel_crtc_state *pipe_config =
3281 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003282
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003283 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3284 crtc->base.mode = crtc->base.state->mode;
3285
3286 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3287 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3288 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003289
3290 /*
3291 * Update pipe size and adjust fitter if needed: the reason for this is
3292 * that in compute_mode_changes we check the native mode (not the pfit
3293 * mode) to see if we can flip rather than do a full mode set. In the
3294 * fastboot case, we'll flip, but if we don't update the pipesrc and
3295 * pfit state, we'll end up with a big fb scanned out into the wrong
3296 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003297 */
3298
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003299 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003300 ((pipe_config->pipe_src_w - 1) << 16) |
3301 (pipe_config->pipe_src_h - 1));
3302
3303 /* on skylake this is done by detaching scalers */
3304 if (INTEL_INFO(dev)->gen >= 9) {
3305 skl_detach_scalers(crtc);
3306
3307 if (pipe_config->pch_pfit.enabled)
3308 skylake_pfit_enable(crtc);
3309 } else if (HAS_PCH_SPLIT(dev)) {
3310 if (pipe_config->pch_pfit.enabled)
3311 ironlake_pfit_enable(crtc);
3312 else if (old_crtc_state->pch_pfit.enabled)
3313 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003314 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003315}
3316
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003317static void intel_fdi_normal_train(struct drm_crtc *crtc)
3318{
3319 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003320 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003321 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3322 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003323 i915_reg_t reg;
3324 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003325
3326 /* enable normal train */
3327 reg = FDI_TX_CTL(pipe);
3328 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003329 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003330 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3331 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003332 } else {
3333 temp &= ~FDI_LINK_TRAIN_NONE;
3334 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003335 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003336 I915_WRITE(reg, temp);
3337
3338 reg = FDI_RX_CTL(pipe);
3339 temp = I915_READ(reg);
3340 if (HAS_PCH_CPT(dev)) {
3341 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3342 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3343 } else {
3344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_NONE;
3346 }
3347 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3348
3349 /* wait one idle pattern time */
3350 POSTING_READ(reg);
3351 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003352
3353 /* IVB wants error correction enabled */
3354 if (IS_IVYBRIDGE(dev))
3355 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3356 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003357}
3358
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359/* The FDI link training functions for ILK/Ibexpeak. */
3360static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003363 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3365 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003366 i915_reg_t reg;
3367 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003369 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003370 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003371
Adam Jacksone1a44742010-06-25 15:32:14 -04003372 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3373 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 reg = FDI_RX_IMR(pipe);
3375 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003376 temp &= ~FDI_RX_SYMBOL_LOCK;
3377 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp);
3379 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003380 udelay(150);
3381
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003383 reg = FDI_TX_CTL(pipe);
3384 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003385 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003386 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 temp &= ~FDI_LINK_TRAIN_NONE;
3388 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 reg = FDI_RX_CTL(pipe);
3392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 temp &= ~FDI_LINK_TRAIN_NONE;
3394 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3396
3397 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398 udelay(150);
3399
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003400 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3402 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3403 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003404
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003406 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3409
3410 if ((temp & FDI_RX_BIT_LOCK)) {
3411 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 break;
3414 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003416 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418
3419 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 reg = FDI_TX_CTL(pipe);
3421 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_RX_CTL(pipe);
3427 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 temp &= ~FDI_LINK_TRAIN_NONE;
3429 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp);
3431
3432 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 udelay(150);
3434
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003436 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439
3440 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003441 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 DRM_DEBUG_KMS("FDI train 2 done.\n");
3443 break;
3444 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003446 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
3449 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003450
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451}
3452
Akshay Joshi0206e352011-08-16 15:34:10 -04003453static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3455 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3456 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3457 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3458};
3459
3460/* The FDI link training functions for SNB/Cougarpoint. */
3461static void gen6_fdi_link_train(struct drm_crtc *crtc)
3462{
3463 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003464 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3466 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003467 i915_reg_t reg;
3468 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469
Adam Jacksone1a44742010-06-25 15:32:14 -04003470 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3471 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = FDI_RX_IMR(pipe);
3473 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003474 temp &= ~FDI_RX_SYMBOL_LOCK;
3475 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp);
3477
3478 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003479 udelay(150);
3480
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 reg = FDI_TX_CTL(pipe);
3483 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003484 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003485 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_NONE;
3487 temp |= FDI_LINK_TRAIN_PATTERN_1;
3488 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3489 /* SNB-B */
3490 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492
Daniel Vetterd74cf322012-10-26 10:58:13 +02003493 I915_WRITE(FDI_RX_MISC(pipe),
3494 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3495
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 reg = FDI_RX_CTL(pipe);
3497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 if (HAS_PCH_CPT(dev)) {
3499 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3500 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3501 } else {
3502 temp &= ~FDI_LINK_TRAIN_NONE;
3503 temp |= FDI_LINK_TRAIN_PATTERN_1;
3504 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3506
3507 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 udelay(150);
3509
Akshay Joshi0206e352011-08-16 15:34:10 -04003510 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003511 reg = FDI_TX_CTL(pipe);
3512 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 I915_WRITE(reg, temp);
3516
3517 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 udelay(500);
3519
Sean Paulfa37d392012-03-02 12:53:39 -05003520 for (retry = 0; retry < 5; retry++) {
3521 reg = FDI_RX_IIR(pipe);
3522 temp = I915_READ(reg);
3523 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3524 if (temp & FDI_RX_BIT_LOCK) {
3525 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3526 DRM_DEBUG_KMS("FDI train 1 done.\n");
3527 break;
3528 }
3529 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530 }
Sean Paulfa37d392012-03-02 12:53:39 -05003531 if (retry < 5)
3532 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533 }
3534 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536
3537 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003538 reg = FDI_TX_CTL(pipe);
3539 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003540 temp &= ~FDI_LINK_TRAIN_NONE;
3541 temp |= FDI_LINK_TRAIN_PATTERN_2;
3542 if (IS_GEN6(dev)) {
3543 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3544 /* SNB-B */
3545 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3546 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 reg = FDI_RX_CTL(pipe);
3550 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003551 if (HAS_PCH_CPT(dev)) {
3552 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3553 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3554 } else {
3555 temp &= ~FDI_LINK_TRAIN_NONE;
3556 temp |= FDI_LINK_TRAIN_PATTERN_2;
3557 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 I915_WRITE(reg, temp);
3559
3560 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561 udelay(150);
3562
Akshay Joshi0206e352011-08-16 15:34:10 -04003563 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003564 reg = FDI_TX_CTL(pipe);
3565 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3567 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 I915_WRITE(reg, temp);
3569
3570 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 udelay(500);
3572
Sean Paulfa37d392012-03-02 12:53:39 -05003573 for (retry = 0; retry < 5; retry++) {
3574 reg = FDI_RX_IIR(pipe);
3575 temp = I915_READ(reg);
3576 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3577 if (temp & FDI_RX_SYMBOL_LOCK) {
3578 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3579 DRM_DEBUG_KMS("FDI train 2 done.\n");
3580 break;
3581 }
3582 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003583 }
Sean Paulfa37d392012-03-02 12:53:39 -05003584 if (retry < 5)
3585 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 }
3587 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589
3590 DRM_DEBUG_KMS("FDI train done.\n");
3591}
3592
Jesse Barnes357555c2011-04-28 15:09:55 -07003593/* Manual link training for Ivy Bridge A0 parts */
3594static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3595{
3596 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003597 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3599 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003600 i915_reg_t reg;
3601 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003602
3603 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3604 for train result */
3605 reg = FDI_RX_IMR(pipe);
3606 temp = I915_READ(reg);
3607 temp &= ~FDI_RX_SYMBOL_LOCK;
3608 temp &= ~FDI_RX_BIT_LOCK;
3609 I915_WRITE(reg, temp);
3610
3611 POSTING_READ(reg);
3612 udelay(150);
3613
Daniel Vetter01a415f2012-10-27 15:58:40 +02003614 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3615 I915_READ(FDI_RX_IIR(pipe)));
3616
Jesse Barnes139ccd32013-08-19 11:04:55 -07003617 /* Try each vswing and preemphasis setting twice before moving on */
3618 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3619 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003620 reg = FDI_TX_CTL(pipe);
3621 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003622 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3623 temp &= ~FDI_TX_ENABLE;
3624 I915_WRITE(reg, temp);
3625
3626 reg = FDI_RX_CTL(pipe);
3627 temp = I915_READ(reg);
3628 temp &= ~FDI_LINK_TRAIN_AUTO;
3629 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3630 temp &= ~FDI_RX_ENABLE;
3631 I915_WRITE(reg, temp);
3632
3633 /* enable CPU FDI TX and PCH FDI RX */
3634 reg = FDI_TX_CTL(pipe);
3635 temp = I915_READ(reg);
3636 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003637 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003638 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003639 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003640 temp |= snb_b_fdi_train_param[j/2];
3641 temp |= FDI_COMPOSITE_SYNC;
3642 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3643
3644 I915_WRITE(FDI_RX_MISC(pipe),
3645 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3646
3647 reg = FDI_RX_CTL(pipe);
3648 temp = I915_READ(reg);
3649 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3650 temp |= FDI_COMPOSITE_SYNC;
3651 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3652
3653 POSTING_READ(reg);
3654 udelay(1); /* should be 0.5us */
3655
3656 for (i = 0; i < 4; i++) {
3657 reg = FDI_RX_IIR(pipe);
3658 temp = I915_READ(reg);
3659 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3660
3661 if (temp & FDI_RX_BIT_LOCK ||
3662 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3663 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3664 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3665 i);
3666 break;
3667 }
3668 udelay(1); /* should be 0.5us */
3669 }
3670 if (i == 4) {
3671 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3672 continue;
3673 }
3674
3675 /* Train 2 */
3676 reg = FDI_TX_CTL(pipe);
3677 temp = I915_READ(reg);
3678 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3679 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3680 I915_WRITE(reg, temp);
3681
3682 reg = FDI_RX_CTL(pipe);
3683 temp = I915_READ(reg);
3684 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3685 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003686 I915_WRITE(reg, temp);
3687
3688 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003689 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003690
Jesse Barnes139ccd32013-08-19 11:04:55 -07003691 for (i = 0; i < 4; i++) {
3692 reg = FDI_RX_IIR(pipe);
3693 temp = I915_READ(reg);
3694 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003695
Jesse Barnes139ccd32013-08-19 11:04:55 -07003696 if (temp & FDI_RX_SYMBOL_LOCK ||
3697 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3698 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3699 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3700 i);
3701 goto train_done;
3702 }
3703 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003704 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003705 if (i == 4)
3706 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003707 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003708
Jesse Barnes139ccd32013-08-19 11:04:55 -07003709train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003710 DRM_DEBUG_KMS("FDI train done.\n");
3711}
3712
Daniel Vetter88cefb62012-08-12 19:27:14 +02003713static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003714{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003715 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003716 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003717 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003718 i915_reg_t reg;
3719 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003720
Jesse Barnes0e23b992010-09-10 11:10:00 -07003721 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003722 reg = FDI_RX_CTL(pipe);
3723 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003724 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003725 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003726 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003727 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3728
3729 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003730 udelay(200);
3731
3732 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003733 temp = I915_READ(reg);
3734 I915_WRITE(reg, temp | FDI_PCDCLK);
3735
3736 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003737 udelay(200);
3738
Paulo Zanoni20749732012-11-23 15:30:38 -02003739 /* Enable CPU FDI TX PLL, always on for Ironlake */
3740 reg = FDI_TX_CTL(pipe);
3741 temp = I915_READ(reg);
3742 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3743 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003744
Paulo Zanoni20749732012-11-23 15:30:38 -02003745 POSTING_READ(reg);
3746 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003747 }
3748}
3749
Daniel Vetter88cefb62012-08-12 19:27:14 +02003750static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3751{
3752 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003753 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003754 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003755 i915_reg_t reg;
3756 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003757
3758 /* Switch from PCDclk to Rawclk */
3759 reg = FDI_RX_CTL(pipe);
3760 temp = I915_READ(reg);
3761 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3762
3763 /* Disable CPU FDI TX PLL */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3767
3768 POSTING_READ(reg);
3769 udelay(100);
3770
3771 reg = FDI_RX_CTL(pipe);
3772 temp = I915_READ(reg);
3773 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3774
3775 /* Wait for the clocks to turn off. */
3776 POSTING_READ(reg);
3777 udelay(100);
3778}
3779
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003780static void ironlake_fdi_disable(struct drm_crtc *crtc)
3781{
3782 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003783 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3785 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003786 i915_reg_t reg;
3787 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003788
3789 /* disable CPU FDI tx and PCH FDI rx */
3790 reg = FDI_TX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3793 POSTING_READ(reg);
3794
3795 reg = FDI_RX_CTL(pipe);
3796 temp = I915_READ(reg);
3797 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003798 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003799 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3800
3801 POSTING_READ(reg);
3802 udelay(100);
3803
3804 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003805 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003806 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003807
3808 /* still set train pattern 1 */
3809 reg = FDI_TX_CTL(pipe);
3810 temp = I915_READ(reg);
3811 temp &= ~FDI_LINK_TRAIN_NONE;
3812 temp |= FDI_LINK_TRAIN_PATTERN_1;
3813 I915_WRITE(reg, temp);
3814
3815 reg = FDI_RX_CTL(pipe);
3816 temp = I915_READ(reg);
3817 if (HAS_PCH_CPT(dev)) {
3818 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3819 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3820 } else {
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_1;
3823 }
3824 /* BPC in FDI rx is consistent with that in PIPECONF */
3825 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003826 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003827 I915_WRITE(reg, temp);
3828
3829 POSTING_READ(reg);
3830 udelay(100);
3831}
3832
Chris Wilson5dce5b932014-01-20 10:17:36 +00003833bool intel_has_pending_fb_unpin(struct drm_device *dev)
3834{
3835 struct intel_crtc *crtc;
3836
3837 /* Note that we don't need to be called with mode_config.lock here
3838 * as our list of CRTC objects is static for the lifetime of the
3839 * device and so cannot disappear as we iterate. Similarly, we can
3840 * happily treat the predicates as racy, atomic checks as userspace
3841 * cannot claim and pin a new fb without at least acquring the
3842 * struct_mutex and so serialising with us.
3843 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003844 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003845 if (atomic_read(&crtc->unpin_work_count) == 0)
3846 continue;
3847
Daniel Vetter5a21b662016-05-24 17:13:53 +02003848 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003849 intel_wait_for_vblank(dev, crtc->pipe);
3850
3851 return true;
3852 }
3853
3854 return false;
3855}
3856
Daniel Vetter5a21b662016-05-24 17:13:53 +02003857static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003858{
3859 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003860 struct intel_flip_work *work = intel_crtc->flip_work;
3861
3862 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003863
3864 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003865 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003866
3867 drm_crtc_vblank_put(&intel_crtc->base);
3868
Daniel Vetter5a21b662016-05-24 17:13:53 +02003869 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003870 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003871
3872 trace_i915_flip_complete(intel_crtc->plane,
3873 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003874}
3875
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003876static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003877{
Chris Wilson0f911282012-04-17 10:05:38 +01003878 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003879 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003880 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003881
Daniel Vetter2c10d572012-12-20 21:24:07 +01003882 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003883
3884 ret = wait_event_interruptible_timeout(
3885 dev_priv->pending_flip_queue,
3886 !intel_crtc_has_pending_flip(crtc),
3887 60*HZ);
3888
3889 if (ret < 0)
3890 return ret;
3891
Daniel Vetter5a21b662016-05-24 17:13:53 +02003892 if (ret == 0) {
3893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3894 struct intel_flip_work *work;
3895
3896 spin_lock_irq(&dev->event_lock);
3897 work = intel_crtc->flip_work;
3898 if (work && !is_mmio_work(work)) {
3899 WARN_ONCE(1, "Removing stuck page flip\n");
3900 page_flip_completed(intel_crtc);
3901 }
3902 spin_unlock_irq(&dev->event_lock);
3903 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003904
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003905 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003906}
3907
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003908static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3909{
3910 u32 temp;
3911
3912 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3913
3914 mutex_lock(&dev_priv->sb_lock);
3915
3916 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3917 temp |= SBI_SSCCTL_DISABLE;
3918 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3919
3920 mutex_unlock(&dev_priv->sb_lock);
3921}
3922
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923/* Program iCLKIP clock to the desired frequency */
3924static void lpt_program_iclkip(struct drm_crtc *crtc)
3925{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003926 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003927 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3929 u32 temp;
3930
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003931 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003933 /* The iCLK virtual clock root frequency is in MHz,
3934 * but the adjusted_mode->crtc_clock in in KHz. To get the
3935 * divisors, it is necessary to divide one by another, so we
3936 * convert the virtual clock precision to KHz here for higher
3937 * precision.
3938 */
3939 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 u32 iclk_virtual_root_freq = 172800 * 1000;
3941 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003942 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003943
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003944 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3945 clock << auxdiv);
3946 divsel = (desired_divisor / iclk_pi_range) - 2;
3947 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003949 /*
3950 * Near 20MHz is a corner case which is
3951 * out of range for the 7-bit divisor
3952 */
3953 if (divsel <= 0x7f)
3954 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955 }
3956
3957 /* This should not happen with any sane values */
3958 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3959 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3960 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3961 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3962
3963 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 +03003964 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965 auxdiv,
3966 divsel,
3967 phasedir,
3968 phaseinc);
3969
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003970 mutex_lock(&dev_priv->sb_lock);
3971
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003973 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003974 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3975 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3976 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3977 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3978 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3979 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003980 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003981
3982 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003983 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3985 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003986 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003987
3988 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003989 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003991 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003992
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003993 mutex_unlock(&dev_priv->sb_lock);
3994
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003995 /* Wait for initialization time */
3996 udelay(24);
3997
3998 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3999}
4000
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004001int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4002{
4003 u32 divsel, phaseinc, auxdiv;
4004 u32 iclk_virtual_root_freq = 172800 * 1000;
4005 u32 iclk_pi_range = 64;
4006 u32 desired_divisor;
4007 u32 temp;
4008
4009 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4010 return 0;
4011
4012 mutex_lock(&dev_priv->sb_lock);
4013
4014 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4015 if (temp & SBI_SSCCTL_DISABLE) {
4016 mutex_unlock(&dev_priv->sb_lock);
4017 return 0;
4018 }
4019
4020 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4021 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4022 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4023 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4024 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4025
4026 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4027 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4028 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4029
4030 mutex_unlock(&dev_priv->sb_lock);
4031
4032 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4033
4034 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4035 desired_divisor << auxdiv);
4036}
4037
Daniel Vetter275f01b22013-05-03 11:49:47 +02004038static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4039 enum pipe pch_transcoder)
4040{
4041 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004042 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004043 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004044
4045 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4046 I915_READ(HTOTAL(cpu_transcoder)));
4047 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4048 I915_READ(HBLANK(cpu_transcoder)));
4049 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4050 I915_READ(HSYNC(cpu_transcoder)));
4051
4052 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4053 I915_READ(VTOTAL(cpu_transcoder)));
4054 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4055 I915_READ(VBLANK(cpu_transcoder)));
4056 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4057 I915_READ(VSYNC(cpu_transcoder)));
4058 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4059 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4060}
4061
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004062static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004063{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004064 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004065 uint32_t temp;
4066
4067 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004068 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004069 return;
4070
4071 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4072 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4073
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004074 temp &= ~FDI_BC_BIFURCATION_SELECT;
4075 if (enable)
4076 temp |= FDI_BC_BIFURCATION_SELECT;
4077
4078 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004079 I915_WRITE(SOUTH_CHICKEN1, temp);
4080 POSTING_READ(SOUTH_CHICKEN1);
4081}
4082
4083static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4084{
4085 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004086
4087 switch (intel_crtc->pipe) {
4088 case PIPE_A:
4089 break;
4090 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004091 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004092 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004093 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004094 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004095
4096 break;
4097 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004098 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099
4100 break;
4101 default:
4102 BUG();
4103 }
4104}
4105
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004106/* Return which DP Port should be selected for Transcoder DP control */
4107static enum port
4108intel_trans_dp_port_sel(struct drm_crtc *crtc)
4109{
4110 struct drm_device *dev = crtc->dev;
4111 struct intel_encoder *encoder;
4112
4113 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004114 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004115 encoder->type == INTEL_OUTPUT_EDP)
4116 return enc_to_dig_port(&encoder->base)->port;
4117 }
4118
4119 return -1;
4120}
4121
Jesse Barnesf67a5592011-01-05 10:31:48 -08004122/*
4123 * Enable PCH resources required for PCH ports:
4124 * - PCH PLLs
4125 * - FDI training & RX/TX
4126 * - update transcoder timings
4127 * - DP transcoding bits
4128 * - transcoder
4129 */
4130static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004131{
4132 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004133 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4135 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004136 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004137
Daniel Vetterab9412b2013-05-03 11:49:46 +02004138 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004139
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004140 if (IS_IVYBRIDGE(dev))
4141 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4142
Daniel Vettercd986ab2012-10-26 10:58:12 +02004143 /* Write the TU size bits before fdi link training, so that error
4144 * detection works. */
4145 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4146 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4147
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004148 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004149 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004150
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004151 /* We need to program the right clock selection before writing the pixel
4152 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004153 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004154 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004155
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004157 temp |= TRANS_DPLL_ENABLE(pipe);
4158 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004159 if (intel_crtc->config->shared_dpll ==
4160 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004161 temp |= sel;
4162 else
4163 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004167 /* XXX: pch pll's can be enabled any time before we enable the PCH
4168 * transcoder, and we actually should do this to not upset any PCH
4169 * transcoder that already use the clock when we share it.
4170 *
4171 * Note that enable_shared_dpll tries to do the right thing, but
4172 * get_shared_dpll unconditionally resets the pll - we need that to have
4173 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004174 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004175
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004176 /* set transcoder timing, panel must allow it */
4177 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004178 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004180 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004181
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004183 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004184 const struct drm_display_mode *adjusted_mode =
4185 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004186 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004187 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 temp = I915_READ(reg);
4189 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004190 TRANS_DP_SYNC_MASK |
4191 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004192 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004193 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004194
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004195 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004196 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004197 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004198 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004199
4200 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004201 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004202 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004203 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004204 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004205 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004206 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004207 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004208 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004209 break;
4210 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004211 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004212 }
4213
Chris Wilson5eddb702010-09-11 13:48:45 +01004214 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004215 }
4216
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004217 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004218}
4219
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004220static void lpt_pch_enable(struct drm_crtc *crtc)
4221{
4222 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004223 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004225 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004226
Daniel Vetterab9412b2013-05-03 11:49:46 +02004227 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004228
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004229 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004230
Paulo Zanoni0540e482012-10-31 18:12:40 -02004231 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004232 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004233
Paulo Zanoni937bb612012-10-31 18:12:47 -02004234 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004235}
4236
Daniel Vettera1520312013-05-03 11:49:50 +02004237static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004238{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004239 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004240 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004241 u32 temp;
4242
4243 temp = I915_READ(dslreg);
4244 udelay(500);
4245 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004246 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004247 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004248 }
4249}
4250
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251static int
4252skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4253 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4254 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004256 struct intel_crtc_scaler_state *scaler_state =
4257 &crtc_state->scaler_state;
4258 struct intel_crtc *intel_crtc =
4259 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004260 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004261
4262 need_scaling = intel_rotation_90_or_270(rotation) ?
4263 (src_h != dst_w || src_w != dst_h):
4264 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004265
4266 /*
4267 * if plane is being disabled or scaler is no more required or force detach
4268 * - free scaler binded to this plane/crtc
4269 * - in order to do this, update crtc->scaler_usage
4270 *
4271 * Here scaler state in crtc_state is set free so that
4272 * scaler can be assigned to other user. Actual register
4273 * update to free the scaler is done in plane/panel-fit programming.
4274 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4275 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004277 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004278 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004279 scaler_state->scalers[*scaler_id].in_use = 0;
4280
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004281 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4282 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4283 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004284 scaler_state->scaler_users);
4285 *scaler_id = -1;
4286 }
4287 return 0;
4288 }
4289
4290 /* range checks */
4291 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4292 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4293
4294 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4295 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004296 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004297 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004298 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004299 return -EINVAL;
4300 }
4301
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004302 /* mark this plane as a scaler user in crtc_state */
4303 scaler_state->scaler_users |= (1 << scaler_user);
4304 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4305 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4306 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4307 scaler_state->scaler_users);
4308
4309 return 0;
4310}
4311
4312/**
4313 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4314 *
4315 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004316 *
4317 * Return
4318 * 0 - scaler_usage updated successfully
4319 * error - requested scaling cannot be supported or other error condition
4320 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004321int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322{
4323 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004324 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004325
Ville Syrjälä78108b72016-05-27 20:59:19 +03004326 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4327 intel_crtc->base.base.id, intel_crtc->base.name,
4328 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004330 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004331 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004332 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004333 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004334}
4335
4336/**
4337 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4338 *
4339 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004340 * @plane_state: atomic plane state to update
4341 *
4342 * Return
4343 * 0 - scaler_usage updated successfully
4344 * error - requested scaling cannot be supported or other error condition
4345 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004346static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4347 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004348{
4349
4350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004351 struct intel_plane *intel_plane =
4352 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004353 struct drm_framebuffer *fb = plane_state->base.fb;
4354 int ret;
4355
4356 bool force_detach = !fb || !plane_state->visible;
4357
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004358 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4359 intel_plane->base.base.id, intel_plane->base.name,
4360 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004361
4362 ret = skl_update_scaler(crtc_state, force_detach,
4363 drm_plane_index(&intel_plane->base),
4364 &plane_state->scaler_id,
4365 plane_state->base.rotation,
4366 drm_rect_width(&plane_state->src) >> 16,
4367 drm_rect_height(&plane_state->src) >> 16,
4368 drm_rect_width(&plane_state->dst),
4369 drm_rect_height(&plane_state->dst));
4370
4371 if (ret || plane_state->scaler_id < 0)
4372 return ret;
4373
Chandra Kondurua1b22782015-04-07 15:28:45 -07004374 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004375 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004376 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4377 intel_plane->base.base.id,
4378 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004379 return -EINVAL;
4380 }
4381
4382 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004383 switch (fb->pixel_format) {
4384 case DRM_FORMAT_RGB565:
4385 case DRM_FORMAT_XBGR8888:
4386 case DRM_FORMAT_XRGB8888:
4387 case DRM_FORMAT_ABGR8888:
4388 case DRM_FORMAT_ARGB8888:
4389 case DRM_FORMAT_XRGB2101010:
4390 case DRM_FORMAT_XBGR2101010:
4391 case DRM_FORMAT_YUYV:
4392 case DRM_FORMAT_YVYU:
4393 case DRM_FORMAT_UYVY:
4394 case DRM_FORMAT_VYUY:
4395 break;
4396 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004397 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4398 intel_plane->base.base.id, intel_plane->base.name,
4399 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004400 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004401 }
4402
Chandra Kondurua1b22782015-04-07 15:28:45 -07004403 return 0;
4404}
4405
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004406static void skylake_scaler_disable(struct intel_crtc *crtc)
4407{
4408 int i;
4409
4410 for (i = 0; i < crtc->num_scalers; i++)
4411 skl_detach_scaler(crtc, i);
4412}
4413
4414static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004415{
4416 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004417 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004418 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004419 struct intel_crtc_scaler_state *scaler_state =
4420 &crtc->config->scaler_state;
4421
4422 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4423
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004424 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004425 int id;
4426
4427 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4428 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4429 return;
4430 }
4431
4432 id = scaler_state->scaler_id;
4433 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4434 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4435 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4436 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4437
4438 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004439 }
4440}
4441
Jesse Barnesb074cec2013-04-25 12:55:02 -07004442static void ironlake_pfit_enable(struct intel_crtc *crtc)
4443{
4444 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004445 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004446 int pipe = crtc->pipe;
4447
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004448 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004449 /* Force use of hard-coded filter coefficients
4450 * as some pre-programmed values are broken,
4451 * e.g. x201.
4452 */
4453 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4454 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4455 PF_PIPE_SEL_IVB(pipe));
4456 else
4457 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004458 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4459 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004460 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004461}
4462
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004463void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004464{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004465 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004466 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004467
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004468 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004469 return;
4470
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004471 /*
4472 * We can only enable IPS after we enable a plane and wait for a vblank
4473 * This function is called from post_plane_update, which is run after
4474 * a vblank wait.
4475 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004476
Paulo Zanonid77e4532013-09-24 13:52:55 -03004477 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004478 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004479 mutex_lock(&dev_priv->rps.hw_lock);
4480 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4481 mutex_unlock(&dev_priv->rps.hw_lock);
4482 /* Quoting Art Runyan: "its not safe to expect any particular
4483 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004484 * mailbox." Moreover, the mailbox may return a bogus state,
4485 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004486 */
4487 } else {
4488 I915_WRITE(IPS_CTL, IPS_ENABLE);
4489 /* The bit only becomes 1 in the next vblank, so this wait here
4490 * is essentially intel_wait_for_vblank. If we don't have this
4491 * and don't wait for vblanks until the end of crtc_enable, then
4492 * the HW state readout code will complain that the expected
4493 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004494 if (intel_wait_for_register(dev_priv,
4495 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4496 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004497 DRM_ERROR("Timed out waiting for IPS enable\n");
4498 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004499}
4500
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004501void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004502{
4503 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004504 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004505
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004506 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004507 return;
4508
4509 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004510 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004511 mutex_lock(&dev_priv->rps.hw_lock);
4512 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4513 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004514 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004515 if (intel_wait_for_register(dev_priv,
4516 IPS_CTL, IPS_ENABLE, 0,
4517 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004518 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004519 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004520 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004521 POSTING_READ(IPS_CTL);
4522 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004523
4524 /* We need to wait for a vblank before we can disable the plane. */
4525 intel_wait_for_vblank(dev, crtc->pipe);
4526}
4527
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004528static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004529{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004530 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004531 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004532 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004533
4534 mutex_lock(&dev->struct_mutex);
4535 dev_priv->mm.interruptible = false;
4536 (void) intel_overlay_switch_off(intel_crtc->overlay);
4537 dev_priv->mm.interruptible = true;
4538 mutex_unlock(&dev->struct_mutex);
4539 }
4540
4541 /* Let userspace switch the overlay on again. In most cases userspace
4542 * has to recompute where to put it anyway.
4543 */
4544}
4545
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004546/**
4547 * intel_post_enable_primary - Perform operations after enabling primary plane
4548 * @crtc: the CRTC whose primary plane was just enabled
4549 *
4550 * Performs potentially sleeping operations that must be done after the primary
4551 * plane is enabled, such as updating FBC and IPS. Note that this may be
4552 * called due to an explicit primary plane update, or due to an implicit
4553 * re-enable that is caused when a sprite plane is updated to no longer
4554 * completely hide the primary plane.
4555 */
4556static void
4557intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004558{
4559 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004560 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4562 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004563
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004564 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004565 * FIXME IPS should be fine as long as one plane is
4566 * enabled, but in practice it seems to have problems
4567 * when going from primary only to sprite only and vice
4568 * versa.
4569 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004570 hsw_enable_ips(intel_crtc);
4571
Daniel Vetterf99d7062014-06-19 16:01:59 +02004572 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004573 * Gen2 reports pipe underruns whenever all planes are disabled.
4574 * So don't enable underrun reporting before at least some planes
4575 * are enabled.
4576 * FIXME: Need to fix the logic to work when we turn off all planes
4577 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004578 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004579 if (IS_GEN2(dev))
4580 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4581
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004582 /* Underruns don't always raise interrupts, so check manually. */
4583 intel_check_cpu_fifo_underruns(dev_priv);
4584 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004585}
4586
Ville Syrjälä2622a082016-03-09 19:07:26 +02004587/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004588static void
4589intel_pre_disable_primary(struct drm_crtc *crtc)
4590{
4591 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004592 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4594 int pipe = intel_crtc->pipe;
4595
4596 /*
4597 * Gen2 reports pipe underruns whenever all planes are disabled.
4598 * So diasble underrun reporting before all the planes get disabled.
4599 * FIXME: Need to fix the logic to work when we turn off all planes
4600 * but leave the pipe running.
4601 */
4602 if (IS_GEN2(dev))
4603 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4604
4605 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004606 * FIXME IPS should be fine as long as one plane is
4607 * enabled, but in practice it seems to have problems
4608 * when going from primary only to sprite only and vice
4609 * versa.
4610 */
4611 hsw_disable_ips(intel_crtc);
4612}
4613
4614/* FIXME get rid of this and use pre_plane_update */
4615static void
4616intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4617{
4618 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004619 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4621 int pipe = intel_crtc->pipe;
4622
4623 intel_pre_disable_primary(crtc);
4624
4625 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004626 * Vblank time updates from the shadow to live plane control register
4627 * are blocked if the memory self-refresh mode is active at that
4628 * moment. So to make sure the plane gets truly disabled, disable
4629 * first the self-refresh mode. The self-refresh enable bit in turn
4630 * will be checked/applied by the HW only at the next frame start
4631 * event which is after the vblank start event, so we need to have a
4632 * wait-for-vblank between disabling the plane and the pipe.
4633 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004634 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004635 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004636 dev_priv->wm.vlv.cxsr = false;
4637 intel_wait_for_vblank(dev, pipe);
4638 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004639}
4640
Daniel Vetter5a21b662016-05-24 17:13:53 +02004641static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4642{
4643 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4644 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4645 struct intel_crtc_state *pipe_config =
4646 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004647 struct drm_plane *primary = crtc->base.primary;
4648 struct drm_plane_state *old_pri_state =
4649 drm_atomic_get_existing_plane_state(old_state, primary);
4650
Chris Wilson5748b6a2016-08-04 16:32:38 +01004651 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004652
4653 crtc->wm.cxsr_allowed = true;
4654
4655 if (pipe_config->update_wm_post && pipe_config->base.active)
4656 intel_update_watermarks(&crtc->base);
4657
4658 if (old_pri_state) {
4659 struct intel_plane_state *primary_state =
4660 to_intel_plane_state(primary->state);
4661 struct intel_plane_state *old_primary_state =
4662 to_intel_plane_state(old_pri_state);
4663
4664 intel_fbc_post_update(crtc);
4665
4666 if (primary_state->visible &&
4667 (needs_modeset(&pipe_config->base) ||
4668 !old_primary_state->visible))
4669 intel_post_enable_primary(&crtc->base);
4670 }
4671}
4672
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004673static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004674{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004675 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004676 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004677 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004678 struct intel_crtc_state *pipe_config =
4679 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004680 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4681 struct drm_plane *primary = crtc->base.primary;
4682 struct drm_plane_state *old_pri_state =
4683 drm_atomic_get_existing_plane_state(old_state, primary);
4684 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004685
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004686 if (old_pri_state) {
4687 struct intel_plane_state *primary_state =
4688 to_intel_plane_state(primary->state);
4689 struct intel_plane_state *old_primary_state =
4690 to_intel_plane_state(old_pri_state);
4691
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004692 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004693
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004694 if (old_primary_state->visible &&
4695 (modeset || !primary_state->visible))
4696 intel_pre_disable_primary(&crtc->base);
4697 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004698
David Weinehalla4015f92016-05-19 15:50:36 +03004699 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004700 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004701
Ville Syrjälä2622a082016-03-09 19:07:26 +02004702 /*
4703 * Vblank time updates from the shadow to live plane control register
4704 * are blocked if the memory self-refresh mode is active at that
4705 * moment. So to make sure the plane gets truly disabled, disable
4706 * first the self-refresh mode. The self-refresh enable bit in turn
4707 * will be checked/applied by the HW only at the next frame start
4708 * event which is after the vblank start event, so we need to have a
4709 * wait-for-vblank between disabling the plane and the pipe.
4710 */
4711 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004712 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004713 dev_priv->wm.vlv.cxsr = false;
4714 intel_wait_for_vblank(dev, crtc->pipe);
4715 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004716 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004717
Matt Ropered4a6a72016-02-23 17:20:13 -08004718 /*
4719 * IVB workaround: must disable low power watermarks for at least
4720 * one frame before enabling scaling. LP watermarks can be re-enabled
4721 * when scaling is disabled.
4722 *
4723 * WaCxSRDisabledForSpriteScaling:ivb
4724 */
4725 if (pipe_config->disable_lp_wm) {
4726 ilk_disable_lp_wm(dev);
4727 intel_wait_for_vblank(dev, crtc->pipe);
4728 }
4729
4730 /*
4731 * If we're doing a modeset, we're done. No need to do any pre-vblank
4732 * watermark programming here.
4733 */
4734 if (needs_modeset(&pipe_config->base))
4735 return;
4736
4737 /*
4738 * For platforms that support atomic watermarks, program the
4739 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4740 * will be the intermediate values that are safe for both pre- and
4741 * post- vblank; when vblank happens, the 'active' values will be set
4742 * to the final 'target' values and we'll do this again to get the
4743 * optimal watermarks. For gen9+ platforms, the values we program here
4744 * will be the final target values which will get automatically latched
4745 * at vblank time; no further programming will be necessary.
4746 *
4747 * If a platform hasn't been transitioned to atomic watermarks yet,
4748 * we'll continue to update watermarks the old way, if flags tell
4749 * us to.
4750 */
4751 if (dev_priv->display.initial_watermarks != NULL)
4752 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004753 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004754 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004755}
4756
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004757static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004758{
4759 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004761 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004762 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004763
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004764 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004765
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004766 drm_for_each_plane_mask(p, dev, plane_mask)
4767 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004768
Daniel Vetterf99d7062014-06-19 16:01:59 +02004769 /*
4770 * FIXME: Once we grow proper nuclear flip support out of this we need
4771 * to compute the mask of flip planes precisely. For the time being
4772 * consider this a flip to a NULL plane.
4773 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01004774 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004775}
4776
Jesse Barnesf67a5592011-01-05 10:31:48 -08004777static void ironlake_crtc_enable(struct drm_crtc *crtc)
4778{
4779 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004780 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004782 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004783 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004784 struct intel_crtc_state *pipe_config =
4785 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004786
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004787 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004788 return;
4789
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004790 /*
4791 * Sometimes spurious CPU pipe underruns happen during FDI
4792 * training, at least with VGA+HDMI cloning. Suppress them.
4793 *
4794 * On ILK we get an occasional spurious CPU pipe underruns
4795 * between eDP port A enable and vdd enable. Also PCH port
4796 * enable seems to result in the occasional CPU pipe underrun.
4797 *
4798 * Spurious PCH underruns also occur during PCH enabling.
4799 */
4800 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4801 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004802 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004803 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4804
4805 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004806 intel_prepare_shared_dpll(intel_crtc);
4807
Ville Syrjälä37a56502016-06-22 21:57:04 +03004808 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304809 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004810
4811 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004812 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004814 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004815 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004816 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004817 }
4818
4819 ironlake_set_pipeconf(crtc);
4820
Jesse Barnesf67a5592011-01-05 10:31:48 -08004821 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004822
Daniel Vetterf6736a12013-06-05 13:34:30 +02004823 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004824 if (encoder->pre_enable)
4825 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004826
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004827 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004828 /* Note: FDI PLL enabling _must_ be done before we enable the
4829 * cpu pipes, hence this is separate from all the other fdi/pch
4830 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004831 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004832 } else {
4833 assert_fdi_tx_disabled(dev_priv, pipe);
4834 assert_fdi_rx_disabled(dev_priv, pipe);
4835 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004836
Jesse Barnesb074cec2013-04-25 12:55:02 -07004837 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004838
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004839 /*
4840 * On ILK+ LUT must be loaded before the pipe is running but with
4841 * clocks enabled
4842 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004843 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004844
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004845 if (dev_priv->display.initial_watermarks != NULL)
4846 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004847 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004848
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004849 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004850 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004851
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004852 assert_vblank_disabled(crtc);
4853 drm_crtc_vblank_on(crtc);
4854
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004855 for_each_encoder_on_crtc(dev, crtc, encoder)
4856 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004857
4858 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004859 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004860
4861 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4862 if (intel_crtc->config->has_pch_encoder)
4863 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004864 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004865 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004866}
4867
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004868/* IPS only exists on ULT machines and is tied to pipe A. */
4869static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4870{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004871 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004872}
4873
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004874static void haswell_crtc_enable(struct drm_crtc *crtc)
4875{
4876 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004877 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4879 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004880 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004881 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004882 struct intel_crtc_state *pipe_config =
4883 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004885 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004886 return;
4887
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004888 if (intel_crtc->config->has_pch_encoder)
4889 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4890 false);
4891
Imre Deak95a7a2a2016-06-13 16:44:35 +03004892 for_each_encoder_on_crtc(dev, crtc, encoder)
4893 if (encoder->pre_pll_enable)
4894 encoder->pre_pll_enable(encoder);
4895
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004896 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004897 intel_enable_shared_dpll(intel_crtc);
4898
Ville Syrjälä37a56502016-06-22 21:57:04 +03004899 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304900 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004901
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004902 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004903 intel_set_pipe_timings(intel_crtc);
4904
Jani Nikulabc58be62016-03-18 17:05:39 +02004905 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004906
Jani Nikula4d1de972016-03-18 17:05:42 +02004907 if (cpu_transcoder != TRANSCODER_EDP &&
4908 !transcoder_is_dsi(cpu_transcoder)) {
4909 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004910 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004911 }
4912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004913 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004914 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004915 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004916 }
4917
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004918 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004919 haswell_set_pipeconf(crtc);
4920
Jani Nikula391bf042016-03-18 17:05:40 +02004921 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004922
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004923 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004924
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004926
Daniel Vetter6b698512015-11-28 11:05:39 +01004927 if (intel_crtc->config->has_pch_encoder)
4928 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4929 else
4930 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4931
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304932 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004933 if (encoder->pre_enable)
4934 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304935 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004936
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004937 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004938 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004939
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004940 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304941 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004942
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004943 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004944 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004945 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004946 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004947
4948 /*
4949 * On ILK+ LUT must be loaded before the pipe is running but with
4950 * clocks enabled
4951 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004952 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953
Paulo Zanoni1f544382012-10-24 11:32:00 -02004954 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004955 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304956 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004957
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004958 if (dev_priv->display.initial_watermarks != NULL)
4959 dev_priv->display.initial_watermarks(pipe_config);
4960 else
4961 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004962
4963 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004964 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004965 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004966
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004967 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004968 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004969
Jani Nikulaa65347b2015-11-27 12:21:46 +02004970 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004971 intel_ddi_set_vc_payload_alloc(crtc, true);
4972
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004973 assert_vblank_disabled(crtc);
4974 drm_crtc_vblank_on(crtc);
4975
Jani Nikula8807e552013-08-30 19:40:32 +03004976 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004977 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004978 intel_opregion_notify_encoder(encoder, true);
4979 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004980
Daniel Vetter6b698512015-11-28 11:05:39 +01004981 if (intel_crtc->config->has_pch_encoder) {
4982 intel_wait_for_vblank(dev, pipe);
4983 intel_wait_for_vblank(dev, pipe);
4984 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004985 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4986 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004987 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004988
Paulo Zanonie4916942013-09-20 16:21:19 -03004989 /* If we change the relative order between pipe/planes enabling, we need
4990 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004991 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4992 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4993 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4994 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4995 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996}
4997
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004998static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004999{
5000 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005001 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005002 int pipe = crtc->pipe;
5003
5004 /* To avoid upsetting the power well on haswell only disable the pfit if
5005 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005006 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005007 I915_WRITE(PF_CTL(pipe), 0);
5008 I915_WRITE(PF_WIN_POS(pipe), 0);
5009 I915_WRITE(PF_WIN_SZ(pipe), 0);
5010 }
5011}
5012
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013static void ironlake_crtc_disable(struct drm_crtc *crtc)
5014{
5015 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005016 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005018 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005019 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005020
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005021 /*
5022 * Sometimes spurious CPU pipe underruns happen when the
5023 * pipe is already disabled, but FDI RX/TX is still enabled.
5024 * Happens at least with VGA+HDMI cloning. Suppress them.
5025 */
5026 if (intel_crtc->config->has_pch_encoder) {
5027 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005028 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005029 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005030
Daniel Vetterea9d7582012-07-10 10:42:52 +02005031 for_each_encoder_on_crtc(dev, crtc, encoder)
5032 encoder->disable(encoder);
5033
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005034 drm_crtc_vblank_off(crtc);
5035 assert_vblank_disabled(crtc);
5036
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005037 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005038
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005039 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005040
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005041 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005042 ironlake_fdi_disable(crtc);
5043
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005044 for_each_encoder_on_crtc(dev, crtc, encoder)
5045 if (encoder->post_disable)
5046 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005047
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005048 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005049 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005050
Daniel Vetterd925c592013-06-05 13:34:04 +02005051 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005052 i915_reg_t reg;
5053 u32 temp;
5054
Daniel Vetterd925c592013-06-05 13:34:04 +02005055 /* disable TRANS_DP_CTL */
5056 reg = TRANS_DP_CTL(pipe);
5057 temp = I915_READ(reg);
5058 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5059 TRANS_DP_PORT_SEL_MASK);
5060 temp |= TRANS_DP_PORT_SEL_NONE;
5061 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005062
Daniel Vetterd925c592013-06-05 13:34:04 +02005063 /* disable DPLL_SEL */
5064 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005065 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005066 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005067 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005068
Daniel Vetterd925c592013-06-05 13:34:04 +02005069 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005070 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005071
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005072 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005073 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005074}
5075
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076static void haswell_crtc_disable(struct drm_crtc *crtc)
5077{
5078 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005079 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5081 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005082 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005083
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005084 if (intel_crtc->config->has_pch_encoder)
5085 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5086 false);
5087
Jani Nikula8807e552013-08-30 19:40:32 +03005088 for_each_encoder_on_crtc(dev, crtc, encoder) {
5089 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005091 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005092
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005093 drm_crtc_vblank_off(crtc);
5094 assert_vblank_disabled(crtc);
5095
Jani Nikula4d1de972016-03-18 17:05:42 +02005096 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005097 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005098 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005099
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005100 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005101 intel_ddi_set_vc_payload_alloc(crtc, false);
5102
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005103 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305104 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005105
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005106 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005107 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005108 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005109 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005110
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005111 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305112 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005113
Imre Deak97b040a2014-06-25 22:01:50 +03005114 for_each_encoder_on_crtc(dev, crtc, encoder)
5115 if (encoder->post_disable)
5116 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005117
Ville Syrjälä92966a32015-12-08 16:05:48 +02005118 if (intel_crtc->config->has_pch_encoder) {
5119 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005120 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005121 intel_ddi_fdi_disable(crtc);
5122
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005123 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5124 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005125 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005126}
5127
Jesse Barnes2dd24552013-04-25 12:55:01 -07005128static void i9xx_pfit_enable(struct intel_crtc *crtc)
5129{
5130 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005131 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005132 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005133
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005134 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005135 return;
5136
Daniel Vetterc0b03412013-05-28 12:05:54 +02005137 /*
5138 * The panel fitter should only be adjusted whilst the pipe is disabled,
5139 * according to register description and PRM.
5140 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005141 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5142 assert_pipe_disabled(dev_priv, crtc->pipe);
5143
Jesse Barnesb074cec2013-04-25 12:55:02 -07005144 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5145 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005146
5147 /* Border color in case we don't scale up to the full screen. Black by
5148 * default, change to something else for debugging. */
5149 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005150}
5151
Dave Airlied05410f2014-06-05 13:22:59 +10005152static enum intel_display_power_domain port_to_power_domain(enum port port)
5153{
5154 switch (port) {
5155 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005156 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005157 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005158 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005159 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005160 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005161 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005162 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005163 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005164 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005165 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005166 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005167 return POWER_DOMAIN_PORT_OTHER;
5168 }
5169}
5170
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005171static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5172{
5173 switch (port) {
5174 case PORT_A:
5175 return POWER_DOMAIN_AUX_A;
5176 case PORT_B:
5177 return POWER_DOMAIN_AUX_B;
5178 case PORT_C:
5179 return POWER_DOMAIN_AUX_C;
5180 case PORT_D:
5181 return POWER_DOMAIN_AUX_D;
5182 case PORT_E:
5183 /* FIXME: Check VBT for actual wiring of PORT E */
5184 return POWER_DOMAIN_AUX_D;
5185 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005186 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005187 return POWER_DOMAIN_AUX_A;
5188 }
5189}
5190
Imre Deak319be8a2014-03-04 19:22:57 +02005191enum intel_display_power_domain
5192intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005193{
Imre Deak319be8a2014-03-04 19:22:57 +02005194 struct drm_device *dev = intel_encoder->base.dev;
5195 struct intel_digital_port *intel_dig_port;
5196
5197 switch (intel_encoder->type) {
5198 case INTEL_OUTPUT_UNKNOWN:
5199 /* Only DDI platforms should ever use this output type */
5200 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005201 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005202 case INTEL_OUTPUT_HDMI:
5203 case INTEL_OUTPUT_EDP:
5204 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005205 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005206 case INTEL_OUTPUT_DP_MST:
5207 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5208 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005209 case INTEL_OUTPUT_ANALOG:
5210 return POWER_DOMAIN_PORT_CRT;
5211 case INTEL_OUTPUT_DSI:
5212 return POWER_DOMAIN_PORT_DSI;
5213 default:
5214 return POWER_DOMAIN_PORT_OTHER;
5215 }
5216}
5217
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005218enum intel_display_power_domain
5219intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5220{
5221 struct drm_device *dev = intel_encoder->base.dev;
5222 struct intel_digital_port *intel_dig_port;
5223
5224 switch (intel_encoder->type) {
5225 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005226 case INTEL_OUTPUT_HDMI:
5227 /*
5228 * Only DDI platforms should ever use these output types.
5229 * We can get here after the HDMI detect code has already set
5230 * the type of the shared encoder. Since we can't be sure
5231 * what's the status of the given connectors, play safe and
5232 * run the DP detection too.
5233 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005234 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005235 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005236 case INTEL_OUTPUT_EDP:
5237 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5238 return port_to_aux_power_domain(intel_dig_port->port);
5239 case INTEL_OUTPUT_DP_MST:
5240 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5241 return port_to_aux_power_domain(intel_dig_port->port);
5242 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005243 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005244 return POWER_DOMAIN_AUX_A;
5245 }
5246}
5247
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005248static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5249 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005250{
5251 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005252 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5254 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005255 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005256 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005257
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005258 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005259 return 0;
5260
Imre Deak77d22dc2014-03-05 16:20:52 +02005261 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5262 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005263 if (crtc_state->pch_pfit.enabled ||
5264 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005265 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5266
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005267 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5268 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5269
Imre Deak319be8a2014-03-04 19:22:57 +02005270 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005271 }
Imre Deak319be8a2014-03-04 19:22:57 +02005272
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005273 if (crtc_state->shared_dpll)
5274 mask |= BIT(POWER_DOMAIN_PLLS);
5275
Imre Deak77d22dc2014-03-05 16:20:52 +02005276 return mask;
5277}
5278
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005279static unsigned long
5280modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5281 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005282{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005283 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5285 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005286 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005287
5288 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005289 intel_crtc->enabled_power_domains = new_domains =
5290 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005291
Daniel Vetter5a21b662016-05-24 17:13:53 +02005292 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005293
5294 for_each_power_domain(domain, domains)
5295 intel_display_power_get(dev_priv, domain);
5296
Daniel Vetter5a21b662016-05-24 17:13:53 +02005297 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005298}
5299
5300static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5301 unsigned long domains)
5302{
5303 enum intel_display_power_domain domain;
5304
5305 for_each_power_domain(domain, domains)
5306 intel_display_power_put(dev_priv, domain);
5307}
5308
Mika Kaholaadafdc62015-08-18 14:36:59 +03005309static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5310{
5311 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5312
5313 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5314 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5315 return max_cdclk_freq;
5316 else if (IS_CHERRYVIEW(dev_priv))
5317 return max_cdclk_freq*95/100;
5318 else if (INTEL_INFO(dev_priv)->gen < 4)
5319 return 2*max_cdclk_freq*90/100;
5320 else
5321 return max_cdclk_freq*90/100;
5322}
5323
Ville Syrjäläb2045352016-05-13 23:41:27 +03005324static int skl_calc_cdclk(int max_pixclk, int vco);
5325
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005326static void intel_update_max_cdclk(struct drm_device *dev)
5327{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005328 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005329
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005330 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005331 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005332 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005333
Ville Syrjäläb2045352016-05-13 23:41:27 +03005334 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005335 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005336
5337 /*
5338 * Use the lower (vco 8640) cdclk values as a
5339 * first guess. skl_calc_cdclk() will correct it
5340 * if the preferred vco is 8100 instead.
5341 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005342 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005343 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005344 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005345 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005346 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005347 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005348 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005349 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005350
5351 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005352 } else if (IS_BROXTON(dev)) {
5353 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005354 } else if (IS_BROADWELL(dev)) {
5355 /*
5356 * FIXME with extra cooling we can allow
5357 * 540 MHz for ULX and 675 Mhz for ULT.
5358 * How can we know if extra cooling is
5359 * available? PCI ID, VTB, something else?
5360 */
5361 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5362 dev_priv->max_cdclk_freq = 450000;
5363 else if (IS_BDW_ULX(dev))
5364 dev_priv->max_cdclk_freq = 450000;
5365 else if (IS_BDW_ULT(dev))
5366 dev_priv->max_cdclk_freq = 540000;
5367 else
5368 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005369 } else if (IS_CHERRYVIEW(dev)) {
5370 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005371 } else if (IS_VALLEYVIEW(dev)) {
5372 dev_priv->max_cdclk_freq = 400000;
5373 } else {
5374 /* otherwise assume cdclk is fixed */
5375 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5376 }
5377
Mika Kaholaadafdc62015-08-18 14:36:59 +03005378 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5379
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005380 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5381 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005382
5383 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5384 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005385}
5386
5387static void intel_update_cdclk(struct drm_device *dev)
5388{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005389 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005390
5391 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005392
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005393 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005394 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5395 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5396 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005397 else
5398 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5399 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005400
5401 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005402 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5403 * Programmng [sic] note: bit[9:2] should be programmed to the number
5404 * of cdclk that generates 4MHz reference clock freq which is used to
5405 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005406 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005407 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005408 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005409}
5410
Ville Syrjälä92891e42016-05-11 22:44:45 +03005411/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5412static int skl_cdclk_decimal(int cdclk)
5413{
5414 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5415}
5416
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005417static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5418{
5419 int ratio;
5420
5421 if (cdclk == dev_priv->cdclk_pll.ref)
5422 return 0;
5423
5424 switch (cdclk) {
5425 default:
5426 MISSING_CASE(cdclk);
5427 case 144000:
5428 case 288000:
5429 case 384000:
5430 case 576000:
5431 ratio = 60;
5432 break;
5433 case 624000:
5434 ratio = 65;
5435 break;
5436 }
5437
5438 return dev_priv->cdclk_pll.ref * ratio;
5439}
5440
Ville Syrjälä2b730012016-05-13 23:41:34 +03005441static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5442{
5443 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5444
5445 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005446 if (intel_wait_for_register(dev_priv,
5447 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5448 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005449 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005450
5451 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005452}
5453
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005454static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005455{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005456 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005457 u32 val;
5458
5459 val = I915_READ(BXT_DE_PLL_CTL);
5460 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005461 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005462 I915_WRITE(BXT_DE_PLL_CTL, val);
5463
5464 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5465
5466 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005467 if (intel_wait_for_register(dev_priv,
5468 BXT_DE_PLL_ENABLE,
5469 BXT_DE_PLL_LOCK,
5470 BXT_DE_PLL_LOCK,
5471 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005472 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005473
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005474 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005475}
5476
Imre Deak324513c2016-06-13 16:44:36 +03005477static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305478{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005479 u32 val, divider;
5480 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305481
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005482 vco = bxt_de_pll_vco(dev_priv, cdclk);
5483
5484 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5485
5486 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5487 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5488 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305489 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305490 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005491 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305493 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005494 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305496 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005497 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305498 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305499 break;
5500 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005501 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5502 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305503
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005504 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5505 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506 }
5507
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305508 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005509 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305510 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5511 0x80000000);
5512 mutex_unlock(&dev_priv->rps.hw_lock);
5513
5514 if (ret) {
5515 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005516 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305517 return;
5518 }
5519
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005520 if (dev_priv->cdclk_pll.vco != 0 &&
5521 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005522 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305523
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005524 if (dev_priv->cdclk_pll.vco != vco)
5525 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305526
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005527 val = divider | skl_cdclk_decimal(cdclk);
5528 /*
5529 * FIXME if only the cd2x divider needs changing, it could be done
5530 * without shutting off the pipe (if only one pipe is active).
5531 */
5532 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5533 /*
5534 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5535 * enable otherwise.
5536 */
5537 if (cdclk >= 500000)
5538 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5539 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305540
5541 mutex_lock(&dev_priv->rps.hw_lock);
5542 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005543 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305544 mutex_unlock(&dev_priv->rps.hw_lock);
5545
5546 if (ret) {
5547 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005548 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305549 return;
5550 }
5551
Chris Wilson91c8a322016-07-05 10:40:23 +01005552 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305553}
5554
Imre Deakd66a2192016-05-24 15:38:33 +03005555static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305556{
Imre Deakd66a2192016-05-24 15:38:33 +03005557 u32 cdctl, expected;
5558
Chris Wilson91c8a322016-07-05 10:40:23 +01005559 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305560
Imre Deakd66a2192016-05-24 15:38:33 +03005561 if (dev_priv->cdclk_pll.vco == 0 ||
5562 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5563 goto sanitize;
5564
5565 /* DPLL okay; verify the cdclock
5566 *
5567 * Some BIOS versions leave an incorrect decimal frequency value and
5568 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5569 * so sanitize this register.
5570 */
5571 cdctl = I915_READ(CDCLK_CTL);
5572 /*
5573 * Let's ignore the pipe field, since BIOS could have configured the
5574 * dividers both synching to an active pipe, or asynchronously
5575 * (PIPE_NONE).
5576 */
5577 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5578
5579 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5580 skl_cdclk_decimal(dev_priv->cdclk_freq);
5581 /*
5582 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5583 * enable otherwise.
5584 */
5585 if (dev_priv->cdclk_freq >= 500000)
5586 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5587
5588 if (cdctl == expected)
5589 /* All well; nothing to sanitize */
5590 return;
5591
5592sanitize:
5593 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5594
5595 /* force cdclk programming */
5596 dev_priv->cdclk_freq = 0;
5597
5598 /* force full PLL disable + enable */
5599 dev_priv->cdclk_pll.vco = -1;
5600}
5601
Imre Deak324513c2016-06-13 16:44:36 +03005602void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005603{
5604 bxt_sanitize_cdclk(dev_priv);
5605
5606 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005607 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005608
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305609 /*
5610 * FIXME:
5611 * - The initial CDCLK needs to be read from VBT.
5612 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305613 */
Imre Deak324513c2016-06-13 16:44:36 +03005614 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305615}
5616
Imre Deak324513c2016-06-13 16:44:36 +03005617void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305618{
Imre Deak324513c2016-06-13 16:44:36 +03005619 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305620}
5621
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005622static int skl_calc_cdclk(int max_pixclk, int vco)
5623{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005624 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005625 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005626 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005627 else if (max_pixclk > 432000)
5628 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005629 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005630 return 432000;
5631 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005632 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005633 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005634 if (max_pixclk > 540000)
5635 return 675000;
5636 else if (max_pixclk > 450000)
5637 return 540000;
5638 else if (max_pixclk > 337500)
5639 return 450000;
5640 else
5641 return 337500;
5642 }
5643}
5644
Ville Syrjäläea617912016-05-13 23:41:24 +03005645static void
5646skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005647{
Ville Syrjäläea617912016-05-13 23:41:24 +03005648 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005649
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005650 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005651 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005652
Ville Syrjäläea617912016-05-13 23:41:24 +03005653 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005654 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005655 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005656
Imre Deak1c3f7702016-05-24 15:38:32 +03005657 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5658 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005659
Ville Syrjäläea617912016-05-13 23:41:24 +03005660 val = I915_READ(DPLL_CTRL1);
5661
Imre Deak1c3f7702016-05-24 15:38:32 +03005662 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5663 DPLL_CTRL1_SSC(SKL_DPLL0) |
5664 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5665 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5666 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005667
Ville Syrjäläea617912016-05-13 23:41:24 +03005668 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5669 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5670 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5671 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5672 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005673 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005674 break;
5675 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5676 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005677 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005678 break;
5679 default:
5680 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005681 break;
5682 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005683}
5684
Ville Syrjäläb2045352016-05-13 23:41:27 +03005685void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5686{
5687 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5688
5689 dev_priv->skl_preferred_vco_freq = vco;
5690
5691 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005692 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005693}
5694
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005696skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005697{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005698 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699 u32 val;
5700
Ville Syrjälä63911d72016-05-13 23:41:32 +03005701 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005702
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005703 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005704 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005705 I915_WRITE(CDCLK_CTL, val);
5706 POSTING_READ(CDCLK_CTL);
5707
5708 /*
5709 * We always enable DPLL0 with the lowest link rate possible, but still
5710 * taking into account the VCO required to operate the eDP panel at the
5711 * desired frequency. The usual DP link rates operate with a VCO of
5712 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5713 * The modeset code is responsible for the selection of the exact link
5714 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005715 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005716 */
5717 val = I915_READ(DPLL_CTRL1);
5718
5719 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5720 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5721 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005722 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005723 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5724 SKL_DPLL0);
5725 else
5726 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5727 SKL_DPLL0);
5728
5729 I915_WRITE(DPLL_CTRL1, val);
5730 POSTING_READ(DPLL_CTRL1);
5731
5732 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5733
Chris Wilsone24ca052016-06-30 15:33:05 +01005734 if (intel_wait_for_register(dev_priv,
5735 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5736 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005737 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005738
Ville Syrjälä63911d72016-05-13 23:41:32 +03005739 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005740
5741 /* We'll want to keep using the current vco from now on. */
5742 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005743}
5744
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005745static void
5746skl_dpll0_disable(struct drm_i915_private *dev_priv)
5747{
5748 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01005749 if (intel_wait_for_register(dev_priv,
5750 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5751 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005752 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005753
Ville Syrjälä63911d72016-05-13 23:41:32 +03005754 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005755}
5756
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005757static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5758{
5759 int ret;
5760 u32 val;
5761
5762 /* inform PCU we want to change CDCLK */
5763 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5764 mutex_lock(&dev_priv->rps.hw_lock);
5765 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5766 mutex_unlock(&dev_priv->rps.hw_lock);
5767
5768 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5769}
5770
5771static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5772{
Ville Syrjälä848496e2016-07-13 16:32:03 +03005773 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005774}
5775
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005776static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005777{
Chris Wilson91c8a322016-07-05 10:40:23 +01005778 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005779 u32 freq_select, pcu_ack;
5780
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005781 WARN_ON((cdclk == 24000) != (vco == 0));
5782
Ville Syrjälä63911d72016-05-13 23:41:32 +03005783 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005784
5785 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5786 DRM_ERROR("failed to inform PCU about cdclk change\n");
5787 return;
5788 }
5789
5790 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005791 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005792 case 450000:
5793 case 432000:
5794 freq_select = CDCLK_FREQ_450_432;
5795 pcu_ack = 1;
5796 break;
5797 case 540000:
5798 freq_select = CDCLK_FREQ_540;
5799 pcu_ack = 2;
5800 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005801 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005802 case 337500:
5803 default:
5804 freq_select = CDCLK_FREQ_337_308;
5805 pcu_ack = 0;
5806 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005807 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005808 case 675000:
5809 freq_select = CDCLK_FREQ_675_617;
5810 pcu_ack = 3;
5811 break;
5812 }
5813
Ville Syrjälä63911d72016-05-13 23:41:32 +03005814 if (dev_priv->cdclk_pll.vco != 0 &&
5815 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005816 skl_dpll0_disable(dev_priv);
5817
Ville Syrjälä63911d72016-05-13 23:41:32 +03005818 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005819 skl_dpll0_enable(dev_priv, vco);
5820
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005821 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005822 POSTING_READ(CDCLK_CTL);
5823
5824 /* inform PCU of the change */
5825 mutex_lock(&dev_priv->rps.hw_lock);
5826 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5827 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005828
5829 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005830}
5831
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005832static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5833
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005834void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5835{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005836 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005837}
5838
5839void skl_init_cdclk(struct drm_i915_private *dev_priv)
5840{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005841 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005842
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005843 skl_sanitize_cdclk(dev_priv);
5844
Ville Syrjälä63911d72016-05-13 23:41:32 +03005845 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005846 /*
5847 * Use the current vco as our initial
5848 * guess as to what the preferred vco is.
5849 */
5850 if (dev_priv->skl_preferred_vco_freq == 0)
5851 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005852 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005853 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005854 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005855
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005856 vco = dev_priv->skl_preferred_vco_freq;
5857 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005858 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005859 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005860
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005861 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005862}
5863
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005864static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305865{
Ville Syrjälä09492492016-05-13 23:41:28 +03005866 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305867
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305868 /*
5869 * check if the pre-os intialized the display
5870 * There is SWF18 scratchpad register defined which is set by the
5871 * pre-os which can be used by the OS drivers to check the status
5872 */
5873 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5874 goto sanitize;
5875
Chris Wilson91c8a322016-07-05 10:40:23 +01005876 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03005877 /* Is PLL enabled and locked ? */
5878 if (dev_priv->cdclk_pll.vco == 0 ||
5879 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5880 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005881
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305882 /* DPLL okay; verify the cdclock
5883 *
5884 * Noticed in some instances that the freq selection is correct but
5885 * decimal part is programmed wrong from BIOS where pre-os does not
5886 * enable display. Verify the same as well.
5887 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005888 cdctl = I915_READ(CDCLK_CTL);
5889 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5890 skl_cdclk_decimal(dev_priv->cdclk_freq);
5891 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305892 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005893 return;
5894
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305895sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005896 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005897
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005898 /* force cdclk programming */
5899 dev_priv->cdclk_freq = 0;
5900 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005901 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305902}
5903
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904/* Adjust CDclk dividers to allow high res or save power if possible */
5905static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5906{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005907 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908 u32 val, cmd;
5909
Vandana Kannan164dfd22014-11-24 13:37:41 +05305910 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5911 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005912
Ville Syrjälädfcab172014-06-13 13:37:47 +03005913 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005914 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005915 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916 cmd = 1;
5917 else
5918 cmd = 0;
5919
5920 mutex_lock(&dev_priv->rps.hw_lock);
5921 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5922 val &= ~DSPFREQGUAR_MASK;
5923 val |= (cmd << DSPFREQGUAR_SHIFT);
5924 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5925 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5926 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5927 50)) {
5928 DRM_ERROR("timed out waiting for CDclk change\n");
5929 }
5930 mutex_unlock(&dev_priv->rps.hw_lock);
5931
Ville Syrjälä54433e92015-05-26 20:42:31 +03005932 mutex_lock(&dev_priv->sb_lock);
5933
Ville Syrjälädfcab172014-06-13 13:37:47 +03005934 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005935 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005937 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939 /* adjust cdclk divider */
5940 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005941 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005942 val |= divider;
5943 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005944
5945 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005946 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005947 50))
5948 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949 }
5950
Jesse Barnes30a970c2013-11-04 13:48:12 -08005951 /* adjust self-refresh exit latency value */
5952 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5953 val &= ~0x7f;
5954
5955 /*
5956 * For high bandwidth configs, we set a higher latency in the bunit
5957 * so that the core display fetch happens in time to avoid underruns.
5958 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005959 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005960 val |= 4500 / 250; /* 4.5 usec */
5961 else
5962 val |= 3000 / 250; /* 3.0 usec */
5963 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005964
Ville Syrjäläa5805162015-05-26 20:42:30 +03005965 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005966
Ville Syrjäläb6283052015-06-03 15:45:07 +03005967 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005968}
5969
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005970static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5971{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005972 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005973 u32 val, cmd;
5974
Vandana Kannan164dfd22014-11-24 13:37:41 +05305975 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5976 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005977
5978 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005979 case 333333:
5980 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005981 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005982 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005983 break;
5984 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005985 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005986 return;
5987 }
5988
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005989 /*
5990 * Specs are full of misinformation, but testing on actual
5991 * hardware has shown that we just need to write the desired
5992 * CCK divider into the Punit register.
5993 */
5994 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5995
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005996 mutex_lock(&dev_priv->rps.hw_lock);
5997 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5998 val &= ~DSPFREQGUAR_MASK_CHV;
5999 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6000 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6001 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6002 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6003 50)) {
6004 DRM_ERROR("timed out waiting for CDclk change\n");
6005 }
6006 mutex_unlock(&dev_priv->rps.hw_lock);
6007
Ville Syrjäläb6283052015-06-03 15:45:07 +03006008 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006009}
6010
Jesse Barnes30a970c2013-11-04 13:48:12 -08006011static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6012 int max_pixclk)
6013{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006014 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006015 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006016
Jesse Barnes30a970c2013-11-04 13:48:12 -08006017 /*
6018 * Really only a few cases to deal with, as only 4 CDclks are supported:
6019 * 200MHz
6020 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006021 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006022 * 400MHz (VLV only)
6023 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6024 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006025 *
6026 * We seem to get an unstable or solid color picture at 200MHz.
6027 * Not sure what's wrong. For now use 200MHz only when all pipes
6028 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006029 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006030 if (!IS_CHERRYVIEW(dev_priv) &&
6031 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006032 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006033 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006034 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006035 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006036 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006037 else
6038 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006039}
6040
Imre Deak324513c2016-06-13 16:44:36 +03006041static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006042{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006043 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306044 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006045 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306046 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006047 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306048 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006049 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306050 return 288000;
6051 else
6052 return 144000;
6053}
6054
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006055/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006056static int intel_mode_max_pixclk(struct drm_device *dev,
6057 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006058{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006059 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006060 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006061 struct drm_crtc *crtc;
6062 struct drm_crtc_state *crtc_state;
6063 unsigned max_pixclk = 0, i;
6064 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006065
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006066 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6067 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006068
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006069 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6070 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006071
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006072 if (crtc_state->enable)
6073 pixclk = crtc_state->adjusted_mode.crtc_clock;
6074
6075 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006076 }
6077
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006078 for_each_pipe(dev_priv, pipe)
6079 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6080
Jesse Barnes30a970c2013-11-04 13:48:12 -08006081 return max_pixclk;
6082}
6083
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006084static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006085{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006086 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006087 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006088 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006089 struct intel_atomic_state *intel_state =
6090 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006091
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006092 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006093 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306094
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006095 if (!intel_state->active_crtcs)
6096 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6097
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006098 return 0;
6099}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006100
Imre Deak324513c2016-06-13 16:44:36 +03006101static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006102{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006103 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006104 struct intel_atomic_state *intel_state =
6105 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006106
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006107 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006108 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006109
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006110 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006111 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006112
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006113 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006114}
6115
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006116static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6117{
6118 unsigned int credits, default_credits;
6119
6120 if (IS_CHERRYVIEW(dev_priv))
6121 default_credits = PFI_CREDIT(12);
6122 else
6123 default_credits = PFI_CREDIT(8);
6124
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006125 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006126 /* CHV suggested value is 31 or 63 */
6127 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006128 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006129 else
6130 credits = PFI_CREDIT(15);
6131 } else {
6132 credits = default_credits;
6133 }
6134
6135 /*
6136 * WA - write default credits before re-programming
6137 * FIXME: should we also set the resend bit here?
6138 */
6139 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6140 default_credits);
6141
6142 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6143 credits | PFI_CREDIT_RESEND);
6144
6145 /*
6146 * FIXME is this guaranteed to clear
6147 * immediately or should we poll for it?
6148 */
6149 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6150}
6151
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006152static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006153{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006154 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006155 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006156 struct intel_atomic_state *old_intel_state =
6157 to_intel_atomic_state(old_state);
6158 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006159
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006160 /*
6161 * FIXME: We can end up here with all power domains off, yet
6162 * with a CDCLK frequency other than the minimum. To account
6163 * for this take the PIPE-A power domain, which covers the HW
6164 * blocks needed for the following programming. This can be
6165 * removed once it's guaranteed that we get here either with
6166 * the minimum CDCLK set, or the required power domains
6167 * enabled.
6168 */
6169 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006170
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006171 if (IS_CHERRYVIEW(dev))
6172 cherryview_set_cdclk(dev, req_cdclk);
6173 else
6174 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006175
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006176 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006177
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006178 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006179}
6180
Jesse Barnes89b667f2013-04-18 14:51:36 -07006181static void valleyview_crtc_enable(struct drm_crtc *crtc)
6182{
6183 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006184 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6186 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006187 struct intel_crtc_state *pipe_config =
6188 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006189 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006190
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006191 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006192 return;
6193
Ville Syrjälä37a56502016-06-22 21:57:04 +03006194 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306195 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006196
6197 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006198 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006199
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006200 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006201 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006202
6203 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6204 I915_WRITE(CHV_CANVAS(pipe), 0);
6205 }
6206
Daniel Vetter5b18e572014-04-24 23:55:06 +02006207 i9xx_set_pipeconf(intel_crtc);
6208
Jesse Barnes89b667f2013-04-18 14:51:36 -07006209 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006210
Daniel Vettera72e4c92014-09-30 10:56:47 +02006211 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006212
Jesse Barnes89b667f2013-04-18 14:51:36 -07006213 for_each_encoder_on_crtc(dev, crtc, encoder)
6214 if (encoder->pre_pll_enable)
6215 encoder->pre_pll_enable(encoder);
6216
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006217 if (IS_CHERRYVIEW(dev)) {
6218 chv_prepare_pll(intel_crtc, intel_crtc->config);
6219 chv_enable_pll(intel_crtc, intel_crtc->config);
6220 } else {
6221 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6222 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006223 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006224
6225 for_each_encoder_on_crtc(dev, crtc, encoder)
6226 if (encoder->pre_enable)
6227 encoder->pre_enable(encoder);
6228
Jesse Barnes2dd24552013-04-25 12:55:01 -07006229 i9xx_pfit_enable(intel_crtc);
6230
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006231 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006232
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006233 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006234 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006235
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006236 assert_vblank_disabled(crtc);
6237 drm_crtc_vblank_on(crtc);
6238
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006239 for_each_encoder_on_crtc(dev, crtc, encoder)
6240 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006241}
6242
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006243static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6244{
6245 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006246 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006248 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6249 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006250}
6251
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006252static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006253{
6254 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006255 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006257 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006258 struct intel_crtc_state *pipe_config =
6259 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006260 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006261
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006262 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006263 return;
6264
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006265 i9xx_set_pll_dividers(intel_crtc);
6266
Ville Syrjälä37a56502016-06-22 21:57:04 +03006267 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306268 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006269
6270 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006271 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006272
Daniel Vetter5b18e572014-04-24 23:55:06 +02006273 i9xx_set_pipeconf(intel_crtc);
6274
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006275 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006276
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006277 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006278 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006279
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006280 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006281 if (encoder->pre_enable)
6282 encoder->pre_enable(encoder);
6283
Daniel Vetterf6736a12013-06-05 13:34:30 +02006284 i9xx_enable_pll(intel_crtc);
6285
Jesse Barnes2dd24552013-04-25 12:55:01 -07006286 i9xx_pfit_enable(intel_crtc);
6287
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006288 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006289
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006290 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006291 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006292
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006293 assert_vblank_disabled(crtc);
6294 drm_crtc_vblank_on(crtc);
6295
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006296 for_each_encoder_on_crtc(dev, crtc, encoder)
6297 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006298}
6299
Daniel Vetter87476d62013-04-11 16:29:06 +02006300static void i9xx_pfit_disable(struct intel_crtc *crtc)
6301{
6302 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006303 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006304
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006305 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006306 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006307
6308 assert_pipe_disabled(dev_priv, crtc->pipe);
6309
Daniel Vetter328d8e82013-05-08 10:36:31 +02006310 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6311 I915_READ(PFIT_CONTROL));
6312 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006313}
6314
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006315static void i9xx_crtc_disable(struct drm_crtc *crtc)
6316{
6317 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006318 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006320 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006321 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006322
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006323 /*
6324 * On gen2 planes are double buffered but the pipe isn't, so we must
6325 * wait for planes to fully turn off before disabling the pipe.
6326 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006327 if (IS_GEN2(dev))
6328 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006329
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006330 for_each_encoder_on_crtc(dev, crtc, encoder)
6331 encoder->disable(encoder);
6332
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006333 drm_crtc_vblank_off(crtc);
6334 assert_vblank_disabled(crtc);
6335
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006336 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006337
Daniel Vetter87476d62013-04-11 16:29:06 +02006338 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006339
Jesse Barnes89b667f2013-04-18 14:51:36 -07006340 for_each_encoder_on_crtc(dev, crtc, encoder)
6341 if (encoder->post_disable)
6342 encoder->post_disable(encoder);
6343
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006344 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006345 if (IS_CHERRYVIEW(dev))
6346 chv_disable_pll(dev_priv, pipe);
6347 else if (IS_VALLEYVIEW(dev))
6348 vlv_disable_pll(dev_priv, pipe);
6349 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006350 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006351 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006352
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006353 for_each_encoder_on_crtc(dev, crtc, encoder)
6354 if (encoder->post_pll_disable)
6355 encoder->post_pll_disable(encoder);
6356
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006357 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006358 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006359}
6360
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006361static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006362{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006363 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006365 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006366 enum intel_display_power_domain domain;
6367 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006368
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006369 if (!intel_crtc->active)
6370 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006371
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006372 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006373 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006374
Ville Syrjälä2622a082016-03-09 19:07:26 +02006375 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006376
6377 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6378 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006379 }
6380
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006381 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006382
Ville Syrjälä78108b72016-05-27 20:59:19 +03006383 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6384 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006385
6386 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6387 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006388 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006389 crtc->enabled = false;
6390 crtc->state->connector_mask = 0;
6391 crtc->state->encoder_mask = 0;
6392
6393 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6394 encoder->base.crtc = NULL;
6395
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006396 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006397 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006398 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006399
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006400 domains = intel_crtc->enabled_power_domains;
6401 for_each_power_domain(domain, domains)
6402 intel_display_power_put(dev_priv, domain);
6403 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006404
6405 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6406 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006407}
6408
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006409/*
6410 * turn all crtc's off, but do not adjust state
6411 * This has to be paired with a call to intel_modeset_setup_hw_state.
6412 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006413int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006414{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006415 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006416 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006417 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006418
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006419 state = drm_atomic_helper_suspend(dev);
6420 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006421 if (ret)
6422 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006423 else
6424 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006425 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006426}
6427
Chris Wilsonea5b2132010-08-04 13:50:23 +01006428void intel_encoder_destroy(struct drm_encoder *encoder)
6429{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006430 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006431
Chris Wilsonea5b2132010-08-04 13:50:23 +01006432 drm_encoder_cleanup(encoder);
6433 kfree(intel_encoder);
6434}
6435
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006436/* Cross check the actual hw state with our own modeset state tracking (and it's
6437 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006438static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006439{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006440 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006441
6442 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6443 connector->base.base.id,
6444 connector->base.name);
6445
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006446 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006447 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006448 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006449
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006450 I915_STATE_WARN(!crtc,
6451 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006452
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006453 if (!crtc)
6454 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006455
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006456 I915_STATE_WARN(!crtc->state->active,
6457 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006458
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006459 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006460 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006461
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006462 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006463 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006464
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006465 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006466 "attached encoder crtc differs from connector crtc\n");
6467 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006468 I915_STATE_WARN(crtc && crtc->state->active,
6469 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006470 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006471 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006472 }
6473}
6474
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006475int intel_connector_init(struct intel_connector *connector)
6476{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006477 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006478
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006479 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006480 return -ENOMEM;
6481
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006482 return 0;
6483}
6484
6485struct intel_connector *intel_connector_alloc(void)
6486{
6487 struct intel_connector *connector;
6488
6489 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6490 if (!connector)
6491 return NULL;
6492
6493 if (intel_connector_init(connector) < 0) {
6494 kfree(connector);
6495 return NULL;
6496 }
6497
6498 return connector;
6499}
6500
Daniel Vetterf0947c32012-07-02 13:10:34 +02006501/* Simple connector->get_hw_state implementation for encoders that support only
6502 * one connector and no cloning and hence the encoder state determines the state
6503 * of the connector. */
6504bool intel_connector_get_hw_state(struct intel_connector *connector)
6505{
Daniel Vetter24929352012-07-02 20:28:59 +02006506 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006507 struct intel_encoder *encoder = connector->encoder;
6508
6509 return encoder->get_hw_state(encoder, &pipe);
6510}
6511
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006512static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006513{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006514 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6515 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006516
6517 return 0;
6518}
6519
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006520static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006521 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006522{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006523 struct drm_atomic_state *state = pipe_config->base.state;
6524 struct intel_crtc *other_crtc;
6525 struct intel_crtc_state *other_crtc_state;
6526
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006527 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6528 pipe_name(pipe), pipe_config->fdi_lanes);
6529 if (pipe_config->fdi_lanes > 4) {
6530 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6531 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006532 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006533 }
6534
Paulo Zanonibafb6552013-11-02 21:07:44 -07006535 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006536 if (pipe_config->fdi_lanes > 2) {
6537 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6538 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006539 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006540 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006541 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006542 }
6543 }
6544
6545 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006546 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006547
6548 /* Ivybridge 3 pipe is really complicated */
6549 switch (pipe) {
6550 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006551 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006552 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006553 if (pipe_config->fdi_lanes <= 2)
6554 return 0;
6555
6556 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6557 other_crtc_state =
6558 intel_atomic_get_crtc_state(state, other_crtc);
6559 if (IS_ERR(other_crtc_state))
6560 return PTR_ERR(other_crtc_state);
6561
6562 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006563 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6564 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006565 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006566 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006567 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006568 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006569 if (pipe_config->fdi_lanes > 2) {
6570 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6571 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006572 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006573 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006574
6575 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6576 other_crtc_state =
6577 intel_atomic_get_crtc_state(state, other_crtc);
6578 if (IS_ERR(other_crtc_state))
6579 return PTR_ERR(other_crtc_state);
6580
6581 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006582 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006583 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006584 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006585 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006586 default:
6587 BUG();
6588 }
6589}
6590
Daniel Vettere29c22c2013-02-21 00:00:16 +01006591#define RETRY 1
6592static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006593 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006594{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006595 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006596 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006597 int lane, link_bw, fdi_dotclock, ret;
6598 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006599
Daniel Vettere29c22c2013-02-21 00:00:16 +01006600retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006601 /* FDI is a binary signal running at ~2.7GHz, encoding
6602 * each output octet as 10 bits. The actual frequency
6603 * is stored as a divider into a 100MHz clock, and the
6604 * mode pixel clock is stored in units of 1KHz.
6605 * Hence the bw of each lane in terms of the mode signal
6606 * is:
6607 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006608 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006609
Damien Lespiau241bfc32013-09-25 16:45:37 +01006610 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006611
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006612 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006613 pipe_config->pipe_bpp);
6614
6615 pipe_config->fdi_lanes = lane;
6616
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006617 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006618 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006619
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006620 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006621 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006622 pipe_config->pipe_bpp -= 2*3;
6623 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6624 pipe_config->pipe_bpp);
6625 needs_recompute = true;
6626 pipe_config->bw_constrained = true;
6627
6628 goto retry;
6629 }
6630
6631 if (needs_recompute)
6632 return RETRY;
6633
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006634 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006635}
6636
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006637static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6638 struct intel_crtc_state *pipe_config)
6639{
6640 if (pipe_config->pipe_bpp > 24)
6641 return false;
6642
6643 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006644 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006645 return true;
6646
6647 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006648 * We compare against max which means we must take
6649 * the increased cdclk requirement into account when
6650 * calculating the new cdclk.
6651 *
6652 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006653 */
6654 return ilk_pipe_pixel_rate(pipe_config) <=
6655 dev_priv->max_cdclk_freq * 95 / 100;
6656}
6657
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006658static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006659 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006660{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006661 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006662 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006663
Jani Nikulad330a952014-01-21 11:24:25 +02006664 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006665 hsw_crtc_supports_ips(crtc) &&
6666 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006667}
6668
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006669static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6670{
6671 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6672
6673 /* GDG double wide on either pipe, otherwise pipe A only */
6674 return INTEL_INFO(dev_priv)->gen < 4 &&
6675 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6676}
6677
Daniel Vettera43f6e02013-06-07 23:10:32 +02006678static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006679 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006680{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006681 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006682 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006683 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006684 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006685
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006686 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006687 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006688
6689 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006690 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006691 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006692 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006693 if (intel_crtc_supports_double_wide(crtc) &&
6694 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006695 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006696 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006697 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006698 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006699
Ville Syrjäläf3261152016-05-24 21:34:18 +03006700 if (adjusted_mode->crtc_clock > clock_limit) {
6701 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6702 adjusted_mode->crtc_clock, clock_limit,
6703 yesno(pipe_config->double_wide));
6704 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006705 }
Chris Wilson89749352010-09-12 18:25:19 +01006706
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006707 /*
6708 * Pipe horizontal size must be even in:
6709 * - DVO ganged mode
6710 * - LVDS dual channel mode
6711 * - Double wide pipe
6712 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006713 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006714 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6715 pipe_config->pipe_src_w &= ~1;
6716
Damien Lespiau8693a822013-05-03 18:48:11 +01006717 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6718 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006719 */
6720 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006721 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006722 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006723
Damien Lespiauf5adf942013-06-24 18:29:34 +01006724 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006725 hsw_compute_ips_config(crtc, pipe_config);
6726
Daniel Vetter877d48d2013-04-19 11:24:43 +02006727 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006728 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006729
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006730 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006731}
6732
Ville Syrjälä1652d192015-03-31 14:12:01 +03006733static int skylake_get_display_clock_speed(struct drm_device *dev)
6734{
6735 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006736 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006737
Ville Syrjäläea617912016-05-13 23:41:24 +03006738 skl_dpll0_update(dev_priv);
6739
Ville Syrjälä63911d72016-05-13 23:41:32 +03006740 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006741 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006742
Ville Syrjäläea617912016-05-13 23:41:24 +03006743 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006744
Ville Syrjälä63911d72016-05-13 23:41:32 +03006745 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006746 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6747 case CDCLK_FREQ_450_432:
6748 return 432000;
6749 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006750 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006751 case CDCLK_FREQ_540:
6752 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006753 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006754 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006755 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006756 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006757 }
6758 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006759 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6760 case CDCLK_FREQ_450_432:
6761 return 450000;
6762 case CDCLK_FREQ_337_308:
6763 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006764 case CDCLK_FREQ_540:
6765 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006766 case CDCLK_FREQ_675_617:
6767 return 675000;
6768 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006769 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006770 }
6771 }
6772
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006773 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006774}
6775
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006776static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6777{
6778 u32 val;
6779
6780 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006781 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006782
6783 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006784 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006785 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006786
Imre Deak1c3f7702016-05-24 15:38:32 +03006787 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6788 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006789
6790 val = I915_READ(BXT_DE_PLL_CTL);
6791 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6792 dev_priv->cdclk_pll.ref;
6793}
6794
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006795static int broxton_get_display_clock_speed(struct drm_device *dev)
6796{
6797 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006798 u32 divider;
6799 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006800
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006801 bxt_de_pll_update(dev_priv);
6802
Ville Syrjäläf5986242016-05-13 23:41:37 +03006803 vco = dev_priv->cdclk_pll.vco;
6804 if (vco == 0)
6805 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006806
Ville Syrjäläf5986242016-05-13 23:41:37 +03006807 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006808
Ville Syrjäläf5986242016-05-13 23:41:37 +03006809 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006810 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006811 div = 2;
6812 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006813 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006814 div = 3;
6815 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006816 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006817 div = 4;
6818 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006819 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006820 div = 8;
6821 break;
6822 default:
6823 MISSING_CASE(divider);
6824 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006825 }
6826
Ville Syrjäläf5986242016-05-13 23:41:37 +03006827 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006828}
6829
Ville Syrjälä1652d192015-03-31 14:12:01 +03006830static int broadwell_get_display_clock_speed(struct drm_device *dev)
6831{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006832 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006833 uint32_t lcpll = I915_READ(LCPLL_CTL);
6834 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6835
6836 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6837 return 800000;
6838 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6839 return 450000;
6840 else if (freq == LCPLL_CLK_FREQ_450)
6841 return 450000;
6842 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6843 return 540000;
6844 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6845 return 337500;
6846 else
6847 return 675000;
6848}
6849
6850static int haswell_get_display_clock_speed(struct drm_device *dev)
6851{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006852 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006853 uint32_t lcpll = I915_READ(LCPLL_CTL);
6854 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6855
6856 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6857 return 800000;
6858 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6859 return 450000;
6860 else if (freq == LCPLL_CLK_FREQ_450)
6861 return 450000;
6862 else if (IS_HSW_ULT(dev))
6863 return 337500;
6864 else
6865 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006866}
6867
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006868static int valleyview_get_display_clock_speed(struct drm_device *dev)
6869{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006870 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6871 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006872}
6873
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006874static int ilk_get_display_clock_speed(struct drm_device *dev)
6875{
6876 return 450000;
6877}
6878
Jesse Barnese70236a2009-09-21 10:42:27 -07006879static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006880{
Jesse Barnese70236a2009-09-21 10:42:27 -07006881 return 400000;
6882}
Jesse Barnes79e53942008-11-07 14:24:08 -08006883
Jesse Barnese70236a2009-09-21 10:42:27 -07006884static int i915_get_display_clock_speed(struct drm_device *dev)
6885{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006886 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006887}
Jesse Barnes79e53942008-11-07 14:24:08 -08006888
Jesse Barnese70236a2009-09-21 10:42:27 -07006889static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6890{
6891 return 200000;
6892}
Jesse Barnes79e53942008-11-07 14:24:08 -08006893
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006894static int pnv_get_display_clock_speed(struct drm_device *dev)
6895{
6896 u16 gcfgc = 0;
6897
6898 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6899
6900 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6901 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006902 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006903 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006904 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006905 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006906 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006907 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6908 return 200000;
6909 default:
6910 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6911 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006912 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006913 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006914 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006915 }
6916}
6917
Jesse Barnese70236a2009-09-21 10:42:27 -07006918static int i915gm_get_display_clock_speed(struct drm_device *dev)
6919{
6920 u16 gcfgc = 0;
6921
6922 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6923
6924 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006925 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006926 else {
6927 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6928 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006929 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006930 default:
6931 case GC_DISPLAY_CLOCK_190_200_MHZ:
6932 return 190000;
6933 }
6934 }
6935}
Jesse Barnes79e53942008-11-07 14:24:08 -08006936
Jesse Barnese70236a2009-09-21 10:42:27 -07006937static int i865_get_display_clock_speed(struct drm_device *dev)
6938{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006939 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006940}
6941
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006942static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006943{
6944 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006945
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006946 /*
6947 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6948 * encoding is different :(
6949 * FIXME is this the right way to detect 852GM/852GMV?
6950 */
6951 if (dev->pdev->revision == 0x1)
6952 return 133333;
6953
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006954 pci_bus_read_config_word(dev->pdev->bus,
6955 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6956
Jesse Barnese70236a2009-09-21 10:42:27 -07006957 /* Assume that the hardware is in the high speed state. This
6958 * should be the default.
6959 */
6960 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6961 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006962 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006963 case GC_CLOCK_100_200:
6964 return 200000;
6965 case GC_CLOCK_166_250:
6966 return 250000;
6967 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006968 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006969 case GC_CLOCK_133_266:
6970 case GC_CLOCK_133_266_2:
6971 case GC_CLOCK_166_266:
6972 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006973 }
6974
6975 /* Shouldn't happen */
6976 return 0;
6977}
6978
6979static int i830_get_display_clock_speed(struct drm_device *dev)
6980{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006981 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006982}
6983
Ville Syrjälä34edce22015-05-22 11:22:33 +03006984static unsigned int intel_hpll_vco(struct drm_device *dev)
6985{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006986 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03006987 static const unsigned int blb_vco[8] = {
6988 [0] = 3200000,
6989 [1] = 4000000,
6990 [2] = 5333333,
6991 [3] = 4800000,
6992 [4] = 6400000,
6993 };
6994 static const unsigned int pnv_vco[8] = {
6995 [0] = 3200000,
6996 [1] = 4000000,
6997 [2] = 5333333,
6998 [3] = 4800000,
6999 [4] = 2666667,
7000 };
7001 static const unsigned int cl_vco[8] = {
7002 [0] = 3200000,
7003 [1] = 4000000,
7004 [2] = 5333333,
7005 [3] = 6400000,
7006 [4] = 3333333,
7007 [5] = 3566667,
7008 [6] = 4266667,
7009 };
7010 static const unsigned int elk_vco[8] = {
7011 [0] = 3200000,
7012 [1] = 4000000,
7013 [2] = 5333333,
7014 [3] = 4800000,
7015 };
7016 static const unsigned int ctg_vco[8] = {
7017 [0] = 3200000,
7018 [1] = 4000000,
7019 [2] = 5333333,
7020 [3] = 6400000,
7021 [4] = 2666667,
7022 [5] = 4266667,
7023 };
7024 const unsigned int *vco_table;
7025 unsigned int vco;
7026 uint8_t tmp = 0;
7027
7028 /* FIXME other chipsets? */
7029 if (IS_GM45(dev))
7030 vco_table = ctg_vco;
7031 else if (IS_G4X(dev))
7032 vco_table = elk_vco;
7033 else if (IS_CRESTLINE(dev))
7034 vco_table = cl_vco;
7035 else if (IS_PINEVIEW(dev))
7036 vco_table = pnv_vco;
7037 else if (IS_G33(dev))
7038 vco_table = blb_vco;
7039 else
7040 return 0;
7041
7042 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7043
7044 vco = vco_table[tmp & 0x7];
7045 if (vco == 0)
7046 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7047 else
7048 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7049
7050 return vco;
7051}
7052
7053static int gm45_get_display_clock_speed(struct drm_device *dev)
7054{
7055 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7056 uint16_t tmp = 0;
7057
7058 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7059
7060 cdclk_sel = (tmp >> 12) & 0x1;
7061
7062 switch (vco) {
7063 case 2666667:
7064 case 4000000:
7065 case 5333333:
7066 return cdclk_sel ? 333333 : 222222;
7067 case 3200000:
7068 return cdclk_sel ? 320000 : 228571;
7069 default:
7070 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7071 return 222222;
7072 }
7073}
7074
7075static int i965gm_get_display_clock_speed(struct drm_device *dev)
7076{
7077 static const uint8_t div_3200[] = { 16, 10, 8 };
7078 static const uint8_t div_4000[] = { 20, 12, 10 };
7079 static const uint8_t div_5333[] = { 24, 16, 14 };
7080 const uint8_t *div_table;
7081 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7082 uint16_t tmp = 0;
7083
7084 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7085
7086 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7087
7088 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7089 goto fail;
7090
7091 switch (vco) {
7092 case 3200000:
7093 div_table = div_3200;
7094 break;
7095 case 4000000:
7096 div_table = div_4000;
7097 break;
7098 case 5333333:
7099 div_table = div_5333;
7100 break;
7101 default:
7102 goto fail;
7103 }
7104
7105 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7106
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007107fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007108 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7109 return 200000;
7110}
7111
7112static int g33_get_display_clock_speed(struct drm_device *dev)
7113{
7114 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7115 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7116 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7117 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7118 const uint8_t *div_table;
7119 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7120 uint16_t tmp = 0;
7121
7122 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7123
7124 cdclk_sel = (tmp >> 4) & 0x7;
7125
7126 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7127 goto fail;
7128
7129 switch (vco) {
7130 case 3200000:
7131 div_table = div_3200;
7132 break;
7133 case 4000000:
7134 div_table = div_4000;
7135 break;
7136 case 4800000:
7137 div_table = div_4800;
7138 break;
7139 case 5333333:
7140 div_table = div_5333;
7141 break;
7142 default:
7143 goto fail;
7144 }
7145
7146 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7147
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007148fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007149 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7150 return 190476;
7151}
7152
Zhenyu Wang2c072452009-06-05 15:38:42 +08007153static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007154intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007155{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007156 while (*num > DATA_LINK_M_N_MASK ||
7157 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007158 *num >>= 1;
7159 *den >>= 1;
7160 }
7161}
7162
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007163static void compute_m_n(unsigned int m, unsigned int n,
7164 uint32_t *ret_m, uint32_t *ret_n)
7165{
7166 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7167 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7168 intel_reduce_m_n_ratio(ret_m, ret_n);
7169}
7170
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007171void
7172intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7173 int pixel_clock, int link_clock,
7174 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007175{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007176 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007177
7178 compute_m_n(bits_per_pixel * pixel_clock,
7179 link_clock * nlanes * 8,
7180 &m_n->gmch_m, &m_n->gmch_n);
7181
7182 compute_m_n(pixel_clock, link_clock,
7183 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007184}
7185
Chris Wilsona7615032011-01-12 17:04:08 +00007186static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7187{
Jani Nikulad330a952014-01-21 11:24:25 +02007188 if (i915.panel_use_ssc >= 0)
7189 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007190 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007191 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007192}
7193
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007194static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007195{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007196 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007197}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007198
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007199static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7200{
7201 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007202}
7203
Daniel Vetterf47709a2013-03-28 10:42:02 +01007204static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007205 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007206 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007207{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007208 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007209 u32 fp, fp2 = 0;
7210
7211 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007213 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007214 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007215 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007216 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007217 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007218 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007219 }
7220
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007221 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007222
Daniel Vetterf47709a2013-03-28 10:42:02 +01007223 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007224 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007225 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007226 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007227 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007228 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007229 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007230 }
7231}
7232
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007233static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7234 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007235{
7236 u32 reg_val;
7237
7238 /*
7239 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7240 * and set it to a reasonable value instead.
7241 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243 reg_val &= 0xffffff00;
7244 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007246
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007247 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007248 reg_val &= 0x8cffffff;
7249 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007255
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007257 reg_val &= 0x00ffffff;
7258 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007259 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007260}
7261
Daniel Vetterb5518422013-05-03 11:49:48 +02007262static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7263 struct intel_link_m_n *m_n)
7264{
7265 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007266 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007267 int pipe = crtc->pipe;
7268
Daniel Vettere3b95f12013-05-03 11:49:49 +02007269 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7270 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7271 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7272 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007273}
7274
7275static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007276 struct intel_link_m_n *m_n,
7277 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007278{
7279 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007280 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007281 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007282 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007283
7284 if (INTEL_INFO(dev)->gen >= 5) {
7285 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7286 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7287 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7288 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007289 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7290 * for gen < 8) and if DRRS is supported (to make sure the
7291 * registers are not unnecessarily accessed).
7292 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307293 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007294 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007295 I915_WRITE(PIPE_DATA_M2(transcoder),
7296 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7297 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7298 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7299 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7300 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007301 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007302 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7303 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7304 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7305 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007306 }
7307}
7308
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307309void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007310{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307311 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7312
7313 if (m_n == M1_N1) {
7314 dp_m_n = &crtc->config->dp_m_n;
7315 dp_m2_n2 = &crtc->config->dp_m2_n2;
7316 } else if (m_n == M2_N2) {
7317
7318 /*
7319 * M2_N2 registers are not supported. Hence m2_n2 divider value
7320 * needs to be programmed into M1_N1.
7321 */
7322 dp_m_n = &crtc->config->dp_m2_n2;
7323 } else {
7324 DRM_ERROR("Unsupported divider value\n");
7325 return;
7326 }
7327
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007328 if (crtc->config->has_pch_encoder)
7329 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007330 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307331 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007332}
7333
Daniel Vetter251ac862015-06-18 10:30:24 +02007334static void vlv_compute_dpll(struct intel_crtc *crtc,
7335 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007336{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007337 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007338 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007339 if (crtc->pipe != PIPE_A)
7340 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007341
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007342 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007343 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007344 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7345 DPLL_EXT_BUFFER_ENABLE_VLV;
7346
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007347 pipe_config->dpll_hw_state.dpll_md =
7348 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7349}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007350
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007351static void chv_compute_dpll(struct intel_crtc *crtc,
7352 struct intel_crtc_state *pipe_config)
7353{
7354 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007355 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007356 if (crtc->pipe != PIPE_A)
7357 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7358
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007359 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007360 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007361 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7362
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007363 pipe_config->dpll_hw_state.dpll_md =
7364 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007365}
7366
Ville Syrjäläd288f652014-10-28 13:20:22 +02007367static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007368 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007369{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007370 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007371 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007372 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007373 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007374 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007375 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007376
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007377 /* Enable Refclk */
7378 I915_WRITE(DPLL(pipe),
7379 pipe_config->dpll_hw_state.dpll &
7380 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7381
7382 /* No need to actually set up the DPLL with DSI */
7383 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7384 return;
7385
Ville Syrjäläa5805162015-05-26 20:42:30 +03007386 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007387
Ville Syrjäläd288f652014-10-28 13:20:22 +02007388 bestn = pipe_config->dpll.n;
7389 bestm1 = pipe_config->dpll.m1;
7390 bestm2 = pipe_config->dpll.m2;
7391 bestp1 = pipe_config->dpll.p1;
7392 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007393
Jesse Barnes89b667f2013-04-18 14:51:36 -07007394 /* See eDP HDMI DPIO driver vbios notes doc */
7395
7396 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007397 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007398 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007399
7400 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007401 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007402
7403 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007404 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007405 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007407
7408 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007409 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007410
7411 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007412 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7413 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7414 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007415 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007416
7417 /*
7418 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7419 * but we don't support that).
7420 * Note: don't use the DAC post divider as it seems unstable.
7421 */
7422 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007423 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007424
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007425 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007426 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007427
Jesse Barnes89b667f2013-04-18 14:51:36 -07007428 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007429 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007430 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7431 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007432 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007433 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007434 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007435 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007436 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007437
Ville Syrjälä37a56502016-06-22 21:57:04 +03007438 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007439 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007440 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007441 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007442 0x0df40000);
7443 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007444 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007445 0x0df70000);
7446 } else { /* HDMI or VGA */
7447 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007448 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007449 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007450 0x0df70000);
7451 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007452 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007453 0x0df40000);
7454 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007455
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007456 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007457 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007458 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007459 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007460 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007461
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007462 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007463 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007464}
7465
Ville Syrjäläd288f652014-10-28 13:20:22 +02007466static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007467 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007468{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007469 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007470 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007471 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007472 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307473 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007474 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307475 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307476 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007477
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007478 /* Enable Refclk and SSC */
7479 I915_WRITE(DPLL(pipe),
7480 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7481
7482 /* No need to actually set up the DPLL with DSI */
7483 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7484 return;
7485
Ville Syrjäläd288f652014-10-28 13:20:22 +02007486 bestn = pipe_config->dpll.n;
7487 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7488 bestm1 = pipe_config->dpll.m1;
7489 bestm2 = pipe_config->dpll.m2 >> 22;
7490 bestp1 = pipe_config->dpll.p1;
7491 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307492 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307493 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307494 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007495
Ville Syrjäläa5805162015-05-26 20:42:30 +03007496 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007497
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007498 /* p1 and p2 divider */
7499 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7500 5 << DPIO_CHV_S1_DIV_SHIFT |
7501 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7502 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7503 1 << DPIO_CHV_K_DIV_SHIFT);
7504
7505 /* Feedback post-divider - m2 */
7506 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7507
7508 /* Feedback refclk divider - n and m1 */
7509 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7510 DPIO_CHV_M1_DIV_BY_2 |
7511 1 << DPIO_CHV_N_DIV_SHIFT);
7512
7513 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007514 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007515
7516 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307517 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7518 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7519 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7520 if (bestm2_frac)
7521 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7522 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007523
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307524 /* Program digital lock detect threshold */
7525 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7526 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7527 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7528 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7529 if (!bestm2_frac)
7530 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7531 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7532
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007533 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307534 if (vco == 5400000) {
7535 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7536 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7537 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7538 tribuf_calcntr = 0x9;
7539 } else if (vco <= 6200000) {
7540 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7541 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7542 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7543 tribuf_calcntr = 0x9;
7544 } else if (vco <= 6480000) {
7545 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7546 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7547 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7548 tribuf_calcntr = 0x8;
7549 } else {
7550 /* Not supported. Apply the same limits as in the max case */
7551 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7552 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7553 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7554 tribuf_calcntr = 0;
7555 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007556 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7557
Ville Syrjälä968040b2015-03-11 22:52:08 +02007558 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307559 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7560 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7561 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7562
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007563 /* AFC Recal */
7564 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7565 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7566 DPIO_AFC_RECAL);
7567
Ville Syrjäläa5805162015-05-26 20:42:30 +03007568 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007569}
7570
Ville Syrjäläd288f652014-10-28 13:20:22 +02007571/**
7572 * vlv_force_pll_on - forcibly enable just the PLL
7573 * @dev_priv: i915 private structure
7574 * @pipe: pipe PLL to enable
7575 * @dpll: PLL configuration
7576 *
7577 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7578 * in cases where we need the PLL enabled even when @pipe is not going to
7579 * be enabled.
7580 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007581int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7582 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007583{
7584 struct intel_crtc *crtc =
7585 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007586 struct intel_crtc_state *pipe_config;
7587
7588 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7589 if (!pipe_config)
7590 return -ENOMEM;
7591
7592 pipe_config->base.crtc = &crtc->base;
7593 pipe_config->pixel_multiplier = 1;
7594 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007595
7596 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007597 chv_compute_dpll(crtc, pipe_config);
7598 chv_prepare_pll(crtc, pipe_config);
7599 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007600 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007601 vlv_compute_dpll(crtc, pipe_config);
7602 vlv_prepare_pll(crtc, pipe_config);
7603 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007604 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007605
7606 kfree(pipe_config);
7607
7608 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007609}
7610
7611/**
7612 * vlv_force_pll_off - forcibly disable just the PLL
7613 * @dev_priv: i915 private structure
7614 * @pipe: pipe PLL to disable
7615 *
7616 * Disable the PLL for @pipe. To be used in cases where we need
7617 * the PLL enabled even when @pipe is not going to be enabled.
7618 */
7619void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7620{
7621 if (IS_CHERRYVIEW(dev))
7622 chv_disable_pll(to_i915(dev), pipe);
7623 else
7624 vlv_disable_pll(to_i915(dev), pipe);
7625}
7626
Daniel Vetter251ac862015-06-18 10:30:24 +02007627static void i9xx_compute_dpll(struct intel_crtc *crtc,
7628 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007629 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007630{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007631 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007632 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007633 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007634 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007635
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007636 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307637
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007638 dpll = DPLL_VGA_MODE_DIS;
7639
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007640 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007641 dpll |= DPLLB_MODE_LVDS;
7642 else
7643 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007644
Daniel Vetteref1b4602013-06-01 17:17:04 +02007645 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007646 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007647 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007648 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007649
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007650 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7651 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007652 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007653
Ville Syrjälä37a56502016-06-22 21:57:04 +03007654 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007655 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007656
7657 /* compute bitmask from p1 value */
7658 if (IS_PINEVIEW(dev))
7659 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7660 else {
7661 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7662 if (IS_G4X(dev) && reduced_clock)
7663 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7664 }
7665 switch (clock->p2) {
7666 case 5:
7667 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7668 break;
7669 case 7:
7670 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7671 break;
7672 case 10:
7673 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7674 break;
7675 case 14:
7676 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7677 break;
7678 }
7679 if (INTEL_INFO(dev)->gen >= 4)
7680 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7681
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007682 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007683 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007684 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007685 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007686 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7687 else
7688 dpll |= PLL_REF_INPUT_DREFCLK;
7689
7690 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007691 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007692
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007693 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007694 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007695 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007696 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007697 }
7698}
7699
Daniel Vetter251ac862015-06-18 10:30:24 +02007700static void i8xx_compute_dpll(struct intel_crtc *crtc,
7701 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007702 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007703{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007704 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007705 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007706 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007707 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007708
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007709 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307710
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007711 dpll = DPLL_VGA_MODE_DIS;
7712
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007713 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007714 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7715 } else {
7716 if (clock->p1 == 2)
7717 dpll |= PLL_P1_DIVIDE_BY_TWO;
7718 else
7719 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7720 if (clock->p2 == 4)
7721 dpll |= PLL_P2_DIVIDE_BY_4;
7722 }
7723
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007724 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007725 dpll |= DPLL_DVO_2X_MODE;
7726
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007727 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007728 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007729 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7730 else
7731 dpll |= PLL_REF_INPUT_DREFCLK;
7732
7733 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007734 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007735}
7736
Daniel Vetter8a654f32013-06-01 17:16:22 +02007737static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007738{
7739 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007740 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007741 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007742 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007743 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007744 uint32_t crtc_vtotal, crtc_vblank_end;
7745 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007746
7747 /* We need to be careful not to changed the adjusted mode, for otherwise
7748 * the hw state checker will get angry at the mismatch. */
7749 crtc_vtotal = adjusted_mode->crtc_vtotal;
7750 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007751
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007752 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007753 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007754 crtc_vtotal -= 1;
7755 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007756
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007757 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007758 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7759 else
7760 vsyncshift = adjusted_mode->crtc_hsync_start -
7761 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007762 if (vsyncshift < 0)
7763 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007764 }
7765
7766 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007767 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007768
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007769 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007770 (adjusted_mode->crtc_hdisplay - 1) |
7771 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007772 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007773 (adjusted_mode->crtc_hblank_start - 1) |
7774 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007775 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007776 (adjusted_mode->crtc_hsync_start - 1) |
7777 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7778
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007779 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007780 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007781 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007782 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007783 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007784 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007785 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007786 (adjusted_mode->crtc_vsync_start - 1) |
7787 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7788
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007789 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7790 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7791 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7792 * bits. */
7793 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7794 (pipe == PIPE_B || pipe == PIPE_C))
7795 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7796
Jani Nikulabc58be62016-03-18 17:05:39 +02007797}
7798
7799static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7800{
7801 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007802 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007803 enum pipe pipe = intel_crtc->pipe;
7804
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007805 /* pipesrc controls the size that is scaled from, which should
7806 * always be the user's requested size.
7807 */
7808 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007809 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7810 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007811}
7812
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007813static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007814 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007815{
7816 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007817 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007818 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7819 uint32_t tmp;
7820
7821 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007822 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7823 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007824 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007825 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7826 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007827 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007828 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7829 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007830
7831 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007832 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7833 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007834 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007835 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7836 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007837 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007838 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7839 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007840
7841 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007842 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7843 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7844 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007845 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007846}
7847
7848static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7849 struct intel_crtc_state *pipe_config)
7850{
7851 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007852 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007853 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007854
7855 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007856 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7857 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7858
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007859 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7860 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007861}
7862
Daniel Vetterf6a83282014-02-11 15:28:57 -08007863void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007864 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007865{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007866 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7867 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7868 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7869 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007870
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007871 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7872 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7873 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7874 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007875
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007876 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007877 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007878
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007879 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7880 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007881
7882 mode->hsync = drm_mode_hsync(mode);
7883 mode->vrefresh = drm_mode_vrefresh(mode);
7884 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007885}
7886
Daniel Vetter84b046f2013-02-19 18:48:54 +01007887static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7888{
7889 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007890 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007891 uint32_t pipeconf;
7892
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007893 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007894
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007895 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7896 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7897 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007898
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007899 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007900 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007901
Daniel Vetterff9ce462013-04-24 14:57:17 +02007902 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007903 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007904 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007905 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007906 pipeconf |= PIPECONF_DITHER_EN |
7907 PIPECONF_DITHER_TYPE_SP;
7908
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007909 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007910 case 18:
7911 pipeconf |= PIPECONF_6BPC;
7912 break;
7913 case 24:
7914 pipeconf |= PIPECONF_8BPC;
7915 break;
7916 case 30:
7917 pipeconf |= PIPECONF_10BPC;
7918 break;
7919 default:
7920 /* Case prevented by intel_choose_pipe_bpp_dither. */
7921 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007922 }
7923 }
7924
7925 if (HAS_PIPE_CXSR(dev)) {
7926 if (intel_crtc->lowfreq_avail) {
7927 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7928 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7929 } else {
7930 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007931 }
7932 }
7933
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007934 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007935 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007936 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007937 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7938 else
7939 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7940 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007941 pipeconf |= PIPECONF_PROGRESSIVE;
7942
Wayne Boyer666a4532015-12-09 12:29:35 -08007943 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7944 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007945 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007946
Daniel Vetter84b046f2013-02-19 18:48:54 +01007947 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7948 POSTING_READ(PIPECONF(intel_crtc->pipe));
7949}
7950
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007951static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7952 struct intel_crtc_state *crtc_state)
7953{
7954 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007955 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007956 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007957 int refclk = 48000;
7958
7959 memset(&crtc_state->dpll_hw_state, 0,
7960 sizeof(crtc_state->dpll_hw_state));
7961
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007962 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007963 if (intel_panel_use_ssc(dev_priv)) {
7964 refclk = dev_priv->vbt.lvds_ssc_freq;
7965 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7966 }
7967
7968 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007969 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007970 limit = &intel_limits_i8xx_dvo;
7971 } else {
7972 limit = &intel_limits_i8xx_dac;
7973 }
7974
7975 if (!crtc_state->clock_set &&
7976 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7977 refclk, NULL, &crtc_state->dpll)) {
7978 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7979 return -EINVAL;
7980 }
7981
7982 i8xx_compute_dpll(crtc, crtc_state, NULL);
7983
7984 return 0;
7985}
7986
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007987static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7988 struct intel_crtc_state *crtc_state)
7989{
7990 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007991 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007992 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007993 int refclk = 96000;
7994
7995 memset(&crtc_state->dpll_hw_state, 0,
7996 sizeof(crtc_state->dpll_hw_state));
7997
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007998 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007999 if (intel_panel_use_ssc(dev_priv)) {
8000 refclk = dev_priv->vbt.lvds_ssc_freq;
8001 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8002 }
8003
8004 if (intel_is_dual_link_lvds(dev))
8005 limit = &intel_limits_g4x_dual_channel_lvds;
8006 else
8007 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008008 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8009 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008010 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008011 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008012 limit = &intel_limits_g4x_sdvo;
8013 } else {
8014 /* The option is for other outputs */
8015 limit = &intel_limits_i9xx_sdvo;
8016 }
8017
8018 if (!crtc_state->clock_set &&
8019 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8020 refclk, NULL, &crtc_state->dpll)) {
8021 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8022 return -EINVAL;
8023 }
8024
8025 i9xx_compute_dpll(crtc, crtc_state, NULL);
8026
8027 return 0;
8028}
8029
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008030static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8031 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008032{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008033 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008034 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008035 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008036 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008037
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008038 memset(&crtc_state->dpll_hw_state, 0,
8039 sizeof(crtc_state->dpll_hw_state));
8040
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008041 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008042 if (intel_panel_use_ssc(dev_priv)) {
8043 refclk = dev_priv->vbt.lvds_ssc_freq;
8044 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8045 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008046
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008047 limit = &intel_limits_pineview_lvds;
8048 } else {
8049 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008050 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008051
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008052 if (!crtc_state->clock_set &&
8053 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8054 refclk, NULL, &crtc_state->dpll)) {
8055 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8056 return -EINVAL;
8057 }
8058
8059 i9xx_compute_dpll(crtc, crtc_state, NULL);
8060
8061 return 0;
8062}
8063
8064static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8065 struct intel_crtc_state *crtc_state)
8066{
8067 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008068 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008069 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008070 int refclk = 96000;
8071
8072 memset(&crtc_state->dpll_hw_state, 0,
8073 sizeof(crtc_state->dpll_hw_state));
8074
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008075 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008076 if (intel_panel_use_ssc(dev_priv)) {
8077 refclk = dev_priv->vbt.lvds_ssc_freq;
8078 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008079 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008080
8081 limit = &intel_limits_i9xx_lvds;
8082 } else {
8083 limit = &intel_limits_i9xx_sdvo;
8084 }
8085
8086 if (!crtc_state->clock_set &&
8087 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8088 refclk, NULL, &crtc_state->dpll)) {
8089 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8090 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008091 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008092
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008093 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008094
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008095 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008096}
8097
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008098static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8099 struct intel_crtc_state *crtc_state)
8100{
8101 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008102 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008103
8104 memset(&crtc_state->dpll_hw_state, 0,
8105 sizeof(crtc_state->dpll_hw_state));
8106
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008107 if (!crtc_state->clock_set &&
8108 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8109 refclk, NULL, &crtc_state->dpll)) {
8110 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8111 return -EINVAL;
8112 }
8113
8114 chv_compute_dpll(crtc, crtc_state);
8115
8116 return 0;
8117}
8118
8119static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8120 struct intel_crtc_state *crtc_state)
8121{
8122 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008123 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008124
8125 memset(&crtc_state->dpll_hw_state, 0,
8126 sizeof(crtc_state->dpll_hw_state));
8127
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008128 if (!crtc_state->clock_set &&
8129 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8130 refclk, NULL, &crtc_state->dpll)) {
8131 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8132 return -EINVAL;
8133 }
8134
8135 vlv_compute_dpll(crtc, crtc_state);
8136
8137 return 0;
8138}
8139
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008140static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008141 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008142{
8143 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008144 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008145 uint32_t tmp;
8146
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008147 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8148 return;
8149
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008150 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008151 if (!(tmp & PFIT_ENABLE))
8152 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008153
Daniel Vetter06922822013-07-11 13:35:40 +02008154 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008155 if (INTEL_INFO(dev)->gen < 4) {
8156 if (crtc->pipe != PIPE_B)
8157 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008158 } else {
8159 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8160 return;
8161 }
8162
Daniel Vetter06922822013-07-11 13:35:40 +02008163 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008164 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008165}
8166
Jesse Barnesacbec812013-09-20 11:29:32 -07008167static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008168 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008169{
8170 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008171 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008172 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008173 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008174 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008175 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008176
Ville Syrjäläb5219732016-03-15 16:40:01 +02008177 /* In case of DSI, DPLL will not be used */
8178 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308179 return;
8180
Ville Syrjäläa5805162015-05-26 20:42:30 +03008181 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008182 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008183 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008184
8185 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8186 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8187 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8188 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8189 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8190
Imre Deakdccbea32015-06-22 23:35:51 +03008191 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008192}
8193
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008194static void
8195i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8196 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008197{
8198 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008199 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008200 u32 val, base, offset;
8201 int pipe = crtc->pipe, plane = crtc->plane;
8202 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008203 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008204 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008205 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008206
Damien Lespiau42a7b082015-02-05 19:35:13 +00008207 val = I915_READ(DSPCNTR(plane));
8208 if (!(val & DISPLAY_PLANE_ENABLE))
8209 return;
8210
Damien Lespiaud9806c92015-01-21 14:07:19 +00008211 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008212 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008213 DRM_DEBUG_KMS("failed to alloc fb\n");
8214 return;
8215 }
8216
Damien Lespiau1b842c82015-01-21 13:50:54 +00008217 fb = &intel_fb->base;
8218
Daniel Vetter18c52472015-02-10 17:16:09 +00008219 if (INTEL_INFO(dev)->gen >= 4) {
8220 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008221 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008222 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8223 }
8224 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008225
8226 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008227 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008228 fb->pixel_format = fourcc;
8229 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008230
8231 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008232 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008233 offset = I915_READ(DSPTILEOFF(plane));
8234 else
8235 offset = I915_READ(DSPLINOFF(plane));
8236 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8237 } else {
8238 base = I915_READ(DSPADDR(plane));
8239 }
8240 plane_config->base = base;
8241
8242 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008243 fb->width = ((val >> 16) & 0xfff) + 1;
8244 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008245
8246 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008247 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008248
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008249 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008250 fb->pixel_format,
8251 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008252
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008253 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008254
Damien Lespiau2844a922015-01-20 12:51:48 +00008255 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8256 pipe_name(pipe), plane, fb->width, fb->height,
8257 fb->bits_per_pixel, base, fb->pitches[0],
8258 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008259
Damien Lespiau2d140302015-02-05 17:22:18 +00008260 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008261}
8262
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008263static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008264 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008265{
8266 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008267 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008268 int pipe = pipe_config->cpu_transcoder;
8269 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008270 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008271 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008272 int refclk = 100000;
8273
Ville Syrjäläb5219732016-03-15 16:40:01 +02008274 /* In case of DSI, DPLL will not be used */
8275 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8276 return;
8277
Ville Syrjäläa5805162015-05-26 20:42:30 +03008278 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008279 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8280 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8281 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8282 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008283 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008284 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008285
8286 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008287 clock.m2 = (pll_dw0 & 0xff) << 22;
8288 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8289 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008290 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8291 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8292 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8293
Imre Deakdccbea32015-06-22 23:35:51 +03008294 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008295}
8296
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008297static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008298 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008299{
8300 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008301 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008302 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008303 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008304 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008305
Imre Deak17290502016-02-12 18:55:11 +02008306 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8307 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008308 return false;
8309
Daniel Vettere143a212013-07-04 12:01:15 +02008310 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008311 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008312
Imre Deak17290502016-02-12 18:55:11 +02008313 ret = false;
8314
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008315 tmp = I915_READ(PIPECONF(crtc->pipe));
8316 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008317 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008318
Wayne Boyer666a4532015-12-09 12:29:35 -08008319 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008320 switch (tmp & PIPECONF_BPC_MASK) {
8321 case PIPECONF_6BPC:
8322 pipe_config->pipe_bpp = 18;
8323 break;
8324 case PIPECONF_8BPC:
8325 pipe_config->pipe_bpp = 24;
8326 break;
8327 case PIPECONF_10BPC:
8328 pipe_config->pipe_bpp = 30;
8329 break;
8330 default:
8331 break;
8332 }
8333 }
8334
Wayne Boyer666a4532015-12-09 12:29:35 -08008335 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8336 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008337 pipe_config->limited_color_range = true;
8338
Ville Syrjälä282740f2013-09-04 18:30:03 +03008339 if (INTEL_INFO(dev)->gen < 4)
8340 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8341
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008342 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008343 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008344
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008345 i9xx_get_pfit_config(crtc, pipe_config);
8346
Daniel Vetter6c49f242013-06-06 12:45:25 +02008347 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008348 /* No way to read it out on pipes B and C */
8349 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8350 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8351 else
8352 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008353 pipe_config->pixel_multiplier =
8354 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8355 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008356 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008357 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8358 tmp = I915_READ(DPLL(crtc->pipe));
8359 pipe_config->pixel_multiplier =
8360 ((tmp & SDVO_MULTIPLIER_MASK)
8361 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8362 } else {
8363 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8364 * port and will be fixed up in the encoder->get_config
8365 * function. */
8366 pipe_config->pixel_multiplier = 1;
8367 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008368 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008369 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008370 /*
8371 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8372 * on 830. Filter it out here so that we don't
8373 * report errors due to that.
8374 */
8375 if (IS_I830(dev))
8376 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8377
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008378 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8379 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008380 } else {
8381 /* Mask out read-only status bits. */
8382 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8383 DPLL_PORTC_READY_MASK |
8384 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008385 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008386
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008387 if (IS_CHERRYVIEW(dev))
8388 chv_crtc_clock_get(crtc, pipe_config);
8389 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008390 vlv_crtc_clock_get(crtc, pipe_config);
8391 else
8392 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008393
Ville Syrjälä0f646142015-08-26 19:39:18 +03008394 /*
8395 * Normally the dotclock is filled in by the encoder .get_config()
8396 * but in case the pipe is enabled w/o any ports we need a sane
8397 * default.
8398 */
8399 pipe_config->base.adjusted_mode.crtc_clock =
8400 pipe_config->port_clock / pipe_config->pixel_multiplier;
8401
Imre Deak17290502016-02-12 18:55:11 +02008402 ret = true;
8403
8404out:
8405 intel_display_power_put(dev_priv, power_domain);
8406
8407 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008408}
8409
Paulo Zanonidde86e22012-12-01 12:04:25 -02008410static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008411{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008412 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008413 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008414 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008415 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008416 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008417 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008418 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008419 bool has_ck505 = false;
8420 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008421 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008422
8423 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008424 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008425 switch (encoder->type) {
8426 case INTEL_OUTPUT_LVDS:
8427 has_panel = true;
8428 has_lvds = true;
8429 break;
8430 case INTEL_OUTPUT_EDP:
8431 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008432 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008433 has_cpu_edp = true;
8434 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008435 default:
8436 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008437 }
8438 }
8439
Keith Packard99eb6a02011-09-26 14:29:12 -07008440 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008441 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008442 can_ssc = has_ck505;
8443 } else {
8444 has_ck505 = false;
8445 can_ssc = true;
8446 }
8447
Lyude1c1a24d2016-06-14 11:04:09 -04008448 /* Check if any DPLLs are using the SSC source */
8449 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8450 u32 temp = I915_READ(PCH_DPLL(i));
8451
8452 if (!(temp & DPLL_VCO_ENABLE))
8453 continue;
8454
8455 if ((temp & PLL_REF_INPUT_MASK) ==
8456 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8457 using_ssc_source = true;
8458 break;
8459 }
8460 }
8461
8462 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8463 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008464
8465 /* Ironlake: try to setup display ref clock before DPLL
8466 * enabling. This is only under driver's control after
8467 * PCH B stepping, previous chipset stepping should be
8468 * ignoring this setting.
8469 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008470 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008471
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008472 /* As we must carefully and slowly disable/enable each source in turn,
8473 * compute the final state we want first and check if we need to
8474 * make any changes at all.
8475 */
8476 final = val;
8477 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008478 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008479 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008480 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008481 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8482
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008483 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008484 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008485 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008486
Keith Packard199e5d72011-09-22 12:01:57 -07008487 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008488 final |= DREF_SSC_SOURCE_ENABLE;
8489
8490 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8491 final |= DREF_SSC1_ENABLE;
8492
8493 if (has_cpu_edp) {
8494 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8495 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8496 else
8497 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8498 } else
8499 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008500 } else if (using_ssc_source) {
8501 final |= DREF_SSC_SOURCE_ENABLE;
8502 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008503 }
8504
8505 if (final == val)
8506 return;
8507
8508 /* Always enable nonspread source */
8509 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8510
8511 if (has_ck505)
8512 val |= DREF_NONSPREAD_CK505_ENABLE;
8513 else
8514 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8515
8516 if (has_panel) {
8517 val &= ~DREF_SSC_SOURCE_MASK;
8518 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008519
Keith Packard199e5d72011-09-22 12:01:57 -07008520 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008521 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008522 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008523 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008524 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008525 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008526
8527 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008528 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008529 POSTING_READ(PCH_DREF_CONTROL);
8530 udelay(200);
8531
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008532 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008533
8534 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008535 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008536 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008537 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008538 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008539 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008540 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008541 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008542 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008543
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008544 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008545 POSTING_READ(PCH_DREF_CONTROL);
8546 udelay(200);
8547 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008548 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008549
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008550 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008551
8552 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008553 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008554
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008555 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008556 POSTING_READ(PCH_DREF_CONTROL);
8557 udelay(200);
8558
Lyude1c1a24d2016-06-14 11:04:09 -04008559 if (!using_ssc_source) {
8560 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008561
Lyude1c1a24d2016-06-14 11:04:09 -04008562 /* Turn off the SSC source */
8563 val &= ~DREF_SSC_SOURCE_MASK;
8564 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008565
Lyude1c1a24d2016-06-14 11:04:09 -04008566 /* Turn off SSC1 */
8567 val &= ~DREF_SSC1_ENABLE;
8568
8569 I915_WRITE(PCH_DREF_CONTROL, val);
8570 POSTING_READ(PCH_DREF_CONTROL);
8571 udelay(200);
8572 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008573 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008574
8575 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008576}
8577
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008578static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008579{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008580 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008581
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008582 tmp = I915_READ(SOUTH_CHICKEN2);
8583 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8584 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008585
Imre Deakcf3598c2016-06-28 13:37:31 +03008586 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8587 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008588 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008589
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008590 tmp = I915_READ(SOUTH_CHICKEN2);
8591 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8592 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008593
Imre Deakcf3598c2016-06-28 13:37:31 +03008594 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8595 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008596 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008597}
8598
8599/* WaMPhyProgramming:hsw */
8600static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8601{
8602 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008603
8604 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8605 tmp &= ~(0xFF << 24);
8606 tmp |= (0x12 << 24);
8607 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8608
Paulo Zanonidde86e22012-12-01 12:04:25 -02008609 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8610 tmp |= (1 << 11);
8611 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8612
8613 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8614 tmp |= (1 << 11);
8615 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8616
Paulo Zanonidde86e22012-12-01 12:04:25 -02008617 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8618 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8619 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8620
8621 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8622 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8623 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8624
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008625 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8626 tmp &= ~(7 << 13);
8627 tmp |= (5 << 13);
8628 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008629
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008630 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8631 tmp &= ~(7 << 13);
8632 tmp |= (5 << 13);
8633 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008634
8635 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8636 tmp &= ~0xFF;
8637 tmp |= 0x1C;
8638 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8639
8640 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8641 tmp &= ~0xFF;
8642 tmp |= 0x1C;
8643 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8644
8645 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8646 tmp &= ~(0xFF << 16);
8647 tmp |= (0x1C << 16);
8648 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8649
8650 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8651 tmp &= ~(0xFF << 16);
8652 tmp |= (0x1C << 16);
8653 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8654
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008655 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8656 tmp |= (1 << 27);
8657 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008658
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008659 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8660 tmp |= (1 << 27);
8661 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008662
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008663 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8664 tmp &= ~(0xF << 28);
8665 tmp |= (4 << 28);
8666 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008667
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008668 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8669 tmp &= ~(0xF << 28);
8670 tmp |= (4 << 28);
8671 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008672}
8673
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008674/* Implements 3 different sequences from BSpec chapter "Display iCLK
8675 * Programming" based on the parameters passed:
8676 * - Sequence to enable CLKOUT_DP
8677 * - Sequence to enable CLKOUT_DP without spread
8678 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8679 */
8680static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8681 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008682{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008683 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008684 uint32_t reg, tmp;
8685
8686 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8687 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008688 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008689 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008690
Ville Syrjäläa5805162015-05-26 20:42:30 +03008691 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008692
8693 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8694 tmp &= ~SBI_SSCCTL_DISABLE;
8695 tmp |= SBI_SSCCTL_PATHALT;
8696 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8697
8698 udelay(24);
8699
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008700 if (with_spread) {
8701 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8702 tmp &= ~SBI_SSCCTL_PATHALT;
8703 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008704
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008705 if (with_fdi) {
8706 lpt_reset_fdi_mphy(dev_priv);
8707 lpt_program_fdi_mphy(dev_priv);
8708 }
8709 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008710
Ville Syrjäläc2699522015-08-27 23:55:59 +03008711 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008712 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8713 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8714 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008715
Ville Syrjäläa5805162015-05-26 20:42:30 +03008716 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008717}
8718
Paulo Zanoni47701c32013-07-23 11:19:25 -03008719/* Sequence to disable CLKOUT_DP */
8720static void lpt_disable_clkout_dp(struct drm_device *dev)
8721{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008722 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008723 uint32_t reg, tmp;
8724
Ville Syrjäläa5805162015-05-26 20:42:30 +03008725 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008726
Ville Syrjäläc2699522015-08-27 23:55:59 +03008727 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008728 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8729 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8730 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8731
8732 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8733 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8734 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8735 tmp |= SBI_SSCCTL_PATHALT;
8736 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8737 udelay(32);
8738 }
8739 tmp |= SBI_SSCCTL_DISABLE;
8740 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8741 }
8742
Ville Syrjäläa5805162015-05-26 20:42:30 +03008743 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008744}
8745
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008746#define BEND_IDX(steps) ((50 + (steps)) / 5)
8747
8748static const uint16_t sscdivintphase[] = {
8749 [BEND_IDX( 50)] = 0x3B23,
8750 [BEND_IDX( 45)] = 0x3B23,
8751 [BEND_IDX( 40)] = 0x3C23,
8752 [BEND_IDX( 35)] = 0x3C23,
8753 [BEND_IDX( 30)] = 0x3D23,
8754 [BEND_IDX( 25)] = 0x3D23,
8755 [BEND_IDX( 20)] = 0x3E23,
8756 [BEND_IDX( 15)] = 0x3E23,
8757 [BEND_IDX( 10)] = 0x3F23,
8758 [BEND_IDX( 5)] = 0x3F23,
8759 [BEND_IDX( 0)] = 0x0025,
8760 [BEND_IDX( -5)] = 0x0025,
8761 [BEND_IDX(-10)] = 0x0125,
8762 [BEND_IDX(-15)] = 0x0125,
8763 [BEND_IDX(-20)] = 0x0225,
8764 [BEND_IDX(-25)] = 0x0225,
8765 [BEND_IDX(-30)] = 0x0325,
8766 [BEND_IDX(-35)] = 0x0325,
8767 [BEND_IDX(-40)] = 0x0425,
8768 [BEND_IDX(-45)] = 0x0425,
8769 [BEND_IDX(-50)] = 0x0525,
8770};
8771
8772/*
8773 * Bend CLKOUT_DP
8774 * steps -50 to 50 inclusive, in steps of 5
8775 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8776 * change in clock period = -(steps / 10) * 5.787 ps
8777 */
8778static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8779{
8780 uint32_t tmp;
8781 int idx = BEND_IDX(steps);
8782
8783 if (WARN_ON(steps % 5 != 0))
8784 return;
8785
8786 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8787 return;
8788
8789 mutex_lock(&dev_priv->sb_lock);
8790
8791 if (steps % 10 != 0)
8792 tmp = 0xAAAAAAAB;
8793 else
8794 tmp = 0x00000000;
8795 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8796
8797 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8798 tmp &= 0xffff0000;
8799 tmp |= sscdivintphase[idx];
8800 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8801
8802 mutex_unlock(&dev_priv->sb_lock);
8803}
8804
8805#undef BEND_IDX
8806
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008807static void lpt_init_pch_refclk(struct drm_device *dev)
8808{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008809 struct intel_encoder *encoder;
8810 bool has_vga = false;
8811
Damien Lespiaub2784e12014-08-05 11:29:37 +01008812 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008813 switch (encoder->type) {
8814 case INTEL_OUTPUT_ANALOG:
8815 has_vga = true;
8816 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008817 default:
8818 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008819 }
8820 }
8821
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008822 if (has_vga) {
8823 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008824 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008825 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008826 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008827 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008828}
8829
Paulo Zanonidde86e22012-12-01 12:04:25 -02008830/*
8831 * Initialize reference clocks when the driver loads
8832 */
8833void intel_init_pch_refclk(struct drm_device *dev)
8834{
8835 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8836 ironlake_init_pch_refclk(dev);
8837 else if (HAS_PCH_LPT(dev))
8838 lpt_init_pch_refclk(dev);
8839}
8840
Daniel Vetter6ff93602013-04-19 11:24:36 +02008841static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008842{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008843 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8845 int pipe = intel_crtc->pipe;
8846 uint32_t val;
8847
Daniel Vetter78114072013-06-13 00:54:57 +02008848 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008850 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008851 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008852 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008853 break;
8854 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008855 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008856 break;
8857 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008858 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008859 break;
8860 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008861 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008862 break;
8863 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008864 /* Case prevented by intel_choose_pipe_bpp_dither. */
8865 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008866 }
8867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008868 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008869 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8870
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008871 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008872 val |= PIPECONF_INTERLACED_ILK;
8873 else
8874 val |= PIPECONF_PROGRESSIVE;
8875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008876 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008877 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008878
Paulo Zanonic8203562012-09-12 10:06:29 -03008879 I915_WRITE(PIPECONF(pipe), val);
8880 POSTING_READ(PIPECONF(pipe));
8881}
8882
Daniel Vetter6ff93602013-04-19 11:24:36 +02008883static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008884{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008885 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008887 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008888 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008889
Jani Nikula391bf042016-03-18 17:05:40 +02008890 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008891 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8892
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008893 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008894 val |= PIPECONF_INTERLACED_ILK;
8895 else
8896 val |= PIPECONF_PROGRESSIVE;
8897
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008898 I915_WRITE(PIPECONF(cpu_transcoder), val);
8899 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008900}
8901
Jani Nikula391bf042016-03-18 17:05:40 +02008902static void haswell_set_pipemisc(struct drm_crtc *crtc)
8903{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008904 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8906
8907 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8908 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008909
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008910 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008911 case 18:
8912 val |= PIPEMISC_DITHER_6_BPC;
8913 break;
8914 case 24:
8915 val |= PIPEMISC_DITHER_8_BPC;
8916 break;
8917 case 30:
8918 val |= PIPEMISC_DITHER_10_BPC;
8919 break;
8920 case 36:
8921 val |= PIPEMISC_DITHER_12_BPC;
8922 break;
8923 default:
8924 /* Case prevented by pipe_config_set_bpp. */
8925 BUG();
8926 }
8927
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008928 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008929 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8930
Jani Nikula391bf042016-03-18 17:05:40 +02008931 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008932 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008933}
8934
Paulo Zanonid4b19312012-11-29 11:29:32 -02008935int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8936{
8937 /*
8938 * Account for spread spectrum to avoid
8939 * oversubscribing the link. Max center spread
8940 * is 2.5%; use 5% for safety's sake.
8941 */
8942 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008943 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008944}
8945
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008946static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008947{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008948 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008949}
8950
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008951static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8952 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008953 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008954{
8955 struct drm_crtc *crtc = &intel_crtc->base;
8956 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008957 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008958 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008959 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008960
Chris Wilsonc1858122010-12-03 21:35:48 +00008961 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008962 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008963 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008964 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008965 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008966 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008967 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008968 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008969 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008970
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008971 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008972
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008973 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8974 fp |= FP_CB_TUNE;
8975
8976 if (reduced_clock) {
8977 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8978
8979 if (reduced_clock->m < factor * reduced_clock->n)
8980 fp2 |= FP_CB_TUNE;
8981 } else {
8982 fp2 = fp;
8983 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008984
Chris Wilson5eddb702010-09-11 13:48:45 +01008985 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008986
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008987 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008988 dpll |= DPLLB_MODE_LVDS;
8989 else
8990 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008991
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008992 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008993 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008994
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008995 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8996 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008997 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008998
Ville Syrjälä37a56502016-06-22 21:57:04 +03008999 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009000 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009001
Eric Anholta07d6782011-03-30 13:01:08 -07009002 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009003 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009004 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009005 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009006
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009007 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009008 case 5:
9009 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9010 break;
9011 case 7:
9012 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9013 break;
9014 case 10:
9015 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9016 break;
9017 case 14:
9018 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9019 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009020 }
9021
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009022 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9023 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009024 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009025 else
9026 dpll |= PLL_REF_INPUT_DREFCLK;
9027
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009028 dpll |= DPLL_VCO_ENABLE;
9029
9030 crtc_state->dpll_hw_state.dpll = dpll;
9031 crtc_state->dpll_hw_state.fp0 = fp;
9032 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009033}
9034
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009035static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9036 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009037{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009038 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009039 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009040 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009041 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009042 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009043 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009044 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009045
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009046 memset(&crtc_state->dpll_hw_state, 0,
9047 sizeof(crtc_state->dpll_hw_state));
9048
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009049 crtc->lowfreq_avail = false;
9050
9051 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9052 if (!crtc_state->has_pch_encoder)
9053 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009054
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009055 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009056 if (intel_panel_use_ssc(dev_priv)) {
9057 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9058 dev_priv->vbt.lvds_ssc_freq);
9059 refclk = dev_priv->vbt.lvds_ssc_freq;
9060 }
9061
9062 if (intel_is_dual_link_lvds(dev)) {
9063 if (refclk == 100000)
9064 limit = &intel_limits_ironlake_dual_lvds_100m;
9065 else
9066 limit = &intel_limits_ironlake_dual_lvds;
9067 } else {
9068 if (refclk == 100000)
9069 limit = &intel_limits_ironlake_single_lvds_100m;
9070 else
9071 limit = &intel_limits_ironlake_single_lvds;
9072 }
9073 } else {
9074 limit = &intel_limits_ironlake_dac;
9075 }
9076
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009077 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009078 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9079 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009080 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9081 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009082 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009083
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009084 ironlake_compute_dpll(crtc, crtc_state,
9085 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009086
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009087 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9088 if (pll == NULL) {
9089 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9090 pipe_name(crtc->pipe));
9091 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009092 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009093
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009094 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009095 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009096 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009097
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009098 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009099}
9100
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009101static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9102 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009103{
9104 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009105 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009106 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009107
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009108 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9109 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9110 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9111 & ~TU_SIZE_MASK;
9112 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9113 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9114 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9115}
9116
9117static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9118 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009119 struct intel_link_m_n *m_n,
9120 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009121{
9122 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009123 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009124 enum pipe pipe = crtc->pipe;
9125
9126 if (INTEL_INFO(dev)->gen >= 5) {
9127 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9128 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9129 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9130 & ~TU_SIZE_MASK;
9131 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9132 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9133 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009134 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9135 * gen < 8) and if DRRS is supported (to make sure the
9136 * registers are not unnecessarily read).
9137 */
9138 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009139 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009140 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9141 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9142 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9143 & ~TU_SIZE_MASK;
9144 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9145 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9146 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9147 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009148 } else {
9149 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9150 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9151 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9152 & ~TU_SIZE_MASK;
9153 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9154 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9155 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9156 }
9157}
9158
9159void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009160 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009161{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009162 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009163 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9164 else
9165 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009166 &pipe_config->dp_m_n,
9167 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009168}
9169
Daniel Vetter72419202013-04-04 13:28:53 +02009170static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009171 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009172{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009173 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009174 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009175}
9176
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009177static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009178 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009179{
9180 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009181 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009182 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9183 uint32_t ps_ctrl = 0;
9184 int id = -1;
9185 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009186
Chandra Kondurua1b22782015-04-07 15:28:45 -07009187 /* find scaler attached to this pipe */
9188 for (i = 0; i < crtc->num_scalers; i++) {
9189 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9190 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9191 id = i;
9192 pipe_config->pch_pfit.enabled = true;
9193 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9194 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9195 break;
9196 }
9197 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009198
Chandra Kondurua1b22782015-04-07 15:28:45 -07009199 scaler_state->scaler_id = id;
9200 if (id >= 0) {
9201 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9202 } else {
9203 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009204 }
9205}
9206
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009207static void
9208skylake_get_initial_plane_config(struct intel_crtc *crtc,
9209 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009210{
9211 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009212 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009213 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009214 int pipe = crtc->pipe;
9215 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009216 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009217 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009218 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009219
Damien Lespiaud9806c92015-01-21 14:07:19 +00009220 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009221 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009222 DRM_DEBUG_KMS("failed to alloc fb\n");
9223 return;
9224 }
9225
Damien Lespiau1b842c82015-01-21 13:50:54 +00009226 fb = &intel_fb->base;
9227
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009228 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009229 if (!(val & PLANE_CTL_ENABLE))
9230 goto error;
9231
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009232 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9233 fourcc = skl_format_to_fourcc(pixel_format,
9234 val & PLANE_CTL_ORDER_RGBX,
9235 val & PLANE_CTL_ALPHA_MASK);
9236 fb->pixel_format = fourcc;
9237 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9238
Damien Lespiau40f46282015-02-27 11:15:21 +00009239 tiling = val & PLANE_CTL_TILED_MASK;
9240 switch (tiling) {
9241 case PLANE_CTL_TILED_LINEAR:
9242 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9243 break;
9244 case PLANE_CTL_TILED_X:
9245 plane_config->tiling = I915_TILING_X;
9246 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9247 break;
9248 case PLANE_CTL_TILED_Y:
9249 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9250 break;
9251 case PLANE_CTL_TILED_YF:
9252 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9253 break;
9254 default:
9255 MISSING_CASE(tiling);
9256 goto error;
9257 }
9258
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009259 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9260 plane_config->base = base;
9261
9262 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9263
9264 val = I915_READ(PLANE_SIZE(pipe, 0));
9265 fb->height = ((val >> 16) & 0xfff) + 1;
9266 fb->width = ((val >> 0) & 0x1fff) + 1;
9267
9268 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009269 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009270 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009271 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9272
9273 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009274 fb->pixel_format,
9275 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009276
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009277 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009278
9279 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9280 pipe_name(pipe), fb->width, fb->height,
9281 fb->bits_per_pixel, base, fb->pitches[0],
9282 plane_config->size);
9283
Damien Lespiau2d140302015-02-05 17:22:18 +00009284 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009285 return;
9286
9287error:
9288 kfree(fb);
9289}
9290
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009291static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009292 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009293{
9294 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009295 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009296 uint32_t tmp;
9297
9298 tmp = I915_READ(PF_CTL(crtc->pipe));
9299
9300 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009301 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009302 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9303 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009304
9305 /* We currently do not free assignements of panel fitters on
9306 * ivb/hsw (since we don't use the higher upscaling modes which
9307 * differentiates them) so just WARN about this case for now. */
9308 if (IS_GEN7(dev)) {
9309 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9310 PF_PIPE_SEL_IVB(crtc->pipe));
9311 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009312 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009313}
9314
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009315static void
9316ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9317 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009318{
9319 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009320 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009321 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009322 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009323 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009324 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009325 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009326 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009327
Damien Lespiau42a7b082015-02-05 19:35:13 +00009328 val = I915_READ(DSPCNTR(pipe));
9329 if (!(val & DISPLAY_PLANE_ENABLE))
9330 return;
9331
Damien Lespiaud9806c92015-01-21 14:07:19 +00009332 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009333 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009334 DRM_DEBUG_KMS("failed to alloc fb\n");
9335 return;
9336 }
9337
Damien Lespiau1b842c82015-01-21 13:50:54 +00009338 fb = &intel_fb->base;
9339
Daniel Vetter18c52472015-02-10 17:16:09 +00009340 if (INTEL_INFO(dev)->gen >= 4) {
9341 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009342 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009343 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9344 }
9345 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009346
9347 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009348 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009349 fb->pixel_format = fourcc;
9350 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009351
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009352 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009353 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009354 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009355 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009356 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009357 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009358 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009359 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009360 }
9361 plane_config->base = base;
9362
9363 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009364 fb->width = ((val >> 16) & 0xfff) + 1;
9365 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009366
9367 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009368 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009369
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009370 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009371 fb->pixel_format,
9372 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009373
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009374 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009375
Damien Lespiau2844a922015-01-20 12:51:48 +00009376 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9377 pipe_name(pipe), fb->width, fb->height,
9378 fb->bits_per_pixel, base, fb->pitches[0],
9379 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009380
Damien Lespiau2d140302015-02-05 17:22:18 +00009381 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009382}
9383
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009384static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009385 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009386{
9387 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009388 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009389 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009390 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009391 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009392
Imre Deak17290502016-02-12 18:55:11 +02009393 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9394 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009395 return false;
9396
Daniel Vettere143a212013-07-04 12:01:15 +02009397 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009398 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009399
Imre Deak17290502016-02-12 18:55:11 +02009400 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009401 tmp = I915_READ(PIPECONF(crtc->pipe));
9402 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009403 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009404
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009405 switch (tmp & PIPECONF_BPC_MASK) {
9406 case PIPECONF_6BPC:
9407 pipe_config->pipe_bpp = 18;
9408 break;
9409 case PIPECONF_8BPC:
9410 pipe_config->pipe_bpp = 24;
9411 break;
9412 case PIPECONF_10BPC:
9413 pipe_config->pipe_bpp = 30;
9414 break;
9415 case PIPECONF_12BPC:
9416 pipe_config->pipe_bpp = 36;
9417 break;
9418 default:
9419 break;
9420 }
9421
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009422 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9423 pipe_config->limited_color_range = true;
9424
Daniel Vetterab9412b2013-05-03 11:49:46 +02009425 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009426 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009427 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009428
Daniel Vetter88adfff2013-03-28 10:42:01 +01009429 pipe_config->has_pch_encoder = true;
9430
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009431 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9432 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9433 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009434
9435 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009436
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009437 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009438 /*
9439 * The pipe->pch transcoder and pch transcoder->pll
9440 * mapping is fixed.
9441 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009442 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009443 } else {
9444 tmp = I915_READ(PCH_DPLL_SEL);
9445 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009446 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009447 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009448 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009449 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009450
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009451 pipe_config->shared_dpll =
9452 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9453 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009454
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009455 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9456 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009457
9458 tmp = pipe_config->dpll_hw_state.dpll;
9459 pipe_config->pixel_multiplier =
9460 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9461 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009462
9463 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009464 } else {
9465 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009466 }
9467
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009468 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009469 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009470
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009471 ironlake_get_pfit_config(crtc, pipe_config);
9472
Imre Deak17290502016-02-12 18:55:11 +02009473 ret = true;
9474
9475out:
9476 intel_display_power_put(dev_priv, power_domain);
9477
9478 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009479}
9480
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009481static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9482{
Chris Wilson91c8a322016-07-05 10:40:23 +01009483 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009484 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009485
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009486 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009487 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009488 pipe_name(crtc->pipe));
9489
Rob Clarke2c719b2014-12-15 13:56:32 -05009490 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9491 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009492 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9493 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009494 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009495 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009496 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009497 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009498 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009499 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009500 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009501 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009502 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009503 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009504 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009505
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009506 /*
9507 * In theory we can still leave IRQs enabled, as long as only the HPD
9508 * interrupts remain enabled. We used to check for that, but since it's
9509 * gen-specific and since we only disable LCPLL after we fully disable
9510 * the interrupts, the check below should be enough.
9511 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009512 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009513}
9514
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009515static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9516{
Chris Wilson91c8a322016-07-05 10:40:23 +01009517 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009518
9519 if (IS_HASWELL(dev))
9520 return I915_READ(D_COMP_HSW);
9521 else
9522 return I915_READ(D_COMP_BDW);
9523}
9524
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009525static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9526{
Chris Wilson91c8a322016-07-05 10:40:23 +01009527 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009528
9529 if (IS_HASWELL(dev)) {
9530 mutex_lock(&dev_priv->rps.hw_lock);
9531 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9532 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009533 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009534 mutex_unlock(&dev_priv->rps.hw_lock);
9535 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009536 I915_WRITE(D_COMP_BDW, val);
9537 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009538 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009539}
9540
9541/*
9542 * This function implements pieces of two sequences from BSpec:
9543 * - Sequence for display software to disable LCPLL
9544 * - Sequence for display software to allow package C8+
9545 * The steps implemented here are just the steps that actually touch the LCPLL
9546 * register. Callers should take care of disabling all the display engine
9547 * functions, doing the mode unset, fixing interrupts, etc.
9548 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009549static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9550 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009551{
9552 uint32_t val;
9553
9554 assert_can_disable_lcpll(dev_priv);
9555
9556 val = I915_READ(LCPLL_CTL);
9557
9558 if (switch_to_fclk) {
9559 val |= LCPLL_CD_SOURCE_FCLK;
9560 I915_WRITE(LCPLL_CTL, val);
9561
Imre Deakf53dd632016-06-28 13:37:32 +03009562 if (wait_for_us(I915_READ(LCPLL_CTL) &
9563 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009564 DRM_ERROR("Switching to FCLK failed\n");
9565
9566 val = I915_READ(LCPLL_CTL);
9567 }
9568
9569 val |= LCPLL_PLL_DISABLE;
9570 I915_WRITE(LCPLL_CTL, val);
9571 POSTING_READ(LCPLL_CTL);
9572
Chris Wilson24d84412016-06-30 15:33:07 +01009573 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009574 DRM_ERROR("LCPLL still locked\n");
9575
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009576 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009577 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009578 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009579 ndelay(100);
9580
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009581 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9582 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009583 DRM_ERROR("D_COMP RCOMP still in progress\n");
9584
9585 if (allow_power_down) {
9586 val = I915_READ(LCPLL_CTL);
9587 val |= LCPLL_POWER_DOWN_ALLOW;
9588 I915_WRITE(LCPLL_CTL, val);
9589 POSTING_READ(LCPLL_CTL);
9590 }
9591}
9592
9593/*
9594 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9595 * source.
9596 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009597static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009598{
9599 uint32_t val;
9600
9601 val = I915_READ(LCPLL_CTL);
9602
9603 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9604 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9605 return;
9606
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009607 /*
9608 * Make sure we're not on PC8 state before disabling PC8, otherwise
9609 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009610 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009611 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009612
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009613 if (val & LCPLL_POWER_DOWN_ALLOW) {
9614 val &= ~LCPLL_POWER_DOWN_ALLOW;
9615 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009616 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009617 }
9618
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009619 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009620 val |= D_COMP_COMP_FORCE;
9621 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009622 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009623
9624 val = I915_READ(LCPLL_CTL);
9625 val &= ~LCPLL_PLL_DISABLE;
9626 I915_WRITE(LCPLL_CTL, val);
9627
Chris Wilson93220c02016-06-30 15:33:08 +01009628 if (intel_wait_for_register(dev_priv,
9629 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9630 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009631 DRM_ERROR("LCPLL not locked yet\n");
9632
9633 if (val & LCPLL_CD_SOURCE_FCLK) {
9634 val = I915_READ(LCPLL_CTL);
9635 val &= ~LCPLL_CD_SOURCE_FCLK;
9636 I915_WRITE(LCPLL_CTL, val);
9637
Imre Deakf53dd632016-06-28 13:37:32 +03009638 if (wait_for_us((I915_READ(LCPLL_CTL) &
9639 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009640 DRM_ERROR("Switching back to LCPLL failed\n");
9641 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009642
Mika Kuoppala59bad942015-01-16 11:34:40 +02009643 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009644 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009645}
9646
Paulo Zanoni765dab672014-03-07 20:08:18 -03009647/*
9648 * Package states C8 and deeper are really deep PC states that can only be
9649 * reached when all the devices on the system allow it, so even if the graphics
9650 * device allows PC8+, it doesn't mean the system will actually get to these
9651 * states. Our driver only allows PC8+ when going into runtime PM.
9652 *
9653 * The requirements for PC8+ are that all the outputs are disabled, the power
9654 * well is disabled and most interrupts are disabled, and these are also
9655 * requirements for runtime PM. When these conditions are met, we manually do
9656 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9657 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9658 * hang the machine.
9659 *
9660 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9661 * the state of some registers, so when we come back from PC8+ we need to
9662 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9663 * need to take care of the registers kept by RC6. Notice that this happens even
9664 * if we don't put the device in PCI D3 state (which is what currently happens
9665 * because of the runtime PM support).
9666 *
9667 * For more, read "Display Sequences for Package C8" on the hardware
9668 * documentation.
9669 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009670void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009671{
Chris Wilson91c8a322016-07-05 10:40:23 +01009672 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009673 uint32_t val;
9674
Paulo Zanonic67a4702013-08-19 13:18:09 -03009675 DRM_DEBUG_KMS("Enabling package C8+\n");
9676
Ville Syrjäläc2699522015-08-27 23:55:59 +03009677 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009678 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9679 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9680 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9681 }
9682
9683 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009684 hsw_disable_lcpll(dev_priv, true, true);
9685}
9686
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009687void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009688{
Chris Wilson91c8a322016-07-05 10:40:23 +01009689 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009690 uint32_t val;
9691
Paulo Zanonic67a4702013-08-19 13:18:09 -03009692 DRM_DEBUG_KMS("Disabling package C8+\n");
9693
9694 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009695 lpt_init_pch_refclk(dev);
9696
Ville Syrjäläc2699522015-08-27 23:55:59 +03009697 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009698 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9699 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9700 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9701 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009702}
9703
Imre Deak324513c2016-06-13 16:44:36 +03009704static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309705{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009706 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009707 struct intel_atomic_state *old_intel_state =
9708 to_intel_atomic_state(old_state);
9709 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309710
Imre Deak324513c2016-06-13 16:44:36 +03009711 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309712}
9713
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009714/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009715static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009716{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009717 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009718 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009719 struct drm_crtc *crtc;
9720 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009721 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009722 unsigned max_pixel_rate = 0, i;
9723 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009724
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009725 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9726 sizeof(intel_state->min_pixclk));
9727
9728 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009729 int pixel_rate;
9730
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009731 crtc_state = to_intel_crtc_state(cstate);
9732 if (!crtc_state->base.enable) {
9733 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009734 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009735 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009736
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009737 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009738
9739 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009740 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009741 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9742
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009743 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009744 }
9745
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009746 for_each_pipe(dev_priv, pipe)
9747 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9748
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009749 return max_pixel_rate;
9750}
9751
9752static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9753{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009754 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009755 uint32_t val, data;
9756 int ret;
9757
9758 if (WARN((I915_READ(LCPLL_CTL) &
9759 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9760 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9761 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9762 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9763 "trying to change cdclk frequency with cdclk not enabled\n"))
9764 return;
9765
9766 mutex_lock(&dev_priv->rps.hw_lock);
9767 ret = sandybridge_pcode_write(dev_priv,
9768 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9769 mutex_unlock(&dev_priv->rps.hw_lock);
9770 if (ret) {
9771 DRM_ERROR("failed to inform pcode about cdclk change\n");
9772 return;
9773 }
9774
9775 val = I915_READ(LCPLL_CTL);
9776 val |= LCPLL_CD_SOURCE_FCLK;
9777 I915_WRITE(LCPLL_CTL, val);
9778
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009779 if (wait_for_us(I915_READ(LCPLL_CTL) &
9780 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009781 DRM_ERROR("Switching to FCLK failed\n");
9782
9783 val = I915_READ(LCPLL_CTL);
9784 val &= ~LCPLL_CLK_FREQ_MASK;
9785
9786 switch (cdclk) {
9787 case 450000:
9788 val |= LCPLL_CLK_FREQ_450;
9789 data = 0;
9790 break;
9791 case 540000:
9792 val |= LCPLL_CLK_FREQ_54O_BDW;
9793 data = 1;
9794 break;
9795 case 337500:
9796 val |= LCPLL_CLK_FREQ_337_5_BDW;
9797 data = 2;
9798 break;
9799 case 675000:
9800 val |= LCPLL_CLK_FREQ_675_BDW;
9801 data = 3;
9802 break;
9803 default:
9804 WARN(1, "invalid cdclk frequency\n");
9805 return;
9806 }
9807
9808 I915_WRITE(LCPLL_CTL, val);
9809
9810 val = I915_READ(LCPLL_CTL);
9811 val &= ~LCPLL_CD_SOURCE_FCLK;
9812 I915_WRITE(LCPLL_CTL, val);
9813
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009814 if (wait_for_us((I915_READ(LCPLL_CTL) &
9815 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009816 DRM_ERROR("Switching back to LCPLL failed\n");
9817
9818 mutex_lock(&dev_priv->rps.hw_lock);
9819 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9820 mutex_unlock(&dev_priv->rps.hw_lock);
9821
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009822 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9823
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009824 intel_update_cdclk(dev);
9825
9826 WARN(cdclk != dev_priv->cdclk_freq,
9827 "cdclk requested %d kHz but got %d kHz\n",
9828 cdclk, dev_priv->cdclk_freq);
9829}
9830
Ville Syrjälä587c7912016-05-11 22:44:41 +03009831static int broadwell_calc_cdclk(int max_pixclk)
9832{
9833 if (max_pixclk > 540000)
9834 return 675000;
9835 else if (max_pixclk > 450000)
9836 return 540000;
9837 else if (max_pixclk > 337500)
9838 return 450000;
9839 else
9840 return 337500;
9841}
9842
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009843static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009844{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009845 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009846 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009847 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009848 int cdclk;
9849
9850 /*
9851 * FIXME should also account for plane ratio
9852 * once 64bpp pixel formats are supported.
9853 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009854 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009855
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009856 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009857 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9858 cdclk, dev_priv->max_cdclk_freq);
9859 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009860 }
9861
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009862 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9863 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009864 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009865
9866 return 0;
9867}
9868
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009869static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009870{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009871 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009872 struct intel_atomic_state *old_intel_state =
9873 to_intel_atomic_state(old_state);
9874 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009875
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009876 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009877}
9878
Clint Taylorc89e39f2016-05-13 23:41:21 +03009879static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9880{
9881 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9882 struct drm_i915_private *dev_priv = to_i915(state->dev);
9883 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009884 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009885 int cdclk;
9886
9887 /*
9888 * FIXME should also account for plane ratio
9889 * once 64bpp pixel formats are supported.
9890 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009891 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009892
9893 /*
9894 * FIXME move the cdclk caclulation to
9895 * compute_config() so we can fail gracegully.
9896 */
9897 if (cdclk > dev_priv->max_cdclk_freq) {
9898 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9899 cdclk, dev_priv->max_cdclk_freq);
9900 cdclk = dev_priv->max_cdclk_freq;
9901 }
9902
9903 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9904 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009905 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009906
9907 return 0;
9908}
9909
9910static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9911{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009912 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9913 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9914 unsigned int req_cdclk = intel_state->dev_cdclk;
9915 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009916
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009917 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009918}
9919
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009920static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9921 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009922{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009923 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009924 if (!intel_ddi_pll_select(crtc, crtc_state))
9925 return -EINVAL;
9926 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009927
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009928 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009929
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009930 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009931}
9932
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309933static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9934 enum port port,
9935 struct intel_crtc_state *pipe_config)
9936{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009937 enum intel_dpll_id id;
9938
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309939 switch (port) {
9940 case PORT_A:
9941 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009942 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309943 break;
9944 case PORT_B:
9945 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009946 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309947 break;
9948 case PORT_C:
9949 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009950 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309951 break;
9952 default:
9953 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009954 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309955 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009956
9957 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309958}
9959
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009960static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9961 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009962 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009963{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009964 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009965 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009966
9967 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9968 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9969
9970 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009971 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009972 id = DPLL_ID_SKL_DPLL0;
9973 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009974 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009975 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009976 break;
9977 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009978 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009979 break;
9980 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009981 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009982 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009983 default:
9984 MISSING_CASE(pipe_config->ddi_pll_sel);
9985 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009986 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009987
9988 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009989}
9990
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009991static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9992 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009993 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009994{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009995 enum intel_dpll_id id;
9996
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009997 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9998
9999 switch (pipe_config->ddi_pll_sel) {
10000 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010001 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010002 break;
10003 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010004 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010005 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010006 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010007 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010008 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010009 case PORT_CLK_SEL_LCPLL_810:
10010 id = DPLL_ID_LCPLL_810;
10011 break;
10012 case PORT_CLK_SEL_LCPLL_1350:
10013 id = DPLL_ID_LCPLL_1350;
10014 break;
10015 case PORT_CLK_SEL_LCPLL_2700:
10016 id = DPLL_ID_LCPLL_2700;
10017 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010018 default:
10019 MISSING_CASE(pipe_config->ddi_pll_sel);
10020 /* fall through */
10021 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010022 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010023 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010024
10025 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010026}
10027
Jani Nikulacf304292016-03-18 17:05:41 +020010028static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10029 struct intel_crtc_state *pipe_config,
10030 unsigned long *power_domain_mask)
10031{
10032 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010033 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010034 enum intel_display_power_domain power_domain;
10035 u32 tmp;
10036
Imre Deakd9a7bc62016-05-12 16:18:50 +030010037 /*
10038 * The pipe->transcoder mapping is fixed with the exception of the eDP
10039 * transcoder handled below.
10040 */
Jani Nikulacf304292016-03-18 17:05:41 +020010041 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10042
10043 /*
10044 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10045 * consistency and less surprising code; it's in always on power).
10046 */
10047 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10048 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10049 enum pipe trans_edp_pipe;
10050 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10051 default:
10052 WARN(1, "unknown pipe linked to edp transcoder\n");
10053 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10054 case TRANS_DDI_EDP_INPUT_A_ON:
10055 trans_edp_pipe = PIPE_A;
10056 break;
10057 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10058 trans_edp_pipe = PIPE_B;
10059 break;
10060 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10061 trans_edp_pipe = PIPE_C;
10062 break;
10063 }
10064
10065 if (trans_edp_pipe == crtc->pipe)
10066 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10067 }
10068
10069 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10070 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10071 return false;
10072 *power_domain_mask |= BIT(power_domain);
10073
10074 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10075
10076 return tmp & PIPECONF_ENABLE;
10077}
10078
Jani Nikula4d1de972016-03-18 17:05:42 +020010079static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10080 struct intel_crtc_state *pipe_config,
10081 unsigned long *power_domain_mask)
10082{
10083 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010084 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010085 enum intel_display_power_domain power_domain;
10086 enum port port;
10087 enum transcoder cpu_transcoder;
10088 u32 tmp;
10089
Jani Nikula4d1de972016-03-18 17:05:42 +020010090 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10091 if (port == PORT_A)
10092 cpu_transcoder = TRANSCODER_DSI_A;
10093 else
10094 cpu_transcoder = TRANSCODER_DSI_C;
10095
10096 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10097 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10098 continue;
10099 *power_domain_mask |= BIT(power_domain);
10100
Imre Deakdb18b6a2016-03-24 12:41:40 +020010101 /*
10102 * The PLL needs to be enabled with a valid divider
10103 * configuration, otherwise accessing DSI registers will hang
10104 * the machine. See BSpec North Display Engine
10105 * registers/MIPI[BXT]. We can break out here early, since we
10106 * need the same DSI PLL to be enabled for both DSI ports.
10107 */
10108 if (!intel_dsi_pll_is_enabled(dev_priv))
10109 break;
10110
Jani Nikula4d1de972016-03-18 17:05:42 +020010111 /* XXX: this works for video mode only */
10112 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10113 if (!(tmp & DPI_ENABLE))
10114 continue;
10115
10116 tmp = I915_READ(MIPI_CTRL(port));
10117 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10118 continue;
10119
10120 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010121 break;
10122 }
10123
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010124 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010125}
10126
Daniel Vetter26804af2014-06-25 22:01:55 +030010127static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010128 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010129{
10130 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010131 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010132 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010133 enum port port;
10134 uint32_t tmp;
10135
10136 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10137
10138 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10139
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010140 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010141 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010142 else if (IS_BROXTON(dev))
10143 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010144 else
10145 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010146
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010147 pll = pipe_config->shared_dpll;
10148 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010149 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10150 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010151 }
10152
Daniel Vetter26804af2014-06-25 22:01:55 +030010153 /*
10154 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10155 * DDI E. So just check whether this pipe is wired to DDI E and whether
10156 * the PCH transcoder is on.
10157 */
Damien Lespiauca370452013-12-03 13:56:24 +000010158 if (INTEL_INFO(dev)->gen < 9 &&
10159 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010160 pipe_config->has_pch_encoder = true;
10161
10162 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10163 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10164 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10165
10166 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10167 }
10168}
10169
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010170static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010171 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010172{
10173 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010174 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010175 enum intel_display_power_domain power_domain;
10176 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010177 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010178
Imre Deak17290502016-02-12 18:55:11 +020010179 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10180 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010181 return false;
Imre Deak17290502016-02-12 18:55:11 +020010182 power_domain_mask = BIT(power_domain);
10183
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010184 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010185
Jani Nikulacf304292016-03-18 17:05:41 +020010186 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010187
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010188 if (IS_BROXTON(dev_priv) &&
10189 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10190 WARN_ON(active);
10191 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010192 }
10193
Jani Nikulacf304292016-03-18 17:05:41 +020010194 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010195 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010196
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010197 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010198 haswell_get_ddi_port_state(crtc, pipe_config);
10199 intel_get_pipe_timings(crtc, pipe_config);
10200 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010201
Jani Nikulabc58be62016-03-18 17:05:39 +020010202 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010203
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010204 pipe_config->gamma_mode =
10205 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10206
Chandra Kondurua1b22782015-04-07 15:28:45 -070010207 if (INTEL_INFO(dev)->gen >= 9) {
10208 skl_init_scalers(dev, crtc, pipe_config);
10209 }
10210
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010211 if (INTEL_INFO(dev)->gen >= 9) {
10212 pipe_config->scaler_state.scaler_id = -1;
10213 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10214 }
10215
Imre Deak17290502016-02-12 18:55:11 +020010216 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10217 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10218 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010219 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010220 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010221 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010222 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010223 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010224
Jesse Barnese59150d2014-01-07 13:30:45 -080010225 if (IS_HASWELL(dev))
10226 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10227 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010228
Jani Nikula4d1de972016-03-18 17:05:42 +020010229 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10230 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010231 pipe_config->pixel_multiplier =
10232 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10233 } else {
10234 pipe_config->pixel_multiplier = 1;
10235 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010236
Imre Deak17290502016-02-12 18:55:11 +020010237out:
10238 for_each_power_domain(power_domain, power_domain_mask)
10239 intel_display_power_put(dev_priv, power_domain);
10240
Jani Nikulacf304292016-03-18 17:05:41 +020010241 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010242}
10243
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010244static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10245 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010246{
10247 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010248 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010250 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010251
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010252 if (plane_state && plane_state->visible) {
10253 unsigned int width = plane_state->base.crtc_w;
10254 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010255 unsigned int stride = roundup_pow_of_two(width) * 4;
10256
10257 switch (stride) {
10258 default:
10259 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10260 width, stride);
10261 stride = 256;
10262 /* fallthrough */
10263 case 256:
10264 case 512:
10265 case 1024:
10266 case 2048:
10267 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010268 }
10269
Ville Syrjälädc41c152014-08-13 11:57:05 +030010270 cntl |= CURSOR_ENABLE |
10271 CURSOR_GAMMA_ENABLE |
10272 CURSOR_FORMAT_ARGB |
10273 CURSOR_STRIDE(stride);
10274
10275 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010276 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010277
Ville Syrjälädc41c152014-08-13 11:57:05 +030010278 if (intel_crtc->cursor_cntl != 0 &&
10279 (intel_crtc->cursor_base != base ||
10280 intel_crtc->cursor_size != size ||
10281 intel_crtc->cursor_cntl != cntl)) {
10282 /* On these chipsets we can only modify the base/size/stride
10283 * whilst the cursor is disabled.
10284 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010285 I915_WRITE(CURCNTR(PIPE_A), 0);
10286 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010287 intel_crtc->cursor_cntl = 0;
10288 }
10289
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010290 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010291 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010292 intel_crtc->cursor_base = base;
10293 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010294
10295 if (intel_crtc->cursor_size != size) {
10296 I915_WRITE(CURSIZE, size);
10297 intel_crtc->cursor_size = size;
10298 }
10299
Chris Wilson4b0e3332014-05-30 16:35:26 +030010300 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010301 I915_WRITE(CURCNTR(PIPE_A), cntl);
10302 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010303 intel_crtc->cursor_cntl = cntl;
10304 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010305}
10306
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010307static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10308 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010309{
10310 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010311 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10313 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010314 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010315
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010316 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010317 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010318 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010319 case 64:
10320 cntl |= CURSOR_MODE_64_ARGB_AX;
10321 break;
10322 case 128:
10323 cntl |= CURSOR_MODE_128_ARGB_AX;
10324 break;
10325 case 256:
10326 cntl |= CURSOR_MODE_256_ARGB_AX;
10327 break;
10328 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010329 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010330 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010331 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010332 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010333
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010334 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010335 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010336
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010337 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10338 cntl |= CURSOR_ROTATE_180;
10339 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010340
Chris Wilson4b0e3332014-05-30 16:35:26 +030010341 if (intel_crtc->cursor_cntl != cntl) {
10342 I915_WRITE(CURCNTR(pipe), cntl);
10343 POSTING_READ(CURCNTR(pipe));
10344 intel_crtc->cursor_cntl = cntl;
10345 }
10346
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010347 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010348 I915_WRITE(CURBASE(pipe), base);
10349 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010350
10351 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010352}
10353
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010354/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010355static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010356 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010357{
10358 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010359 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10361 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010362 u32 base = intel_crtc->cursor_addr;
10363 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010364
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010365 if (plane_state) {
10366 int x = plane_state->base.crtc_x;
10367 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010368
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010369 if (x < 0) {
10370 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10371 x = -x;
10372 }
10373 pos |= x << CURSOR_X_SHIFT;
10374
10375 if (y < 0) {
10376 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10377 y = -y;
10378 }
10379 pos |= y << CURSOR_Y_SHIFT;
10380
10381 /* ILK+ do this automagically */
10382 if (HAS_GMCH_DISPLAY(dev) &&
10383 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10384 base += (plane_state->base.crtc_h *
10385 plane_state->base.crtc_w - 1) * 4;
10386 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010387 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010388
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010389 I915_WRITE(CURPOS(pipe), pos);
10390
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010391 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010392 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010393 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010394 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010395}
10396
Ville Syrjälädc41c152014-08-13 11:57:05 +030010397static bool cursor_size_ok(struct drm_device *dev,
10398 uint32_t width, uint32_t height)
10399{
10400 if (width == 0 || height == 0)
10401 return false;
10402
10403 /*
10404 * 845g/865g are special in that they are only limited by
10405 * the width of their cursors, the height is arbitrary up to
10406 * the precision of the register. Everything else requires
10407 * square cursors, limited to a few power-of-two sizes.
10408 */
10409 if (IS_845G(dev) || IS_I865G(dev)) {
10410 if ((width & 63) != 0)
10411 return false;
10412
10413 if (width > (IS_845G(dev) ? 64 : 512))
10414 return false;
10415
10416 if (height > 1023)
10417 return false;
10418 } else {
10419 switch (width | height) {
10420 case 256:
10421 case 128:
10422 if (IS_GEN2(dev))
10423 return false;
10424 case 64:
10425 break;
10426 default:
10427 return false;
10428 }
10429 }
10430
10431 return true;
10432}
10433
Jesse Barnes79e53942008-11-07 14:24:08 -080010434/* VESA 640x480x72Hz mode to set on the pipe */
10435static struct drm_display_mode load_detect_mode = {
10436 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10437 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10438};
10439
Daniel Vettera8bb6812014-02-10 18:00:39 +010010440struct drm_framebuffer *
10441__intel_framebuffer_create(struct drm_device *dev,
10442 struct drm_mode_fb_cmd2 *mode_cmd,
10443 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010444{
10445 struct intel_framebuffer *intel_fb;
10446 int ret;
10447
10448 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010449 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010450 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010451
10452 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010453 if (ret)
10454 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010455
10456 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010457
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010458err:
10459 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010460 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010461}
10462
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010463static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010464intel_framebuffer_create(struct drm_device *dev,
10465 struct drm_mode_fb_cmd2 *mode_cmd,
10466 struct drm_i915_gem_object *obj)
10467{
10468 struct drm_framebuffer *fb;
10469 int ret;
10470
10471 ret = i915_mutex_lock_interruptible(dev);
10472 if (ret)
10473 return ERR_PTR(ret);
10474 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10475 mutex_unlock(&dev->struct_mutex);
10476
10477 return fb;
10478}
10479
Chris Wilsond2dff872011-04-19 08:36:26 +010010480static u32
10481intel_framebuffer_pitch_for_width(int width, int bpp)
10482{
10483 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10484 return ALIGN(pitch, 64);
10485}
10486
10487static u32
10488intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10489{
10490 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010491 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010492}
10493
10494static struct drm_framebuffer *
10495intel_framebuffer_create_for_mode(struct drm_device *dev,
10496 struct drm_display_mode *mode,
10497 int depth, int bpp)
10498{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010499 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010500 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010501 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010502
Dave Gordond37cd8a2016-04-22 19:14:32 +010010503 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010504 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010505 if (IS_ERR(obj))
10506 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010507
10508 mode_cmd.width = mode->hdisplay;
10509 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010510 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10511 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010512 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010513
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010514 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10515 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010010516 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010517
10518 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010519}
10520
10521static struct drm_framebuffer *
10522mode_fits_in_fbdev(struct drm_device *dev,
10523 struct drm_display_mode *mode)
10524{
Daniel Vetter06957262015-08-10 13:34:08 +020010525#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010526 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010527 struct drm_i915_gem_object *obj;
10528 struct drm_framebuffer *fb;
10529
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010530 if (!dev_priv->fbdev)
10531 return NULL;
10532
10533 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010534 return NULL;
10535
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010536 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010537 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010538
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010539 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010540 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10541 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010542 return NULL;
10543
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010544 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010545 return NULL;
10546
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010547 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010548 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010549#else
10550 return NULL;
10551#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010552}
10553
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010554static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10555 struct drm_crtc *crtc,
10556 struct drm_display_mode *mode,
10557 struct drm_framebuffer *fb,
10558 int x, int y)
10559{
10560 struct drm_plane_state *plane_state;
10561 int hdisplay, vdisplay;
10562 int ret;
10563
10564 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10565 if (IS_ERR(plane_state))
10566 return PTR_ERR(plane_state);
10567
10568 if (mode)
10569 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10570 else
10571 hdisplay = vdisplay = 0;
10572
10573 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10574 if (ret)
10575 return ret;
10576 drm_atomic_set_fb_for_plane(plane_state, fb);
10577 plane_state->crtc_x = 0;
10578 plane_state->crtc_y = 0;
10579 plane_state->crtc_w = hdisplay;
10580 plane_state->crtc_h = vdisplay;
10581 plane_state->src_x = x << 16;
10582 plane_state->src_y = y << 16;
10583 plane_state->src_w = hdisplay << 16;
10584 plane_state->src_h = vdisplay << 16;
10585
10586 return 0;
10587}
10588
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010589bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010590 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010591 struct intel_load_detect_pipe *old,
10592 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010593{
10594 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010595 struct intel_encoder *intel_encoder =
10596 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010597 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010598 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010599 struct drm_crtc *crtc = NULL;
10600 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010601 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010602 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010603 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010604 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010605 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010606 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010607
Chris Wilsond2dff872011-04-19 08:36:26 +010010608 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010609 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010610 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010611
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010612 old->restore_state = NULL;
10613
Rob Clark51fd3712013-11-19 12:10:12 -050010614retry:
10615 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10616 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010617 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010618
Jesse Barnes79e53942008-11-07 14:24:08 -080010619 /*
10620 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010621 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010622 * - if the connector already has an assigned crtc, use it (but make
10623 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010624 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010625 * - try to find the first unused crtc that can drive this connector,
10626 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010627 */
10628
10629 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010630 if (connector->state->crtc) {
10631 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010632
Rob Clark51fd3712013-11-19 12:10:12 -050010633 ret = drm_modeset_lock(&crtc->mutex, ctx);
10634 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010635 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010636
10637 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010638 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 }
10640
10641 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010642 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010643 i++;
10644 if (!(encoder->possible_crtcs & (1 << i)))
10645 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010646
10647 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10648 if (ret)
10649 goto fail;
10650
10651 if (possible_crtc->state->enable) {
10652 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010653 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010654 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010655
10656 crtc = possible_crtc;
10657 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 }
10659
10660 /*
10661 * If we didn't find an unused CRTC, don't use any.
10662 */
10663 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010664 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010665 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010666 }
10667
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010668found:
10669 intel_crtc = to_intel_crtc(crtc);
10670
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010671 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10672 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010673 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010674
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010675 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010676 restore_state = drm_atomic_state_alloc(dev);
10677 if (!state || !restore_state) {
10678 ret = -ENOMEM;
10679 goto fail;
10680 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010681
10682 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010683 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010684
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010685 connector_state = drm_atomic_get_connector_state(state, connector);
10686 if (IS_ERR(connector_state)) {
10687 ret = PTR_ERR(connector_state);
10688 goto fail;
10689 }
10690
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010691 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10692 if (ret)
10693 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010694
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010695 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10696 if (IS_ERR(crtc_state)) {
10697 ret = PTR_ERR(crtc_state);
10698 goto fail;
10699 }
10700
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010701 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010702
Chris Wilson64927112011-04-20 07:25:26 +010010703 if (!mode)
10704 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010705
Chris Wilsond2dff872011-04-19 08:36:26 +010010706 /* We need a framebuffer large enough to accommodate all accesses
10707 * that the plane may generate whilst we perform load detection.
10708 * We can not rely on the fbcon either being present (we get called
10709 * during its initialisation to detect all boot displays, or it may
10710 * not even exist) or that it is large enough to satisfy the
10711 * requested mode.
10712 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010713 fb = mode_fits_in_fbdev(dev, mode);
10714 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010715 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010716 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010717 } else
10718 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010719 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010720 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010721 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010722 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010723
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010724 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10725 if (ret)
10726 goto fail;
10727
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010728 drm_framebuffer_unreference(fb);
10729
10730 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10731 if (ret)
10732 goto fail;
10733
10734 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10735 if (!ret)
10736 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10737 if (!ret)
10738 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10739 if (ret) {
10740 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10741 goto fail;
10742 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010743
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010744 ret = drm_atomic_commit(state);
10745 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010746 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010747 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010748 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010749
10750 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010751
Jesse Barnes79e53942008-11-07 14:24:08 -080010752 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010753 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010754 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010755
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010756fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010757 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010758 drm_atomic_state_free(restore_state);
10759 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010760
Rob Clark51fd3712013-11-19 12:10:12 -050010761 if (ret == -EDEADLK) {
10762 drm_modeset_backoff(ctx);
10763 goto retry;
10764 }
10765
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010766 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010767}
10768
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010769void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010770 struct intel_load_detect_pipe *old,
10771 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010772{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010773 struct intel_encoder *intel_encoder =
10774 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010775 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010776 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010777 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010778
Chris Wilsond2dff872011-04-19 08:36:26 +010010779 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010780 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010781 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010782
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010783 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010784 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010785
10786 ret = drm_atomic_commit(state);
10787 if (ret) {
10788 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10789 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010790 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010791}
10792
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010793static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010794 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010795{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010796 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010797 u32 dpll = pipe_config->dpll_hw_state.dpll;
10798
10799 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010800 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010801 else if (HAS_PCH_SPLIT(dev))
10802 return 120000;
10803 else if (!IS_GEN2(dev))
10804 return 96000;
10805 else
10806 return 48000;
10807}
10808
Jesse Barnes79e53942008-11-07 14:24:08 -080010809/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010810static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010811 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010812{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010813 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010814 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010815 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010816 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010817 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010818 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010819 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010820 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010821
10822 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010823 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010824 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010825 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010826
10827 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010828 if (IS_PINEVIEW(dev)) {
10829 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10830 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010831 } else {
10832 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10833 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10834 }
10835
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010836 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010837 if (IS_PINEVIEW(dev))
10838 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10839 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010840 else
10841 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010842 DPLL_FPA01_P1_POST_DIV_SHIFT);
10843
10844 switch (dpll & DPLL_MODE_MASK) {
10845 case DPLLB_MODE_DAC_SERIAL:
10846 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10847 5 : 10;
10848 break;
10849 case DPLLB_MODE_LVDS:
10850 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10851 7 : 14;
10852 break;
10853 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010854 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010855 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010856 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010857 }
10858
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010859 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010860 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010861 else
Imre Deakdccbea32015-06-22 23:35:51 +030010862 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010863 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010864 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010865 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010866
10867 if (is_lvds) {
10868 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10869 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010870
10871 if (lvds & LVDS_CLKB_POWER_UP)
10872 clock.p2 = 7;
10873 else
10874 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010875 } else {
10876 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10877 clock.p1 = 2;
10878 else {
10879 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10880 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10881 }
10882 if (dpll & PLL_P2_DIVIDE_BY_4)
10883 clock.p2 = 4;
10884 else
10885 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010886 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010887
Imre Deakdccbea32015-06-22 23:35:51 +030010888 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010889 }
10890
Ville Syrjälä18442d02013-09-13 16:00:08 +030010891 /*
10892 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010893 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010894 * encoder's get_config() function.
10895 */
Imre Deakdccbea32015-06-22 23:35:51 +030010896 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010897}
10898
Ville Syrjälä6878da02013-09-13 15:59:11 +030010899int intel_dotclock_calculate(int link_freq,
10900 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010901{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010902 /*
10903 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010904 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010905 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010906 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010907 *
10908 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010909 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010910 */
10911
Ville Syrjälä6878da02013-09-13 15:59:11 +030010912 if (!m_n->link_n)
10913 return 0;
10914
10915 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10916}
10917
Ville Syrjälä18442d02013-09-13 16:00:08 +030010918static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010919 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010920{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010921 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010922
10923 /* read out port_clock from the DPLL */
10924 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010925
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010926 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010927 * In case there is an active pipe without active ports,
10928 * we may need some idea for the dotclock anyway.
10929 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010930 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010931 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010932 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010933 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010934}
10935
10936/** Returns the currently programmed mode of the given pipe. */
10937struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10938 struct drm_crtc *crtc)
10939{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010940 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010942 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010943 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010944 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010945 int htot = I915_READ(HTOTAL(cpu_transcoder));
10946 int hsync = I915_READ(HSYNC(cpu_transcoder));
10947 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10948 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010949 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010950
10951 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10952 if (!mode)
10953 return NULL;
10954
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010955 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10956 if (!pipe_config) {
10957 kfree(mode);
10958 return NULL;
10959 }
10960
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010961 /*
10962 * Construct a pipe_config sufficient for getting the clock info
10963 * back out of crtc_clock_get.
10964 *
10965 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10966 * to use a real value here instead.
10967 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010968 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10969 pipe_config->pixel_multiplier = 1;
10970 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10971 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10972 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10973 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010974
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010975 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010976 mode->hdisplay = (htot & 0xffff) + 1;
10977 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10978 mode->hsync_start = (hsync & 0xffff) + 1;
10979 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10980 mode->vdisplay = (vtot & 0xffff) + 1;
10981 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10982 mode->vsync_start = (vsync & 0xffff) + 1;
10983 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10984
10985 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010986
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010987 kfree(pipe_config);
10988
Jesse Barnes79e53942008-11-07 14:24:08 -080010989 return mode;
10990}
10991
10992static void intel_crtc_destroy(struct drm_crtc *crtc)
10993{
10994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010995 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010996 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010997
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010998 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010999 work = intel_crtc->flip_work;
11000 intel_crtc->flip_work = NULL;
11001 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011002
Daniel Vetter5a21b662016-05-24 17:13:53 +020011003 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011004 cancel_work_sync(&work->mmio_work);
11005 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011006 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011007 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011008
11009 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011010
Jesse Barnes79e53942008-11-07 14:24:08 -080011011 kfree(intel_crtc);
11012}
11013
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011014static void intel_unpin_work_fn(struct work_struct *__work)
11015{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011016 struct intel_flip_work *work =
11017 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011018 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11019 struct drm_device *dev = crtc->base.dev;
11020 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011021
Daniel Vetter5a21b662016-05-24 17:13:53 +020011022 if (is_mmio_work(work))
11023 flush_work(&work->mmio_work);
11024
11025 mutex_lock(&dev->struct_mutex);
11026 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011027 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011028 mutex_unlock(&dev->struct_mutex);
11029
Chris Wilsone8a261e2016-07-20 13:31:49 +010011030 i915_gem_request_put(work->flip_queued_req);
11031
Chris Wilson5748b6a2016-08-04 16:32:38 +010011032 intel_frontbuffer_flip_complete(to_i915(dev),
11033 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011034 intel_fbc_post_update(crtc);
11035 drm_framebuffer_unreference(work->old_fb);
11036
11037 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11038 atomic_dec(&crtc->unpin_work_count);
11039
11040 kfree(work);
11041}
11042
11043/* Is 'a' after or equal to 'b'? */
11044static bool g4x_flip_count_after_eq(u32 a, u32 b)
11045{
11046 return !((a - b) & 0x80000000);
11047}
11048
11049static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11050 struct intel_flip_work *work)
11051{
11052 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011053 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011054 unsigned reset_counter;
11055
11056 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11057 if (crtc->reset_counter != reset_counter)
11058 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011059
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011060 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011061 * The relevant registers doen't exist on pre-ctg.
11062 * As the flip done interrupt doesn't trigger for mmio
11063 * flips on gmch platforms, a flip count check isn't
11064 * really needed there. But since ctg has the registers,
11065 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011066 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011067 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11068 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011069
Daniel Vetter5a21b662016-05-24 17:13:53 +020011070 /*
11071 * BDW signals flip done immediately if the plane
11072 * is disabled, even if the plane enable is already
11073 * armed to occur at the next vblank :(
11074 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011075
Daniel Vetter5a21b662016-05-24 17:13:53 +020011076 /*
11077 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11078 * used the same base address. In that case the mmio flip might
11079 * have completed, but the CS hasn't even executed the flip yet.
11080 *
11081 * A flip count check isn't enough as the CS might have updated
11082 * the base address just after start of vblank, but before we
11083 * managed to process the interrupt. This means we'd complete the
11084 * CS flip too soon.
11085 *
11086 * Combining both checks should get us a good enough result. It may
11087 * still happen that the CS flip has been executed, but has not
11088 * yet actually completed. But in case the base address is the same
11089 * anyway, we don't really care.
11090 */
11091 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11092 crtc->flip_work->gtt_offset &&
11093 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11094 crtc->flip_work->flip_count);
11095}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011096
Daniel Vetter5a21b662016-05-24 17:13:53 +020011097static bool
11098__pageflip_finished_mmio(struct intel_crtc *crtc,
11099 struct intel_flip_work *work)
11100{
11101 /*
11102 * MMIO work completes when vblank is different from
11103 * flip_queued_vblank.
11104 *
11105 * Reset counter value doesn't matter, this is handled by
11106 * i915_wait_request finishing early, so no need to handle
11107 * reset here.
11108 */
11109 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011110}
11111
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011112
11113static bool pageflip_finished(struct intel_crtc *crtc,
11114 struct intel_flip_work *work)
11115{
11116 if (!atomic_read(&work->pending))
11117 return false;
11118
11119 smp_rmb();
11120
Daniel Vetter5a21b662016-05-24 17:13:53 +020011121 if (is_mmio_work(work))
11122 return __pageflip_finished_mmio(crtc, work);
11123 else
11124 return __pageflip_finished_cs(crtc, work);
11125}
11126
11127void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11128{
Chris Wilson91c8a322016-07-05 10:40:23 +010011129 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011130 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11132 struct intel_flip_work *work;
11133 unsigned long flags;
11134
11135 /* Ignore early vblank irqs */
11136 if (!crtc)
11137 return;
11138
Daniel Vetterf3260382014-09-15 14:55:23 +020011139 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011140 * This is called both by irq handlers and the reset code (to complete
11141 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011142 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011143 spin_lock_irqsave(&dev->event_lock, flags);
11144 work = intel_crtc->flip_work;
11145
11146 if (work != NULL &&
11147 !is_mmio_work(work) &&
11148 pageflip_finished(intel_crtc, work))
11149 page_flip_completed(intel_crtc);
11150
11151 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011152}
11153
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011154void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011155{
Chris Wilson91c8a322016-07-05 10:40:23 +010011156 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011157 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11159 struct intel_flip_work *work;
11160 unsigned long flags;
11161
11162 /* Ignore early vblank irqs */
11163 if (!crtc)
11164 return;
11165
11166 /*
11167 * This is called both by irq handlers and the reset code (to complete
11168 * lost pageflips) so needs the full irqsave spinlocks.
11169 */
11170 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011171 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011172
Daniel Vetter5a21b662016-05-24 17:13:53 +020011173 if (work != NULL &&
11174 is_mmio_work(work) &&
11175 pageflip_finished(intel_crtc, work))
11176 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011177
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011178 spin_unlock_irqrestore(&dev->event_lock, flags);
11179}
11180
Daniel Vetter5a21b662016-05-24 17:13:53 +020011181static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11182 struct intel_flip_work *work)
11183{
11184 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11185
11186 /* Ensure that the work item is consistent when activating it ... */
11187 smp_mb__before_atomic();
11188 atomic_set(&work->pending, 1);
11189}
11190
11191static int intel_gen2_queue_flip(struct drm_device *dev,
11192 struct drm_crtc *crtc,
11193 struct drm_framebuffer *fb,
11194 struct drm_i915_gem_object *obj,
11195 struct drm_i915_gem_request *req,
11196 uint32_t flags)
11197{
Chris Wilson7e37f882016-08-02 22:50:21 +010011198 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11200 u32 flip_mask;
11201 int ret;
11202
11203 ret = intel_ring_begin(req, 6);
11204 if (ret)
11205 return ret;
11206
11207 /* Can't queue multiple flips, so wait for the previous
11208 * one to finish before executing the next.
11209 */
11210 if (intel_crtc->plane)
11211 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11212 else
11213 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011214 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11215 intel_ring_emit(ring, MI_NOOP);
11216 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011217 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011218 intel_ring_emit(ring, fb->pitches[0]);
11219 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11220 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011221
11222 return 0;
11223}
11224
11225static int intel_gen3_queue_flip(struct drm_device *dev,
11226 struct drm_crtc *crtc,
11227 struct drm_framebuffer *fb,
11228 struct drm_i915_gem_object *obj,
11229 struct drm_i915_gem_request *req,
11230 uint32_t flags)
11231{
Chris Wilson7e37f882016-08-02 22:50:21 +010011232 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11234 u32 flip_mask;
11235 int ret;
11236
11237 ret = intel_ring_begin(req, 6);
11238 if (ret)
11239 return ret;
11240
11241 if (intel_crtc->plane)
11242 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11243 else
11244 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011245 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11246 intel_ring_emit(ring, MI_NOOP);
11247 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011248 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011249 intel_ring_emit(ring, fb->pitches[0]);
11250 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11251 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011252
11253 return 0;
11254}
11255
11256static int intel_gen4_queue_flip(struct drm_device *dev,
11257 struct drm_crtc *crtc,
11258 struct drm_framebuffer *fb,
11259 struct drm_i915_gem_object *obj,
11260 struct drm_i915_gem_request *req,
11261 uint32_t flags)
11262{
Chris Wilson7e37f882016-08-02 22:50:21 +010011263 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011264 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11266 uint32_t pf, pipesrc;
11267 int ret;
11268
11269 ret = intel_ring_begin(req, 4);
11270 if (ret)
11271 return ret;
11272
11273 /* i965+ uses the linear or tiled offsets from the
11274 * Display Registers (which do not change across a page-flip)
11275 * so we need only reprogram the base address.
11276 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011277 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011278 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011279 intel_ring_emit(ring, fb->pitches[0]);
11280 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Chris Wilson3e510a82016-08-05 10:14:23 +010011281 i915_gem_object_get_tiling(obj));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011282
11283 /* XXX Enabling the panel-fitter across page-flip is so far
11284 * untested on non-native modes, so ignore it for now.
11285 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11286 */
11287 pf = 0;
11288 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011289 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011290
11291 return 0;
11292}
11293
11294static int intel_gen6_queue_flip(struct drm_device *dev,
11295 struct drm_crtc *crtc,
11296 struct drm_framebuffer *fb,
11297 struct drm_i915_gem_object *obj,
11298 struct drm_i915_gem_request *req,
11299 uint32_t flags)
11300{
Chris Wilson7e37f882016-08-02 22:50:21 +010011301 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011302 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11304 uint32_t pf, pipesrc;
11305 int ret;
11306
11307 ret = intel_ring_begin(req, 4);
11308 if (ret)
11309 return ret;
11310
Chris Wilsonb5321f32016-08-02 22:50:18 +010011311 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011312 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson3e510a82016-08-05 10:14:23 +010011313 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011314 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011315
11316 /* Contrary to the suggestions in the documentation,
11317 * "Enable Panel Fitter" does not seem to be required when page
11318 * flipping with a non-native mode, and worse causes a normal
11319 * modeset to fail.
11320 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11321 */
11322 pf = 0;
11323 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011324 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011325
11326 return 0;
11327}
11328
11329static int intel_gen7_queue_flip(struct drm_device *dev,
11330 struct drm_crtc *crtc,
11331 struct drm_framebuffer *fb,
11332 struct drm_i915_gem_object *obj,
11333 struct drm_i915_gem_request *req,
11334 uint32_t flags)
11335{
Chris Wilson7e37f882016-08-02 22:50:21 +010011336 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11338 uint32_t plane_bit = 0;
11339 int len, ret;
11340
11341 switch (intel_crtc->plane) {
11342 case PLANE_A:
11343 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11344 break;
11345 case PLANE_B:
11346 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11347 break;
11348 case PLANE_C:
11349 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11350 break;
11351 default:
11352 WARN_ONCE(1, "unknown plane in flip command\n");
11353 return -ENODEV;
11354 }
11355
11356 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011357 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011358 len += 6;
11359 /*
11360 * On Gen 8, SRM is now taking an extra dword to accommodate
11361 * 48bits addresses, and we need a NOOP for the batch size to
11362 * stay even.
11363 */
11364 if (IS_GEN8(dev))
11365 len += 2;
11366 }
11367
11368 /*
11369 * BSpec MI_DISPLAY_FLIP for IVB:
11370 * "The full packet must be contained within the same cache line."
11371 *
11372 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11373 * cacheline, if we ever start emitting more commands before
11374 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11375 * then do the cacheline alignment, and finally emit the
11376 * MI_DISPLAY_FLIP.
11377 */
11378 ret = intel_ring_cacheline_align(req);
11379 if (ret)
11380 return ret;
11381
11382 ret = intel_ring_begin(req, len);
11383 if (ret)
11384 return ret;
11385
11386 /* Unmask the flip-done completion message. Note that the bspec says that
11387 * we should do this for both the BCS and RCS, and that we must not unmask
11388 * more than one flip event at any time (or ensure that one flip message
11389 * can be sent by waiting for flip-done prior to queueing new flips).
11390 * Experimentation says that BCS works despite DERRMR masking all
11391 * flip-done completion events and that unmasking all planes at once
11392 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11393 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11394 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011395 if (req->engine->id == RCS) {
11396 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11397 intel_ring_emit_reg(ring, DERRMR);
11398 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011399 DERRMR_PIPEB_PRI_FLIP_DONE |
11400 DERRMR_PIPEC_PRI_FLIP_DONE));
11401 if (IS_GEN8(dev))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011402 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011403 MI_SRM_LRM_GLOBAL_GTT);
11404 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011405 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011406 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011407 intel_ring_emit_reg(ring, DERRMR);
11408 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011409 if (IS_GEN8(dev)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011410 intel_ring_emit(ring, 0);
11411 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011412 }
11413 }
11414
Chris Wilsonb5321f32016-08-02 22:50:18 +010011415 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Chris Wilson3e510a82016-08-05 10:14:23 +010011416 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011417 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11418 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011419
11420 return 0;
11421}
11422
11423static bool use_mmio_flip(struct intel_engine_cs *engine,
11424 struct drm_i915_gem_object *obj)
11425{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011426 struct reservation_object *resv;
11427
Daniel Vetter5a21b662016-05-24 17:13:53 +020011428 /*
11429 * This is not being used for older platforms, because
11430 * non-availability of flip done interrupt forces us to use
11431 * CS flips. Older platforms derive flip done using some clever
11432 * tricks involving the flip_pending status bits and vblank irqs.
11433 * So using MMIO flips there would disrupt this mechanism.
11434 */
11435
11436 if (engine == NULL)
11437 return true;
11438
11439 if (INTEL_GEN(engine->i915) < 5)
11440 return false;
11441
11442 if (i915.use_mmio_flip < 0)
11443 return false;
11444 else if (i915.use_mmio_flip > 0)
11445 return true;
11446 else if (i915.enable_execlists)
11447 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011448
11449 resv = i915_gem_object_get_dmabuf_resv(obj);
11450 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011451 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011452
Chris Wilsond72d9082016-08-04 07:52:31 +010011453 return engine != i915_gem_active_get_engine(&obj->last_write,
11454 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011455}
11456
11457static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11458 unsigned int rotation,
11459 struct intel_flip_work *work)
11460{
11461 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011462 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011463 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11464 const enum pipe pipe = intel_crtc->pipe;
11465 u32 ctl, stride, tile_height;
11466
11467 ctl = I915_READ(PLANE_CTL(pipe, 0));
11468 ctl &= ~PLANE_CTL_TILED_MASK;
11469 switch (fb->modifier[0]) {
11470 case DRM_FORMAT_MOD_NONE:
11471 break;
11472 case I915_FORMAT_MOD_X_TILED:
11473 ctl |= PLANE_CTL_TILED_X;
11474 break;
11475 case I915_FORMAT_MOD_Y_TILED:
11476 ctl |= PLANE_CTL_TILED_Y;
11477 break;
11478 case I915_FORMAT_MOD_Yf_TILED:
11479 ctl |= PLANE_CTL_TILED_YF;
11480 break;
11481 default:
11482 MISSING_CASE(fb->modifier[0]);
11483 }
11484
11485 /*
11486 * The stride is either expressed as a multiple of 64 bytes chunks for
11487 * linear buffers or in number of tiles for tiled buffers.
11488 */
11489 if (intel_rotation_90_or_270(rotation)) {
11490 /* stride = Surface height in tiles */
11491 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11492 stride = DIV_ROUND_UP(fb->height, tile_height);
11493 } else {
11494 stride = fb->pitches[0] /
11495 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11496 fb->pixel_format);
11497 }
11498
11499 /*
11500 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11501 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11502 */
11503 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11504 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11505
11506 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11507 POSTING_READ(PLANE_SURF(pipe, 0));
11508}
11509
11510static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11511 struct intel_flip_work *work)
11512{
11513 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011514 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011515 struct intel_framebuffer *intel_fb =
11516 to_intel_framebuffer(intel_crtc->base.primary->fb);
11517 struct drm_i915_gem_object *obj = intel_fb->obj;
11518 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11519 u32 dspcntr;
11520
11521 dspcntr = I915_READ(reg);
11522
Chris Wilson3e510a82016-08-05 10:14:23 +010011523 if (i915_gem_object_is_tiled(obj))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011524 dspcntr |= DISPPLANE_TILED;
11525 else
11526 dspcntr &= ~DISPPLANE_TILED;
11527
11528 I915_WRITE(reg, dspcntr);
11529
11530 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11531 POSTING_READ(DSPSURF(intel_crtc->plane));
11532}
11533
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011534static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011535{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011536 struct intel_flip_work *work =
11537 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011538 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11540 struct intel_framebuffer *intel_fb =
11541 to_intel_framebuffer(crtc->base.primary->fb);
11542 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011543 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011544
11545 if (work->flip_queued_req)
Chris Wilson776f3232016-08-04 07:52:40 +010011546 WARN_ON(i915_wait_request(work->flip_queued_req,
11547 false, NULL,
11548 NO_WAITBOOST));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011549
11550 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011551 resv = i915_gem_object_get_dmabuf_resv(obj);
11552 if (resv)
11553 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011554 MAX_SCHEDULE_TIMEOUT) < 0);
11555
11556 intel_pipe_update_start(crtc);
11557
11558 if (INTEL_GEN(dev_priv) >= 9)
11559 skl_do_mmio_flip(crtc, work->rotation, work);
11560 else
11561 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11562 ilk_do_mmio_flip(crtc, work);
11563
11564 intel_pipe_update_end(crtc, work);
11565}
11566
11567static int intel_default_queue_flip(struct drm_device *dev,
11568 struct drm_crtc *crtc,
11569 struct drm_framebuffer *fb,
11570 struct drm_i915_gem_object *obj,
11571 struct drm_i915_gem_request *req,
11572 uint32_t flags)
11573{
11574 return -ENODEV;
11575}
11576
11577static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11578 struct intel_crtc *intel_crtc,
11579 struct intel_flip_work *work)
11580{
11581 u32 addr, vblank;
11582
11583 if (!atomic_read(&work->pending))
11584 return false;
11585
11586 smp_rmb();
11587
11588 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11589 if (work->flip_ready_vblank == 0) {
11590 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011591 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011592 return false;
11593
11594 work->flip_ready_vblank = vblank;
11595 }
11596
11597 if (vblank - work->flip_ready_vblank < 3)
11598 return false;
11599
11600 /* Potential stall - if we see that the flip has happened,
11601 * assume a missed interrupt. */
11602 if (INTEL_GEN(dev_priv) >= 4)
11603 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11604 else
11605 addr = I915_READ(DSPADDR(intel_crtc->plane));
11606
11607 /* There is a potential issue here with a false positive after a flip
11608 * to the same address. We could address this by checking for a
11609 * non-incrementing frame counter.
11610 */
11611 return addr == work->gtt_offset;
11612}
11613
11614void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11615{
Chris Wilson91c8a322016-07-05 10:40:23 +010011616 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011617 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011619 struct intel_flip_work *work;
11620
11621 WARN_ON(!in_interrupt());
11622
11623 if (crtc == NULL)
11624 return;
11625
11626 spin_lock(&dev->event_lock);
11627 work = intel_crtc->flip_work;
11628
11629 if (work != NULL && !is_mmio_work(work) &&
11630 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11631 WARN_ONCE(1,
11632 "Kicking stuck page flip: queued at %d, now %d\n",
11633 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11634 page_flip_completed(intel_crtc);
11635 work = NULL;
11636 }
11637
11638 if (work != NULL && !is_mmio_work(work) &&
11639 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11640 intel_queue_rps_boost_for_request(work->flip_queued_req);
11641 spin_unlock(&dev->event_lock);
11642}
11643
11644static int intel_crtc_page_flip(struct drm_crtc *crtc,
11645 struct drm_framebuffer *fb,
11646 struct drm_pending_vblank_event *event,
11647 uint32_t page_flip_flags)
11648{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011649 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011650 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011651 struct drm_framebuffer *old_fb = crtc->primary->fb;
11652 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11654 struct drm_plane *primary = crtc->primary;
11655 enum pipe pipe = intel_crtc->pipe;
11656 struct intel_flip_work *work;
11657 struct intel_engine_cs *engine;
11658 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010011659 struct drm_i915_gem_request *request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011660 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011661
Daniel Vetter5a21b662016-05-24 17:13:53 +020011662 /*
11663 * drm_mode_page_flip_ioctl() should already catch this, but double
11664 * check to be safe. In the future we may enable pageflipping from
11665 * a disabled primary plane.
11666 */
11667 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11668 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011669
Daniel Vetter5a21b662016-05-24 17:13:53 +020011670 /* Can't change pixel format via MI display flips. */
11671 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11672 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011673
Daniel Vetter5a21b662016-05-24 17:13:53 +020011674 /*
11675 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11676 * Note that pitch changes could also affect these register.
11677 */
11678 if (INTEL_INFO(dev)->gen > 3 &&
11679 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11680 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11681 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011682
Daniel Vetter5a21b662016-05-24 17:13:53 +020011683 if (i915_terminally_wedged(&dev_priv->gpu_error))
11684 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011685
Daniel Vetter5a21b662016-05-24 17:13:53 +020011686 work = kzalloc(sizeof(*work), GFP_KERNEL);
11687 if (work == NULL)
11688 return -ENOMEM;
11689
11690 work->event = event;
11691 work->crtc = crtc;
11692 work->old_fb = old_fb;
11693 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011694
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011695 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011696 if (ret)
11697 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011698
Daniel Vetter5a21b662016-05-24 17:13:53 +020011699 /* We borrow the event spin lock for protecting flip_work */
11700 spin_lock_irq(&dev->event_lock);
11701 if (intel_crtc->flip_work) {
11702 /* Before declaring the flip queue wedged, check if
11703 * the hardware completed the operation behind our backs.
11704 */
11705 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11706 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11707 page_flip_completed(intel_crtc);
11708 } else {
11709 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11710 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011711
Daniel Vetter5a21b662016-05-24 17:13:53 +020011712 drm_crtc_vblank_put(crtc);
11713 kfree(work);
11714 return -EBUSY;
11715 }
11716 }
11717 intel_crtc->flip_work = work;
11718 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011719
Daniel Vetter5a21b662016-05-24 17:13:53 +020011720 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11721 flush_workqueue(dev_priv->wq);
11722
11723 /* Reference the objects for the scheduled work. */
11724 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011725
11726 crtc->primary->fb = fb;
11727 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011728
11729 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11730 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011731
Chris Wilson25dc5562016-07-20 13:31:52 +010011732 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011733
11734 ret = i915_mutex_lock_interruptible(dev);
11735 if (ret)
11736 goto cleanup;
11737
11738 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11739 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11740 ret = -EIO;
11741 goto cleanup;
11742 }
11743
11744 atomic_inc(&intel_crtc->unpin_work_count);
11745
11746 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11747 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11748
11749 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11750 engine = &dev_priv->engine[BCS];
Chris Wilson3e510a82016-08-05 10:14:23 +010011751 if (i915_gem_object_get_tiling(obj) !=
11752 i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011753 /* vlv: DISPLAY_FLIP fails to change tiling */
11754 engine = NULL;
11755 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11756 engine = &dev_priv->engine[BCS];
11757 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsond72d9082016-08-04 07:52:31 +010011758 engine = i915_gem_active_get_engine(&obj->last_write,
11759 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011760 if (engine == NULL || engine->id != RCS)
11761 engine = &dev_priv->engine[BCS];
11762 } else {
11763 engine = &dev_priv->engine[RCS];
11764 }
11765
11766 mmio_flip = use_mmio_flip(engine, obj);
11767
Daniel Vetter5a21b662016-05-24 17:13:53 +020011768 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11769 if (ret)
11770 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011771
Daniel Vetter5a21b662016-05-24 17:13:53 +020011772 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11773 obj, 0);
11774 work->gtt_offset += intel_crtc->dspaddr_offset;
11775 work->rotation = crtc->primary->state->rotation;
11776
11777 if (mmio_flip) {
11778 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11779
Chris Wilsond72d9082016-08-04 07:52:31 +010011780 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11781 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011782 schedule_work(&work->mmio_work);
11783 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010011784 request = i915_gem_request_alloc(engine, engine->last_context);
11785 if (IS_ERR(request)) {
11786 ret = PTR_ERR(request);
11787 goto cleanup_unpin;
11788 }
11789
11790 ret = i915_gem_object_sync(obj, request);
11791 if (ret)
11792 goto cleanup_request;
11793
Daniel Vetter5a21b662016-05-24 17:13:53 +020011794 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11795 page_flip_flags);
11796 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010011797 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011798
11799 intel_mark_page_flip_active(intel_crtc, work);
11800
Chris Wilson8e637172016-08-02 22:50:26 +010011801 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011802 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011803 }
11804
Daniel Vetter5a21b662016-05-24 17:13:53 +020011805 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11806 to_intel_plane(primary)->frontbuffer_bit);
11807 mutex_unlock(&dev->struct_mutex);
11808
Chris Wilson5748b6a2016-08-04 16:32:38 +010011809 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020011810 to_intel_plane(primary)->frontbuffer_bit);
11811
11812 trace_i915_flip_request(intel_crtc->plane, obj);
11813
11814 return 0;
11815
Chris Wilson8e637172016-08-02 22:50:26 +010011816cleanup_request:
11817 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011818cleanup_unpin:
11819 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11820cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020011821 atomic_dec(&intel_crtc->unpin_work_count);
11822 mutex_unlock(&dev->struct_mutex);
11823cleanup:
11824 crtc->primary->fb = old_fb;
11825 update_state_fb(crtc->primary);
11826
Chris Wilson34911fd2016-07-20 13:31:54 +010011827 i915_gem_object_put_unlocked(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011828 drm_framebuffer_unreference(work->old_fb);
11829
11830 spin_lock_irq(&dev->event_lock);
11831 intel_crtc->flip_work = NULL;
11832 spin_unlock_irq(&dev->event_lock);
11833
11834 drm_crtc_vblank_put(crtc);
11835free_work:
11836 kfree(work);
11837
11838 if (ret == -EIO) {
11839 struct drm_atomic_state *state;
11840 struct drm_plane_state *plane_state;
11841
11842out_hang:
11843 state = drm_atomic_state_alloc(dev);
11844 if (!state)
11845 return -ENOMEM;
11846 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11847
11848retry:
11849 plane_state = drm_atomic_get_plane_state(state, primary);
11850 ret = PTR_ERR_OR_ZERO(plane_state);
11851 if (!ret) {
11852 drm_atomic_set_fb_for_plane(plane_state, fb);
11853
11854 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11855 if (!ret)
11856 ret = drm_atomic_commit(state);
11857 }
11858
11859 if (ret == -EDEADLK) {
11860 drm_modeset_backoff(state->acquire_ctx);
11861 drm_atomic_state_clear(state);
11862 goto retry;
11863 }
11864
11865 if (ret)
11866 drm_atomic_state_free(state);
11867
11868 if (ret == 0 && event) {
11869 spin_lock_irq(&dev->event_lock);
11870 drm_crtc_send_vblank_event(crtc, event);
11871 spin_unlock_irq(&dev->event_lock);
11872 }
11873 }
11874 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011875}
11876
Daniel Vetter5a21b662016-05-24 17:13:53 +020011877
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011878/**
11879 * intel_wm_need_update - Check whether watermarks need updating
11880 * @plane: drm plane
11881 * @state: new plane state
11882 *
11883 * Check current plane state versus the new one to determine whether
11884 * watermarks need to be recalculated.
11885 *
11886 * Returns true or false.
11887 */
11888static bool intel_wm_need_update(struct drm_plane *plane,
11889 struct drm_plane_state *state)
11890{
Matt Roperd21fbe82015-09-24 15:53:12 -070011891 struct intel_plane_state *new = to_intel_plane_state(state);
11892 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11893
11894 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011895 if (new->visible != cur->visible)
11896 return true;
11897
11898 if (!cur->base.fb || !new->base.fb)
11899 return false;
11900
11901 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11902 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011903 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11904 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11905 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11906 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011907 return true;
11908
11909 return false;
11910}
11911
Matt Roperd21fbe82015-09-24 15:53:12 -070011912static bool needs_scaling(struct intel_plane_state *state)
11913{
11914 int src_w = drm_rect_width(&state->src) >> 16;
11915 int src_h = drm_rect_height(&state->src) >> 16;
11916 int dst_w = drm_rect_width(&state->dst);
11917 int dst_h = drm_rect_height(&state->dst);
11918
11919 return (src_w != dst_w || src_h != dst_h);
11920}
11921
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011922int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11923 struct drm_plane_state *plane_state)
11924{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011925 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011926 struct drm_crtc *crtc = crtc_state->crtc;
11927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11928 struct drm_plane *plane = plane_state->plane;
11929 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011930 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011931 struct intel_plane_state *old_plane_state =
11932 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011933 bool mode_changed = needs_modeset(crtc_state);
11934 bool was_crtc_enabled = crtc->state->active;
11935 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011936 bool turn_off, turn_on, visible, was_visible;
11937 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011938 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011939
Chris Wilson84114992016-07-02 15:36:06 +010011940 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011941 ret = skl_update_scaler_plane(
11942 to_intel_crtc_state(crtc_state),
11943 to_intel_plane_state(plane_state));
11944 if (ret)
11945 return ret;
11946 }
11947
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011948 was_visible = old_plane_state->visible;
11949 visible = to_intel_plane_state(plane_state)->visible;
11950
11951 if (!was_crtc_enabled && WARN_ON(was_visible))
11952 was_visible = false;
11953
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011954 /*
11955 * Visibility is calculated as if the crtc was on, but
11956 * after scaler setup everything depends on it being off
11957 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011958 *
11959 * FIXME this is wrong for watermarks. Watermarks should also
11960 * be computed as if the pipe would be active. Perhaps move
11961 * per-plane wm computation to the .check_plane() hook, and
11962 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011963 */
11964 if (!is_crtc_enabled)
11965 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011966
11967 if (!was_visible && !visible)
11968 return 0;
11969
Maarten Lankhorste8861672016-02-24 11:24:26 +010011970 if (fb != old_plane_state->base.fb)
11971 pipe_config->fb_changed = true;
11972
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011973 turn_off = was_visible && (!visible || mode_changed);
11974 turn_on = visible && (!was_visible || mode_changed);
11975
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011976 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011977 intel_crtc->base.base.id,
11978 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011979 plane->base.id, plane->name,
11980 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011981
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011982 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11983 plane->base.id, plane->name,
11984 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011985 turn_off, turn_on, mode_changed);
11986
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011987 if (turn_on) {
11988 pipe_config->update_wm_pre = true;
11989
11990 /* must disable cxsr around plane enable/disable */
11991 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11992 pipe_config->disable_cxsr = true;
11993 } else if (turn_off) {
11994 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011995
Ville Syrjälä852eb002015-06-24 22:00:07 +030011996 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011997 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011998 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011999 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012000 /* FIXME bollocks */
12001 pipe_config->update_wm_pre = true;
12002 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012003 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012004
Matt Ropered4a6a72016-02-23 17:20:13 -080012005 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012006 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12007 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012008 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12009
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012010 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012011 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012012
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012013 /*
12014 * WaCxSRDisabledForSpriteScaling:ivb
12015 *
12016 * cstate->update_wm was already set above, so this flag will
12017 * take effect when we commit and program watermarks.
12018 */
12019 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12020 needs_scaling(to_intel_plane_state(plane_state)) &&
12021 !needs_scaling(old_plane_state))
12022 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012023
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012024 return 0;
12025}
12026
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012027static bool encoders_cloneable(const struct intel_encoder *a,
12028 const struct intel_encoder *b)
12029{
12030 /* masks could be asymmetric, so check both ways */
12031 return a == b || (a->cloneable & (1 << b->type) &&
12032 b->cloneable & (1 << a->type));
12033}
12034
12035static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12036 struct intel_crtc *crtc,
12037 struct intel_encoder *encoder)
12038{
12039 struct intel_encoder *source_encoder;
12040 struct drm_connector *connector;
12041 struct drm_connector_state *connector_state;
12042 int i;
12043
12044 for_each_connector_in_state(state, connector, connector_state, i) {
12045 if (connector_state->crtc != &crtc->base)
12046 continue;
12047
12048 source_encoder =
12049 to_intel_encoder(connector_state->best_encoder);
12050 if (!encoders_cloneable(encoder, source_encoder))
12051 return false;
12052 }
12053
12054 return true;
12055}
12056
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012057static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12058 struct drm_crtc_state *crtc_state)
12059{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012060 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012061 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012063 struct intel_crtc_state *pipe_config =
12064 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012065 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012066 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012067 bool mode_changed = needs_modeset(crtc_state);
12068
Ville Syrjälä852eb002015-06-24 22:00:07 +030012069 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012070 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012071
Maarten Lankhorstad421372015-06-15 12:33:42 +020012072 if (mode_changed && crtc_state->enable &&
12073 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012074 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012075 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12076 pipe_config);
12077 if (ret)
12078 return ret;
12079 }
12080
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012081 if (crtc_state->color_mgmt_changed) {
12082 ret = intel_color_check(crtc, crtc_state);
12083 if (ret)
12084 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012085
12086 /*
12087 * Changing color management on Intel hardware is
12088 * handled as part of planes update.
12089 */
12090 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012091 }
12092
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012093 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012094 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012095 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012096 if (ret) {
12097 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012098 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012099 }
12100 }
12101
12102 if (dev_priv->display.compute_intermediate_wm &&
12103 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12104 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12105 return 0;
12106
12107 /*
12108 * Calculate 'intermediate' watermarks that satisfy both the
12109 * old state and the new state. We can program these
12110 * immediately.
12111 */
12112 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12113 intel_crtc,
12114 pipe_config);
12115 if (ret) {
12116 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12117 return ret;
12118 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012119 } else if (dev_priv->display.compute_intermediate_wm) {
12120 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12121 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012122 }
12123
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012124 if (INTEL_INFO(dev)->gen >= 9) {
12125 if (mode_changed)
12126 ret = skl_update_scaler_crtc(pipe_config);
12127
12128 if (!ret)
12129 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12130 pipe_config);
12131 }
12132
12133 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012134}
12135
Jani Nikula65b38e02015-04-13 11:26:56 +030012136static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012137 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012138 .atomic_begin = intel_begin_crtc_commit,
12139 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012140 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012141};
12142
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012143static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12144{
12145 struct intel_connector *connector;
12146
12147 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012148 if (connector->base.state->crtc)
12149 drm_connector_unreference(&connector->base);
12150
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012151 if (connector->base.encoder) {
12152 connector->base.state->best_encoder =
12153 connector->base.encoder;
12154 connector->base.state->crtc =
12155 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012156
12157 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012158 } else {
12159 connector->base.state->best_encoder = NULL;
12160 connector->base.state->crtc = NULL;
12161 }
12162 }
12163}
12164
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012165static void
Robin Schroereba905b2014-05-18 02:24:50 +020012166connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012167 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012168{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012169 int bpp = pipe_config->pipe_bpp;
12170
12171 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12172 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012173 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012174
12175 /* Don't use an invalid EDID bpc value */
12176 if (connector->base.display_info.bpc &&
12177 connector->base.display_info.bpc * 3 < bpp) {
12178 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12179 bpp, connector->base.display_info.bpc*3);
12180 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12181 }
12182
Jani Nikula013dd9e2016-01-13 16:35:20 +020012183 /* Clamp bpp to default limit on screens without EDID 1.4 */
12184 if (connector->base.display_info.bpc == 0) {
12185 int type = connector->base.connector_type;
12186 int clamp_bpp = 24;
12187
12188 /* Fall back to 18 bpp when DP sink capability is unknown. */
12189 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12190 type == DRM_MODE_CONNECTOR_eDP)
12191 clamp_bpp = 18;
12192
12193 if (bpp > clamp_bpp) {
12194 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12195 bpp, clamp_bpp);
12196 pipe_config->pipe_bpp = clamp_bpp;
12197 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012198 }
12199}
12200
12201static int
12202compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012203 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012204{
12205 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012206 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012207 struct drm_connector *connector;
12208 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012209 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012210
Wayne Boyer666a4532015-12-09 12:29:35 -080012211 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012212 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012213 else if (INTEL_INFO(dev)->gen >= 5)
12214 bpp = 12*3;
12215 else
12216 bpp = 8*3;
12217
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012218
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012219 pipe_config->pipe_bpp = bpp;
12220
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012221 state = pipe_config->base.state;
12222
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012223 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012224 for_each_connector_in_state(state, connector, connector_state, i) {
12225 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012226 continue;
12227
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012228 connected_sink_compute_bpp(to_intel_connector(connector),
12229 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012230 }
12231
12232 return bpp;
12233}
12234
Daniel Vetter644db712013-09-19 14:53:58 +020012235static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12236{
12237 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12238 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012239 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012240 mode->crtc_hdisplay, mode->crtc_hsync_start,
12241 mode->crtc_hsync_end, mode->crtc_htotal,
12242 mode->crtc_vdisplay, mode->crtc_vsync_start,
12243 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12244}
12245
Daniel Vetterc0b03412013-05-28 12:05:54 +020012246static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012247 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012248 const char *context)
12249{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012250 struct drm_device *dev = crtc->base.dev;
12251 struct drm_plane *plane;
12252 struct intel_plane *intel_plane;
12253 struct intel_plane_state *state;
12254 struct drm_framebuffer *fb;
12255
Ville Syrjälä78108b72016-05-27 20:59:19 +030012256 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12257 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012258 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012259
Jani Nikulada205632016-03-15 21:51:10 +020012260 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012261 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12262 pipe_config->pipe_bpp, pipe_config->dither);
12263 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12264 pipe_config->has_pch_encoder,
12265 pipe_config->fdi_lanes,
12266 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12267 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12268 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012269 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ä37a56502016-06-22 21:57:04 +030012270 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012271 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012272 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12273 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12274 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012275
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012276 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012277 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012278 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012279 pipe_config->dp_m2_n2.gmch_m,
12280 pipe_config->dp_m2_n2.gmch_n,
12281 pipe_config->dp_m2_n2.link_m,
12282 pipe_config->dp_m2_n2.link_n,
12283 pipe_config->dp_m2_n2.tu);
12284
Daniel Vetter55072d12014-11-20 16:10:28 +010012285 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12286 pipe_config->has_audio,
12287 pipe_config->has_infoframe);
12288
Daniel Vetterc0b03412013-05-28 12:05:54 +020012289 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012290 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012291 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012292 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12293 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012294 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012295 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12296 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012297 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12298 crtc->num_scalers,
12299 pipe_config->scaler_state.scaler_users,
12300 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012301 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12302 pipe_config->gmch_pfit.control,
12303 pipe_config->gmch_pfit.pgm_ratios,
12304 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012305 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012306 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012307 pipe_config->pch_pfit.size,
12308 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012309 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012310 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012311
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012312 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012313 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012314 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012315 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012316 pipe_config->ddi_pll_sel,
12317 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012318 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012319 pipe_config->dpll_hw_state.pll0,
12320 pipe_config->dpll_hw_state.pll1,
12321 pipe_config->dpll_hw_state.pll2,
12322 pipe_config->dpll_hw_state.pll3,
12323 pipe_config->dpll_hw_state.pll6,
12324 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012325 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012326 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012327 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012328 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012329 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12330 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12331 pipe_config->ddi_pll_sel,
12332 pipe_config->dpll_hw_state.ctrl1,
12333 pipe_config->dpll_hw_state.cfgcr1,
12334 pipe_config->dpll_hw_state.cfgcr2);
12335 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012336 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 +010012337 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012338 pipe_config->dpll_hw_state.wrpll,
12339 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012340 } else {
12341 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12342 "fp0: 0x%x, fp1: 0x%x\n",
12343 pipe_config->dpll_hw_state.dpll,
12344 pipe_config->dpll_hw_state.dpll_md,
12345 pipe_config->dpll_hw_state.fp0,
12346 pipe_config->dpll_hw_state.fp1);
12347 }
12348
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012349 DRM_DEBUG_KMS("planes on this crtc\n");
12350 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12351 intel_plane = to_intel_plane(plane);
12352 if (intel_plane->pipe != crtc->pipe)
12353 continue;
12354
12355 state = to_intel_plane_state(plane->state);
12356 fb = state->base.fb;
12357 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012358 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12359 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012360 continue;
12361 }
12362
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012363 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12364 plane->base.id, plane->name);
12365 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12366 fb->base.id, fb->width, fb->height,
12367 drm_get_format_name(fb->pixel_format));
12368 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12369 state->scaler_id,
12370 state->src.x1 >> 16, state->src.y1 >> 16,
12371 drm_rect_width(&state->src) >> 16,
12372 drm_rect_height(&state->src) >> 16,
12373 state->dst.x1, state->dst.y1,
12374 drm_rect_width(&state->dst),
12375 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012376 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012377}
12378
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012379static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012380{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012381 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012382 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012383 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012384 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012385
12386 /*
12387 * Walk the connector list instead of the encoder
12388 * list to detect the problem on ddi platforms
12389 * where there's just one encoder per digital port.
12390 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012391 drm_for_each_connector(connector, dev) {
12392 struct drm_connector_state *connector_state;
12393 struct intel_encoder *encoder;
12394
12395 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12396 if (!connector_state)
12397 connector_state = connector->state;
12398
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012399 if (!connector_state->best_encoder)
12400 continue;
12401
12402 encoder = to_intel_encoder(connector_state->best_encoder);
12403
12404 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012405
12406 switch (encoder->type) {
12407 unsigned int port_mask;
12408 case INTEL_OUTPUT_UNKNOWN:
12409 if (WARN_ON(!HAS_DDI(dev)))
12410 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012411 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012412 case INTEL_OUTPUT_HDMI:
12413 case INTEL_OUTPUT_EDP:
12414 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12415
12416 /* the same port mustn't appear more than once */
12417 if (used_ports & port_mask)
12418 return false;
12419
12420 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012421 break;
12422 case INTEL_OUTPUT_DP_MST:
12423 used_mst_ports |=
12424 1 << enc_to_mst(&encoder->base)->primary->port;
12425 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012426 default:
12427 break;
12428 }
12429 }
12430
Ville Syrjälä477321e2016-07-28 17:50:40 +030012431 /* can't mix MST and SST/HDMI on the same port */
12432 if (used_ports & used_mst_ports)
12433 return false;
12434
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012435 return true;
12436}
12437
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012438static void
12439clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12440{
12441 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012442 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012443 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012444 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012445 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012446 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012447
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012448 /* FIXME: before the switch to atomic started, a new pipe_config was
12449 * kzalloc'd. Code that depends on any field being zero should be
12450 * fixed, so that the crtc_state can be safely duplicated. For now,
12451 * only fields that are know to not cause problems are preserved. */
12452
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012453 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012454 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012455 shared_dpll = crtc_state->shared_dpll;
12456 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012457 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012458 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012459
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012460 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012461
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012462 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012463 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012464 crtc_state->shared_dpll = shared_dpll;
12465 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012466 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012467 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012468}
12469
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012470static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012471intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012472 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012473{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012474 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012475 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012476 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012477 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012478 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012479 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012480 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012481
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012482 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012483
Daniel Vettere143a212013-07-04 12:01:15 +020012484 pipe_config->cpu_transcoder =
12485 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012486
Imre Deak2960bc92013-07-30 13:36:32 +030012487 /*
12488 * Sanitize sync polarity flags based on requested ones. If neither
12489 * positive or negative polarity is requested, treat this as meaning
12490 * negative polarity.
12491 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012492 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012493 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012494 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012495
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012496 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012497 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012498 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012499
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012500 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12501 pipe_config);
12502 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012503 goto fail;
12504
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012505 /*
12506 * Determine the real pipe dimensions. Note that stereo modes can
12507 * increase the actual pipe size due to the frame doubling and
12508 * insertion of additional space for blanks between the frame. This
12509 * is stored in the crtc timings. We use the requested mode to do this
12510 * computation to clearly distinguish it from the adjusted mode, which
12511 * can be changed by the connectors in the below retry loop.
12512 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012513 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012514 &pipe_config->pipe_src_w,
12515 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012516
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012517 for_each_connector_in_state(state, connector, connector_state, i) {
12518 if (connector_state->crtc != crtc)
12519 continue;
12520
12521 encoder = to_intel_encoder(connector_state->best_encoder);
12522
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012523 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12524 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12525 goto fail;
12526 }
12527
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012528 /*
12529 * Determine output_types before calling the .compute_config()
12530 * hooks so that the hooks can use this information safely.
12531 */
12532 pipe_config->output_types |= 1 << encoder->type;
12533 }
12534
Daniel Vettere29c22c2013-02-21 00:00:16 +010012535encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012536 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012537 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012538 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012539
Daniel Vetter135c81b2013-07-21 21:37:09 +020012540 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012541 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12542 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012543
Daniel Vetter7758a112012-07-08 19:40:39 +020012544 /* Pass our mode to the connectors and the CRTC to give them a chance to
12545 * adjust it according to limitations or connector properties, and also
12546 * a chance to reject the mode entirely.
12547 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012548 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012549 if (connector_state->crtc != crtc)
12550 continue;
12551
12552 encoder = to_intel_encoder(connector_state->best_encoder);
12553
Daniel Vetterefea6e82013-07-21 21:36:59 +020012554 if (!(encoder->compute_config(encoder, pipe_config))) {
12555 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012556 goto fail;
12557 }
12558 }
12559
Daniel Vetterff9a6752013-06-01 17:16:21 +020012560 /* Set default port clock if not overwritten by the encoder. Needs to be
12561 * done afterwards in case the encoder adjusts the mode. */
12562 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012563 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012564 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012565
Daniel Vettera43f6e02013-06-07 23:10:32 +020012566 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012567 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012568 DRM_DEBUG_KMS("CRTC fixup failed\n");
12569 goto fail;
12570 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012571
12572 if (ret == RETRY) {
12573 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12574 ret = -EINVAL;
12575 goto fail;
12576 }
12577
12578 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12579 retry = false;
12580 goto encoder_retry;
12581 }
12582
Daniel Vettere8fa4272015-08-12 11:43:34 +020012583 /* Dithering seems to not pass-through bits correctly when it should, so
12584 * only enable it on 6bpc panels. */
12585 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012586 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012587 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012588
Daniel Vetter7758a112012-07-08 19:40:39 +020012589fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012590 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012591}
12592
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012593static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012594intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012595{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012596 struct drm_crtc *crtc;
12597 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012598 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012599
Ville Syrjälä76688512014-01-10 11:28:06 +020012600 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012601 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012602 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012603
12604 /* Update hwmode for vblank functions */
12605 if (crtc->state->active)
12606 crtc->hwmode = crtc->state->adjusted_mode;
12607 else
12608 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012609
12610 /*
12611 * Update legacy state to satisfy fbc code. This can
12612 * be removed when fbc uses the atomic state.
12613 */
12614 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12615 struct drm_plane_state *plane_state = crtc->primary->state;
12616
12617 crtc->primary->fb = plane_state->fb;
12618 crtc->x = plane_state->src_x >> 16;
12619 crtc->y = plane_state->src_y >> 16;
12620 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012621 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012622}
12623
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012624static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012625{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012626 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012627
12628 if (clock1 == clock2)
12629 return true;
12630
12631 if (!clock1 || !clock2)
12632 return false;
12633
12634 diff = abs(clock1 - clock2);
12635
12636 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12637 return true;
12638
12639 return false;
12640}
12641
Daniel Vetter25c5b262012-07-08 22:08:04 +020012642#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12643 list_for_each_entry((intel_crtc), \
12644 &(dev)->mode_config.crtc_list, \
12645 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012646 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012647
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012648static bool
12649intel_compare_m_n(unsigned int m, unsigned int n,
12650 unsigned int m2, unsigned int n2,
12651 bool exact)
12652{
12653 if (m == m2 && n == n2)
12654 return true;
12655
12656 if (exact || !m || !n || !m2 || !n2)
12657 return false;
12658
12659 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12660
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012661 if (n > n2) {
12662 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012663 m2 <<= 1;
12664 n2 <<= 1;
12665 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012666 } else if (n < n2) {
12667 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012668 m <<= 1;
12669 n <<= 1;
12670 }
12671 }
12672
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012673 if (n != n2)
12674 return false;
12675
12676 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012677}
12678
12679static bool
12680intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12681 struct intel_link_m_n *m2_n2,
12682 bool adjust)
12683{
12684 if (m_n->tu == m2_n2->tu &&
12685 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12686 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12687 intel_compare_m_n(m_n->link_m, m_n->link_n,
12688 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12689 if (adjust)
12690 *m2_n2 = *m_n;
12691
12692 return true;
12693 }
12694
12695 return false;
12696}
12697
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012698static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012699intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012700 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012701 struct intel_crtc_state *pipe_config,
12702 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012703{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012704 bool ret = true;
12705
12706#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12707 do { \
12708 if (!adjust) \
12709 DRM_ERROR(fmt, ##__VA_ARGS__); \
12710 else \
12711 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12712 } while (0)
12713
Daniel Vetter66e985c2013-06-05 13:34:20 +020012714#define PIPE_CONF_CHECK_X(name) \
12715 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012716 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012717 "(expected 0x%08x, found 0x%08x)\n", \
12718 current_config->name, \
12719 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012720 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012721 }
12722
Daniel Vetter08a24032013-04-19 11:25:34 +020012723#define PIPE_CONF_CHECK_I(name) \
12724 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012725 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012726 "(expected %i, found %i)\n", \
12727 current_config->name, \
12728 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012729 ret = false; \
12730 }
12731
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012732#define PIPE_CONF_CHECK_P(name) \
12733 if (current_config->name != pipe_config->name) { \
12734 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12735 "(expected %p, found %p)\n", \
12736 current_config->name, \
12737 pipe_config->name); \
12738 ret = false; \
12739 }
12740
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012741#define PIPE_CONF_CHECK_M_N(name) \
12742 if (!intel_compare_link_m_n(&current_config->name, \
12743 &pipe_config->name,\
12744 adjust)) { \
12745 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12746 "(expected tu %i gmch %i/%i link %i/%i, " \
12747 "found tu %i, gmch %i/%i link %i/%i)\n", \
12748 current_config->name.tu, \
12749 current_config->name.gmch_m, \
12750 current_config->name.gmch_n, \
12751 current_config->name.link_m, \
12752 current_config->name.link_n, \
12753 pipe_config->name.tu, \
12754 pipe_config->name.gmch_m, \
12755 pipe_config->name.gmch_n, \
12756 pipe_config->name.link_m, \
12757 pipe_config->name.link_n); \
12758 ret = false; \
12759 }
12760
Daniel Vetter55c561a2016-03-30 11:34:36 +020012761/* This is required for BDW+ where there is only one set of registers for
12762 * switching between high and low RR.
12763 * This macro can be used whenever a comparison has to be made between one
12764 * hw state and multiple sw state variables.
12765 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012766#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12767 if (!intel_compare_link_m_n(&current_config->name, \
12768 &pipe_config->name, adjust) && \
12769 !intel_compare_link_m_n(&current_config->alt_name, \
12770 &pipe_config->name, adjust)) { \
12771 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12772 "(expected tu %i gmch %i/%i link %i/%i, " \
12773 "or tu %i gmch %i/%i link %i/%i, " \
12774 "found tu %i, gmch %i/%i link %i/%i)\n", \
12775 current_config->name.tu, \
12776 current_config->name.gmch_m, \
12777 current_config->name.gmch_n, \
12778 current_config->name.link_m, \
12779 current_config->name.link_n, \
12780 current_config->alt_name.tu, \
12781 current_config->alt_name.gmch_m, \
12782 current_config->alt_name.gmch_n, \
12783 current_config->alt_name.link_m, \
12784 current_config->alt_name.link_n, \
12785 pipe_config->name.tu, \
12786 pipe_config->name.gmch_m, \
12787 pipe_config->name.gmch_n, \
12788 pipe_config->name.link_m, \
12789 pipe_config->name.link_n); \
12790 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012791 }
12792
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012793#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12794 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012795 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012796 "(expected %i, found %i)\n", \
12797 current_config->name & (mask), \
12798 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012799 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012800 }
12801
Ville Syrjälä5e550652013-09-06 23:29:07 +030012802#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12803 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012804 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012805 "(expected %i, found %i)\n", \
12806 current_config->name, \
12807 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012808 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012809 }
12810
Daniel Vetterbb760062013-06-06 14:55:52 +020012811#define PIPE_CONF_QUIRK(quirk) \
12812 ((current_config->quirks | pipe_config->quirks) & (quirk))
12813
Daniel Vettereccb1402013-05-22 00:50:22 +020012814 PIPE_CONF_CHECK_I(cpu_transcoder);
12815
Daniel Vetter08a24032013-04-19 11:25:34 +020012816 PIPE_CONF_CHECK_I(has_pch_encoder);
12817 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012818 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012819
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012820 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012821 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012822
12823 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012824 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012825
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012826 if (current_config->has_drrs)
12827 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12828 } else
12829 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012830
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012831 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012832
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012833 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12834 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12835 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12836 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12837 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12838 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012839
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012840 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12841 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12842 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12843 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12844 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12845 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012846
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012847 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012848 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012849 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012850 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012851 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012852 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012853
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012854 PIPE_CONF_CHECK_I(has_audio);
12855
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012856 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012857 DRM_MODE_FLAG_INTERLACE);
12858
Daniel Vetterbb760062013-06-06 14:55:52 +020012859 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012860 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012861 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012862 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012863 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012864 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012865 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012866 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012867 DRM_MODE_FLAG_NVSYNC);
12868 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012869
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012870 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012871 /* pfit ratios are autocomputed by the hw on gen4+ */
12872 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012873 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012874 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012875
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012876 if (!adjust) {
12877 PIPE_CONF_CHECK_I(pipe_src_w);
12878 PIPE_CONF_CHECK_I(pipe_src_h);
12879
12880 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12881 if (current_config->pch_pfit.enabled) {
12882 PIPE_CONF_CHECK_X(pch_pfit.pos);
12883 PIPE_CONF_CHECK_X(pch_pfit.size);
12884 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012885
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012886 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12887 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012888
Jesse Barnese59150d2014-01-07 13:30:45 -080012889 /* BDW+ don't expose a synchronous way to read the state */
12890 if (IS_HASWELL(dev))
12891 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012892
Ville Syrjälä282740f2013-09-04 18:30:03 +030012893 PIPE_CONF_CHECK_I(double_wide);
12894
Daniel Vetter26804af2014-06-25 22:01:55 +030012895 PIPE_CONF_CHECK_X(ddi_pll_sel);
12896
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012897 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012898 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012899 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012900 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12901 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012902 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012903 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012904 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12905 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12906 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012907
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012908 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12909 PIPE_CONF_CHECK_X(dsi_pll.div);
12910
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012911 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12912 PIPE_CONF_CHECK_I(pipe_bpp);
12913
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012914 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012915 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012916
Daniel Vetter66e985c2013-06-05 13:34:20 +020012917#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012918#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012919#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012920#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012921#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012922#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012923#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012924
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012925 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012926}
12927
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012928static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12929 const struct intel_crtc_state *pipe_config)
12930{
12931 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012932 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012933 &pipe_config->fdi_m_n);
12934 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12935
12936 /*
12937 * FDI already provided one idea for the dotclock.
12938 * Yell if the encoder disagrees.
12939 */
12940 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12941 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12942 fdi_dotclock, dotclock);
12943 }
12944}
12945
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012946static void verify_wm_state(struct drm_crtc *crtc,
12947 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012948{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012949 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012950 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012951 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012952 struct skl_ddb_entry *hw_entry, *sw_entry;
12953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12954 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012955 int plane;
12956
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012957 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012958 return;
12959
12960 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12961 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12962
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012963 /* planes */
12964 for_each_plane(dev_priv, pipe, plane) {
12965 hw_entry = &hw_ddb.plane[pipe][plane];
12966 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012967
12968 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12969 continue;
12970
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012971 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12972 "(expected (%u,%u), found (%u,%u))\n",
12973 pipe_name(pipe), plane + 1,
12974 sw_entry->start, sw_entry->end,
12975 hw_entry->start, hw_entry->end);
12976 }
12977
12978 /* cursor */
12979 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12980 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12981
12982 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012983 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12984 "(expected (%u,%u), found (%u,%u))\n",
12985 pipe_name(pipe),
12986 sw_entry->start, sw_entry->end,
12987 hw_entry->start, hw_entry->end);
12988 }
12989}
12990
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012991static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012992verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012993{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012994 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012995
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012996 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012997 struct drm_encoder *encoder = connector->encoder;
12998 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012999
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013000 if (state->crtc != crtc)
13001 continue;
13002
Daniel Vetter5a21b662016-05-24 17:13:53 +020013003 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013004
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013005 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013006 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013007 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013008}
13009
13010static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013011verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013012{
13013 struct intel_encoder *encoder;
13014 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013015
Damien Lespiaub2784e12014-08-05 11:29:37 +010013016 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013017 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013018 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013019
13020 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13021 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013022 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013023
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013024 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013025 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013026 continue;
13027 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013028
13029 I915_STATE_WARN(connector->base.state->crtc !=
13030 encoder->base.crtc,
13031 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013032 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013033
Rob Clarke2c719b2014-12-15 13:56:32 -050013034 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013035 "encoder's enabled state mismatch "
13036 "(expected %i, found %i)\n",
13037 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013038
13039 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013040 bool active;
13041
13042 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013043 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013044 "encoder detached but still enabled on pipe %c.\n",
13045 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013046 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013047 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013048}
13049
13050static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013051verify_crtc_state(struct drm_crtc *crtc,
13052 struct drm_crtc_state *old_crtc_state,
13053 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013054{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013055 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013056 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013057 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13059 struct intel_crtc_state *pipe_config, *sw_config;
13060 struct drm_atomic_state *old_state;
13061 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013062
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013063 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013064 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013065 pipe_config = to_intel_crtc_state(old_crtc_state);
13066 memset(pipe_config, 0, sizeof(*pipe_config));
13067 pipe_config->base.crtc = crtc;
13068 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013069
Ville Syrjälä78108b72016-05-27 20:59:19 +030013070 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013071
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013072 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013073
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013074 /* hw state is inconsistent with the pipe quirk */
13075 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13076 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13077 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013078
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013079 I915_STATE_WARN(new_crtc_state->active != active,
13080 "crtc active state doesn't match with hw state "
13081 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013082
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013083 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13084 "transitional active state does not match atomic hw state "
13085 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013086
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013087 for_each_encoder_on_crtc(dev, crtc, encoder) {
13088 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013089
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013090 active = encoder->get_hw_state(encoder, &pipe);
13091 I915_STATE_WARN(active != new_crtc_state->active,
13092 "[ENCODER:%i] active %i with crtc active %i\n",
13093 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013094
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013095 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13096 "Encoder connected to wrong pipe %c\n",
13097 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013098
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013099 if (active) {
13100 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013101 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013102 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013103 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013104
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013105 if (!new_crtc_state->active)
13106 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013107
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013108 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013109
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013110 sw_config = to_intel_crtc_state(crtc->state);
13111 if (!intel_pipe_config_compare(dev, sw_config,
13112 pipe_config, false)) {
13113 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13114 intel_dump_pipe_config(intel_crtc, pipe_config,
13115 "[hw state]");
13116 intel_dump_pipe_config(intel_crtc, sw_config,
13117 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013118 }
13119}
13120
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013121static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013122verify_single_dpll_state(struct drm_i915_private *dev_priv,
13123 struct intel_shared_dpll *pll,
13124 struct drm_crtc *crtc,
13125 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013126{
13127 struct intel_dpll_hw_state dpll_hw_state;
13128 unsigned crtc_mask;
13129 bool active;
13130
13131 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13132
13133 DRM_DEBUG_KMS("%s\n", pll->name);
13134
13135 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13136
13137 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13138 I915_STATE_WARN(!pll->on && pll->active_mask,
13139 "pll in active use but not on in sw tracking\n");
13140 I915_STATE_WARN(pll->on && !pll->active_mask,
13141 "pll is on but not used by any active crtc\n");
13142 I915_STATE_WARN(pll->on != active,
13143 "pll on state mismatch (expected %i, found %i)\n",
13144 pll->on, active);
13145 }
13146
13147 if (!crtc) {
13148 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13149 "more active pll users than references: %x vs %x\n",
13150 pll->active_mask, pll->config.crtc_mask);
13151
13152 return;
13153 }
13154
13155 crtc_mask = 1 << drm_crtc_index(crtc);
13156
13157 if (new_state->active)
13158 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13159 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13160 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13161 else
13162 I915_STATE_WARN(pll->active_mask & crtc_mask,
13163 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13164 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13165
13166 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13167 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13168 crtc_mask, pll->config.crtc_mask);
13169
13170 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13171 &dpll_hw_state,
13172 sizeof(dpll_hw_state)),
13173 "pll hw state mismatch\n");
13174}
13175
13176static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013177verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13178 struct drm_crtc_state *old_crtc_state,
13179 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013180{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013181 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013182 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13183 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13184
13185 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013186 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013187
13188 if (old_state->shared_dpll &&
13189 old_state->shared_dpll != new_state->shared_dpll) {
13190 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13191 struct intel_shared_dpll *pll = old_state->shared_dpll;
13192
13193 I915_STATE_WARN(pll->active_mask & crtc_mask,
13194 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13195 pipe_name(drm_crtc_index(crtc)));
13196 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13197 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13198 pipe_name(drm_crtc_index(crtc)));
13199 }
13200}
13201
13202static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013203intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013204 struct drm_crtc_state *old_state,
13205 struct drm_crtc_state *new_state)
13206{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013207 if (!needs_modeset(new_state) &&
13208 !to_intel_crtc_state(new_state)->update_pipe)
13209 return;
13210
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013211 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013212 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013213 verify_crtc_state(crtc, old_state, new_state);
13214 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013215}
13216
13217static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013218verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013219{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013220 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013221 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013222
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013223 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013224 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013225}
Daniel Vetter53589012013-06-05 13:34:16 +020013226
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013227static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013228intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013229{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013230 verify_encoder_state(dev);
13231 verify_connector_state(dev, NULL);
13232 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013233}
13234
Ville Syrjälä80715b22014-05-15 20:23:23 +030013235static void update_scanline_offset(struct intel_crtc *crtc)
13236{
13237 struct drm_device *dev = crtc->base.dev;
13238
13239 /*
13240 * The scanline counter increments at the leading edge of hsync.
13241 *
13242 * On most platforms it starts counting from vtotal-1 on the
13243 * first active line. That means the scanline counter value is
13244 * always one less than what we would expect. Ie. just after
13245 * start of vblank, which also occurs at start of hsync (on the
13246 * last active line), the scanline counter will read vblank_start-1.
13247 *
13248 * On gen2 the scanline counter starts counting from 1 instead
13249 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13250 * to keep the value positive), instead of adding one.
13251 *
13252 * On HSW+ the behaviour of the scanline counter depends on the output
13253 * type. For DP ports it behaves like most other platforms, but on HDMI
13254 * there's an extra 1 line difference. So we need to add two instead of
13255 * one to the value.
13256 */
13257 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013258 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013259 int vtotal;
13260
Ville Syrjälä124abe02015-09-08 13:40:45 +030013261 vtotal = adjusted_mode->crtc_vtotal;
13262 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013263 vtotal /= 2;
13264
13265 crtc->scanline_offset = vtotal - 1;
13266 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013267 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013268 crtc->scanline_offset = 2;
13269 } else
13270 crtc->scanline_offset = 1;
13271}
13272
Maarten Lankhorstad421372015-06-15 12:33:42 +020013273static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013274{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013275 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013276 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013277 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013278 struct drm_crtc *crtc;
13279 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013280 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013281
13282 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013283 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013284
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013285 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013287 struct intel_shared_dpll *old_dpll =
13288 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013289
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013290 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013291 continue;
13292
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013293 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013294
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013295 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013296 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013297
Maarten Lankhorstad421372015-06-15 12:33:42 +020013298 if (!shared_dpll)
13299 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13300
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013301 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013302 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013303}
13304
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013305/*
13306 * This implements the workaround described in the "notes" section of the mode
13307 * set sequence documentation. When going from no pipes or single pipe to
13308 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13309 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13310 */
13311static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13312{
13313 struct drm_crtc_state *crtc_state;
13314 struct intel_crtc *intel_crtc;
13315 struct drm_crtc *crtc;
13316 struct intel_crtc_state *first_crtc_state = NULL;
13317 struct intel_crtc_state *other_crtc_state = NULL;
13318 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13319 int i;
13320
13321 /* look at all crtc's that are going to be enabled in during modeset */
13322 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13323 intel_crtc = to_intel_crtc(crtc);
13324
13325 if (!crtc_state->active || !needs_modeset(crtc_state))
13326 continue;
13327
13328 if (first_crtc_state) {
13329 other_crtc_state = to_intel_crtc_state(crtc_state);
13330 break;
13331 } else {
13332 first_crtc_state = to_intel_crtc_state(crtc_state);
13333 first_pipe = intel_crtc->pipe;
13334 }
13335 }
13336
13337 /* No workaround needed? */
13338 if (!first_crtc_state)
13339 return 0;
13340
13341 /* w/a possibly needed, check how many crtc's are already enabled. */
13342 for_each_intel_crtc(state->dev, intel_crtc) {
13343 struct intel_crtc_state *pipe_config;
13344
13345 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13346 if (IS_ERR(pipe_config))
13347 return PTR_ERR(pipe_config);
13348
13349 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13350
13351 if (!pipe_config->base.active ||
13352 needs_modeset(&pipe_config->base))
13353 continue;
13354
13355 /* 2 or more enabled crtcs means no need for w/a */
13356 if (enabled_pipe != INVALID_PIPE)
13357 return 0;
13358
13359 enabled_pipe = intel_crtc->pipe;
13360 }
13361
13362 if (enabled_pipe != INVALID_PIPE)
13363 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13364 else if (other_crtc_state)
13365 other_crtc_state->hsw_workaround_pipe = first_pipe;
13366
13367 return 0;
13368}
13369
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013370static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13371{
13372 struct drm_crtc *crtc;
13373 struct drm_crtc_state *crtc_state;
13374 int ret = 0;
13375
13376 /* add all active pipes to the state */
13377 for_each_crtc(state->dev, crtc) {
13378 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13379 if (IS_ERR(crtc_state))
13380 return PTR_ERR(crtc_state);
13381
13382 if (!crtc_state->active || needs_modeset(crtc_state))
13383 continue;
13384
13385 crtc_state->mode_changed = true;
13386
13387 ret = drm_atomic_add_affected_connectors(state, crtc);
13388 if (ret)
13389 break;
13390
13391 ret = drm_atomic_add_affected_planes(state, crtc);
13392 if (ret)
13393 break;
13394 }
13395
13396 return ret;
13397}
13398
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013399static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013400{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013401 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013402 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013403 struct drm_crtc *crtc;
13404 struct drm_crtc_state *crtc_state;
13405 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013406
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013407 if (!check_digital_port_conflicts(state)) {
13408 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13409 return -EINVAL;
13410 }
13411
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013412 intel_state->modeset = true;
13413 intel_state->active_crtcs = dev_priv->active_crtcs;
13414
13415 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13416 if (crtc_state->active)
13417 intel_state->active_crtcs |= 1 << i;
13418 else
13419 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013420
13421 if (crtc_state->active != crtc->state->active)
13422 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013423 }
13424
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013425 /*
13426 * See if the config requires any additional preparation, e.g.
13427 * to adjust global state with pipes off. We need to do this
13428 * here so we can get the modeset_pipe updated config for the new
13429 * mode set on this crtc. For other crtcs we need to use the
13430 * adjusted_mode bits in the crtc directly.
13431 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013432 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013433 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013434 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013435 if (!intel_state->cdclk_pll_vco)
13436 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013437
Clint Taylorc89e39f2016-05-13 23:41:21 +030013438 ret = dev_priv->display.modeset_calc_cdclk(state);
13439 if (ret < 0)
13440 return ret;
13441
13442 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013443 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013444 ret = intel_modeset_all_pipes(state);
13445
13446 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013447 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013448
13449 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13450 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013451 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013452 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013453
Maarten Lankhorstad421372015-06-15 12:33:42 +020013454 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013455
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013456 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013457 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013458
Maarten Lankhorstad421372015-06-15 12:33:42 +020013459 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013460}
13461
Matt Roperaa363132015-09-24 15:53:18 -070013462/*
13463 * Handle calculation of various watermark data at the end of the atomic check
13464 * phase. The code here should be run after the per-crtc and per-plane 'check'
13465 * handlers to ensure that all derived state has been updated.
13466 */
Matt Roper55994c22016-05-12 07:06:08 -070013467static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013468{
13469 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013470 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013471
13472 /* Is there platform-specific watermark information to calculate? */
13473 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013474 return dev_priv->display.compute_global_watermarks(state);
13475
13476 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013477}
13478
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013479/**
13480 * intel_atomic_check - validate state object
13481 * @dev: drm device
13482 * @state: state to validate
13483 */
13484static int intel_atomic_check(struct drm_device *dev,
13485 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013486{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013487 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013488 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013489 struct drm_crtc *crtc;
13490 struct drm_crtc_state *crtc_state;
13491 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013492 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013493
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013494 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013495 if (ret)
13496 return ret;
13497
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013498 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013499 struct intel_crtc_state *pipe_config =
13500 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013501
13502 /* Catch I915_MODE_FLAG_INHERITED */
13503 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13504 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013505
Daniel Vetter26495482015-07-15 14:15:52 +020013506 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013507 continue;
13508
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013509 if (!crtc_state->enable) {
13510 any_ms = true;
13511 continue;
13512 }
13513
Daniel Vetter26495482015-07-15 14:15:52 +020013514 /* FIXME: For only active_changed we shouldn't need to do any
13515 * state recomputation at all. */
13516
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013517 ret = drm_atomic_add_affected_connectors(state, crtc);
13518 if (ret)
13519 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013520
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013521 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013522 if (ret) {
13523 intel_dump_pipe_config(to_intel_crtc(crtc),
13524 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013525 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013526 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013527
Jani Nikula73831232015-11-19 10:26:30 +020013528 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013529 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013530 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013531 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013532 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013533 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013534 }
13535
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013536 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013537 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013538
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013539 ret = drm_atomic_add_affected_planes(state, crtc);
13540 if (ret)
13541 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013542
Daniel Vetter26495482015-07-15 14:15:52 +020013543 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13544 needs_modeset(crtc_state) ?
13545 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013546 }
13547
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013548 if (any_ms) {
13549 ret = intel_modeset_checks(state);
13550
13551 if (ret)
13552 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013553 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013554 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013555
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013556 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013557 if (ret)
13558 return ret;
13559
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013560 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013561 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013562}
13563
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013564static int intel_atomic_prepare_commit(struct drm_device *dev,
13565 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013566 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013567{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013568 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013569 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013570 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013571 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013572 struct drm_crtc *crtc;
13573 int i, ret;
13574
Daniel Vetter5a21b662016-05-24 17:13:53 +020013575 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13576 if (state->legacy_cursor_update)
13577 continue;
13578
13579 ret = intel_crtc_wait_for_pending_flips(crtc);
13580 if (ret)
13581 return ret;
13582
13583 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13584 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013585 }
13586
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013587 ret = mutex_lock_interruptible(&dev->struct_mutex);
13588 if (ret)
13589 return ret;
13590
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013591 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013592 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013593
Dave Airlie21daaee2016-05-05 09:56:30 +100013594 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013595 for_each_plane_in_state(state, plane, plane_state, i) {
13596 struct intel_plane_state *intel_plane_state =
13597 to_intel_plane_state(plane_state);
13598
13599 if (!intel_plane_state->wait_req)
13600 continue;
13601
Chris Wilson776f3232016-08-04 07:52:40 +010013602 ret = i915_wait_request(intel_plane_state->wait_req,
13603 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013604 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013605 /* Any hang should be swallowed by the wait */
13606 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013607 mutex_lock(&dev->struct_mutex);
13608 drm_atomic_helper_cleanup_planes(dev, state);
13609 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013610 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013611 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013612 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013613 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013614
13615 return ret;
13616}
13617
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013618u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13619{
13620 struct drm_device *dev = crtc->base.dev;
13621
13622 if (!dev->max_vblank_count)
13623 return drm_accurate_vblank_count(&crtc->base);
13624
13625 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13626}
13627
Daniel Vetter5a21b662016-05-24 17:13:53 +020013628static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13629 struct drm_i915_private *dev_priv,
13630 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013631{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013632 unsigned last_vblank_count[I915_MAX_PIPES];
13633 enum pipe pipe;
13634 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013635
Daniel Vetter5a21b662016-05-24 17:13:53 +020013636 if (!crtc_mask)
13637 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013638
Daniel Vetter5a21b662016-05-24 17:13:53 +020013639 for_each_pipe(dev_priv, pipe) {
13640 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013641
Daniel Vetter5a21b662016-05-24 17:13:53 +020013642 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013643 continue;
13644
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013645 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013646 if (WARN_ON(ret != 0)) {
13647 crtc_mask &= ~(1 << pipe);
13648 continue;
13649 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013650
Daniel Vetter5a21b662016-05-24 17:13:53 +020013651 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13652 }
13653
13654 for_each_pipe(dev_priv, pipe) {
13655 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13656 long lret;
13657
13658 if (!((1 << pipe) & crtc_mask))
13659 continue;
13660
13661 lret = wait_event_timeout(dev->vblank[pipe].queue,
13662 last_vblank_count[pipe] !=
13663 drm_crtc_vblank_count(crtc),
13664 msecs_to_jiffies(50));
13665
13666 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13667
13668 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013669 }
13670}
13671
Daniel Vetter5a21b662016-05-24 17:13:53 +020013672static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013673{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013674 /* fb updated, need to unpin old fb */
13675 if (crtc_state->fb_changed)
13676 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013677
Daniel Vetter5a21b662016-05-24 17:13:53 +020013678 /* wm changes, need vblank before final wm's */
13679 if (crtc_state->update_wm_post)
13680 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013681
Daniel Vetter5a21b662016-05-24 17:13:53 +020013682 /*
13683 * cxsr is re-enabled after vblank.
13684 * This is already handled by crtc_state->update_wm_post,
13685 * but added for clarity.
13686 */
13687 if (crtc_state->disable_cxsr)
13688 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013689
Daniel Vetter5a21b662016-05-24 17:13:53 +020013690 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013691}
13692
Daniel Vetter94f05022016-06-14 18:01:00 +020013693static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013694{
Daniel Vetter94f05022016-06-14 18:01:00 +020013695 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013696 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013697 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013698 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013699 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013700 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013701 struct drm_plane *plane;
13702 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013703 bool hw_check = intel_state->modeset;
13704 unsigned long put_domains[I915_MAX_PIPES] = {};
13705 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013706 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013707
Daniel Vetter94f05022016-06-14 18:01:00 +020013708 for_each_plane_in_state(state, plane, plane_state, i) {
13709 struct intel_plane_state *intel_plane_state =
13710 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013711
Daniel Vetter94f05022016-06-14 18:01:00 +020013712 if (!intel_plane_state->wait_req)
13713 continue;
13714
Chris Wilson776f3232016-08-04 07:52:40 +010013715 ret = i915_wait_request(intel_plane_state->wait_req,
13716 true, NULL, NULL);
Daniel Vetter94f05022016-06-14 18:01:00 +020013717 /* EIO should be eaten, and we can't get interrupted in the
13718 * worker, and blocking commits have waited already. */
13719 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013720 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013721
Daniel Vetterea0000f2016-06-13 16:13:46 +020013722 drm_atomic_helper_wait_for_dependencies(state);
13723
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013724 if (intel_state->modeset) {
13725 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13726 sizeof(intel_state->min_pixclk));
13727 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013728 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013729
13730 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013731 }
13732
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013733 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13735
Daniel Vetter5a21b662016-05-24 17:13:53 +020013736 if (needs_modeset(crtc->state) ||
13737 to_intel_crtc_state(crtc->state)->update_pipe) {
13738 hw_check = true;
13739
13740 put_domains[to_intel_crtc(crtc)->pipe] =
13741 modeset_get_crtc_power_domains(crtc,
13742 to_intel_crtc_state(crtc->state));
13743 }
13744
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013745 if (!needs_modeset(crtc->state))
13746 continue;
13747
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013748 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013749
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013750 if (old_crtc_state->active) {
13751 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013752 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013753 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013754 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013755 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013756
13757 /*
13758 * Underruns don't always raise
13759 * interrupts, so check manually.
13760 */
13761 intel_check_cpu_fifo_underruns(dev_priv);
13762 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013763
13764 if (!crtc->state->active)
13765 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013766 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013767 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013768
Daniel Vetterea9d7582012-07-10 10:42:52 +020013769 /* Only after disabling all output pipelines that will be changed can we
13770 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013771 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013772
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013773 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013774 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013775
13776 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013777 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013778 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013779 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013780
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013781 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013782 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013783
Daniel Vettera6778b32012-07-02 09:56:42 +020013784 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013785 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13787 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013788 struct intel_crtc_state *pipe_config =
13789 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013790
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013791 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013792 update_scanline_offset(to_intel_crtc(crtc));
13793 dev_priv->display.crtc_enable(crtc);
13794 }
13795
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013796 /* Complete events for now disable pipes here. */
13797 if (modeset && !crtc->state->active && crtc->state->event) {
13798 spin_lock_irq(&dev->event_lock);
13799 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13800 spin_unlock_irq(&dev->event_lock);
13801
13802 crtc->state->event = NULL;
13803 }
13804
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013805 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013806 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013807
Daniel Vetter5a21b662016-05-24 17:13:53 +020013808 if (crtc->state->active &&
13809 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013810 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013811
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013812 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013813 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013814
Daniel Vetter5a21b662016-05-24 17:13:53 +020013815 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13816 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013817 }
13818
Daniel Vetter94f05022016-06-14 18:01:00 +020013819 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13820 * already, but still need the state for the delayed optimization. To
13821 * fix this:
13822 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13823 * - schedule that vblank worker _before_ calling hw_done
13824 * - at the start of commit_tail, cancel it _synchrously
13825 * - switch over to the vblank wait helper in the core after that since
13826 * we don't need out special handling any more.
13827 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013828 if (!state->legacy_cursor_update)
13829 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13830
13831 /*
13832 * Now that the vblank has passed, we can go ahead and program the
13833 * optimal watermarks on platforms that need two-step watermark
13834 * programming.
13835 *
13836 * TODO: Move this (and other cleanup) to an async worker eventually.
13837 */
13838 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13839 intel_cstate = to_intel_crtc_state(crtc->state);
13840
13841 if (dev_priv->display.optimize_watermarks)
13842 dev_priv->display.optimize_watermarks(intel_cstate);
13843 }
13844
13845 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13846 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13847
13848 if (put_domains[i])
13849 modeset_put_power_domains(dev_priv, put_domains[i]);
13850
13851 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13852 }
13853
Daniel Vetter94f05022016-06-14 18:01:00 +020013854 drm_atomic_helper_commit_hw_done(state);
13855
Daniel Vetter5a21b662016-05-24 17:13:53 +020013856 if (intel_state->modeset)
13857 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13858
13859 mutex_lock(&dev->struct_mutex);
13860 drm_atomic_helper_cleanup_planes(dev, state);
13861 mutex_unlock(&dev->struct_mutex);
13862
Daniel Vetterea0000f2016-06-13 16:13:46 +020013863 drm_atomic_helper_commit_cleanup_done(state);
13864
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013865 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013866
Mika Kuoppala75714942015-12-16 09:26:48 +020013867 /* As one of the primary mmio accessors, KMS has a high likelihood
13868 * of triggering bugs in unclaimed access. After we finish
13869 * modesetting, see if an error has been flagged, and if so
13870 * enable debugging for the next modeset - and hope we catch
13871 * the culprit.
13872 *
13873 * XXX note that we assume display power is on at this point.
13874 * This might hold true now but we need to add pm helper to check
13875 * unclaimed only when the hardware is on, as atomic commits
13876 * can happen also when the device is completely off.
13877 */
13878 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013879}
13880
13881static void intel_atomic_commit_work(struct work_struct *work)
13882{
13883 struct drm_atomic_state *state = container_of(work,
13884 struct drm_atomic_state,
13885 commit_work);
13886 intel_atomic_commit_tail(state);
13887}
13888
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013889static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13890{
13891 struct drm_plane_state *old_plane_state;
13892 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013893 int i;
13894
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010013895 for_each_plane_in_state(state, plane, old_plane_state, i)
13896 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
13897 intel_fb_obj(plane->state->fb),
13898 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013899}
13900
Daniel Vetter94f05022016-06-14 18:01:00 +020013901/**
13902 * intel_atomic_commit - commit validated state object
13903 * @dev: DRM device
13904 * @state: the top-level driver state object
13905 * @nonblock: nonblocking commit
13906 *
13907 * This function commits a top-level state object that has been validated
13908 * with drm_atomic_helper_check().
13909 *
13910 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13911 * nonblocking commits are only safe for pure plane updates. Everything else
13912 * should work though.
13913 *
13914 * RETURNS
13915 * Zero for success or -errno.
13916 */
13917static int intel_atomic_commit(struct drm_device *dev,
13918 struct drm_atomic_state *state,
13919 bool nonblock)
13920{
13921 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013922 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013923 int ret = 0;
13924
13925 if (intel_state->modeset && nonblock) {
13926 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13927 return -EINVAL;
13928 }
13929
13930 ret = drm_atomic_helper_setup_commit(state, nonblock);
13931 if (ret)
13932 return ret;
13933
13934 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13935
13936 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13937 if (ret) {
13938 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13939 return ret;
13940 }
13941
13942 drm_atomic_helper_swap_state(state, true);
13943 dev_priv->wm.distrust_bios_wm = false;
13944 dev_priv->wm.skl_results = intel_state->wm_results;
13945 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013946 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013947
13948 if (nonblock)
13949 queue_work(system_unbound_wq, &state->commit_work);
13950 else
13951 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013952
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013953 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013954}
13955
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013956void intel_crtc_restore_mode(struct drm_crtc *crtc)
13957{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013958 struct drm_device *dev = crtc->dev;
13959 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013960 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013961 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013962
13963 state = drm_atomic_state_alloc(dev);
13964 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013965 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13966 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013967 return;
13968 }
13969
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013970 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013971
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013972retry:
13973 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13974 ret = PTR_ERR_OR_ZERO(crtc_state);
13975 if (!ret) {
13976 if (!crtc_state->active)
13977 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013978
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013979 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013980 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013981 }
13982
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013983 if (ret == -EDEADLK) {
13984 drm_atomic_state_clear(state);
13985 drm_modeset_backoff(state->acquire_ctx);
13986 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013987 }
13988
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013989 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013990out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013991 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013992}
13993
Daniel Vetter25c5b262012-07-08 22:08:04 +020013994#undef for_each_intel_crtc_masked
13995
Bob Paauwea8784872016-07-15 14:59:02 +010013996/*
13997 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13998 * drm_atomic_helper_legacy_gamma_set() directly.
13999 */
14000static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14001 u16 *red, u16 *green, u16 *blue,
14002 uint32_t size)
14003{
14004 struct drm_device *dev = crtc->dev;
14005 struct drm_mode_config *config = &dev->mode_config;
14006 struct drm_crtc_state *state;
14007 int ret;
14008
14009 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14010 if (ret)
14011 return ret;
14012
14013 /*
14014 * Make sure we update the legacy properties so this works when
14015 * atomic is not enabled.
14016 */
14017
14018 state = crtc->state;
14019
14020 drm_object_property_set_value(&crtc->base,
14021 config->degamma_lut_property,
14022 (state->degamma_lut) ?
14023 state->degamma_lut->base.id : 0);
14024
14025 drm_object_property_set_value(&crtc->base,
14026 config->ctm_property,
14027 (state->ctm) ?
14028 state->ctm->base.id : 0);
14029
14030 drm_object_property_set_value(&crtc->base,
14031 config->gamma_lut_property,
14032 (state->gamma_lut) ?
14033 state->gamma_lut->base.id : 0);
14034
14035 return 0;
14036}
14037
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014038static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014039 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014040 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014041 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014042 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014043 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014044 .atomic_duplicate_state = intel_crtc_duplicate_state,
14045 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014046};
14047
Matt Roper6beb8c232014-12-01 15:40:14 -080014048/**
14049 * intel_prepare_plane_fb - Prepare fb for usage on plane
14050 * @plane: drm plane to prepare for
14051 * @fb: framebuffer to prepare for presentation
14052 *
14053 * Prepares a framebuffer for usage on a display plane. Generally this
14054 * involves pinning the underlying object and updating the frontbuffer tracking
14055 * bits. Some older platforms need special physical address handling for
14056 * cursor planes.
14057 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014058 * Must be called with struct_mutex held.
14059 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014060 * Returns 0 on success, negative error code on failure.
14061 */
14062int
14063intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014064 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014065{
14066 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014067 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014068 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014069 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014070 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014071 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014072
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014073 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014074 return 0;
14075
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014076 if (old_obj) {
14077 struct drm_crtc_state *crtc_state =
14078 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14079
14080 /* Big Hammer, we also need to ensure that any pending
14081 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14082 * current scanout is retired before unpinning the old
14083 * framebuffer. Note that we rely on userspace rendering
14084 * into the buffer attached to the pipe they are waiting
14085 * on. If not, userspace generates a GPU hang with IPEHR
14086 * point to the MI_WAIT_FOR_EVENT.
14087 *
14088 * This should only fail upon a hung GPU, in which case we
14089 * can safely continue.
14090 */
14091 if (needs_modeset(crtc_state))
14092 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014093 if (ret) {
14094 /* GPU hangs should have been swallowed by the wait */
14095 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014096 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014097 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014098 }
14099
Chris Wilsonc37efb92016-06-17 08:28:47 +010014100 if (!obj)
14101 return 0;
14102
Daniel Vetter5a21b662016-05-24 17:13:53 +020014103 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014104 resv = i915_gem_object_get_dmabuf_resv(obj);
14105 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014106 long lret;
14107
Chris Wilsonc37efb92016-06-17 08:28:47 +010014108 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014109 MAX_SCHEDULE_TIMEOUT);
14110 if (lret == -ERESTARTSYS)
14111 return lret;
14112
14113 WARN(lret < 0, "waiting returns %li\n", lret);
14114 }
14115
Chris Wilsonc37efb92016-06-17 08:28:47 +010014116 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014117 INTEL_INFO(dev)->cursor_needs_physical) {
14118 int align = IS_I830(dev) ? 16 * 1024 : 256;
14119 ret = i915_gem_object_attach_phys(obj, align);
14120 if (ret)
14121 DRM_DEBUG_KMS("failed to attach phys object\n");
14122 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014123 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014124 }
14125
Chris Wilsonc37efb92016-06-17 08:28:47 +010014126 if (ret == 0) {
Chris Wilson27c01aa2016-08-04 07:52:30 +010014127 to_intel_plane_state(new_state)->wait_req =
Chris Wilsond72d9082016-08-04 07:52:31 +010014128 i915_gem_active_get(&obj->last_write,
14129 &obj->base.dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014130 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014131
Matt Roper6beb8c232014-12-01 15:40:14 -080014132 return ret;
14133}
14134
Matt Roper38f3ce32014-12-02 07:45:25 -080014135/**
14136 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14137 * @plane: drm plane to clean up for
14138 * @fb: old framebuffer that was on plane
14139 *
14140 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014141 *
14142 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014143 */
14144void
14145intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014146 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014147{
14148 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014149 struct intel_plane_state *old_intel_state;
Keith Packard84978252016-07-31 00:54:51 -070014150 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014151 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14152 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014153
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014154 old_intel_state = to_intel_plane_state(old_state);
14155
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014156 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014157 return;
14158
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014159 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14160 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014161 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014162
Keith Packard84978252016-07-31 00:54:51 -070014163 i915_gem_request_assign(&intel_state->wait_req, NULL);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014164 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014165}
14166
Chandra Konduru6156a452015-04-27 13:48:39 -070014167int
14168skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14169{
14170 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014171 int crtc_clock, cdclk;
14172
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014173 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014174 return DRM_PLANE_HELPER_NO_SCALING;
14175
Chandra Konduru6156a452015-04-27 13:48:39 -070014176 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014177 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014178
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014179 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014180 return DRM_PLANE_HELPER_NO_SCALING;
14181
14182 /*
14183 * skl max scale is lower of:
14184 * close to 3 but not 3, -1 is for that purpose
14185 * or
14186 * cdclk/crtc_clock
14187 */
14188 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14189
14190 return max_scale;
14191}
14192
Matt Roper465c1202014-05-29 08:06:54 -070014193static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014194intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014195 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014196 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014197{
Matt Roper2b875c22014-12-01 15:40:13 -080014198 struct drm_crtc *crtc = state->base.crtc;
14199 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014200 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014201 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14202 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014203
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014204 if (INTEL_INFO(plane->dev)->gen >= 9) {
14205 /* use scaler when colorkey is not required */
14206 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14207 min_scale = 1;
14208 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14209 }
Sonika Jindald8106362015-04-10 14:37:28 +053014210 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014211 }
Sonika Jindald8106362015-04-10 14:37:28 +053014212
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014213 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14214 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014215 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014216 min_scale, max_scale,
14217 can_position, true,
14218 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014219}
14220
Daniel Vetter5a21b662016-05-24 17:13:53 +020014221static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14222 struct drm_crtc_state *old_crtc_state)
14223{
14224 struct drm_device *dev = crtc->dev;
14225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14226 struct intel_crtc_state *old_intel_state =
14227 to_intel_crtc_state(old_crtc_state);
14228 bool modeset = needs_modeset(crtc->state);
14229
14230 /* Perform vblank evasion around commit operation */
14231 intel_pipe_update_start(intel_crtc);
14232
14233 if (modeset)
14234 return;
14235
14236 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14237 intel_color_set_csc(crtc->state);
14238 intel_color_load_luts(crtc->state);
14239 }
14240
14241 if (to_intel_crtc_state(crtc->state)->update_pipe)
14242 intel_update_pipe_config(intel_crtc, old_intel_state);
14243 else if (INTEL_INFO(dev)->gen >= 9)
14244 skl_detach_scalers(intel_crtc);
14245}
14246
14247static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14248 struct drm_crtc_state *old_crtc_state)
14249{
14250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14251
14252 intel_pipe_update_end(intel_crtc, NULL);
14253}
14254
Matt Ropercf4c7c12014-12-04 10:27:42 -080014255/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014256 * intel_plane_destroy - destroy a plane
14257 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014258 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014259 * Common destruction function for all types of planes (primary, cursor,
14260 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014261 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014262void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014263{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014264 if (!plane)
14265 return;
14266
Matt Roper465c1202014-05-29 08:06:54 -070014267 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014268 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014269}
14270
Matt Roper65a3fea2015-01-21 16:35:42 -080014271const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014272 .update_plane = drm_atomic_helper_update_plane,
14273 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014274 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014275 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014276 .atomic_get_property = intel_plane_atomic_get_property,
14277 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014278 .atomic_duplicate_state = intel_plane_duplicate_state,
14279 .atomic_destroy_state = intel_plane_destroy_state,
14280
Matt Roper465c1202014-05-29 08:06:54 -070014281};
14282
14283static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14284 int pipe)
14285{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014286 struct intel_plane *primary = NULL;
14287 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014288 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014289 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014290 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014291
14292 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014293 if (!primary)
14294 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014295
Matt Roper8e7d6882015-01-21 16:35:41 -080014296 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014297 if (!state)
14298 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014299 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014300
Matt Roper465c1202014-05-29 08:06:54 -070014301 primary->can_scale = false;
14302 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014303 if (INTEL_INFO(dev)->gen >= 9) {
14304 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014305 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014306 }
Matt Roper465c1202014-05-29 08:06:54 -070014307 primary->pipe = pipe;
14308 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014309 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014310 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014311 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14312 primary->plane = !pipe;
14313
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014314 if (INTEL_INFO(dev)->gen >= 9) {
14315 intel_primary_formats = skl_primary_formats;
14316 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014317
14318 primary->update_plane = skylake_update_primary_plane;
14319 primary->disable_plane = skylake_disable_primary_plane;
14320 } else if (HAS_PCH_SPLIT(dev)) {
14321 intel_primary_formats = i965_primary_formats;
14322 num_formats = ARRAY_SIZE(i965_primary_formats);
14323
14324 primary->update_plane = ironlake_update_primary_plane;
14325 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014326 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014327 intel_primary_formats = i965_primary_formats;
14328 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014329
14330 primary->update_plane = i9xx_update_primary_plane;
14331 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014332 } else {
14333 intel_primary_formats = i8xx_primary_formats;
14334 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014335
14336 primary->update_plane = i9xx_update_primary_plane;
14337 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014338 }
14339
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014340 if (INTEL_INFO(dev)->gen >= 9)
14341 ret = drm_universal_plane_init(dev, &primary->base, 0,
14342 &intel_plane_funcs,
14343 intel_primary_formats, num_formats,
14344 DRM_PLANE_TYPE_PRIMARY,
14345 "plane 1%c", pipe_name(pipe));
14346 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14347 ret = drm_universal_plane_init(dev, &primary->base, 0,
14348 &intel_plane_funcs,
14349 intel_primary_formats, num_formats,
14350 DRM_PLANE_TYPE_PRIMARY,
14351 "primary %c", pipe_name(pipe));
14352 else
14353 ret = drm_universal_plane_init(dev, &primary->base, 0,
14354 &intel_plane_funcs,
14355 intel_primary_formats, num_formats,
14356 DRM_PLANE_TYPE_PRIMARY,
14357 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014358 if (ret)
14359 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014360
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014361 if (INTEL_INFO(dev)->gen >= 4)
14362 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014363
Matt Roperea2c67b2014-12-23 10:41:52 -080014364 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14365
Matt Roper465c1202014-05-29 08:06:54 -070014366 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014367
14368fail:
14369 kfree(state);
14370 kfree(primary);
14371
14372 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014373}
14374
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014375void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14376{
14377 if (!dev->mode_config.rotation_property) {
14378 unsigned long flags = BIT(DRM_ROTATE_0) |
14379 BIT(DRM_ROTATE_180);
14380
14381 if (INTEL_INFO(dev)->gen >= 9)
14382 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14383
14384 dev->mode_config.rotation_property =
14385 drm_mode_create_rotation_property(dev, flags);
14386 }
14387 if (dev->mode_config.rotation_property)
14388 drm_object_attach_property(&plane->base.base,
14389 dev->mode_config.rotation_property,
14390 plane->base.state->rotation);
14391}
14392
Matt Roper3d7d6512014-06-10 08:28:13 -070014393static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014394intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014395 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014396 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014397{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014398 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014399 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014400 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014401 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014402 unsigned stride;
14403 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014404
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014405 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14406 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014407 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014408 DRM_PLANE_HELPER_NO_SCALING,
14409 DRM_PLANE_HELPER_NO_SCALING,
14410 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014411 if (ret)
14412 return ret;
14413
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014414 /* if we want to turn off the cursor ignore width and height */
14415 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014416 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014417
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014418 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014419 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014420 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14421 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014422 return -EINVAL;
14423 }
14424
Matt Roperea2c67b2014-12-23 10:41:52 -080014425 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14426 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014427 DRM_DEBUG_KMS("buffer is too small\n");
14428 return -ENOMEM;
14429 }
14430
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014431 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014432 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014433 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014434 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014435
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014436 /*
14437 * There's something wrong with the cursor on CHV pipe C.
14438 * If it straddles the left edge of the screen then
14439 * moving it away from the edge or disabling it often
14440 * results in a pipe underrun, and often that can lead to
14441 * dead pipe (constant underrun reported, and it scans
14442 * out just a solid color). To recover from that, the
14443 * display power well must be turned off and on again.
14444 * Refuse the put the cursor into that compromised position.
14445 */
14446 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14447 state->visible && state->base.crtc_x < 0) {
14448 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14449 return -EINVAL;
14450 }
14451
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014452 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014453}
14454
Matt Roperf4a2cf22014-12-01 15:40:12 -080014455static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014456intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014457 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014458{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14460
14461 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014462 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014463}
14464
14465static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014466intel_update_cursor_plane(struct drm_plane *plane,
14467 const struct intel_crtc_state *crtc_state,
14468 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014469{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014470 struct drm_crtc *crtc = crtc_state->base.crtc;
14471 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014472 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014473 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014474 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014475
Matt Roperf4a2cf22014-12-01 15:40:12 -080014476 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014477 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014478 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014479 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014480 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014481 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014482
Gustavo Padovana912f122014-12-01 15:40:10 -080014483 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014484 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014485}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014486
Matt Roper3d7d6512014-06-10 08:28:13 -070014487static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14488 int pipe)
14489{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014490 struct intel_plane *cursor = NULL;
14491 struct intel_plane_state *state = NULL;
14492 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014493
14494 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014495 if (!cursor)
14496 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014497
Matt Roper8e7d6882015-01-21 16:35:41 -080014498 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014499 if (!state)
14500 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014501 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014502
Matt Roper3d7d6512014-06-10 08:28:13 -070014503 cursor->can_scale = false;
14504 cursor->max_downscale = 1;
14505 cursor->pipe = pipe;
14506 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014507 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014508 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014509 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014510 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014511
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014512 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14513 &intel_plane_funcs,
14514 intel_cursor_formats,
14515 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014516 DRM_PLANE_TYPE_CURSOR,
14517 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014518 if (ret)
14519 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014520
14521 if (INTEL_INFO(dev)->gen >= 4) {
14522 if (!dev->mode_config.rotation_property)
14523 dev->mode_config.rotation_property =
14524 drm_mode_create_rotation_property(dev,
14525 BIT(DRM_ROTATE_0) |
14526 BIT(DRM_ROTATE_180));
14527 if (dev->mode_config.rotation_property)
14528 drm_object_attach_property(&cursor->base.base,
14529 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014530 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014531 }
14532
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014533 if (INTEL_INFO(dev)->gen >=9)
14534 state->scaler_id = -1;
14535
Matt Roperea2c67b2014-12-23 10:41:52 -080014536 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14537
Matt Roper3d7d6512014-06-10 08:28:13 -070014538 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014539
14540fail:
14541 kfree(state);
14542 kfree(cursor);
14543
14544 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014545}
14546
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014547static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14548 struct intel_crtc_state *crtc_state)
14549{
14550 int i;
14551 struct intel_scaler *intel_scaler;
14552 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14553
14554 for (i = 0; i < intel_crtc->num_scalers; i++) {
14555 intel_scaler = &scaler_state->scalers[i];
14556 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014557 intel_scaler->mode = PS_SCALER_MODE_DYN;
14558 }
14559
14560 scaler_state->scaler_id = -1;
14561}
14562
Hannes Ederb358d0a2008-12-18 21:18:47 +010014563static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014564{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014565 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014566 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014567 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014568 struct drm_plane *primary = NULL;
14569 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014570 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014571
Daniel Vetter955382f2013-09-19 14:05:45 +020014572 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014573 if (intel_crtc == NULL)
14574 return;
14575
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014576 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14577 if (!crtc_state)
14578 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014579 intel_crtc->config = crtc_state;
14580 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014581 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014582
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014583 /* initialize shared scalers */
14584 if (INTEL_INFO(dev)->gen >= 9) {
14585 if (pipe == PIPE_C)
14586 intel_crtc->num_scalers = 1;
14587 else
14588 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14589
14590 skl_init_scalers(dev, intel_crtc, crtc_state);
14591 }
14592
Matt Roper465c1202014-05-29 08:06:54 -070014593 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014594 if (!primary)
14595 goto fail;
14596
14597 cursor = intel_cursor_plane_create(dev, pipe);
14598 if (!cursor)
14599 goto fail;
14600
Matt Roper465c1202014-05-29 08:06:54 -070014601 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014602 cursor, &intel_crtc_funcs,
14603 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014604 if (ret)
14605 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014606
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014607 /*
14608 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014609 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014610 */
Jesse Barnes80824002009-09-10 15:28:06 -070014611 intel_crtc->pipe = pipe;
14612 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014613 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014614 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014615 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014616 }
14617
Chris Wilson4b0e3332014-05-30 16:35:26 +030014618 intel_crtc->cursor_base = ~0;
14619 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014620 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014621
Ville Syrjälä852eb002015-06-24 22:00:07 +030014622 intel_crtc->wm.cxsr_allowed = true;
14623
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014624 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14625 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14626 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14627 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14628
Jesse Barnes79e53942008-11-07 14:24:08 -080014629 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014630
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014631 intel_color_init(&intel_crtc->base);
14632
Daniel Vetter87b6b102014-05-15 15:33:46 +020014633 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014634 return;
14635
14636fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014637 intel_plane_destroy(primary);
14638 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014639 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014640 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014641}
14642
Jesse Barnes752aa882013-10-31 18:55:49 +020014643enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14644{
14645 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014646 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014647
Rob Clark51fd3712013-11-19 12:10:12 -050014648 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014649
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014650 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014651 return INVALID_PIPE;
14652
14653 return to_intel_crtc(encoder->crtc)->pipe;
14654}
14655
Carl Worth08d7b3d2009-04-29 14:43:54 -070014656int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014657 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014658{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014659 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014660 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014661 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014662
Rob Clark7707e652014-07-17 23:30:04 -040014663 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014664 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014665 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014666
Rob Clark7707e652014-07-17 23:30:04 -040014667 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014668 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014669
Daniel Vetterc05422d2009-08-11 16:05:30 +020014670 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014671}
14672
Daniel Vetter66a92782012-07-12 20:08:18 +020014673static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014674{
Daniel Vetter66a92782012-07-12 20:08:18 +020014675 struct drm_device *dev = encoder->base.dev;
14676 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014677 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014678 int entry = 0;
14679
Damien Lespiaub2784e12014-08-05 11:29:37 +010014680 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014681 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014682 index_mask |= (1 << entry);
14683
Jesse Barnes79e53942008-11-07 14:24:08 -080014684 entry++;
14685 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014686
Jesse Barnes79e53942008-11-07 14:24:08 -080014687 return index_mask;
14688}
14689
Chris Wilson4d302442010-12-14 19:21:29 +000014690static bool has_edp_a(struct drm_device *dev)
14691{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014692 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014693
14694 if (!IS_MOBILE(dev))
14695 return false;
14696
14697 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14698 return false;
14699
Damien Lespiaue3589902014-02-07 19:12:50 +000014700 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014701 return false;
14702
14703 return true;
14704}
14705
Jesse Barnes84b4e042014-06-25 08:24:29 -070014706static bool intel_crt_present(struct drm_device *dev)
14707{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014708 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014709
Damien Lespiau884497e2013-12-03 13:56:23 +000014710 if (INTEL_INFO(dev)->gen >= 9)
14711 return false;
14712
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014713 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014714 return false;
14715
14716 if (IS_CHERRYVIEW(dev))
14717 return false;
14718
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014719 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14720 return false;
14721
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014722 /* DDI E can't be used if DDI A requires 4 lanes */
14723 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14724 return false;
14725
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014726 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014727 return false;
14728
14729 return true;
14730}
14731
Imre Deak44cb7342016-08-10 14:07:29 +030014732static void intel_pps_init(struct drm_i915_private *dev_priv)
14733{
14734 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
14735 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14736 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14737 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14738 else
14739 dev_priv->pps_mmio_base = PPS_BASE;
14740}
14741
Jesse Barnes79e53942008-11-07 14:24:08 -080014742static void intel_setup_outputs(struct drm_device *dev)
14743{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014744 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014745 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014746 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014747
Imre Deak44cb7342016-08-10 14:07:29 +030014748 intel_pps_init(dev_priv);
14749
Imre Deak97a824e12016-06-21 11:51:47 +030014750 /*
14751 * intel_edp_init_connector() depends on this completing first, to
14752 * prevent the registeration of both eDP and LVDS and the incorrect
14753 * sharing of the PPS.
14754 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014755 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014756
Jesse Barnes84b4e042014-06-25 08:24:29 -070014757 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014758 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014759
Vandana Kannanc776eb22014-08-19 12:05:01 +053014760 if (IS_BROXTON(dev)) {
14761 /*
14762 * FIXME: Broxton doesn't support port detection via the
14763 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14764 * detect the ports.
14765 */
14766 intel_ddi_init(dev, PORT_A);
14767 intel_ddi_init(dev, PORT_B);
14768 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014769
14770 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014771 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014772 int found;
14773
Jesse Barnesde31fac2015-03-06 15:53:32 -080014774 /*
14775 * Haswell uses DDI functions to detect digital outputs.
14776 * On SKL pre-D0 the strap isn't connected, so we assume
14777 * it's there.
14778 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014779 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014780 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014781 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014782 intel_ddi_init(dev, PORT_A);
14783
14784 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14785 * register */
14786 found = I915_READ(SFUSE_STRAP);
14787
14788 if (found & SFUSE_STRAP_DDIB_DETECTED)
14789 intel_ddi_init(dev, PORT_B);
14790 if (found & SFUSE_STRAP_DDIC_DETECTED)
14791 intel_ddi_init(dev, PORT_C);
14792 if (found & SFUSE_STRAP_DDID_DETECTED)
14793 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014794 /*
14795 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14796 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014797 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014798 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14799 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14800 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14801 intel_ddi_init(dev, PORT_E);
14802
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014803 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014804 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014805 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014806
14807 if (has_edp_a(dev))
14808 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014809
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014810 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014811 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014812 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014813 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014814 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014815 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014816 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014817 }
14818
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014819 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014820 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014821
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014822 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014823 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014824
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014825 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014826 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014827
Daniel Vetter270b3042012-10-27 15:52:05 +020014828 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014829 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014830 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014831 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014832
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014833 /*
14834 * The DP_DETECTED bit is the latched state of the DDC
14835 * SDA pin at boot. However since eDP doesn't require DDC
14836 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14837 * eDP ports may have been muxed to an alternate function.
14838 * Thus we can't rely on the DP_DETECTED bit alone to detect
14839 * eDP ports. Consult the VBT as well as DP_DETECTED to
14840 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014841 *
14842 * Sadly the straps seem to be missing sometimes even for HDMI
14843 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14844 * and VBT for the presence of the port. Additionally we can't
14845 * trust the port type the VBT declares as we've seen at least
14846 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014847 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014848 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014849 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14850 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014851 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014852 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014853 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014854
Chris Wilson457c52d2016-06-01 08:27:50 +010014855 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014856 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14857 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014858 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014859 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014860 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014861
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014862 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014863 /*
14864 * eDP not supported on port D,
14865 * so no need to worry about it
14866 */
14867 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14868 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014869 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014870 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14871 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014872 }
14873
Jani Nikula3cfca972013-08-27 15:12:26 +030014874 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014875 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014876 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014877
Paulo Zanonie2debe92013-02-18 19:00:27 -030014878 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014879 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014880 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014881 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014882 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014883 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014884 }
Ma Ling27185ae2009-08-24 13:50:23 +080014885
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014886 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014887 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014888 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014889
14890 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014891
Paulo Zanonie2debe92013-02-18 19:00:27 -030014892 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014893 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014894 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014895 }
Ma Ling27185ae2009-08-24 13:50:23 +080014896
Paulo Zanonie2debe92013-02-18 19:00:27 -030014897 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014898
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014899 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014900 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014901 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014902 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014903 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014904 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014905 }
Ma Ling27185ae2009-08-24 13:50:23 +080014906
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014907 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014908 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014909 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014910 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014911 intel_dvo_init(dev);
14912
Zhenyu Wang103a1962009-11-27 11:44:36 +080014913 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014914 intel_tv_init(dev);
14915
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014916 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014917
Damien Lespiaub2784e12014-08-05 11:29:37 +010014918 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014919 encoder->base.possible_crtcs = encoder->crtc_mask;
14920 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014921 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014922 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014923
Paulo Zanonidde86e22012-12-01 12:04:25 -020014924 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014925
14926 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014927}
14928
14929static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14930{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014931 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014932 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014933
Daniel Vetteref2d6332014-02-10 18:00:38 +010014934 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014935 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014936 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014937 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014938 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014939 kfree(intel_fb);
14940}
14941
14942static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014943 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014944 unsigned int *handle)
14945{
14946 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014947 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014948
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014949 if (obj->userptr.mm) {
14950 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14951 return -EINVAL;
14952 }
14953
Chris Wilson05394f32010-11-08 19:18:58 +000014954 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014955}
14956
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014957static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14958 struct drm_file *file,
14959 unsigned flags, unsigned color,
14960 struct drm_clip_rect *clips,
14961 unsigned num_clips)
14962{
14963 struct drm_device *dev = fb->dev;
14964 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14965 struct drm_i915_gem_object *obj = intel_fb->obj;
14966
14967 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014968 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014969 mutex_unlock(&dev->struct_mutex);
14970
14971 return 0;
14972}
14973
Jesse Barnes79e53942008-11-07 14:24:08 -080014974static const struct drm_framebuffer_funcs intel_fb_funcs = {
14975 .destroy = intel_user_framebuffer_destroy,
14976 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014977 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014978};
14979
Damien Lespiaub3218032015-02-27 11:15:18 +000014980static
14981u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14982 uint32_t pixel_format)
14983{
14984 u32 gen = INTEL_INFO(dev)->gen;
14985
14986 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014987 int cpp = drm_format_plane_cpp(pixel_format, 0);
14988
Damien Lespiaub3218032015-02-27 11:15:18 +000014989 /* "The stride in bytes must not exceed the of the size of 8K
14990 * pixels and 32K bytes."
14991 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014992 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014993 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014994 return 32*1024;
14995 } else if (gen >= 4) {
14996 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14997 return 16*1024;
14998 else
14999 return 32*1024;
15000 } else if (gen >= 3) {
15001 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15002 return 8*1024;
15003 else
15004 return 16*1024;
15005 } else {
15006 /* XXX DSPC is limited to 4k tiled */
15007 return 8*1024;
15008 }
15009}
15010
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015011static int intel_framebuffer_init(struct drm_device *dev,
15012 struct intel_framebuffer *intel_fb,
15013 struct drm_mode_fb_cmd2 *mode_cmd,
15014 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015015{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015016 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000015017 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080015018 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015019 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080015020
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015021 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15022
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015023 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15024 /* Enforce that fb modifier and tiling mode match, but only for
15025 * X-tiled. This is needed for FBC. */
Chris Wilson3e510a82016-08-05 10:14:23 +010015026 if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015027 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15028 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15029 return -EINVAL;
15030 }
15031 } else {
Chris Wilson3e510a82016-08-05 10:14:23 +010015032 if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015033 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Chris Wilson3e510a82016-08-05 10:14:23 +010015034 else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015035 DRM_DEBUG("No Y tiling for legacy addfb\n");
15036 return -EINVAL;
15037 }
15038 }
15039
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015040 /* Passed in modifier sanity checking. */
15041 switch (mode_cmd->modifier[0]) {
15042 case I915_FORMAT_MOD_Y_TILED:
15043 case I915_FORMAT_MOD_Yf_TILED:
15044 if (INTEL_INFO(dev)->gen < 9) {
15045 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15046 mode_cmd->modifier[0]);
15047 return -EINVAL;
15048 }
15049 case DRM_FORMAT_MOD_NONE:
15050 case I915_FORMAT_MOD_X_TILED:
15051 break;
15052 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015053 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15054 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015055 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015056 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015057
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015058 stride_alignment = intel_fb_stride_alignment(dev_priv,
15059 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015060 mode_cmd->pixel_format);
15061 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15062 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15063 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015064 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015065 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015066
Damien Lespiaub3218032015-02-27 11:15:18 +000015067 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15068 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015069 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015070 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15071 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015072 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015073 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015074 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015075 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015076
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015077 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015078 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015079 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015080 mode_cmd->pitches[0],
15081 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015082 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015083 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015084
Ville Syrjälä57779d02012-10-31 17:50:14 +020015085 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015086 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015087 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015088 case DRM_FORMAT_RGB565:
15089 case DRM_FORMAT_XRGB8888:
15090 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015091 break;
15092 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015093 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015094 DRM_DEBUG("unsupported pixel format: %s\n",
15095 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015096 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015097 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015098 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015099 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015100 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15101 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015102 DRM_DEBUG("unsupported pixel format: %s\n",
15103 drm_get_format_name(mode_cmd->pixel_format));
15104 return -EINVAL;
15105 }
15106 break;
15107 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015108 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015109 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015110 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015111 DRM_DEBUG("unsupported pixel format: %s\n",
15112 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015113 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015114 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015115 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015116 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015117 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015118 DRM_DEBUG("unsupported pixel format: %s\n",
15119 drm_get_format_name(mode_cmd->pixel_format));
15120 return -EINVAL;
15121 }
15122 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015123 case DRM_FORMAT_YUYV:
15124 case DRM_FORMAT_UYVY:
15125 case DRM_FORMAT_YVYU:
15126 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015127 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015128 DRM_DEBUG("unsupported pixel format: %s\n",
15129 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015130 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015131 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015132 break;
15133 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015134 DRM_DEBUG("unsupported pixel format: %s\n",
15135 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015136 return -EINVAL;
15137 }
15138
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015139 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15140 if (mode_cmd->offsets[0] != 0)
15141 return -EINVAL;
15142
Damien Lespiauec2c9812015-01-20 12:51:45 +000015143 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015144 mode_cmd->pixel_format,
15145 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015146 /* FIXME drm helper for size checks (especially planar formats)? */
15147 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15148 return -EINVAL;
15149
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015150 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15151 intel_fb->obj = obj;
15152
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015153 intel_fill_fb_info(dev_priv, &intel_fb->base);
15154
Jesse Barnes79e53942008-11-07 14:24:08 -080015155 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15156 if (ret) {
15157 DRM_ERROR("framebuffer init failed %d\n", ret);
15158 return ret;
15159 }
15160
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015161 intel_fb->obj->framebuffer_references++;
15162
Jesse Barnes79e53942008-11-07 14:24:08 -080015163 return 0;
15164}
15165
Jesse Barnes79e53942008-11-07 14:24:08 -080015166static struct drm_framebuffer *
15167intel_user_framebuffer_create(struct drm_device *dev,
15168 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015169 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015170{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015171 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015172 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015173 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015174
Chris Wilson03ac0642016-07-20 13:31:51 +010015175 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15176 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015177 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015178
Daniel Vetter92907cb2015-11-23 09:04:05 +010015179 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015180 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010015181 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015182
15183 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015184}
15185
Daniel Vetter06957262015-08-10 13:34:08 +020015186#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015187static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015188{
15189}
15190#endif
15191
Jesse Barnes79e53942008-11-07 14:24:08 -080015192static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015193 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015194 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015195 .atomic_check = intel_atomic_check,
15196 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015197 .atomic_state_alloc = intel_atomic_state_alloc,
15198 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015199};
15200
Imre Deak88212942016-03-16 13:38:53 +020015201/**
15202 * intel_init_display_hooks - initialize the display modesetting hooks
15203 * @dev_priv: device private
15204 */
15205void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015206{
Imre Deak88212942016-03-16 13:38:53 +020015207 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015208 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015209 dev_priv->display.get_initial_plane_config =
15210 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015211 dev_priv->display.crtc_compute_clock =
15212 haswell_crtc_compute_clock;
15213 dev_priv->display.crtc_enable = haswell_crtc_enable;
15214 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015215 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015216 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015217 dev_priv->display.get_initial_plane_config =
15218 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015219 dev_priv->display.crtc_compute_clock =
15220 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015221 dev_priv->display.crtc_enable = haswell_crtc_enable;
15222 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015223 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015224 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015225 dev_priv->display.get_initial_plane_config =
15226 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015227 dev_priv->display.crtc_compute_clock =
15228 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015229 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15230 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015231 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015232 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015233 dev_priv->display.get_initial_plane_config =
15234 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015235 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15236 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15237 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15238 } else if (IS_VALLEYVIEW(dev_priv)) {
15239 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15240 dev_priv->display.get_initial_plane_config =
15241 i9xx_get_initial_plane_config;
15242 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015243 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15244 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015245 } else if (IS_G4X(dev_priv)) {
15246 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15247 dev_priv->display.get_initial_plane_config =
15248 i9xx_get_initial_plane_config;
15249 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15250 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15251 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015252 } else if (IS_PINEVIEW(dev_priv)) {
15253 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15254 dev_priv->display.get_initial_plane_config =
15255 i9xx_get_initial_plane_config;
15256 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15257 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15258 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015259 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015260 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015261 dev_priv->display.get_initial_plane_config =
15262 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015263 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015264 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15265 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015266 } else {
15267 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15268 dev_priv->display.get_initial_plane_config =
15269 i9xx_get_initial_plane_config;
15270 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15271 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15272 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015273 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015274
Jesse Barnese70236a2009-09-21 10:42:27 -070015275 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015276 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015277 dev_priv->display.get_display_clock_speed =
15278 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015279 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015280 dev_priv->display.get_display_clock_speed =
15281 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015282 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015283 dev_priv->display.get_display_clock_speed =
15284 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015285 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015286 dev_priv->display.get_display_clock_speed =
15287 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015288 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015289 dev_priv->display.get_display_clock_speed =
15290 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015291 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015292 dev_priv->display.get_display_clock_speed =
15293 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015294 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15295 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015296 dev_priv->display.get_display_clock_speed =
15297 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015298 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015299 dev_priv->display.get_display_clock_speed =
15300 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015301 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015302 dev_priv->display.get_display_clock_speed =
15303 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015304 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015305 dev_priv->display.get_display_clock_speed =
15306 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015307 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015308 dev_priv->display.get_display_clock_speed =
15309 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015310 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015311 dev_priv->display.get_display_clock_speed =
15312 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015313 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015314 dev_priv->display.get_display_clock_speed =
15315 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015316 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015317 dev_priv->display.get_display_clock_speed =
15318 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015319 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015320 dev_priv->display.get_display_clock_speed =
15321 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015322 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015323 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015324 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015325 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015326 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015327 dev_priv->display.get_display_clock_speed =
15328 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015329 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015330
Imre Deak88212942016-03-16 13:38:53 +020015331 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015332 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015333 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015334 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015335 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015336 /* FIXME: detect B0+ stepping and use auto training */
15337 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015338 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015339 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015340 }
15341
15342 if (IS_BROADWELL(dev_priv)) {
15343 dev_priv->display.modeset_commit_cdclk =
15344 broadwell_modeset_commit_cdclk;
15345 dev_priv->display.modeset_calc_cdclk =
15346 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015347 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015348 dev_priv->display.modeset_commit_cdclk =
15349 valleyview_modeset_commit_cdclk;
15350 dev_priv->display.modeset_calc_cdclk =
15351 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015352 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015353 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015354 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015355 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015356 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015357 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15358 dev_priv->display.modeset_commit_cdclk =
15359 skl_modeset_commit_cdclk;
15360 dev_priv->display.modeset_calc_cdclk =
15361 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015362 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015363
15364 switch (INTEL_INFO(dev_priv)->gen) {
15365 case 2:
15366 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15367 break;
15368
15369 case 3:
15370 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15371 break;
15372
15373 case 4:
15374 case 5:
15375 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15376 break;
15377
15378 case 6:
15379 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15380 break;
15381 case 7:
15382 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15383 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15384 break;
15385 case 9:
15386 /* Drop through - unsupported since execlist only. */
15387 default:
15388 /* Default just returns -ENODEV to indicate unsupported */
15389 dev_priv->display.queue_flip = intel_default_queue_flip;
15390 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015391}
15392
Jesse Barnesb690e962010-07-19 13:53:12 -070015393/*
15394 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15395 * resume, or other times. This quirk makes sure that's the case for
15396 * affected systems.
15397 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015398static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015399{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015400 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015401
15402 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015403 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015404}
15405
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015406static void quirk_pipeb_force(struct drm_device *dev)
15407{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015408 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015409
15410 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15411 DRM_INFO("applying pipe b force quirk\n");
15412}
15413
Keith Packard435793d2011-07-12 14:56:22 -070015414/*
15415 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15416 */
15417static void quirk_ssc_force_disable(struct drm_device *dev)
15418{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015419 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015420 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015421 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015422}
15423
Carsten Emde4dca20e2012-03-15 15:56:26 +010015424/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015425 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15426 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015427 */
15428static void quirk_invert_brightness(struct drm_device *dev)
15429{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015430 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015431 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015432 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015433}
15434
Scot Doyle9c72cc62014-07-03 23:27:50 +000015435/* Some VBT's incorrectly indicate no backlight is present */
15436static void quirk_backlight_present(struct drm_device *dev)
15437{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015438 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015439 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15440 DRM_INFO("applying backlight present quirk\n");
15441}
15442
Jesse Barnesb690e962010-07-19 13:53:12 -070015443struct intel_quirk {
15444 int device;
15445 int subsystem_vendor;
15446 int subsystem_device;
15447 void (*hook)(struct drm_device *dev);
15448};
15449
Egbert Eich5f85f172012-10-14 15:46:38 +020015450/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15451struct intel_dmi_quirk {
15452 void (*hook)(struct drm_device *dev);
15453 const struct dmi_system_id (*dmi_id_list)[];
15454};
15455
15456static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15457{
15458 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15459 return 1;
15460}
15461
15462static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15463 {
15464 .dmi_id_list = &(const struct dmi_system_id[]) {
15465 {
15466 .callback = intel_dmi_reverse_brightness,
15467 .ident = "NCR Corporation",
15468 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15469 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15470 },
15471 },
15472 { } /* terminating entry */
15473 },
15474 .hook = quirk_invert_brightness,
15475 },
15476};
15477
Ben Widawskyc43b5632012-04-16 14:07:40 -070015478static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015479 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15480 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15481
Jesse Barnesb690e962010-07-19 13:53:12 -070015482 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15483 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15484
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015485 /* 830 needs to leave pipe A & dpll A up */
15486 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15487
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015488 /* 830 needs to leave pipe B & dpll B up */
15489 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15490
Keith Packard435793d2011-07-12 14:56:22 -070015491 /* Lenovo U160 cannot use SSC on LVDS */
15492 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015493
15494 /* Sony Vaio Y cannot use SSC on LVDS */
15495 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015496
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015497 /* Acer Aspire 5734Z must invert backlight brightness */
15498 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15499
15500 /* Acer/eMachines G725 */
15501 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15502
15503 /* Acer/eMachines e725 */
15504 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15505
15506 /* Acer/Packard Bell NCL20 */
15507 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15508
15509 /* Acer Aspire 4736Z */
15510 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015511
15512 /* Acer Aspire 5336 */
15513 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015514
15515 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15516 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015517
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015518 /* Acer C720 Chromebook (Core i3 4005U) */
15519 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15520
jens steinb2a96012014-10-28 20:25:53 +010015521 /* Apple Macbook 2,1 (Core 2 T7400) */
15522 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15523
Jani Nikula1b9448b02015-11-05 11:49:59 +020015524 /* Apple Macbook 4,1 */
15525 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15526
Scot Doyled4967d82014-07-03 23:27:52 +000015527 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15528 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015529
15530 /* HP Chromebook 14 (Celeron 2955U) */
15531 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015532
15533 /* Dell Chromebook 11 */
15534 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015535
15536 /* Dell Chromebook 11 (2015 version) */
15537 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015538};
15539
15540static void intel_init_quirks(struct drm_device *dev)
15541{
15542 struct pci_dev *d = dev->pdev;
15543 int i;
15544
15545 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15546 struct intel_quirk *q = &intel_quirks[i];
15547
15548 if (d->device == q->device &&
15549 (d->subsystem_vendor == q->subsystem_vendor ||
15550 q->subsystem_vendor == PCI_ANY_ID) &&
15551 (d->subsystem_device == q->subsystem_device ||
15552 q->subsystem_device == PCI_ANY_ID))
15553 q->hook(dev);
15554 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015555 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15556 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15557 intel_dmi_quirks[i].hook(dev);
15558 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015559}
15560
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015561/* Disable the VGA plane that we never use */
15562static void i915_disable_vga(struct drm_device *dev)
15563{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015564 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015565 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015566 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015567
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015568 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015569 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015570 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015571 sr1 = inb(VGA_SR_DATA);
15572 outb(sr1 | 1<<5, VGA_SR_DATA);
15573 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15574 udelay(300);
15575
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015576 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015577 POSTING_READ(vga_reg);
15578}
15579
Daniel Vetterf8175862012-04-10 15:50:11 +020015580void intel_modeset_init_hw(struct drm_device *dev)
15581{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015582 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015583
Ville Syrjäläb6283052015-06-03 15:45:07 +030015584 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015585
15586 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15587
Daniel Vetterf8175862012-04-10 15:50:11 +020015588 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015589}
15590
Matt Roperd93c0372015-12-03 11:37:41 -080015591/*
15592 * Calculate what we think the watermarks should be for the state we've read
15593 * out of the hardware and then immediately program those watermarks so that
15594 * we ensure the hardware settings match our internal state.
15595 *
15596 * We can calculate what we think WM's should be by creating a duplicate of the
15597 * current state (which was constructed during hardware readout) and running it
15598 * through the atomic check code to calculate new watermark values in the
15599 * state object.
15600 */
15601static void sanitize_watermarks(struct drm_device *dev)
15602{
15603 struct drm_i915_private *dev_priv = to_i915(dev);
15604 struct drm_atomic_state *state;
15605 struct drm_crtc *crtc;
15606 struct drm_crtc_state *cstate;
15607 struct drm_modeset_acquire_ctx ctx;
15608 int ret;
15609 int i;
15610
15611 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015612 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015613 return;
15614
15615 /*
15616 * We need to hold connection_mutex before calling duplicate_state so
15617 * that the connector loop is protected.
15618 */
15619 drm_modeset_acquire_init(&ctx, 0);
15620retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015621 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015622 if (ret == -EDEADLK) {
15623 drm_modeset_backoff(&ctx);
15624 goto retry;
15625 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015626 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015627 }
15628
15629 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15630 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015631 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015632
Matt Ropered4a6a72016-02-23 17:20:13 -080015633 /*
15634 * Hardware readout is the only time we don't want to calculate
15635 * intermediate watermarks (since we don't trust the current
15636 * watermarks).
15637 */
15638 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15639
Matt Roperd93c0372015-12-03 11:37:41 -080015640 ret = intel_atomic_check(dev, state);
15641 if (ret) {
15642 /*
15643 * If we fail here, it means that the hardware appears to be
15644 * programmed in a way that shouldn't be possible, given our
15645 * understanding of watermark requirements. This might mean a
15646 * mistake in the hardware readout code or a mistake in the
15647 * watermark calculations for a given platform. Raise a WARN
15648 * so that this is noticeable.
15649 *
15650 * If this actually happens, we'll have to just leave the
15651 * BIOS-programmed watermarks untouched and hope for the best.
15652 */
15653 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015654 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015655 }
15656
15657 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015658 for_each_crtc_in_state(state, crtc, cstate, i) {
15659 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15660
Matt Ropered4a6a72016-02-23 17:20:13 -080015661 cs->wm.need_postvbl_update = true;
15662 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015663 }
15664
15665 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015666fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015667 drm_modeset_drop_locks(&ctx);
15668 drm_modeset_acquire_fini(&ctx);
15669}
15670
Jesse Barnes79e53942008-11-07 14:24:08 -080015671void intel_modeset_init(struct drm_device *dev)
15672{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015673 struct drm_i915_private *dev_priv = to_i915(dev);
15674 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015675 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015676 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015677 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015678
15679 drm_mode_config_init(dev);
15680
15681 dev->mode_config.min_width = 0;
15682 dev->mode_config.min_height = 0;
15683
Dave Airlie019d96c2011-09-29 16:20:42 +010015684 dev->mode_config.preferred_depth = 24;
15685 dev->mode_config.prefer_shadow = 1;
15686
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015687 dev->mode_config.allow_fb_modifiers = true;
15688
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015689 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015690
Jesse Barnesb690e962010-07-19 13:53:12 -070015691 intel_init_quirks(dev);
15692
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015693 intel_init_pm(dev);
15694
Ben Widawskye3c74752013-04-05 13:12:39 -070015695 if (INTEL_INFO(dev)->num_pipes == 0)
15696 return;
15697
Lukas Wunner69f92f62015-07-15 13:57:35 +020015698 /*
15699 * There may be no VBT; and if the BIOS enabled SSC we can
15700 * just keep using it to avoid unnecessary flicker. Whereas if the
15701 * BIOS isn't using it, don't assume it will work even if the VBT
15702 * indicates as much.
15703 */
15704 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15705 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15706 DREF_SSC1_ENABLE);
15707
15708 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15709 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15710 bios_lvds_use_ssc ? "en" : "dis",
15711 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15712 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15713 }
15714 }
15715
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015716 if (IS_GEN2(dev)) {
15717 dev->mode_config.max_width = 2048;
15718 dev->mode_config.max_height = 2048;
15719 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015720 dev->mode_config.max_width = 4096;
15721 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015722 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015723 dev->mode_config.max_width = 8192;
15724 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015725 }
Damien Lespiau068be562014-03-28 14:17:49 +000015726
Ville Syrjälädc41c152014-08-13 11:57:05 +030015727 if (IS_845G(dev) || IS_I865G(dev)) {
15728 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15729 dev->mode_config.cursor_height = 1023;
15730 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015731 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15732 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15733 } else {
15734 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15735 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15736 }
15737
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015738 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015739
Zhao Yakui28c97732009-10-09 11:39:41 +080015740 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015741 INTEL_INFO(dev)->num_pipes,
15742 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015743
Damien Lespiau055e3932014-08-18 13:49:10 +010015744 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015745 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015746 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015747 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015748 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015749 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015750 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015751 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015752 }
15753
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015754 intel_update_czclk(dev_priv);
15755 intel_update_cdclk(dev);
15756
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015757 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015758
Ville Syrjäläb2045352016-05-13 23:41:27 +030015759 if (dev_priv->max_cdclk_freq == 0)
15760 intel_update_max_cdclk(dev);
15761
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015762 /* Just disable it once at startup */
15763 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015764 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015765
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015766 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015767 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015768 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015769
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015770 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015771 struct intel_initial_plane_config plane_config = {};
15772
Jesse Barnes46f297f2014-03-07 08:57:48 -080015773 if (!crtc->active)
15774 continue;
15775
Jesse Barnes46f297f2014-03-07 08:57:48 -080015776 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015777 * Note that reserving the BIOS fb up front prevents us
15778 * from stuffing other stolen allocations like the ring
15779 * on top. This prevents some ugliness at boot time, and
15780 * can even allow for smooth boot transitions if the BIOS
15781 * fb is large enough for the active pipe configuration.
15782 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015783 dev_priv->display.get_initial_plane_config(crtc,
15784 &plane_config);
15785
15786 /*
15787 * If the fb is shared between multiple heads, we'll
15788 * just get the first one.
15789 */
15790 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015791 }
Matt Roperd93c0372015-12-03 11:37:41 -080015792
15793 /*
15794 * Make sure hardware watermarks really match the state we read out.
15795 * Note that we need to do this after reconstructing the BIOS fb's
15796 * since the watermark calculation done here will use pstate->fb.
15797 */
15798 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015799}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015800
Daniel Vetter7fad7982012-07-04 17:51:47 +020015801static void intel_enable_pipe_a(struct drm_device *dev)
15802{
15803 struct intel_connector *connector;
15804 struct drm_connector *crt = NULL;
15805 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015806 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015807
15808 /* We can't just switch on the pipe A, we need to set things up with a
15809 * proper mode and output configuration. As a gross hack, enable pipe A
15810 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015811 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015812 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15813 crt = &connector->base;
15814 break;
15815 }
15816 }
15817
15818 if (!crt)
15819 return;
15820
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015821 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015822 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015823}
15824
Daniel Vetterfa555832012-10-10 23:14:00 +020015825static bool
15826intel_check_plane_mapping(struct intel_crtc *crtc)
15827{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015828 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015829 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015830 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015831
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015832 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015833 return true;
15834
Ville Syrjälä649636e2015-09-22 19:50:01 +030015835 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015836
15837 if ((val & DISPLAY_PLANE_ENABLE) &&
15838 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15839 return false;
15840
15841 return true;
15842}
15843
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015844static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15845{
15846 struct drm_device *dev = crtc->base.dev;
15847 struct intel_encoder *encoder;
15848
15849 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15850 return true;
15851
15852 return false;
15853}
15854
Ville Syrjälädd756192016-02-17 21:28:45 +020015855static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15856{
15857 struct drm_device *dev = encoder->base.dev;
15858 struct intel_connector *connector;
15859
15860 for_each_connector_on_encoder(dev, &encoder->base, connector)
15861 return true;
15862
15863 return false;
15864}
15865
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015866static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15867 enum transcoder pch_transcoder)
15868{
15869 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15870 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15871}
15872
Daniel Vetter24929352012-07-02 20:28:59 +020015873static void intel_sanitize_crtc(struct intel_crtc *crtc)
15874{
15875 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015876 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015877 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015878
Daniel Vetter24929352012-07-02 20:28:59 +020015879 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015880 if (!transcoder_is_dsi(cpu_transcoder)) {
15881 i915_reg_t reg = PIPECONF(cpu_transcoder);
15882
15883 I915_WRITE(reg,
15884 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15885 }
Daniel Vetter24929352012-07-02 20:28:59 +020015886
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015887 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015888 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015889 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015890 struct intel_plane *plane;
15891
Daniel Vetter96256042015-02-13 21:03:42 +010015892 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015893
15894 /* Disable everything but the primary plane */
15895 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15896 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15897 continue;
15898
15899 plane->disable_plane(&plane->base, &crtc->base);
15900 }
Daniel Vetter96256042015-02-13 21:03:42 +010015901 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015902
Daniel Vetter24929352012-07-02 20:28:59 +020015903 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015904 * disable the crtc (and hence change the state) if it is wrong. Note
15905 * that gen4+ has a fixed plane -> pipe mapping. */
15906 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015907 bool plane;
15908
Ville Syrjälä78108b72016-05-27 20:59:19 +030015909 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15910 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015911
15912 /* Pipe has the wrong plane attached and the plane is active.
15913 * Temporarily change the plane mapping and disable everything
15914 * ... */
15915 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015916 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015917 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015918 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015919 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015920 }
Daniel Vetter24929352012-07-02 20:28:59 +020015921
Daniel Vetter7fad7982012-07-04 17:51:47 +020015922 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15923 crtc->pipe == PIPE_A && !crtc->active) {
15924 /* BIOS forgot to enable pipe A, this mostly happens after
15925 * resume. Force-enable the pipe to fix this, the update_dpms
15926 * call below we restore the pipe to the right state, but leave
15927 * the required bits on. */
15928 intel_enable_pipe_a(dev);
15929 }
15930
Daniel Vetter24929352012-07-02 20:28:59 +020015931 /* Adjust the state of the output pipe according to whether we
15932 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015933 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015934 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015935
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015936 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015937 /*
15938 * We start out with underrun reporting disabled to avoid races.
15939 * For correct bookkeeping mark this on active crtcs.
15940 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015941 * Also on gmch platforms we dont have any hardware bits to
15942 * disable the underrun reporting. Which means we need to start
15943 * out with underrun reporting disabled also on inactive pipes,
15944 * since otherwise we'll complain about the garbage we read when
15945 * e.g. coming up after runtime pm.
15946 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015947 * No protection against concurrent access is required - at
15948 * worst a fifo underrun happens which also sets this to false.
15949 */
15950 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015951 /*
15952 * We track the PCH trancoder underrun reporting state
15953 * within the crtc. With crtc for pipe A housing the underrun
15954 * reporting state for PCH transcoder A, crtc for pipe B housing
15955 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15956 * and marking underrun reporting as disabled for the non-existing
15957 * PCH transcoders B and C would prevent enabling the south
15958 * error interrupt (see cpt_can_enable_serr_int()).
15959 */
15960 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15961 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015962 }
Daniel Vetter24929352012-07-02 20:28:59 +020015963}
15964
15965static void intel_sanitize_encoder(struct intel_encoder *encoder)
15966{
15967 struct intel_connector *connector;
15968 struct drm_device *dev = encoder->base.dev;
15969
15970 /* We need to check both for a crtc link (meaning that the
15971 * encoder is active and trying to read from a pipe) and the
15972 * pipe itself being active. */
15973 bool has_active_crtc = encoder->base.crtc &&
15974 to_intel_crtc(encoder->base.crtc)->active;
15975
Ville Syrjälädd756192016-02-17 21:28:45 +020015976 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015977 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15978 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015979 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015980
15981 /* Connector is active, but has no active pipe. This is
15982 * fallout from our resume register restoring. Disable
15983 * the encoder manually again. */
15984 if (encoder->base.crtc) {
15985 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15986 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015987 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015988 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015989 if (encoder->post_disable)
15990 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015991 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015992 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015993
15994 /* Inconsistent output/port/pipe state happens presumably due to
15995 * a bug in one of the get_hw_state functions. Or someplace else
15996 * in our code, like the register restore mess on resume. Clamp
15997 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015998 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015999 if (connector->encoder != encoder)
16000 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020016001 connector->base.dpms = DRM_MODE_DPMS_OFF;
16002 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016003 }
16004 }
16005 /* Enabled encoders without active connectors will be fixed in
16006 * the crtc fixup. */
16007}
16008
Imre Deak04098752014-02-18 00:02:16 +020016009void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016010{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016011 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020016012 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016013
Imre Deak04098752014-02-18 00:02:16 +020016014 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16015 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16016 i915_disable_vga(dev);
16017 }
16018}
16019
16020void i915_redisable_vga(struct drm_device *dev)
16021{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016022 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020016023
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016024 /* This function can be called both from intel_modeset_setup_hw_state or
16025 * at a very early point in our resume sequence, where the power well
16026 * structures are not yet restored. Since this function is at a very
16027 * paranoid "someone might have enabled VGA while we were not looking"
16028 * level, just check if the power well is enabled instead of trying to
16029 * follow the "don't touch the power well if we don't need it" policy
16030 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016031 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016032 return;
16033
Imre Deak04098752014-02-18 00:02:16 +020016034 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016035
16036 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016037}
16038
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016039static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016040{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016041 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016042
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016043 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016044}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016045
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016046/* FIXME read out full plane state for all planes */
16047static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016048{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016049 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016050 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016051 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016052
Matt Roper19b8d382015-09-24 15:53:17 -070016053 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016054 primary_get_hw_state(to_intel_plane(primary));
16055
16056 if (plane_state->visible)
16057 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016058}
16059
Daniel Vetter30e984d2013-06-05 13:34:17 +020016060static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016061{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016062 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016063 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016064 struct intel_crtc *crtc;
16065 struct intel_encoder *encoder;
16066 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016067 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016068
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016069 dev_priv->active_crtcs = 0;
16070
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016071 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016072 struct intel_crtc_state *crtc_state = crtc->config;
16073 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016074
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016075 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016076 memset(crtc_state, 0, sizeof(*crtc_state));
16077 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016078
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016079 crtc_state->base.active = crtc_state->base.enable =
16080 dev_priv->display.get_pipe_config(crtc, crtc_state);
16081
16082 crtc->base.enabled = crtc_state->base.enable;
16083 crtc->active = crtc_state->base.active;
16084
16085 if (crtc_state->base.active) {
16086 dev_priv->active_crtcs |= 1 << crtc->pipe;
16087
Clint Taylorc89e39f2016-05-13 23:41:21 +030016088 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016089 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016090 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016091 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16092 else
16093 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016094
16095 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16096 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16097 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016098 }
16099
16100 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016101
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016102 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016103
Ville Syrjälä78108b72016-05-27 20:59:19 +030016104 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16105 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016106 crtc->active ? "enabled" : "disabled");
16107 }
16108
Daniel Vetter53589012013-06-05 13:34:16 +020016109 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16110 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16111
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016112 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16113 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016114 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016115 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016116 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016117 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016118 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016119 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016120
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016121 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016122 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016123 }
16124
Damien Lespiaub2784e12014-08-05 11:29:37 +010016125 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016126 pipe = 0;
16127
16128 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016129 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16130 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016131 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016132 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016133 } else {
16134 encoder->base.crtc = NULL;
16135 }
16136
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016137 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016138 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016139 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016140 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016141 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016142 }
16143
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016144 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016145 if (connector->get_hw_state(connector)) {
16146 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016147
16148 encoder = connector->encoder;
16149 connector->base.encoder = &encoder->base;
16150
16151 if (encoder->base.crtc &&
16152 encoder->base.crtc->state->active) {
16153 /*
16154 * This has to be done during hardware readout
16155 * because anything calling .crtc_disable may
16156 * rely on the connector_mask being accurate.
16157 */
16158 encoder->base.crtc->state->connector_mask |=
16159 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016160 encoder->base.crtc->state->encoder_mask |=
16161 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016162 }
16163
Daniel Vetter24929352012-07-02 20:28:59 +020016164 } else {
16165 connector->base.dpms = DRM_MODE_DPMS_OFF;
16166 connector->base.encoder = NULL;
16167 }
16168 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16169 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016170 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016171 connector->base.encoder ? "enabled" : "disabled");
16172 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016173
16174 for_each_intel_crtc(dev, crtc) {
16175 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16176
16177 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16178 if (crtc->base.state->active) {
16179 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16180 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16181 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16182
16183 /*
16184 * The initial mode needs to be set in order to keep
16185 * the atomic core happy. It wants a valid mode if the
16186 * crtc's enabled, so we do the above call.
16187 *
16188 * At this point some state updated by the connectors
16189 * in their ->detect() callback has not run yet, so
16190 * no recalculation can be done yet.
16191 *
16192 * Even if we could do a recalculation and modeset
16193 * right now it would cause a double modeset if
16194 * fbdev or userspace chooses a different initial mode.
16195 *
16196 * If that happens, someone indicated they wanted a
16197 * mode change, which means it's safe to do a full
16198 * recalculation.
16199 */
16200 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016201
16202 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16203 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016204 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016205
16206 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016207 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016208}
16209
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016210/* Scan out the current hw modeset state,
16211 * and sanitizes it to the current state
16212 */
16213static void
16214intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016215{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016216 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016217 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016218 struct intel_crtc *crtc;
16219 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016220 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016221
16222 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016223
16224 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016225 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016226 intel_sanitize_encoder(encoder);
16227 }
16228
Damien Lespiau055e3932014-08-18 13:49:10 +010016229 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016230 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16231 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016232 intel_dump_pipe_config(crtc, crtc->config,
16233 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016234 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016235
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016236 intel_modeset_update_connector_atomic_state(dev);
16237
Daniel Vetter35c95372013-07-17 06:55:04 +020016238 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16239 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16240
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016241 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016242 continue;
16243
16244 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16245
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016246 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016247 pll->on = false;
16248 }
16249
Wayne Boyer666a4532015-12-09 12:29:35 -080016250 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016251 vlv_wm_get_hw_state(dev);
16252 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016253 skl_wm_get_hw_state(dev);
16254 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016255 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016256
16257 for_each_intel_crtc(dev, crtc) {
16258 unsigned long put_domains;
16259
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016260 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016261 if (WARN_ON(put_domains))
16262 modeset_put_power_domains(dev_priv, put_domains);
16263 }
16264 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016265
16266 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016267}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016268
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016269void intel_display_resume(struct drm_device *dev)
16270{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016271 struct drm_i915_private *dev_priv = to_i915(dev);
16272 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16273 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016274 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020016275
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016276 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030016277 if (state)
16278 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016279
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016280 /*
16281 * This is a cludge because with real atomic modeset mode_config.mutex
16282 * won't be taken. Unfortunately some probed state like
16283 * audio_codec_enable is still protected by mode_config.mutex, so lock
16284 * it here for now.
16285 */
16286 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016287 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016288
Maarten Lankhorst73974892016-08-05 23:28:27 +030016289 while (1) {
16290 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16291 if (ret != -EDEADLK)
16292 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016293
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016294 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016295 }
16296
Maarten Lankhorst73974892016-08-05 23:28:27 +030016297 if (!ret)
16298 ret = __intel_display_resume(dev, state);
16299
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016300 drm_modeset_drop_locks(&ctx);
16301 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016302 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016303
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016304 if (ret) {
16305 DRM_ERROR("Restoring old state failed with %i\n", ret);
16306 drm_atomic_state_free(state);
16307 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016308}
16309
16310void intel_modeset_gem_init(struct drm_device *dev)
16311{
Chris Wilsondc979972016-05-10 14:10:04 +010016312 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016313 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016314 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016315 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016316
Chris Wilsondc979972016-05-10 14:10:04 +010016317 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016318
Chris Wilson1833b132012-05-09 11:56:28 +010016319 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016320
Chris Wilson1ee8da62016-05-12 12:43:23 +010016321 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016322
16323 /*
16324 * Make sure any fbs we allocated at startup are properly
16325 * pinned & fenced. When we do the allocation it's too early
16326 * for this.
16327 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016328 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016329 obj = intel_fb_obj(c->primary->fb);
16330 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016331 continue;
16332
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016333 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016334 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16335 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016336 mutex_unlock(&dev->struct_mutex);
16337 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016338 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16339 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016340 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016341 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016342 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016343 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016344 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016345 }
16346 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016347}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016348
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016349int intel_connector_register(struct drm_connector *connector)
16350{
16351 struct intel_connector *intel_connector = to_intel_connector(connector);
16352 int ret;
16353
16354 ret = intel_backlight_device_register(intel_connector);
16355 if (ret)
16356 goto err;
16357
16358 return 0;
16359
16360err:
16361 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016362}
16363
Chris Wilsonc191eca2016-06-17 11:40:33 +010016364void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016365{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016366 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016367
Chris Wilsone63d87c2016-06-17 11:40:34 +010016368 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016369 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016370}
16371
Jesse Barnes79e53942008-11-07 14:24:08 -080016372void intel_modeset_cleanup(struct drm_device *dev)
16373{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016374 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016375
Chris Wilsondc979972016-05-10 14:10:04 +010016376 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016377
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016378 /*
16379 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016380 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016381 * experience fancy races otherwise.
16382 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016383 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016384
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016385 /*
16386 * Due to the hpd irq storm handling the hotplug work can re-arm the
16387 * poll handlers. Hence disable polling after hpd handling is shut down.
16388 */
Keith Packardf87ea762010-10-03 19:36:26 -070016389 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016390
Jesse Barnes723bfd72010-10-07 16:01:13 -070016391 intel_unregister_dsm_handler();
16392
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016393 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016394
Chris Wilson1630fe72011-07-08 12:22:42 +010016395 /* flush any delayed tasks or pending work */
16396 flush_scheduled_work();
16397
Jesse Barnes79e53942008-11-07 14:24:08 -080016398 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016399
Chris Wilson1ee8da62016-05-12 12:43:23 +010016400 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016401
Chris Wilsondc979972016-05-10 14:10:04 +010016402 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016403
16404 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016405}
16406
Chris Wilsondf0e9242010-09-09 16:20:55 +010016407void intel_connector_attach_encoder(struct intel_connector *connector,
16408 struct intel_encoder *encoder)
16409{
16410 connector->encoder = encoder;
16411 drm_mode_connector_attach_encoder(&connector->base,
16412 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016413}
Dave Airlie28d52042009-09-21 14:33:58 +100016414
16415/*
16416 * set vga decode state - true == enable VGA decode
16417 */
16418int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16419{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016420 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016421 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016422 u16 gmch_ctrl;
16423
Chris Wilson75fa0412014-02-07 18:37:02 -020016424 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16425 DRM_ERROR("failed to read control word\n");
16426 return -EIO;
16427 }
16428
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016429 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16430 return 0;
16431
Dave Airlie28d52042009-09-21 14:33:58 +100016432 if (state)
16433 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16434 else
16435 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016436
16437 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16438 DRM_ERROR("failed to write control word\n");
16439 return -EIO;
16440 }
16441
Dave Airlie28d52042009-09-21 14:33:58 +100016442 return 0;
16443}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016444
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016445struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016446
16447 u32 power_well_driver;
16448
Chris Wilson63b66e52013-08-08 15:12:06 +020016449 int num_transcoders;
16450
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016451 struct intel_cursor_error_state {
16452 u32 control;
16453 u32 position;
16454 u32 base;
16455 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016456 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016457
16458 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016459 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016460 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016461 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016462 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016463
16464 struct intel_plane_error_state {
16465 u32 control;
16466 u32 stride;
16467 u32 size;
16468 u32 pos;
16469 u32 addr;
16470 u32 surface;
16471 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016472 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016473
16474 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016475 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016476 enum transcoder cpu_transcoder;
16477
16478 u32 conf;
16479
16480 u32 htotal;
16481 u32 hblank;
16482 u32 hsync;
16483 u32 vtotal;
16484 u32 vblank;
16485 u32 vsync;
16486 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016487};
16488
16489struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016490intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016491{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016492 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016493 int transcoders[] = {
16494 TRANSCODER_A,
16495 TRANSCODER_B,
16496 TRANSCODER_C,
16497 TRANSCODER_EDP,
16498 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016499 int i;
16500
Chris Wilsonc0336662016-05-06 15:40:21 +010016501 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016502 return NULL;
16503
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016504 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016505 if (error == NULL)
16506 return NULL;
16507
Chris Wilsonc0336662016-05-06 15:40:21 +010016508 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016509 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16510
Damien Lespiau055e3932014-08-18 13:49:10 +010016511 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016512 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016513 __intel_display_power_is_enabled(dev_priv,
16514 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016515 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016516 continue;
16517
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016518 error->cursor[i].control = I915_READ(CURCNTR(i));
16519 error->cursor[i].position = I915_READ(CURPOS(i));
16520 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016521
16522 error->plane[i].control = I915_READ(DSPCNTR(i));
16523 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016524 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016525 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016526 error->plane[i].pos = I915_READ(DSPPOS(i));
16527 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016528 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016529 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016530 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016531 error->plane[i].surface = I915_READ(DSPSURF(i));
16532 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16533 }
16534
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016535 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016536
Chris Wilsonc0336662016-05-06 15:40:21 +010016537 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016538 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016539 }
16540
Jani Nikula4d1de972016-03-18 17:05:42 +020016541 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016542 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016543 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016544 error->num_transcoders++; /* Account for eDP. */
16545
16546 for (i = 0; i < error->num_transcoders; i++) {
16547 enum transcoder cpu_transcoder = transcoders[i];
16548
Imre Deakddf9c532013-11-27 22:02:02 +020016549 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016550 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016551 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016552 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016553 continue;
16554
Chris Wilson63b66e52013-08-08 15:12:06 +020016555 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16556
16557 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16558 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16559 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16560 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16561 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16562 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16563 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016564 }
16565
16566 return error;
16567}
16568
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016569#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16570
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016571void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016572intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016573 struct drm_device *dev,
16574 struct intel_display_error_state *error)
16575{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016576 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016577 int i;
16578
Chris Wilson63b66e52013-08-08 15:12:06 +020016579 if (!error)
16580 return;
16581
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016582 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016583 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016584 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016585 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016586 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016587 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016588 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016589 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016590 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016591 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016592
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016593 err_printf(m, "Plane [%d]:\n", i);
16594 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16595 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016596 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016597 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16598 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016599 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016600 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016601 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016602 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016603 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16604 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016605 }
16606
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016607 err_printf(m, "Cursor [%d]:\n", i);
16608 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16609 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16610 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016611 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016612
16613 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016614 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016615 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016616 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016617 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016618 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16619 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16620 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16621 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16622 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16623 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16624 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16625 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016626}