blob: 9a3919b19413abae27070c34ac822825eefec93e [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 Wilson57822dc2017-02-22 11:40:48 +000040#include "i915_gem_clflush.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
Chris Wilson24dbf512017-02-15 10:59:18 +0000100static int intel_framebuffer_init(struct intel_framebuffer *ifb,
101 struct drm_i915_gem_object *obj,
102 struct drm_mode_fb_cmd2 *mode_cmd);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Nabendu Maiti1c74eea2016-11-29 11:23:14 +0530118static void intel_crtc_init_scalers(struct intel_crtc *crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +0300123static void intel_modeset_setup_hw_state(struct drm_device *dev,
124 struct drm_modeset_acquire_ctx *ctx);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100126
Ma Lingd4906092009-03-18 20:13:27 +0800127struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300128 struct {
129 int min, max;
130 } dot, vco, n, m, m1, m2, p, p1;
131
132 struct {
133 int dot_limit;
134 int p2_slow, p2_fast;
135 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800136};
Jesse Barnes79e53942008-11-07 14:24:08 -0800137
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300138/* returns HPLL frequency in kHz */
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200139int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300140{
141 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
142
143 /* Obtain SKU information */
144 mutex_lock(&dev_priv->sb_lock);
145 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
146 CCK_FUSE_HPLL_FREQ_MASK;
147 mutex_unlock(&dev_priv->sb_lock);
148
149 return vco_freq[hpll_freq] * 1000;
150}
151
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200152int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
153 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300154{
155 u32 val;
156 int divider;
157
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200168 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
169}
170
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +0200171int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
172 const char *name, u32 reg)
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200173{
174 if (dev_priv->hpll_freq == 0)
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200175 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200176
177 return vlv_get_cck_clock(dev_priv, name, reg,
178 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300179}
180
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300181static void intel_update_czclk(struct drm_i915_private *dev_priv)
182{
Wayne Boyer666a4532015-12-09 12:29:35 -0800183 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300184 return;
185
186 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
187 CCK_CZ_CLOCK_CONTROL);
188
189 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
190}
191
Chris Wilson021357a2010-09-07 20:54:59 +0100192static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200193intel_fdi_link_freq(struct drm_i915_private *dev_priv,
194 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100195{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200196 if (HAS_DDI(dev_priv))
197 return pipe_config->port_clock; /* SPLL */
198 else if (IS_GEN5(dev_priv))
199 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200200 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200201 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100202}
203
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300204static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200206 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200207 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400208 .m = { .min = 96, .max = 140 },
209 .m1 = { .min = 18, .max = 26 },
210 .m2 = { .min = 6, .max = 16 },
211 .p = { .min = 4, .max = 128 },
212 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700213 .p2 = { .dot_limit = 165000,
214 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700215};
216
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300217static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200218 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200219 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200220 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200221 .m = { .min = 96, .max = 140 },
222 .m1 = { .min = 18, .max = 26 },
223 .m2 = { .min = 6, .max = 16 },
224 .p = { .min = 4, .max = 128 },
225 .p1 = { .min = 2, .max = 33 },
226 .p2 = { .dot_limit = 165000,
227 .p2_slow = 4, .p2_fast = 4 },
228};
229
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300230static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400231 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200232 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200233 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400234 .m = { .min = 96, .max = 140 },
235 .m1 = { .min = 18, .max = 26 },
236 .m2 = { .min = 6, .max = 16 },
237 .p = { .min = 4, .max = 128 },
238 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700239 .p2 = { .dot_limit = 165000,
240 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700241};
Eric Anholt273e27c2011-03-30 13:01:10 -0700242
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300243static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400244 .dot = { .min = 20000, .max = 400000 },
245 .vco = { .min = 1400000, .max = 2800000 },
246 .n = { .min = 1, .max = 6 },
247 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100248 .m1 = { .min = 8, .max = 18 },
249 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400250 .p = { .min = 5, .max = 80 },
251 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700252 .p2 = { .dot_limit = 200000,
253 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700254};
255
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300256static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400257 .dot = { .min = 20000, .max = 400000 },
258 .vco = { .min = 1400000, .max = 2800000 },
259 .n = { .min = 1, .max = 6 },
260 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100261 .m1 = { .min = 8, .max = 18 },
262 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .p = { .min = 7, .max = 98 },
264 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .p2 = { .dot_limit = 112000,
266 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700267};
268
Eric Anholt273e27c2011-03-30 13:01:10 -0700269
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300270static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .dot = { .min = 25000, .max = 270000 },
272 .vco = { .min = 1750000, .max = 3500000},
273 .n = { .min = 1, .max = 4 },
274 .m = { .min = 104, .max = 138 },
275 .m1 = { .min = 17, .max = 23 },
276 .m2 = { .min = 5, .max = 11 },
277 .p = { .min = 10, .max = 30 },
278 .p1 = { .min = 1, .max = 3},
279 .p2 = { .dot_limit = 270000,
280 .p2_slow = 10,
281 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800282 },
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300285static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .dot = { .min = 22000, .max = 400000 },
287 .vco = { .min = 1750000, .max = 3500000},
288 .n = { .min = 1, .max = 4 },
289 .m = { .min = 104, .max = 138 },
290 .m1 = { .min = 16, .max = 23 },
291 .m2 = { .min = 5, .max = 11 },
292 .p = { .min = 5, .max = 80 },
293 .p1 = { .min = 1, .max = 8},
294 .p2 = { .dot_limit = 165000,
295 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700296};
297
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300298static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .dot = { .min = 20000, .max = 115000 },
300 .vco = { .min = 1750000, .max = 3500000 },
301 .n = { .min = 1, .max = 3 },
302 .m = { .min = 104, .max = 138 },
303 .m1 = { .min = 17, .max = 23 },
304 .m2 = { .min = 5, .max = 11 },
305 .p = { .min = 28, .max = 112 },
306 .p1 = { .min = 2, .max = 8 },
307 .p2 = { .dot_limit = 0,
308 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800309 },
Keith Packarde4b36692009-06-05 19:22:17 -0700310};
311
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300312static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700313 .dot = { .min = 80000, .max = 224000 },
314 .vco = { .min = 1750000, .max = 3500000 },
315 .n = { .min = 1, .max = 3 },
316 .m = { .min = 104, .max = 138 },
317 .m1 = { .min = 17, .max = 23 },
318 .m2 = { .min = 5, .max = 11 },
319 .p = { .min = 14, .max = 42 },
320 .p1 = { .min = 2, .max = 6 },
321 .p2 = { .dot_limit = 0,
322 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800323 },
Keith Packarde4b36692009-06-05 19:22:17 -0700324};
325
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300326static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400327 .dot = { .min = 20000, .max = 400000},
328 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400330 .n = { .min = 3, .max = 6 },
331 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700332 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400333 .m1 = { .min = 0, .max = 0 },
334 .m2 = { .min = 0, .max = 254 },
335 .p = { .min = 5, .max = 80 },
336 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .p2 = { .dot_limit = 200000,
338 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700339};
340
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300341static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400342 .dot = { .min = 20000, .max = 400000 },
343 .vco = { .min = 1700000, .max = 3500000 },
344 .n = { .min = 3, .max = 6 },
345 .m = { .min = 2, .max = 256 },
346 .m1 = { .min = 0, .max = 0 },
347 .m2 = { .min = 0, .max = 254 },
348 .p = { .min = 7, .max = 112 },
349 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .p2 = { .dot_limit = 112000,
351 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700352};
353
Eric Anholt273e27c2011-03-30 13:01:10 -0700354/* Ironlake / Sandybridge
355 *
356 * We calculate clock using (register_value + 2) for N/M1/M2, so here
357 * the range value for them is (actual_value - 2).
358 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300359static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .dot = { .min = 25000, .max = 350000 },
361 .vco = { .min = 1760000, .max = 3510000 },
362 .n = { .min = 1, .max = 5 },
363 .m = { .min = 79, .max = 127 },
364 .m1 = { .min = 12, .max = 22 },
365 .m2 = { .min = 5, .max = 9 },
366 .p = { .min = 5, .max = 80 },
367 .p1 = { .min = 1, .max = 8 },
368 .p2 = { .dot_limit = 225000,
369 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700370};
371
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300372static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .dot = { .min = 25000, .max = 350000 },
374 .vco = { .min = 1760000, .max = 3510000 },
375 .n = { .min = 1, .max = 3 },
376 .m = { .min = 79, .max = 118 },
377 .m1 = { .min = 12, .max = 22 },
378 .m2 = { .min = 5, .max = 9 },
379 .p = { .min = 28, .max = 112 },
380 .p1 = { .min = 2, .max = 8 },
381 .p2 = { .dot_limit = 225000,
382 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800383};
384
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300385static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700386 .dot = { .min = 25000, .max = 350000 },
387 .vco = { .min = 1760000, .max = 3510000 },
388 .n = { .min = 1, .max = 3 },
389 .m = { .min = 79, .max = 127 },
390 .m1 = { .min = 12, .max = 22 },
391 .m2 = { .min = 5, .max = 9 },
392 .p = { .min = 14, .max = 56 },
393 .p1 = { .min = 2, .max = 8 },
394 .p2 = { .dot_limit = 225000,
395 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800396};
397
Eric Anholt273e27c2011-03-30 13:01:10 -0700398/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700400 .dot = { .min = 25000, .max = 350000 },
401 .vco = { .min = 1760000, .max = 3510000 },
402 .n = { .min = 1, .max = 2 },
403 .m = { .min = 79, .max = 126 },
404 .m1 = { .min = 12, .max = 22 },
405 .m2 = { .min = 5, .max = 9 },
406 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400407 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 225000,
409 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800410};
411
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300412static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700413 .dot = { .min = 25000, .max = 350000 },
414 .vco = { .min = 1760000, .max = 3510000 },
415 .n = { .min = 1, .max = 3 },
416 .m = { .min = 79, .max = 126 },
417 .m1 = { .min = 12, .max = 22 },
418 .m2 = { .min = 5, .max = 9 },
419 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400420 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700421 .p2 = { .dot_limit = 225000,
422 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800423};
424
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300425static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300426 /*
427 * These are the data rate limits (measured in fast clocks)
428 * since those are the strictest limits we have. The fast
429 * clock and actual rate limits are more relaxed, so checking
430 * them would make no difference.
431 */
432 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200433 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700434 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700435 .m1 = { .min = 2, .max = 3 },
436 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300437 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300438 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700439};
440
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300441static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300442 /*
443 * These are the data rate limits (measured in fast clocks)
444 * since those are the strictest limits we have. The fast
445 * clock and actual rate limits are more relaxed, so checking
446 * them would make no difference.
447 */
448 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200449 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300450 .n = { .min = 1, .max = 1 },
451 .m1 = { .min = 2, .max = 2 },
452 .m2 = { .min = 24 << 22, .max = 175 << 22 },
453 .p1 = { .min = 2, .max = 4 },
454 .p2 = { .p2_slow = 1, .p2_fast = 14 },
455};
456
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200458 /* FIXME: find real dot limits */
459 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530460 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200461 .n = { .min = 1, .max = 1 },
462 .m1 = { .min = 2, .max = 2 },
463 /* FIXME: find real m2 limits */
464 .m2 = { .min = 2 << 22, .max = 255 << 22 },
465 .p1 = { .min = 2, .max = 4 },
466 .p2 = { .p2_slow = 1, .p2_fast = 20 },
467};
468
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200469static bool
470needs_modeset(struct drm_crtc_state *state)
471{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200472 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200473}
474
Imre Deakdccbea32015-06-22 23:35:51 +0300475/*
476 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
477 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
478 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
479 * The helpers' return value is the rate of the clock that is fed to the
480 * display engine's pipe which can be the above fast dot clock rate or a
481 * divided-down version of it.
482 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500483/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300484static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800485{
Shaohua Li21778322009-02-23 15:19:16 +0800486 clock->m = clock->m2 + 2;
487 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200488 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300489 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300490 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
491 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300492
493 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800494}
495
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200496static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
497{
498 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
499}
500
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300501static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800502{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200503 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800504 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200505 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300506 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300507 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
508 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300509
510 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800511}
512
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300513static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300514{
515 clock->m = clock->m1 * clock->m2;
516 clock->p = clock->p1 * clock->p2;
517 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300518 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300519 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
520 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300521
522 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300523}
524
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300525int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300526{
527 clock->m = clock->m1 * clock->m2;
528 clock->p = clock->p1 * clock->p2;
529 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300530 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300531 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
532 clock->n << 22);
533 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300534
535 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300536}
537
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800538#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800539/**
540 * Returns whether the given set of divisors are valid for a given refclk with
541 * the given connectors.
542 */
543
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100544static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300545 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300546 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800547{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300548 if (clock->n < limit->n.min || limit->n.max < clock->n)
549 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400551 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400553 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800554 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400555 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300556
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100557 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200558 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300559 if (clock->m1 <= clock->m2)
560 INTELPllInvalid("m1 <= m2\n");
561
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100562 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200563 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300564 if (clock->p < limit->p.min || limit->p.max < clock->p)
565 INTELPllInvalid("p out of range\n");
566 if (clock->m < limit->m.min || limit->m.max < clock->m)
567 INTELPllInvalid("m out of range\n");
568 }
569
Jesse Barnes79e53942008-11-07 14:24:08 -0800570 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400571 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800572 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
573 * connector, etc., rather than just a single range.
574 */
575 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400576 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800577
578 return true;
579}
580
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300581static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300582i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300583 const struct intel_crtc_state *crtc_state,
584 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800585{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300586 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800587
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300588 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100590 * For LVDS just rely on its current settings for dual-channel.
591 * We haven't figured out how to reliably set up different
592 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800593 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100594 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300595 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300597 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 } else {
599 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300600 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800601 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300602 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800603 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300604}
605
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200606/*
607 * Returns a set of divisors for the desired target clock with the given
608 * refclk, or FALSE. The returned values represent the clock equation:
609 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
610 *
611 * Target and reference clocks are specified in kHz.
612 *
613 * If match_clock is provided, then best_clock P divider must match the P
614 * divider from @match_clock used for LVDS downclocking.
615 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300616static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300617i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300618 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300619 int target, int refclk, struct dpll *match_clock,
620 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300621{
622 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300623 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300624 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800625
Akshay Joshi0206e352011-08-16 15:34:10 -0400626 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800627
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300628 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
629
Zhao Yakui42158662009-11-20 11:24:18 +0800630 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
631 clock.m1++) {
632 for (clock.m2 = limit->m2.min;
633 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200634 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800635 break;
636 for (clock.n = limit->n.min;
637 clock.n <= limit->n.max; clock.n++) {
638 for (clock.p1 = limit->p1.min;
639 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800640 int this_err;
641
Imre Deakdccbea32015-06-22 23:35:51 +0300642 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100643 if (!intel_PLL_is_valid(to_i915(dev),
644 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000645 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800647 if (match_clock &&
648 clock.p != match_clock->p)
649 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650
651 this_err = abs(clock.dot - target);
652 if (this_err < err) {
653 *best_clock = clock;
654 err = this_err;
655 }
656 }
657 }
658 }
659 }
660
661 return (err != target);
662}
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 */
Ma Lingd4906092009-03-18 20:13:27 +0800674static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300675pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200676 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)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200679{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300681 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200682 int err = target;
683
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200684 memset(best_clock, 0, sizeof(*best_clock));
685
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200688 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 Vetterac58c3f2013-06-01 17:16:17 +0200692 for (clock.n = limit->n.min;
693 clock.n <= limit->n.max; clock.n++) {
694 for (clock.p1 = limit->p1.min;
695 clock.p1 <= limit->p1.max; clock.p1++) {
696 int this_err;
697
Imre Deakdccbea32015-06-22 23:35:51 +0300698 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100699 if (!intel_PLL_is_valid(to_i915(dev),
700 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 &clock))
702 continue;
703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200729 */
Ma Lingd4906092009-03-18 20:13:27 +0800730static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300731g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200732 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800735{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300736 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300737 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800738 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400740 /* approximately equals target * 0.00585 */
741 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800742
743 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300744
745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
Ma Lingd4906092009-03-18 20:13:27 +0800747 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200748 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800749 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200750 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800751 for (clock.m1 = limit->m1.max;
752 clock.m1 >= limit->m1.min; clock.m1--) {
753 for (clock.m2 = limit->m2.max;
754 clock.m2 >= limit->m2.min; clock.m2--) {
755 for (clock.p1 = limit->p1.max;
756 clock.p1 >= limit->p1.min; clock.p1--) {
757 int this_err;
758
Imre Deakdccbea32015-06-22 23:35:51 +0300759 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100760 if (!intel_PLL_is_valid(to_i915(dev),
761 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000762 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800763 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000764
765 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800766 if (this_err < err_most) {
767 *best_clock = clock;
768 err_most = this_err;
769 max_n = clock.n;
770 found = true;
771 }
772 }
773 }
774 }
775 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800776 return found;
777}
Ma Lingd4906092009-03-18 20:13:27 +0800778
Imre Deakd5dd62b2015-03-17 11:40:03 +0200779/*
780 * Check if the calculated PLL configuration is more optimal compared to the
781 * best configuration and error found so far. Return the calculated error.
782 */
783static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300784 const struct dpll *calculated_clock,
785 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200786 unsigned int best_error_ppm,
787 unsigned int *error_ppm)
788{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200789 /*
790 * For CHV ignore the error and consider only the P value.
791 * Prefer a bigger P value based on HW requirements.
792 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100793 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200794 *error_ppm = 0;
795
796 return calculated_clock->p > best_clock->p;
797 }
798
Imre Deak24be4e42015-03-17 11:40:04 +0200799 if (WARN_ON_ONCE(!target_freq))
800 return false;
801
Imre Deakd5dd62b2015-03-17 11:40:03 +0200802 *error_ppm = div_u64(1000000ULL *
803 abs(target_freq - calculated_clock->dot),
804 target_freq);
805 /*
806 * Prefer a better P value over a better (smaller) error if the error
807 * is small. Ensure this preference for future configurations too by
808 * setting the error to 0.
809 */
810 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
811 *error_ppm = 0;
812
813 return true;
814 }
815
816 return *error_ppm + 10 < best_error_ppm;
817}
818
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200819/*
820 * Returns a set of divisors for the desired target clock with the given
821 * refclk, or FALSE. The returned values represent the clock equation:
822 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
823 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800824static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300825vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200826 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300827 int target, int refclk, struct dpll *match_clock,
828 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700829{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200830 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300831 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300832 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300833 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300834 /* min update 19.2 MHz */
835 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300836 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700837
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300838 target *= 5; /* fast clock */
839
840 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700841
842 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300843 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300844 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300845 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300846 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300847 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700848 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300849 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200850 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300851
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300852 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
853 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300854
Imre Deakdccbea32015-06-22 23:35:51 +0300855 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300856
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100857 if (!intel_PLL_is_valid(to_i915(dev),
858 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300859 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300860 continue;
861
Imre Deakd5dd62b2015-03-17 11:40:03 +0200862 if (!vlv_PLL_is_optimal(dev, target,
863 &clock,
864 best_clock,
865 bestppm, &ppm))
866 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300867
Imre Deakd5dd62b2015-03-17 11:40:03 +0200868 *best_clock = clock;
869 bestppm = ppm;
870 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700871 }
872 }
873 }
874 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700875
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300876 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700877}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700878
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200879/*
880 * Returns a set of divisors for the desired target clock with the given
881 * refclk, or FALSE. The returned values represent the clock equation:
882 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
883 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300884static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300885chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200886 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300887 int target, int refclk, struct dpll *match_clock,
888 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300889{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200890 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300891 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200892 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300893 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300894 uint64_t m2;
895 int found = false;
896
897 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200898 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300899
900 /*
901 * Based on hardware doc, the n always set to 1, and m1 always
902 * set to 2. If requires to support 200Mhz refclk, we need to
903 * revisit this because n may not 1 anymore.
904 */
905 clock.n = 1, clock.m1 = 2;
906 target *= 5; /* fast clock */
907
908 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
909 for (clock.p2 = limit->p2.p2_fast;
910 clock.p2 >= limit->p2.p2_slow;
911 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200912 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300913
914 clock.p = clock.p1 * clock.p2;
915
916 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
917 clock.n) << 22, refclk * clock.m1);
918
919 if (m2 > INT_MAX/clock.m1)
920 continue;
921
922 clock.m2 = m2;
923
Imre Deakdccbea32015-06-22 23:35:51 +0300924 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300925
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100926 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927 continue;
928
Imre Deak9ca3ba02015-03-17 11:40:05 +0200929 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
930 best_error_ppm, &error_ppm))
931 continue;
932
933 *best_clock = clock;
934 best_error_ppm = error_ppm;
935 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300936 }
937 }
938
939 return found;
940}
941
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200942bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300943 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200944{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200945 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300946 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200947
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200948 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200949 target_clock, refclk, NULL, best_clock);
950}
951
Ville Syrjälä525b9312016-10-31 22:37:02 +0200952bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300953{
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300954 /* Be paranoid as we can arrive here with only partial
955 * state retrieved from the hardware during setup.
956 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100957 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300958 * as Haswell has gained clock readout/fastboot support.
959 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000960 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300961 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700962 *
963 * FIXME: The intel_crtc->active here should be switched to
964 * crtc->state->active once we have proper CRTC states wired up
965 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300966 */
Ville Syrjälä525b9312016-10-31 22:37:02 +0200967 return crtc->active && crtc->base.primary->state->fb &&
968 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300969}
970
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200971enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
972 enum pipe pipe)
973{
Ville Syrjälä98187832016-10-31 22:37:10 +0200974 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200975
Ville Syrjäläe2af48c2016-10-31 22:37:05 +0200976 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200977}
978
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +0000979static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300980{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200981 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300982 u32 line1, line2;
983 u32 line_mask;
984
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100985 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300986 line_mask = DSL_LINEMASK_GEN2;
987 else
988 line_mask = DSL_LINEMASK_GEN3;
989
990 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +0200991 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300992 line2 = I915_READ(reg) & line_mask;
993
994 return line1 == line2;
995}
996
Keith Packardab7ad7f2010-10-03 00:33:06 -0700997/*
998 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300999 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001000 *
1001 * After disabling a pipe, we can't wait for vblank in the usual way,
1002 * spinning on the vblank interrupt status bit, since we won't actually
1003 * see an interrupt when the pipe is disabled.
1004 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001005 * On Gen4 and above:
1006 * wait for the pipe register state bit to turn off
1007 *
1008 * Otherwise:
1009 * wait for the display line value to settle (it usually
1010 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001011 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001012 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001013static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001014{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001015 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001016 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001017 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001018
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001019 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001020 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001021
Keith Packardab7ad7f2010-10-03 00:33:06 -07001022 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001023 if (intel_wait_for_register(dev_priv,
1024 reg, I965_PIPECONF_ACTIVE, 0,
1025 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001026 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001028 /* Wait for the display line to settle */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001029 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001030 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001031 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001032}
1033
Jesse Barnesb24e7172011-01-04 15:09:30 -08001034/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001035void assert_pll(struct drm_i915_private *dev_priv,
1036 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001037{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001038 u32 val;
1039 bool cur_state;
1040
Ville Syrjälä649636e2015-09-22 19:50:01 +03001041 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001042 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001043 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001044 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001045 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001046}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001047
Jani Nikula23538ef2013-08-27 15:12:22 +03001048/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001049void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001050{
1051 u32 val;
1052 bool cur_state;
1053
Ville Syrjäläa5805162015-05-26 20:42:30 +03001054 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001055 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001056 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001057
1058 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001059 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001060 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001061 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001062}
Jani Nikula23538ef2013-08-27 15:12:22 +03001063
Jesse Barnes040484a2011-01-03 12:14:26 -08001064static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
1066{
Jesse Barnes040484a2011-01-03 12:14:26 -08001067 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001068 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1069 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001070
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001071 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001072 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001073 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001074 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001075 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001076 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001077 cur_state = !!(val & FDI_TX_ENABLE);
1078 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001079 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001080 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001081 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001082}
1083#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1084#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1085
1086static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1087 enum pipe pipe, bool state)
1088{
Jesse Barnes040484a2011-01-03 12:14:26 -08001089 u32 val;
1090 bool cur_state;
1091
Ville Syrjälä649636e2015-09-22 19:50:01 +03001092 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001093 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001094 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001095 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001096 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001097}
1098#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1099#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1100
1101static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1102 enum pipe pipe)
1103{
Jesse Barnes040484a2011-01-03 12:14:26 -08001104 u32 val;
1105
1106 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001107 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001108 return;
1109
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001110 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001111 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001112 return;
1113
Ville Syrjälä649636e2015-09-22 19:50:01 +03001114 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001115 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 -08001116}
1117
Daniel Vetter55607e82013-06-16 21:42:39 +02001118void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001120{
Jesse Barnes040484a2011-01-03 12:14:26 -08001121 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001122 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001123
Ville Syrjälä649636e2015-09-22 19:50:01 +03001124 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001125 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001126 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001127 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001128 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001129}
1130
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001131void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001132{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001133 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001134 u32 val;
1135 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001136 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001137
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001138 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001139 return;
1140
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001141 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001142 u32 port_sel;
1143
Imre Deak44cb7342016-08-10 14:07:29 +03001144 pp_reg = PP_CONTROL(0);
1145 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001146
1147 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1148 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1149 panel_pipe = PIPE_B;
1150 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001151 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001152 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001153 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001154 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001155 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001156 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001157 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1158 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001159 }
1160
1161 val = I915_READ(pp_reg);
1162 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001163 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001164 locked = false;
1165
Rob Clarke2c719b2014-12-15 13:56:32 -05001166 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001167 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001168 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001169}
1170
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001171static void assert_cursor(struct drm_i915_private *dev_priv,
1172 enum pipe pipe, bool state)
1173{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001174 bool cur_state;
1175
Jani Nikula2a307c22016-11-30 17:43:04 +02001176 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001177 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001178 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001179 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001180
Rob Clarke2c719b2014-12-15 13:56:32 -05001181 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001182 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001183 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001184}
1185#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1186#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1187
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001188void assert_pipe(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001190{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001191 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1193 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001194 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001195
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001196 /* we keep both pipes enabled on 830 */
1197 if (IS_I830(dev_priv))
Daniel Vetter8e636782012-01-22 01:36:48 +01001198 state = true;
1199
Imre Deak4feed0e2016-02-12 18:55:14 +02001200 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1201 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001202 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001203 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001204
1205 intel_display_power_put(dev_priv, power_domain);
1206 } else {
1207 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001208 }
1209
Rob Clarke2c719b2014-12-15 13:56:32 -05001210 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001211 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001212 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001213}
1214
Chris Wilson931872f2012-01-16 23:01:13 +00001215static void assert_plane(struct drm_i915_private *dev_priv,
1216 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001217{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001218 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001219 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001220
Ville Syrjälä649636e2015-09-22 19:50:01 +03001221 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001223 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001224 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001225 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001226}
1227
Chris Wilson931872f2012-01-16 23:01:13 +00001228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
Jesse Barnesb24e7172011-01-04 15:09:30 -08001231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001234 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001235
Ville Syrjälä653e1022013-06-04 13:49:05 +03001236 /* Primary planes are fixed to pipes on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001237 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001238 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001239 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001240 "plane %c assertion failure, should be disabled but not\n",
1241 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001242 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001243 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001244
Jesse Barnesb24e7172011-01-04 15:09:30 -08001245 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001246 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001247 u32 val = I915_READ(DSPCNTR(i));
1248 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001249 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001250 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001251 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1252 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001253 }
1254}
1255
Jesse Barnes19332d72013-03-28 09:55:38 -07001256static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001259 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001260
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001261 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001262 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001263 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001264 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001265 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1266 sprite, pipe_name(pipe));
1267 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001268 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001269 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä83c04a62016-11-22 18:02:00 +02001270 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001271 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001272 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001273 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001274 }
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001275 } else if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001276 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001277 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001278 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001279 plane_name(pipe), pipe_name(pipe));
Ville Syrjäläab330812017-04-21 21:14:32 +03001280 } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001281 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001282 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1284 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001285 }
1286}
1287
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001288static void assert_vblank_disabled(struct drm_crtc *crtc)
1289{
Rob Clarke2c719b2014-12-15 13:56:32 -05001290 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001291 drm_crtc_vblank_put(crtc);
1292}
1293
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001294void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001296{
Jesse Barnes92f25842011-01-04 15:09:34 -08001297 u32 val;
1298 bool enabled;
1299
Ville Syrjälä649636e2015-09-22 19:50:01 +03001300 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001301 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001302 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001303 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1304 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001305}
1306
Keith Packard4e634382011-08-06 10:39:45 -07001307static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1308 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001309{
1310 if ((val & DP_PORT_EN) == 0)
1311 return false;
1312
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001313 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001314 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001315 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1316 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001317 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001318 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1319 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
Keith Packard1519b992011-08-06 10:35:34 -07001327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001330 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001331 return false;
1332
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001333 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001334 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001335 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001336 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001337 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1338 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001339 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001340 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001341 return false;
1342 }
1343 return true;
1344}
1345
1346static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 val)
1348{
1349 if ((val & LVDS_PORT_EN) == 0)
1350 return false;
1351
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001352 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001353 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354 return false;
1355 } else {
1356 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1357 return false;
1358 }
1359 return true;
1360}
1361
1362static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
1365 if ((val & ADPA_DAC_ENABLE) == 0)
1366 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001367 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001368 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1369 return false;
1370 } else {
1371 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1372 return false;
1373 }
1374 return true;
1375}
1376
Jesse Barnes291906f2011-02-02 12:28:03 -08001377static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001378 enum pipe pipe, i915_reg_t reg,
1379 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001380{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001381 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001382 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001383 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001384 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001385
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001386 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001387 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001388 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001389}
1390
1391static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001392 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001393{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001394 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001395 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001396 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001397 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001398
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001399 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001400 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001401 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001402}
1403
1404static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
1406{
Jesse Barnes291906f2011-02-02 12:28:03 -08001407 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001408
Keith Packardf0575e92011-07-25 22:12:43 -07001409 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1410 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1411 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001412
Ville Syrjälä649636e2015-09-22 19:50:01 +03001413 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001414 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001415 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001416 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001417
Ville Syrjälä649636e2015-09-22 19:50:01 +03001418 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001419 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001420 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001421 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001422
Paulo Zanonie2debe92013-02-18 19:00:27 -03001423 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1425 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001426}
1427
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001428static void _vlv_enable_pll(struct intel_crtc *crtc,
1429 const struct intel_crtc_state *pipe_config)
1430{
1431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1432 enum pipe pipe = crtc->pipe;
1433
1434 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1435 POSTING_READ(DPLL(pipe));
1436 udelay(150);
1437
Chris Wilson2c30b432016-06-30 15:32:54 +01001438 if (intel_wait_for_register(dev_priv,
1439 DPLL(pipe),
1440 DPLL_LOCK_VLV,
1441 DPLL_LOCK_VLV,
1442 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001443 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1444}
1445
Ville Syrjäläd288f652014-10-28 13:20:22 +02001446static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001447 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001448{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001450 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001451
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001452 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001453
Daniel Vetter87442f72013-06-06 00:52:17 +02001454 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001455 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001456
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001457 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1458 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001459
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001460 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1461 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001462}
1463
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001464
1465static void _chv_enable_pll(struct intel_crtc *crtc,
1466 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001467{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001469 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001470 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001471 u32 tmp;
1472
Ville Syrjäläa5805162015-05-26 20:42:30 +03001473 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001474
1475 /* Enable back the 10bit clock to display controller */
1476 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1477 tmp |= DPIO_DCLKP_EN;
1478 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1479
Ville Syrjälä54433e92015-05-26 20:42:31 +03001480 mutex_unlock(&dev_priv->sb_lock);
1481
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001482 /*
1483 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1484 */
1485 udelay(1);
1486
1487 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001488 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001489
1490 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001491 if (intel_wait_for_register(dev_priv,
1492 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1493 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001494 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001495}
1496
1497static void chv_enable_pll(struct intel_crtc *crtc,
1498 const struct intel_crtc_state *pipe_config)
1499{
1500 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1501 enum pipe pipe = crtc->pipe;
1502
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 /* PLL is protected by panel, make sure we can write it */
1506 assert_panel_unlocked(dev_priv, pipe);
1507
1508 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1509 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001510
Ville Syrjäläc2317752016-03-15 16:39:56 +02001511 if (pipe != PIPE_A) {
1512 /*
1513 * WaPixelRepeatModeFixForC0:chv
1514 *
1515 * DPLLCMD is AWOL. Use chicken bits to propagate
1516 * the value from DPLLBMD to either pipe B or C.
1517 */
1518 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1519 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1520 I915_WRITE(CBR4_VLV, 0);
1521 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1522
1523 /*
1524 * DPLLB VGA mode also seems to cause problems.
1525 * We should always have it disabled.
1526 */
1527 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1528 } else {
1529 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1530 POSTING_READ(DPLL_MD(pipe));
1531 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001532}
1533
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001534static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001535{
1536 struct intel_crtc *crtc;
1537 int count = 0;
1538
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001539 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001540 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1542 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001543
1544 return count;
1545}
1546
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001547static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001548{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001550 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001551 u32 dpll = crtc->config->dpll_hw_state.dpll;
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001552 int i;
Daniel Vetter87442f72013-06-06 00:52:17 +02001553
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001554 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001555
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001556 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001557 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001558 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001559
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001560 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001561 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001562 /*
1563 * It appears to be important that we don't enable this
1564 * for the current pipe before otherwise configuring the
1565 * PLL. No idea how this should be handled if multiple
1566 * DVO outputs are enabled simultaneosly.
1567 */
1568 dpll |= DPLL_DVO_2X_MODE;
1569 I915_WRITE(DPLL(!crtc->pipe),
1570 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1571 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001572
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001573 /*
1574 * Apparently we need to have VGA mode enabled prior to changing
1575 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1576 * dividers, even though the register value does change.
1577 */
1578 I915_WRITE(reg, 0);
1579
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001580 I915_WRITE(reg, dpll);
1581
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001582 /* Wait for the clocks to stabilize. */
1583 POSTING_READ(reg);
1584 udelay(150);
1585
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001586 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001587 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001588 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001589 } else {
1590 /* The pixel multiplier can only be updated once the
1591 * DPLL is enabled and the clocks are stable.
1592 *
1593 * So write it again.
1594 */
1595 I915_WRITE(reg, dpll);
1596 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001597
1598 /* We do this three times for luck */
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001599 for (i = 0; i < 3; i++) {
1600 I915_WRITE(reg, dpll);
1601 POSTING_READ(reg);
1602 udelay(150); /* wait for warmup */
1603 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001604}
1605
1606/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001607 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001608 * @dev_priv: i915 private structure
1609 * @pipe: pipe PLL to disable
1610 *
1611 * Disable the PLL for @pipe, making sure the pipe is off first.
1612 *
1613 * Note! This is for pre-ILK only.
1614 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001615static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001616{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001617 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001618 enum pipe pipe = crtc->pipe;
1619
1620 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001621 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001622 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001623 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624 I915_WRITE(DPLL(PIPE_B),
1625 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1626 I915_WRITE(DPLL(PIPE_A),
1627 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1628 }
1629
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001630 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001631 if (IS_I830(dev_priv))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001632 return;
1633
1634 /* Make sure the pipe isn't still relying on us */
1635 assert_pipe_disabled(dev_priv, pipe);
1636
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001637 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001638 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001639}
1640
Jesse Barnesf6071162013-10-01 10:41:38 -07001641static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1642{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001643 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001644
1645 /* Make sure the pipe isn't still relying on us */
1646 assert_pipe_disabled(dev_priv, pipe);
1647
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001648 val = DPLL_INTEGRATED_REF_CLK_VLV |
1649 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1650 if (pipe != PIPE_A)
1651 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1652
Jesse Barnesf6071162013-10-01 10:41:38 -07001653 I915_WRITE(DPLL(pipe), val);
1654 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001655}
1656
1657static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1658{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001660 u32 val;
1661
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001662 /* Make sure the pipe isn't still relying on us */
1663 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001664
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001665 val = DPLL_SSC_REF_CLK_CHV |
1666 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001667 if (pipe != PIPE_A)
1668 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001669
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001670 I915_WRITE(DPLL(pipe), val);
1671 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001672
Ville Syrjäläa5805162015-05-26 20:42:30 +03001673 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001674
1675 /* Disable 10bit clock to display controller */
1676 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1677 val &= ~DPIO_DCLKP_EN;
1678 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1679
Ville Syrjäläa5805162015-05-26 20:42:30 +03001680 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001681}
1682
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001683void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001684 struct intel_digital_port *dport,
1685 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001686{
1687 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001688 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001689
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001690 switch (dport->port) {
1691 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001692 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001693 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001694 break;
1695 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001696 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001697 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001698 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001699 break;
1700 case PORT_D:
1701 port_mask = DPLL_PORTD_READY_MASK;
1702 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001703 break;
1704 default:
1705 BUG();
1706 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001707
Chris Wilson370004d2016-06-30 15:32:56 +01001708 if (intel_wait_for_register(dev_priv,
1709 dpll_reg, port_mask, expected_mask,
1710 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001711 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1712 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001713}
1714
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001715static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1716 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001717{
Ville Syrjälä98187832016-10-31 22:37:10 +02001718 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1719 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001720 i915_reg_t reg;
1721 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001722
Jesse Barnes040484a2011-01-03 12:14:26 -08001723 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001724 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001725
1726 /* FDI must be feeding us bits for PCH ports */
1727 assert_fdi_tx_enabled(dev_priv, pipe);
1728 assert_fdi_rx_enabled(dev_priv, pipe);
1729
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001730 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001731 /* Workaround: Set the timing override bit before enabling the
1732 * pch transcoder. */
1733 reg = TRANS_CHICKEN2(pipe);
1734 val = I915_READ(reg);
1735 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1736 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001737 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001738
Daniel Vetterab9412b2013-05-03 11:49:46 +02001739 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001740 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001741 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001742
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001743 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001744 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001745 * Make the BPC in transcoder be consistent with
1746 * that in pipeconf reg. For HDMI we must use 8bpc
1747 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001748 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001749 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001750 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001751 val |= PIPECONF_8BPC;
1752 else
1753 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001754 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001755
1756 val &= ~TRANS_INTERLACE_MASK;
1757 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001758 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001759 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001760 val |= TRANS_LEGACY_INTERLACED_ILK;
1761 else
1762 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001763 else
1764 val |= TRANS_PROGRESSIVE;
1765
Jesse Barnes040484a2011-01-03 12:14:26 -08001766 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001767 if (intel_wait_for_register(dev_priv,
1768 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1769 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001770 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001771}
1772
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001773static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001774 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001775{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001776 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001777
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001778 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001779 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001780 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001781
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001782 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001783 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001784 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001785 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001786
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001787 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001788 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001789
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001790 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1791 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001792 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001793 else
1794 val |= TRANS_PROGRESSIVE;
1795
Daniel Vetterab9412b2013-05-03 11:49:46 +02001796 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001797 if (intel_wait_for_register(dev_priv,
1798 LPT_TRANSCONF,
1799 TRANS_STATE_ENABLE,
1800 TRANS_STATE_ENABLE,
1801 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001802 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001803}
1804
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001805static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1806 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001807{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001808 i915_reg_t reg;
1809 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001810
1811 /* FDI relies on the transcoder */
1812 assert_fdi_tx_disabled(dev_priv, pipe);
1813 assert_fdi_rx_disabled(dev_priv, pipe);
1814
Jesse Barnes291906f2011-02-02 12:28:03 -08001815 /* Ports must be off as well */
1816 assert_pch_ports_disabled(dev_priv, pipe);
1817
Daniel Vetterab9412b2013-05-03 11:49:46 +02001818 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001819 val = I915_READ(reg);
1820 val &= ~TRANS_ENABLE;
1821 I915_WRITE(reg, val);
1822 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001823 if (intel_wait_for_register(dev_priv,
1824 reg, TRANS_STATE_ENABLE, 0,
1825 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001826 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001827
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001828 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001829 /* Workaround: Clear the timing override chicken bit again. */
1830 reg = TRANS_CHICKEN2(pipe);
1831 val = I915_READ(reg);
1832 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1833 I915_WRITE(reg, val);
1834 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001835}
1836
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001837void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001838{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001839 u32 val;
1840
Daniel Vetterab9412b2013-05-03 11:49:46 +02001841 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001842 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001843 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001844 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001845 if (intel_wait_for_register(dev_priv,
1846 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1847 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001848 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001849
1850 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001851 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001852 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001853 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001854}
1855
Ville Syrjälä65f21302016-10-14 20:02:53 +03001856enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1857{
1858 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1859
1860 WARN_ON(!crtc->config->has_pch_encoder);
1861
1862 if (HAS_PCH_LPT(dev_priv))
1863 return TRANSCODER_A;
1864 else
1865 return (enum transcoder) crtc->pipe;
1866}
1867
Jesse Barnes92f25842011-01-04 15:09:34 -08001868/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001869 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001870 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001871 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001872 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001873 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001874 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001875static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001876{
Paulo Zanoni03722642014-01-17 13:51:09 -02001877 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001878 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001879 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001880 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001881 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001882 u32 val;
1883
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001884 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1885
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001886 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001887 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001888 assert_sprites_disabled(dev_priv, pipe);
1889
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890 /*
1891 * A pipe without a PLL won't actually be able to drive bits from
1892 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1893 * need the check.
1894 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001895 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001896 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001897 assert_dsi_pll_enabled(dev_priv);
1898 else
1899 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001900 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001901 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001902 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001903 assert_fdi_rx_pll_enabled(dev_priv,
1904 (enum pipe) intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001905 assert_fdi_tx_pll_enabled(dev_priv,
1906 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001907 }
1908 /* FIXME: assert CPU port conditions for SNB+ */
1909 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001910
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001911 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001912 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001913 if (val & PIPECONF_ENABLE) {
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001914 /* we keep both pipes enabled on 830 */
1915 WARN_ON(!IS_I830(dev_priv));
Chris Wilson00d70b12011-03-17 07:18:29 +00001916 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001917 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001918
1919 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001920 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001921
1922 /*
1923 * Until the pipe starts DSL will read as 0, which would cause
1924 * an apparent vblank timestamp jump, which messes up also the
1925 * frame count when it's derived from the timestamps. So let's
1926 * wait for the pipe to start properly before we call
1927 * drm_crtc_vblank_on()
1928 */
1929 if (dev->max_vblank_count == 0 &&
1930 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1931 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932}
1933
1934/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001935 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001936 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08001937 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001938 * Disable the pipe of @crtc, making sure that various hardware
1939 * specific requirements are met, if applicable, e.g. plane
1940 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 *
1942 * Will wait until the pipe has shut down before returning.
1943 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001944static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001945{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001946 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001947 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001948 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001949 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001950 u32 val;
1951
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001952 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1953
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 /*
1955 * Make sure planes won't keep trying to pump pixels to us,
1956 * or we might hang the display.
1957 */
1958 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001959 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001960 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001962 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001963 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001964 if ((val & PIPECONF_ENABLE) == 0)
1965 return;
1966
Ville Syrjälä67adc642014-08-15 01:21:57 +03001967 /*
1968 * Double wide has implications for planes
1969 * so best keep it disabled when not needed.
1970 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001971 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03001972 val &= ~PIPECONF_DOUBLE_WIDE;
1973
1974 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001975 if (!IS_I830(dev_priv))
Ville Syrjälä67adc642014-08-15 01:21:57 +03001976 val &= ~PIPECONF_ENABLE;
1977
1978 I915_WRITE(reg, val);
1979 if ((val & PIPECONF_ENABLE) == 0)
1980 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981}
1982
Ville Syrjälä832be822016-01-12 21:08:33 +02001983static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1984{
1985 return IS_GEN2(dev_priv) ? 2048 : 4096;
1986}
1987
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001988static unsigned int
1989intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001990{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001991 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1992 unsigned int cpp = fb->format->cpp[plane];
1993
1994 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07001995 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001996 return cpp;
1997 case I915_FORMAT_MOD_X_TILED:
1998 if (IS_GEN2(dev_priv))
1999 return 128;
2000 else
2001 return 512;
2002 case I915_FORMAT_MOD_Y_TILED:
2003 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2004 return 128;
2005 else
2006 return 512;
2007 case I915_FORMAT_MOD_Yf_TILED:
2008 switch (cpp) {
2009 case 1:
2010 return 64;
2011 case 2:
2012 case 4:
2013 return 128;
2014 case 8:
2015 case 16:
2016 return 256;
2017 default:
2018 MISSING_CASE(cpp);
2019 return cpp;
2020 }
2021 break;
2022 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002023 MISSING_CASE(fb->modifier);
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002024 return cpp;
2025 }
2026}
2027
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002028static unsigned int
2029intel_tile_height(const struct drm_framebuffer *fb, int plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002030{
Ben Widawsky2f075562017-03-24 14:29:48 -07002031 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä832be822016-01-12 21:08:33 +02002032 return 1;
2033 else
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002034 return intel_tile_size(to_i915(fb->dev)) /
2035 intel_tile_width_bytes(fb, plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002036}
2037
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002038/* Return the tile dimensions in pixel units */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002039static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002040 unsigned int *tile_width,
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002041 unsigned int *tile_height)
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002042{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002043 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2044 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002045
2046 *tile_width = tile_width_bytes / cpp;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002047 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002048}
2049
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002050unsigned int
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002051intel_fb_align_height(const struct drm_framebuffer *fb,
2052 int plane, unsigned int height)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002053{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002054 unsigned int tile_height = intel_tile_height(fb, plane);
Ville Syrjälä832be822016-01-12 21:08:33 +02002055
2056 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002057}
2058
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002059unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2060{
2061 unsigned int size = 0;
2062 int i;
2063
2064 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2065 size += rot_info->plane[i].width * rot_info->plane[i].height;
2066
2067 return size;
2068}
2069
Daniel Vetter75c82a52015-10-14 16:51:04 +02002070static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002071intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2072 const struct drm_framebuffer *fb,
2073 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002074{
Chris Wilson7b92c042017-01-14 00:28:26 +00002075 view->type = I915_GGTT_VIEW_NORMAL;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002076 if (drm_rotation_90_or_270(rotation)) {
Chris Wilson7b92c042017-01-14 00:28:26 +00002077 view->type = I915_GGTT_VIEW_ROTATED;
Chris Wilson8bab11932017-01-14 00:28:25 +00002078 view->rotated = to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002079 }
2080}
2081
Ville Syrjäläfabac482017-03-27 21:55:43 +03002082static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2083{
2084 if (IS_I830(dev_priv))
2085 return 16 * 1024;
2086 else if (IS_I85X(dev_priv))
2087 return 256;
Ville Syrjäläd9e15512017-03-27 21:55:45 +03002088 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2089 return 32;
Ville Syrjäläfabac482017-03-27 21:55:43 +03002090 else
2091 return 4 * 1024;
2092}
2093
Ville Syrjälä603525d2016-01-12 21:08:37 +02002094static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002095{
2096 if (INTEL_INFO(dev_priv)->gen >= 9)
2097 return 256 * 1024;
Jani Nikulac0f86832016-12-07 12:13:04 +02002098 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002099 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002100 return 128 * 1024;
2101 else if (INTEL_INFO(dev_priv)->gen >= 4)
2102 return 4 * 1024;
2103 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002104 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002105}
2106
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002107static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2108 int plane)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002109{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002110 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2111
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02002112 /* AUX_DIST needs only 4K alignment */
2113 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
2114 return 4096;
2115
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002116 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002117 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002118 return intel_linear_alignment(dev_priv);
2119 case I915_FORMAT_MOD_X_TILED:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002120 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002121 return 256 * 1024;
2122 return 0;
2123 case I915_FORMAT_MOD_Y_TILED:
2124 case I915_FORMAT_MOD_Yf_TILED:
2125 return 1 * 1024 * 1024;
2126 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002127 MISSING_CASE(fb->modifier);
Ville Syrjälä603525d2016-01-12 21:08:37 +02002128 return 0;
2129 }
2130}
2131
Chris Wilson058d88c2016-08-15 10:49:06 +01002132struct i915_vma *
2133intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002134{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002135 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002136 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002137 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002138 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002139 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002140 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002141
Matt Roperebcdd392014-07-09 16:22:11 -07002142 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2143
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002144 alignment = intel_surf_alignment(fb, 0);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002145
Ville Syrjälä3465c582016-02-15 22:54:43 +02002146 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002147
Chris Wilson693db182013-03-05 14:52:39 +00002148 /* Note that the w/a also requires 64 PTE of padding following the
2149 * bo. We currently fill all unused PTE with the shadow page and so
2150 * we should always have valid PTE following the scanout preventing
2151 * the VT-d warning.
2152 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002153 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002154 alignment = 256 * 1024;
2155
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002156 /*
2157 * Global gtt pte registers are special registers which actually forward
2158 * writes to a chunk of system memory. Which means that there is no risk
2159 * that the register values disappear as soon as we call
2160 * intel_runtime_pm_put(), so it is correct to wrap only the
2161 * pin/unpin/fence and not more.
2162 */
2163 intel_runtime_pm_get(dev_priv);
2164
Chris Wilson058d88c2016-08-15 10:49:06 +01002165 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002166 if (IS_ERR(vma))
2167 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002168
Chris Wilson05a20d02016-08-18 17:16:55 +01002169 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002170 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2171 * fence, whereas 965+ only requires a fence if using
2172 * framebuffer compression. For simplicity, we always, when
2173 * possible, install a fence as the cost is not that onerous.
2174 *
2175 * If we fail to fence the tiled scanout, then either the
2176 * modeset will reject the change (which is highly unlikely as
2177 * the affected systems, all but one, do not have unmappable
2178 * space) or we will not be able to enable full powersaving
2179 * techniques (also likely not to apply due to various limits
2180 * FBC and the like impose on the size of the buffer, which
2181 * presumably we violated anyway with this unmappable buffer).
2182 * Anyway, it is presumably better to stumble onwards with
2183 * something and try to run the system in a "less than optimal"
2184 * mode that matches the user configuration.
2185 */
2186 if (i915_vma_get_fence(vma) == 0)
2187 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002188 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002189
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002190 i915_vma_get(vma);
Chris Wilson49ef5292016-08-18 17:17:00 +01002191err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002192 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002193 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002194}
2195
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002196void intel_unpin_fb_vma(struct i915_vma *vma)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002197{
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002198 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002199
Chris Wilson49ef5292016-08-18 17:17:00 +01002200 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002201 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002202 i915_vma_put(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002203}
2204
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002205static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2206 unsigned int rotation)
2207{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002208 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002209 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2210 else
2211 return fb->pitches[plane];
2212}
2213
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002214/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002215 * Convert the x/y offsets into a linear offset.
2216 * Only valid with 0/180 degree rotation, which is fine since linear
2217 * offset is only used with linear buffers on pre-hsw and tiled buffers
2218 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2219 */
2220u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002221 const struct intel_plane_state *state,
2222 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002223{
Ville Syrjälä29490562016-01-20 18:02:50 +02002224 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002225 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002226 unsigned int pitch = fb->pitches[plane];
2227
2228 return y * pitch + x * cpp;
2229}
2230
2231/*
2232 * Add the x/y offsets derived from fb->offsets[] to the user
2233 * specified plane src x/y offsets. The resulting x/y offsets
2234 * specify the start of scanout from the beginning of the gtt mapping.
2235 */
2236void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002237 const struct intel_plane_state *state,
2238 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002239
2240{
Ville Syrjälä29490562016-01-20 18:02:50 +02002241 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2242 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002243
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002244 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002245 *x += intel_fb->rotated[plane].x;
2246 *y += intel_fb->rotated[plane].y;
2247 } else {
2248 *x += intel_fb->normal[plane].x;
2249 *y += intel_fb->normal[plane].y;
2250 }
2251}
2252
2253/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002254 * Input tile dimensions and pitch must already be
2255 * rotated to match x and y, and in pixel units.
2256 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002257static u32 _intel_adjust_tile_offset(int *x, int *y,
2258 unsigned int tile_width,
2259 unsigned int tile_height,
2260 unsigned int tile_size,
2261 unsigned int pitch_tiles,
2262 u32 old_offset,
2263 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002264{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002265 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002266 unsigned int tiles;
2267
2268 WARN_ON(old_offset & (tile_size - 1));
2269 WARN_ON(new_offset & (tile_size - 1));
2270 WARN_ON(new_offset > old_offset);
2271
2272 tiles = (old_offset - new_offset) / tile_size;
2273
2274 *y += tiles / pitch_tiles * tile_height;
2275 *x += tiles % pitch_tiles * tile_width;
2276
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002277 /* minimize x in case it got needlessly big */
2278 *y += *x / pitch_pixels * tile_height;
2279 *x %= pitch_pixels;
2280
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002281 return new_offset;
2282}
2283
2284/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002285 * Adjust the tile offset by moving the difference into
2286 * the x/y offsets.
2287 */
2288static u32 intel_adjust_tile_offset(int *x, int *y,
2289 const struct intel_plane_state *state, int plane,
2290 u32 old_offset, u32 new_offset)
2291{
2292 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2293 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002294 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002295 unsigned int rotation = state->base.rotation;
2296 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2297
2298 WARN_ON(new_offset > old_offset);
2299
Ben Widawsky2f075562017-03-24 14:29:48 -07002300 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002301 unsigned int tile_size, tile_width, tile_height;
2302 unsigned int pitch_tiles;
2303
2304 tile_size = intel_tile_size(dev_priv);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002305 intel_tile_dims(fb, plane, &tile_width, &tile_height);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002306
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002307 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002308 pitch_tiles = pitch / tile_height;
2309 swap(tile_width, tile_height);
2310 } else {
2311 pitch_tiles = pitch / (tile_width * cpp);
2312 }
2313
2314 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2315 tile_size, pitch_tiles,
2316 old_offset, new_offset);
2317 } else {
2318 old_offset += *y * pitch + *x * cpp;
2319
2320 *y = (old_offset - new_offset) / pitch;
2321 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2322 }
2323
2324 return new_offset;
2325}
2326
2327/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002328 * Computes the linear offset to the base tile and adjusts
2329 * x, y. bytes per pixel is assumed to be a power-of-two.
2330 *
2331 * In the 90/270 rotated case, x and y are assumed
2332 * to be already rotated to match the rotated GTT view, and
2333 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002334 *
2335 * This function is used when computing the derived information
2336 * under intel_framebuffer, so using any of that information
2337 * here is not allowed. Anything under drm_framebuffer can be
2338 * used. This is why the user has to pass in the pitch since it
2339 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002340 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002341static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2342 int *x, int *y,
2343 const struct drm_framebuffer *fb, int plane,
2344 unsigned int pitch,
2345 unsigned int rotation,
2346 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002347{
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002348 uint64_t fb_modifier = fb->modifier;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002349 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002350 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002351
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002352 if (alignment)
2353 alignment--;
2354
Ben Widawsky2f075562017-03-24 14:29:48 -07002355 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002356 unsigned int tile_size, tile_width, tile_height;
2357 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002358
Ville Syrjäläd8433102016-01-12 21:08:35 +02002359 tile_size = intel_tile_size(dev_priv);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002360 intel_tile_dims(fb, plane, &tile_width, &tile_height);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002361
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002362 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002368
Ville Syrjäläd8433102016-01-12 21:08:35 +02002369 tile_rows = *y / tile_height;
2370 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002371
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002372 tiles = *x / tile_width;
2373 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002374
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002375 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2376 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002377
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002378 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2379 tile_size, pitch_tiles,
2380 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002381 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002382 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002383 offset_aligned = offset & ~alignment;
2384
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002385 *y = (offset & alignment) / pitch;
2386 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002387 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002388
2389 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002390}
2391
Ville Syrjälä6687c902015-09-15 13:16:41 +03002392u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002393 const struct intel_plane_state *state,
2394 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002395{
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002396 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2397 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
Ville Syrjälä29490562016-01-20 18:02:50 +02002398 const struct drm_framebuffer *fb = state->base.fb;
2399 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002400 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002401 u32 alignment;
2402
2403 if (intel_plane->id == PLANE_CURSOR)
2404 alignment = intel_cursor_alignment(dev_priv);
2405 else
2406 alignment = intel_surf_alignment(fb, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002407
2408 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2409 rotation, alignment);
2410}
2411
2412/* Convert the fb->offset[] linear offset into x/y offsets */
2413static void intel_fb_offset_to_xy(int *x, int *y,
2414 const struct drm_framebuffer *fb, int plane)
2415{
Ville Syrjälä353c8592016-12-14 23:30:57 +02002416 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002417 unsigned int pitch = fb->pitches[plane];
2418 u32 linear_offset = fb->offsets[plane];
2419
2420 *y = linear_offset / pitch;
2421 *x = linear_offset % pitch / cpp;
2422}
2423
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002424static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2425{
2426 switch (fb_modifier) {
2427 case I915_FORMAT_MOD_X_TILED:
2428 return I915_TILING_X;
2429 case I915_FORMAT_MOD_Y_TILED:
2430 return I915_TILING_Y;
2431 default:
2432 return I915_TILING_NONE;
2433 }
2434}
2435
Ville Syrjälä6687c902015-09-15 13:16:41 +03002436static int
2437intel_fill_fb_info(struct drm_i915_private *dev_priv,
2438 struct drm_framebuffer *fb)
2439{
2440 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2441 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2442 u32 gtt_offset_rotated = 0;
2443 unsigned int max_size = 0;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +02002444 int i, num_planes = fb->format->num_planes;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002445 unsigned int tile_size = intel_tile_size(dev_priv);
2446
2447 for (i = 0; i < num_planes; i++) {
2448 unsigned int width, height;
2449 unsigned int cpp, size;
2450 u32 offset;
2451 int x, y;
2452
Ville Syrjälä353c8592016-12-14 23:30:57 +02002453 cpp = fb->format->cpp[i];
Ville Syrjälä145fcb12016-11-18 21:53:06 +02002454 width = drm_framebuffer_plane_width(fb->width, fb, i);
2455 height = drm_framebuffer_plane_height(fb->height, fb, i);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002456
2457 intel_fb_offset_to_xy(&x, &y, fb, i);
2458
2459 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002460 * The fence (if used) is aligned to the start of the object
2461 * so having the framebuffer wrap around across the edge of the
2462 * fenced region doesn't really work. We have no API to configure
2463 * the fence start offset within the object (nor could we probably
2464 * on gen2/3). So it's just easier if we just require that the
2465 * fb layout agrees with the fence layout. We already check that the
2466 * fb stride matches the fence stride elsewhere.
2467 */
2468 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2469 (x + width) * cpp > fb->pitches[i]) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002470 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2471 i, fb->offsets[i]);
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002472 return -EINVAL;
2473 }
2474
2475 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002476 * First pixel of the framebuffer from
2477 * the start of the normal gtt mapping.
2478 */
2479 intel_fb->normal[i].x = x;
2480 intel_fb->normal[i].y = y;
2481
2482 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
Ville Syrjälä3ca46c02017-03-07 21:42:09 +02002483 fb, i, fb->pitches[i],
Robert Fossc2c446a2017-05-19 16:50:17 -04002484 DRM_MODE_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002485 offset /= tile_size;
2486
Ben Widawsky2f075562017-03-24 14:29:48 -07002487 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002488 unsigned int tile_width, tile_height;
2489 unsigned int pitch_tiles;
2490 struct drm_rect r;
2491
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002492 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002493
2494 rot_info->plane[i].offset = offset;
2495 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2496 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2497 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2498
2499 intel_fb->rotated[i].pitch =
2500 rot_info->plane[i].height * tile_height;
2501
2502 /* how many tiles does this plane need */
2503 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2504 /*
2505 * If the plane isn't horizontally tile aligned,
2506 * we need one more tile.
2507 */
2508 if (x != 0)
2509 size++;
2510
2511 /* rotate the x/y offsets to match the GTT view */
2512 r.x1 = x;
2513 r.y1 = y;
2514 r.x2 = x + width;
2515 r.y2 = y + height;
2516 drm_rect_rotate(&r,
2517 rot_info->plane[i].width * tile_width,
2518 rot_info->plane[i].height * tile_height,
Robert Fossc2c446a2017-05-19 16:50:17 -04002519 DRM_MODE_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002520 x = r.x1;
2521 y = r.y1;
2522
2523 /* rotate the tile dimensions to match the GTT view */
2524 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2525 swap(tile_width, tile_height);
2526
2527 /*
2528 * We only keep the x/y offsets, so push all of the
2529 * gtt offset into the x/y offsets.
2530 */
Ander Conselvan de Oliveira46a1bd22017-01-20 16:28:44 +02002531 _intel_adjust_tile_offset(&x, &y,
2532 tile_width, tile_height,
2533 tile_size, pitch_tiles,
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002534 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002535
2536 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2537
2538 /*
2539 * First pixel of the framebuffer from
2540 * the start of the rotated gtt mapping.
2541 */
2542 intel_fb->rotated[i].x = x;
2543 intel_fb->rotated[i].y = y;
2544 } else {
2545 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2546 x * cpp, tile_size);
2547 }
2548
2549 /* how many tiles in total needed in the bo */
2550 max_size = max(max_size, offset + size);
2551 }
2552
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002553 if (max_size * tile_size > intel_fb->obj->base.size) {
2554 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2555 max_size * tile_size, intel_fb->obj->base.size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002556 return -EINVAL;
2557 }
2558
2559 return 0;
2560}
2561
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002562static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002563{
2564 switch (format) {
2565 case DISPPLANE_8BPP:
2566 return DRM_FORMAT_C8;
2567 case DISPPLANE_BGRX555:
2568 return DRM_FORMAT_XRGB1555;
2569 case DISPPLANE_BGRX565:
2570 return DRM_FORMAT_RGB565;
2571 default:
2572 case DISPPLANE_BGRX888:
2573 return DRM_FORMAT_XRGB8888;
2574 case DISPPLANE_RGBX888:
2575 return DRM_FORMAT_XBGR8888;
2576 case DISPPLANE_BGRX101010:
2577 return DRM_FORMAT_XRGB2101010;
2578 case DISPPLANE_RGBX101010:
2579 return DRM_FORMAT_XBGR2101010;
2580 }
2581}
2582
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002583static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2584{
2585 switch (format) {
2586 case PLANE_CTL_FORMAT_RGB_565:
2587 return DRM_FORMAT_RGB565;
2588 default:
2589 case PLANE_CTL_FORMAT_XRGB_8888:
2590 if (rgb_order) {
2591 if (alpha)
2592 return DRM_FORMAT_ABGR8888;
2593 else
2594 return DRM_FORMAT_XBGR8888;
2595 } else {
2596 if (alpha)
2597 return DRM_FORMAT_ARGB8888;
2598 else
2599 return DRM_FORMAT_XRGB8888;
2600 }
2601 case PLANE_CTL_FORMAT_XRGB_2101010:
2602 if (rgb_order)
2603 return DRM_FORMAT_XBGR2101010;
2604 else
2605 return DRM_FORMAT_XRGB2101010;
2606 }
2607}
2608
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002609static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002610intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2611 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002612{
2613 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002614 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002615 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002616 struct drm_i915_gem_object *obj = NULL;
2617 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002618 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002619 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2620 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2621 PAGE_SIZE);
2622
2623 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002624
Chris Wilsonff2652e2014-03-10 08:07:02 +00002625 if (plane_config->size == 0)
2626 return false;
2627
Paulo Zanoni3badb492015-09-23 12:52:23 -03002628 /* If the FB is too big, just don't use it since fbdev is not very
2629 * important and we should probably use that space with FBC or other
2630 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002631 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002632 return false;
2633
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002634 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002635 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002636 base_aligned,
2637 base_aligned,
2638 size_aligned);
Chris Wilson24dbf512017-02-15 10:59:18 +00002639 mutex_unlock(&dev->struct_mutex);
2640 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002641 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002642
Chris Wilson3e510a82016-08-05 10:14:23 +01002643 if (plane_config->tiling == I915_TILING_X)
2644 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002645
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002646 mode_cmd.pixel_format = fb->format->format;
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002647 mode_cmd.width = fb->width;
2648 mode_cmd.height = fb->height;
2649 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002650 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002651 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002652
Chris Wilson24dbf512017-02-15 10:59:18 +00002653 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002654 DRM_DEBUG_KMS("intel fb init failed\n");
2655 goto out_unref_obj;
2656 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002657
Jesse Barnes484b41d2014-03-07 08:57:55 -08002658
Daniel Vetterf6936e22015-03-26 12:17:05 +01002659 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002660 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002661
2662out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002663 i915_gem_object_put(obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002664 return false;
2665}
2666
Daniel Vetter5a21b662016-05-24 17:13:53 +02002667/* Update plane->state->fb to match plane->fb after driver-internal updates */
2668static void
2669update_state_fb(struct drm_plane *plane)
2670{
2671 if (plane->fb == plane->state->fb)
2672 return;
2673
2674 if (plane->state->fb)
2675 drm_framebuffer_unreference(plane->state->fb);
2676 plane->state->fb = plane->fb;
2677 if (plane->state->fb)
2678 drm_framebuffer_reference(plane->state->fb);
2679}
2680
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002681static void
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002682intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2683 struct intel_plane_state *plane_state,
2684 bool visible)
2685{
2686 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2687
2688 plane_state->base.visible = visible;
2689
2690 /* FIXME pre-g4x don't work like this */
2691 if (visible) {
2692 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2693 crtc_state->active_planes |= BIT(plane->id);
2694 } else {
2695 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2696 crtc_state->active_planes &= ~BIT(plane->id);
2697 }
2698
2699 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2700 crtc_state->base.crtc->name,
2701 crtc_state->active_planes);
2702}
2703
2704static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002705intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2706 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002707{
2708 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002709 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002710 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002711 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002712 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002713 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002714 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2715 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002716 struct intel_plane_state *intel_state =
2717 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002718 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002719
Damien Lespiau2d140302015-02-05 17:22:18 +00002720 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002721 return;
2722
Daniel Vetterf6936e22015-03-26 12:17:05 +01002723 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002724 fb = &plane_config->fb->base;
2725 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002726 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002727
Damien Lespiau2d140302015-02-05 17:22:18 +00002728 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002729
2730 /*
2731 * Failed to alloc the obj, check to see if we should share
2732 * an fb with another CRTC instead
2733 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002734 for_each_crtc(dev, c) {
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002735 struct intel_plane_state *state;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002736
2737 if (c == &intel_crtc->base)
2738 continue;
2739
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002740 if (!to_intel_crtc(c)->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002741 continue;
2742
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002743 state = to_intel_plane_state(c->primary->state);
2744 if (!state->vma)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002745 continue;
2746
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002747 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2748 fb = c->primary->fb;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002749 drm_framebuffer_reference(fb);
2750 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002751 }
2752 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002753
Matt Roper200757f2015-12-03 11:37:36 -08002754 /*
2755 * We've failed to reconstruct the BIOS FB. Current display state
2756 * indicates that the primary plane is visible, but has a NULL FB,
2757 * which will lead to problems later if we don't fix it up. The
2758 * simplest solution is to just disable the primary plane now and
2759 * pretend the BIOS never had it enabled.
2760 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002761 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2762 to_intel_plane_state(plane_state),
2763 false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02002764 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Ville Syrjälä72259532017-03-02 19:15:05 +02002765 trace_intel_disable_plane(primary, intel_crtc);
Ville Syrjälä282dbf92017-03-27 21:55:33 +03002766 intel_plane->disable_plane(intel_plane, intel_crtc);
Matt Roper200757f2015-12-03 11:37:36 -08002767
Daniel Vetter88595ac2015-03-26 12:42:24 +01002768 return;
2769
2770valid_fb:
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002771 mutex_lock(&dev->struct_mutex);
2772 intel_state->vma =
2773 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2774 mutex_unlock(&dev->struct_mutex);
2775 if (IS_ERR(intel_state->vma)) {
2776 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2777 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2778
2779 intel_state->vma = NULL;
2780 drm_framebuffer_unreference(fb);
2781 return;
2782 }
2783
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002784 plane_state->src_x = 0;
2785 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002786 plane_state->src_w = fb->width << 16;
2787 plane_state->src_h = fb->height << 16;
2788
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002789 plane_state->crtc_x = 0;
2790 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002791 plane_state->crtc_w = fb->width;
2792 plane_state->crtc_h = fb->height;
2793
Rob Clark1638d302016-11-05 11:08:08 -04002794 intel_state->base.src = drm_plane_state_src(plane_state);
2795 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002796
Daniel Vetter88595ac2015-03-26 12:42:24 +01002797 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002798 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002799 dev_priv->preserve_bios_swizzle = true;
2800
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002801 drm_framebuffer_reference(fb);
2802 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002803 primary->crtc = primary->state->crtc = &intel_crtc->base;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002804
2805 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2806 to_intel_plane_state(plane_state),
2807 true);
2808
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002809 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2810 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002811}
2812
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002813static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2814 unsigned int rotation)
2815{
Ville Syrjälä353c8592016-12-14 23:30:57 +02002816 int cpp = fb->format->cpp[plane];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002817
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002818 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002819 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002820 case I915_FORMAT_MOD_X_TILED:
2821 switch (cpp) {
2822 case 8:
2823 return 4096;
2824 case 4:
2825 case 2:
2826 case 1:
2827 return 8192;
2828 default:
2829 MISSING_CASE(cpp);
2830 break;
2831 }
2832 break;
2833 case I915_FORMAT_MOD_Y_TILED:
2834 case I915_FORMAT_MOD_Yf_TILED:
2835 switch (cpp) {
2836 case 8:
2837 return 2048;
2838 case 4:
2839 return 4096;
2840 case 2:
2841 case 1:
2842 return 8192;
2843 default:
2844 MISSING_CASE(cpp);
2845 break;
2846 }
2847 break;
2848 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002849 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002850 }
2851
2852 return 2048;
2853}
2854
2855static int skl_check_main_surface(struct intel_plane_state *plane_state)
2856{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002857 const struct drm_framebuffer *fb = plane_state->base.fb;
2858 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002859 int x = plane_state->base.src.x1 >> 16;
2860 int y = plane_state->base.src.y1 >> 16;
2861 int w = drm_rect_width(&plane_state->base.src) >> 16;
2862 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002863 int max_width = skl_max_plane_width(fb, 0, rotation);
2864 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002865 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002866
2867 if (w > max_width || h > max_height) {
2868 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2869 w, h, max_width, max_height);
2870 return -EINVAL;
2871 }
2872
2873 intel_add_fb_offsets(&x, &y, plane_state, 0);
2874 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002875 alignment = intel_surf_alignment(fb, 0);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002876
2877 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002878 * AUX surface offset is specified as the distance from the
2879 * main surface offset, and it must be non-negative. Make
2880 * sure that is what we will get.
2881 */
2882 if (offset > aux_offset)
2883 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2884 offset, aux_offset & ~(alignment - 1));
2885
2886 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002887 * When using an X-tiled surface, the plane blows up
2888 * if the x offset + width exceed the stride.
2889 *
2890 * TODO: linear and Y-tiled seem fine, Yf untested,
2891 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002892 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02002893 int cpp = fb->format->cpp[0];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002894
2895 while ((x + w) * cpp > fb->pitches[0]) {
2896 if (offset == 0) {
2897 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2898 return -EINVAL;
2899 }
2900
2901 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2902 offset, offset - alignment);
2903 }
2904 }
2905
2906 plane_state->main.offset = offset;
2907 plane_state->main.x = x;
2908 plane_state->main.y = y;
2909
2910 return 0;
2911}
2912
Ville Syrjälä8d970652016-01-28 16:30:28 +02002913static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2914{
2915 const struct drm_framebuffer *fb = plane_state->base.fb;
2916 unsigned int rotation = plane_state->base.rotation;
2917 int max_width = skl_max_plane_width(fb, 1, rotation);
2918 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002919 int x = plane_state->base.src.x1 >> 17;
2920 int y = plane_state->base.src.y1 >> 17;
2921 int w = drm_rect_width(&plane_state->base.src) >> 17;
2922 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002923 u32 offset;
2924
2925 intel_add_fb_offsets(&x, &y, plane_state, 1);
2926 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2927
2928 /* FIXME not quite sure how/if these apply to the chroma plane */
2929 if (w > max_width || h > max_height) {
2930 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2931 w, h, max_width, max_height);
2932 return -EINVAL;
2933 }
2934
2935 plane_state->aux.offset = offset;
2936 plane_state->aux.x = x;
2937 plane_state->aux.y = y;
2938
2939 return 0;
2940}
2941
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002942int skl_check_plane_surface(struct intel_plane_state *plane_state)
2943{
2944 const struct drm_framebuffer *fb = plane_state->base.fb;
2945 unsigned int rotation = plane_state->base.rotation;
2946 int ret;
2947
Ville Syrjäläa5e4c7d2016-11-07 22:20:54 +02002948 if (!plane_state->base.visible)
2949 return 0;
2950
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002951 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002952 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002953 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002954 fb->width << 16, fb->height << 16,
Robert Fossc2c446a2017-05-19 16:50:17 -04002955 DRM_MODE_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002956
Ville Syrjälä8d970652016-01-28 16:30:28 +02002957 /*
2958 * Handle the AUX surface first since
2959 * the main surface setup depends on it.
2960 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002961 if (fb->format->format == DRM_FORMAT_NV12) {
Ville Syrjälä8d970652016-01-28 16:30:28 +02002962 ret = skl_check_nv12_aux_surface(plane_state);
2963 if (ret)
2964 return ret;
2965 } else {
2966 plane_state->aux.offset = ~0xfff;
2967 plane_state->aux.x = 0;
2968 plane_state->aux.y = 0;
2969 }
2970
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002971 ret = skl_check_main_surface(plane_state);
2972 if (ret)
2973 return ret;
2974
2975 return 0;
2976}
2977
Ville Syrjälä7145f602017-03-23 21:27:07 +02002978static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
2979 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002980{
Ville Syrjälä7145f602017-03-23 21:27:07 +02002981 struct drm_i915_private *dev_priv =
2982 to_i915(plane_state->base.plane->dev);
2983 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2984 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002985 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä7145f602017-03-23 21:27:07 +02002986 u32 dspcntr;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002987
Ville Syrjälä7145f602017-03-23 21:27:07 +02002988 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002989
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02002990 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
2991 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Ville Syrjälä7145f602017-03-23 21:27:07 +02002992 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002993
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02002994 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
2995 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002996
Ville Syrjäläd509e282017-03-27 21:55:32 +03002997 if (INTEL_GEN(dev_priv) < 4)
2998 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002999
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003000 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02003001 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003002 dspcntr |= DISPPLANE_8BPP;
3003 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003004 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003005 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003006 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003007 case DRM_FORMAT_RGB565:
3008 dspcntr |= DISPPLANE_BGRX565;
3009 break;
3010 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003011 dspcntr |= DISPPLANE_BGRX888;
3012 break;
3013 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003014 dspcntr |= DISPPLANE_RGBX888;
3015 break;
3016 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003017 dspcntr |= DISPPLANE_BGRX101010;
3018 break;
3019 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003020 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003021 break;
3022 default:
Ville Syrjälä7145f602017-03-23 21:27:07 +02003023 MISSING_CASE(fb->format->format);
3024 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003025 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003026
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003027 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003028 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003029 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003030
Robert Fossc2c446a2017-05-19 16:50:17 -04003031 if (rotation & DRM_MODE_ROTATE_180)
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003032 dspcntr |= DISPPLANE_ROTATE_180;
3033
Robert Fossc2c446a2017-05-19 16:50:17 -04003034 if (rotation & DRM_MODE_REFLECT_X)
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003035 dspcntr |= DISPPLANE_MIRROR;
3036
Ville Syrjälä7145f602017-03-23 21:27:07 +02003037 return dspcntr;
3038}
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003039
Ville Syrjäläf9407ae2017-03-23 21:27:12 +02003040int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003041{
3042 struct drm_i915_private *dev_priv =
3043 to_i915(plane_state->base.plane->dev);
3044 int src_x = plane_state->base.src.x1 >> 16;
3045 int src_y = plane_state->base.src.y1 >> 16;
3046 u32 offset;
3047
3048 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003049
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003050 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003051 offset = intel_compute_tile_offset(&src_x, &src_y,
3052 plane_state, 0);
3053 else
3054 offset = 0;
Daniel Vettere506a0c2012-07-05 12:17:29 +02003055
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003056 /* HSW/BDW do this automagically in hardware */
3057 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3058 unsigned int rotation = plane_state->base.rotation;
3059 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3060 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3061
Robert Fossc2c446a2017-05-19 16:50:17 -04003062 if (rotation & DRM_MODE_ROTATE_180) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003063 src_x += src_w - 1;
3064 src_y += src_h - 1;
Robert Fossc2c446a2017-05-19 16:50:17 -04003065 } else if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003066 src_x += src_w - 1;
3067 }
Sonika Jindal48404c12014-08-22 14:06:04 +05303068 }
3069
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003070 plane_state->main.offset = offset;
3071 plane_state->main.x = src_x;
3072 plane_state->main.y = src_y;
3073
3074 return 0;
3075}
3076
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003077static void i9xx_update_primary_plane(struct intel_plane *primary,
Ville Syrjälä7145f602017-03-23 21:27:07 +02003078 const struct intel_crtc_state *crtc_state,
3079 const struct intel_plane_state *plane_state)
3080{
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003081 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3082 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3083 const struct drm_framebuffer *fb = plane_state->base.fb;
3084 enum plane plane = primary->plane;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003085 u32 linear_offset;
Ville Syrjäläa0864d52017-03-23 21:27:09 +02003086 u32 dspcntr = plane_state->ctl;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003087 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003088 int x = plane_state->main.x;
3089 int y = plane_state->main.y;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003090 unsigned long irqflags;
3091
Ville Syrjälä29490562016-01-20 18:02:50 +02003092 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003093
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003094 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003095 crtc->dspaddr_offset = plane_state->main.offset;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003096 else
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003097 crtc->dspaddr_offset = linear_offset;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003098
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003099 crtc->adjusted_x = x;
3100 crtc->adjusted_y = y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003101
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003102 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3103
Ville Syrjälä78587de2017-03-09 17:44:32 +02003104 if (INTEL_GEN(dev_priv) < 4) {
3105 /* pipesrc and dspsize control the size that is scaled from,
3106 * which should always be the user's requested size.
3107 */
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003108 I915_WRITE_FW(DSPSIZE(plane),
3109 ((crtc_state->pipe_src_h - 1) << 16) |
3110 (crtc_state->pipe_src_w - 1));
3111 I915_WRITE_FW(DSPPOS(plane), 0);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003112 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003113 I915_WRITE_FW(PRIMSIZE(plane),
3114 ((crtc_state->pipe_src_h - 1) << 16) |
3115 (crtc_state->pipe_src_w - 1));
3116 I915_WRITE_FW(PRIMPOS(plane), 0);
3117 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003118 }
3119
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003120 I915_WRITE_FW(reg, dspcntr);
Sonika Jindal48404c12014-08-22 14:06:04 +05303121
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003122 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003123 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3124 I915_WRITE_FW(DSPSURF(plane),
3125 intel_plane_ggtt_offset(plane_state) +
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003126 crtc->dspaddr_offset);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003127 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
3128 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003129 I915_WRITE_FW(DSPSURF(plane),
3130 intel_plane_ggtt_offset(plane_state) +
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003131 crtc->dspaddr_offset);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003132 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3133 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003134 } else {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003135 I915_WRITE_FW(DSPADDR(plane),
3136 intel_plane_ggtt_offset(plane_state) +
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003137 crtc->dspaddr_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003138 }
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003139 POSTING_READ_FW(reg);
3140
3141 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003142}
3143
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003144static void i9xx_disable_primary_plane(struct intel_plane *primary,
3145 struct intel_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003146{
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003147 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3148 enum plane plane = primary->plane;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003149 unsigned long irqflags;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003150
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003151 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3152
3153 I915_WRITE_FW(DSPCNTR(plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003154 if (INTEL_INFO(dev_priv)->gen >= 4)
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003155 I915_WRITE_FW(DSPSURF(plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003156 else
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003157 I915_WRITE_FW(DSPADDR(plane), 0);
3158 POSTING_READ_FW(DSPCNTR(plane));
3159
3160 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003161}
3162
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003163static u32
3164intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
Damien Lespiaub3218032015-02-27 11:15:18 +00003165{
Ben Widawsky2f075562017-03-24 14:29:48 -07003166 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003167 return 64;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003168 else
3169 return intel_tile_width_bytes(fb, plane);
Damien Lespiaub3218032015-02-27 11:15:18 +00003170}
3171
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003172static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3173{
3174 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003175 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003176
3177 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3178 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3179 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003180}
3181
Chandra Kondurua1b22782015-04-07 15:28:45 -07003182/*
3183 * This function detaches (aka. unbinds) unused scalers in hardware
3184 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003185static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003186{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003187 struct intel_crtc_scaler_state *scaler_state;
3188 int i;
3189
Chandra Kondurua1b22782015-04-07 15:28:45 -07003190 scaler_state = &intel_crtc->config->scaler_state;
3191
3192 /* loop through and disable scalers that aren't in use */
3193 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003194 if (!scaler_state->scalers[i].in_use)
3195 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003196 }
3197}
3198
Ville Syrjäläd2196772016-01-28 18:33:11 +02003199u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3200 unsigned int rotation)
3201{
Ville Syrjälä1b500532017-03-07 21:42:08 +02003202 u32 stride;
3203
3204 if (plane >= fb->format->num_planes)
3205 return 0;
3206
3207 stride = intel_fb_pitch(fb, plane, rotation);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003208
3209 /*
3210 * The stride is either expressed as a multiple of 64 bytes chunks for
3211 * linear buffers or in number of tiles for tiled buffers.
3212 */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003213 if (drm_rotation_90_or_270(rotation))
3214 stride /= intel_tile_height(fb, plane);
3215 else
3216 stride /= intel_fb_stride_alignment(fb, plane);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003217
3218 return stride;
3219}
3220
Ville Syrjälä2e881262017-03-17 23:17:56 +02003221static u32 skl_plane_ctl_format(uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -07003222{
Chandra Konduru6156a452015-04-27 13:48:39 -07003223 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003224 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003225 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003226 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003227 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003228 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003229 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003230 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003231 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003232 /*
3233 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3234 * to be already pre-multiplied. We need to add a knob (or a different
3235 * DRM_FORMAT) for user-space to configure that.
3236 */
3237 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003238 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003239 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003240 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003241 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003242 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003243 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003244 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003245 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003246 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003247 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003248 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003249 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003250 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003251 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003252 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003253 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003254 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003255 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003256 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003257 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003258
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003259 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003260}
3261
Ville Syrjälä2e881262017-03-17 23:17:56 +02003262static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
Chandra Konduru6156a452015-04-27 13:48:39 -07003263{
Chandra Konduru6156a452015-04-27 13:48:39 -07003264 switch (fb_modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07003265 case DRM_FORMAT_MOD_LINEAR:
Chandra Konduru6156a452015-04-27 13:48:39 -07003266 break;
3267 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003268 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003269 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003270 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003271 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003272 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003273 default:
3274 MISSING_CASE(fb_modifier);
3275 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003276
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003277 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003278}
3279
Ville Syrjälä2e881262017-03-17 23:17:56 +02003280static u32 skl_plane_ctl_rotation(unsigned int rotation)
Chandra Konduru6156a452015-04-27 13:48:39 -07003281{
Chandra Konduru6156a452015-04-27 13:48:39 -07003282 switch (rotation) {
Robert Fossc2c446a2017-05-19 16:50:17 -04003283 case DRM_MODE_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003284 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303285 /*
Robert Fossc2c446a2017-05-19 16:50:17 -04003286 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
Sonika Jindal1e8df162015-05-20 13:40:48 +05303287 * while i915 HW rotation is clockwise, thats why this swapping.
3288 */
Robert Fossc2c446a2017-05-19 16:50:17 -04003289 case DRM_MODE_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303290 return PLANE_CTL_ROTATE_270;
Robert Fossc2c446a2017-05-19 16:50:17 -04003291 case DRM_MODE_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003292 return PLANE_CTL_ROTATE_180;
Robert Fossc2c446a2017-05-19 16:50:17 -04003293 case DRM_MODE_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303294 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003295 default:
3296 MISSING_CASE(rotation);
3297 }
3298
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003299 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003300}
3301
Ville Syrjälä2e881262017-03-17 23:17:56 +02003302u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3303 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003304{
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003305 struct drm_i915_private *dev_priv =
3306 to_i915(plane_state->base.plane->dev);
3307 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003308 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä2e881262017-03-17 23:17:56 +02003309 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003310 u32 plane_ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003311
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003312 plane_ctl = PLANE_CTL_ENABLE;
3313
Rodrigo Vivi6602be02017-07-06 14:01:13 -07003314 if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003315 plane_ctl |=
3316 PLANE_CTL_PIPE_GAMMA_ENABLE |
3317 PLANE_CTL_PIPE_CSC_ENABLE |
3318 PLANE_CTL_PLANE_GAMMA_DISABLE;
3319 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003320
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003321 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003322 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Chandra Konduru6156a452015-04-27 13:48:39 -07003323 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003324
Ville Syrjälä2e881262017-03-17 23:17:56 +02003325 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3326 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3327 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3328 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3329
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003330 return plane_ctl;
3331}
3332
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003333static void skylake_update_primary_plane(struct intel_plane *plane,
Damien Lespiau70d21f02013-07-03 21:06:04 +01003334 const struct intel_crtc_state *crtc_state,
3335 const struct intel_plane_state *plane_state)
3336{
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003337 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3338 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3339 const struct drm_framebuffer *fb = plane_state->base.fb;
3340 enum plane_id plane_id = plane->id;
3341 enum pipe pipe = plane->pipe;
Ville Syrjäläa0864d52017-03-23 21:27:09 +02003342 u32 plane_ctl = plane_state->ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003343 unsigned int rotation = plane_state->base.rotation;
3344 u32 stride = skl_plane_stride(fb, 0, rotation);
3345 u32 surf_addr = plane_state->main.offset;
3346 int scaler_id = plane_state->scaler_id;
3347 int src_x = plane_state->main.x;
3348 int src_y = plane_state->main.y;
3349 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3350 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3351 int dst_x = plane_state->base.dst.x1;
3352 int dst_y = plane_state->base.dst.y1;
3353 int dst_w = drm_rect_width(&plane_state->base.dst);
3354 int dst_h = drm_rect_height(&plane_state->base.dst);
3355 unsigned long irqflags;
3356
Ville Syrjälä6687c902015-09-15 13:16:41 +03003357 /* Sizes are 0 based */
3358 src_w--;
3359 src_h--;
3360 dst_w--;
3361 dst_h--;
3362
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003363 crtc->dspaddr_offset = surf_addr;
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003364
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003365 crtc->adjusted_x = src_x;
3366 crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003367
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003368 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3369
Rodrigo Vivi6602be02017-07-06 14:01:13 -07003370 if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003371 I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
3372 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3373 PLANE_COLOR_PIPE_CSC_ENABLE |
3374 PLANE_COLOR_PLANE_GAMMA_DISABLE);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003375 }
3376
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003377 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
3378 I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3379 I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
3380 I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003381
3382 if (scaler_id >= 0) {
3383 uint32_t ps_ctrl = 0;
3384
3385 WARN_ON(!dst_w || !dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003386 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
Chandra Konduru6156a452015-04-27 13:48:39 -07003387 crtc_state->scaler_state.scalers[scaler_id].mode;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003388 I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3389 I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3390 I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3391 I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3392 I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003393 } else {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003394 I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
Chandra Konduru6156a452015-04-27 13:48:39 -07003395 }
3396
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003397 I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
3398 intel_plane_ggtt_offset(plane_state) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003399
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003400 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3401
3402 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003403}
3404
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003405static void skylake_disable_primary_plane(struct intel_plane *primary,
3406 struct intel_crtc *crtc)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003407{
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003408 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3409 enum plane_id plane_id = primary->id;
3410 enum pipe pipe = primary->pipe;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003411 unsigned long irqflags;
Lyude62e0fb82016-08-22 12:50:08 -04003412
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003413 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3414
3415 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
3416 I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
3417 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3418
3419 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003420}
3421
Daniel Vetter5a21b662016-05-24 17:13:53 +02003422static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3423{
3424 struct intel_crtc *crtc;
3425
Chris Wilson91c8a322016-07-05 10:40:23 +01003426 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003427 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3428}
3429
Ville Syrjälä75147472014-11-24 18:28:11 +02003430static void intel_update_primary_planes(struct drm_device *dev)
3431{
Ville Syrjälä75147472014-11-24 18:28:11 +02003432 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003433
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003434 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003435 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003436 struct intel_plane_state *plane_state =
3437 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003438
Ville Syrjälä72259532017-03-02 19:15:05 +02003439 if (plane_state->base.visible) {
3440 trace_intel_update_plane(&plane->base,
3441 to_intel_crtc(crtc));
3442
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003443 plane->update_plane(plane,
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003444 to_intel_crtc_state(crtc->state),
3445 plane_state);
Ville Syrjälä72259532017-03-02 19:15:05 +02003446 }
Ville Syrjälä96a02912013-02-18 19:08:49 +02003447 }
3448}
3449
Maarten Lankhorst73974892016-08-05 23:28:27 +03003450static int
3451__intel_display_resume(struct drm_device *dev,
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003452 struct drm_atomic_state *state,
3453 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorst73974892016-08-05 23:28:27 +03003454{
3455 struct drm_crtc_state *crtc_state;
3456 struct drm_crtc *crtc;
3457 int i, ret;
3458
Ville Syrjäläaecd36b2017-06-01 17:36:13 +03003459 intel_modeset_setup_hw_state(dev, ctx);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003460 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003461
3462 if (!state)
3463 return 0;
3464
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01003465 /*
3466 * We've duplicated the state, pointers to the old state are invalid.
3467 *
3468 * Don't attempt to use the old state until we commit the duplicated state.
3469 */
3470 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst73974892016-08-05 23:28:27 +03003471 /*
3472 * Force recalculation even if we restore
3473 * current state. With fast modeset this may not result
3474 * in a modeset when the state is compatible.
3475 */
3476 crtc_state->mode_changed = true;
3477 }
3478
3479 /* ignore any reset values/BIOS leftovers in the WM registers */
Ville Syrjälä602ae832017-03-02 19:15:02 +02003480 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3481 to_intel_atomic_state(state)->skip_intermediate_wm = true;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003482
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003483 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003484
3485 WARN_ON(ret == -EDEADLK);
3486 return ret;
3487}
3488
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003489static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3490{
Ville Syrjäläae981042016-08-05 23:28:30 +03003491 return intel_has_gpu_reset(dev_priv) &&
3492 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003493}
3494
Chris Wilsonc0336662016-05-06 15:40:21 +01003495void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003496{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003497 struct drm_device *dev = &dev_priv->drm;
3498 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3499 struct drm_atomic_state *state;
3500 int ret;
3501
Maarten Lankhorst73974892016-08-05 23:28:27 +03003502 /*
3503 * Need mode_config.mutex so that we don't
3504 * trample ongoing ->detect() and whatnot.
3505 */
3506 mutex_lock(&dev->mode_config.mutex);
3507 drm_modeset_acquire_init(ctx, 0);
3508 while (1) {
3509 ret = drm_modeset_lock_all_ctx(dev, ctx);
3510 if (ret != -EDEADLK)
3511 break;
3512
3513 drm_modeset_backoff(ctx);
3514 }
3515
3516 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003517 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003518 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003519 return;
3520
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003521 /*
3522 * Disabling the crtcs gracefully seems nicer. Also the
3523 * g33 docs say we should at least disable all the planes.
3524 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003525 state = drm_atomic_helper_duplicate_state(dev, ctx);
3526 if (IS_ERR(state)) {
3527 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003528 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003529 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003530 }
3531
3532 ret = drm_atomic_helper_disable_all(dev, ctx);
3533 if (ret) {
3534 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003535 drm_atomic_state_put(state);
3536 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003537 }
3538
3539 dev_priv->modeset_restore_state = state;
3540 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003541}
3542
Chris Wilsonc0336662016-05-06 15:40:21 +01003543void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003544{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003545 struct drm_device *dev = &dev_priv->drm;
3546 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3547 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3548 int ret;
3549
Daniel Vetter5a21b662016-05-24 17:13:53 +02003550 /*
3551 * Flips in the rings will be nuked by the reset,
3552 * so complete all pending flips so that user space
3553 * will get its events and not get stuck.
3554 */
3555 intel_complete_page_flips(dev_priv);
3556
Maarten Lankhorst73974892016-08-05 23:28:27 +03003557 dev_priv->modeset_restore_state = NULL;
3558
Ville Syrjälä75147472014-11-24 18:28:11 +02003559 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003560 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003561 if (!state) {
3562 /*
3563 * Flips in the rings have been nuked by the reset,
3564 * so update the base address of all primary
3565 * planes to the the last fb to make sure we're
3566 * showing the correct fb after a reset.
3567 *
3568 * FIXME: Atomic will make this obsolete since we won't schedule
3569 * CS-based flips (which might get lost in gpu resets) any more.
3570 */
3571 intel_update_primary_planes(dev);
3572 } else {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003573 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003574 if (ret)
3575 DRM_ERROR("Restoring old state failed with %i\n", ret);
3576 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003577 } else {
3578 /*
3579 * The display has been reset as well,
3580 * so need a full re-initialization.
3581 */
3582 intel_runtime_pm_disable_interrupts(dev_priv);
3583 intel_runtime_pm_enable_interrupts(dev_priv);
3584
Imre Deak51f59202016-09-14 13:04:13 +03003585 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003586 intel_modeset_init_hw(dev);
3587
3588 spin_lock_irq(&dev_priv->irq_lock);
3589 if (dev_priv->display.hpd_irq_setup)
3590 dev_priv->display.hpd_irq_setup(dev_priv);
3591 spin_unlock_irq(&dev_priv->irq_lock);
3592
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003593 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003594 if (ret)
3595 DRM_ERROR("Restoring old state failed with %i\n", ret);
3596
3597 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003598 }
3599
Chris Wilson08536952016-10-14 13:18:18 +01003600 if (state)
3601 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003602 drm_modeset_drop_locks(ctx);
3603 drm_modeset_acquire_fini(ctx);
3604 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003605}
3606
Chris Wilson8af29b02016-09-09 14:11:47 +01003607static bool abort_flip_on_reset(struct intel_crtc *crtc)
3608{
3609 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3610
Chris Wilson8c185ec2017-03-16 17:13:02 +00003611 if (i915_reset_backoff(error))
Chris Wilson8af29b02016-09-09 14:11:47 +01003612 return true;
3613
3614 if (crtc->reset_count != i915_reset_count(error))
3615 return true;
3616
3617 return false;
3618}
3619
Chris Wilson7d5e3792014-03-04 13:15:08 +00003620static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3621{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003622 struct drm_device *dev = crtc->dev;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003624 bool pending;
3625
Chris Wilson8af29b02016-09-09 14:11:47 +01003626 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003627 return false;
3628
3629 spin_lock_irq(&dev->event_lock);
3630 pending = to_intel_crtc(crtc)->flip_work != NULL;
3631 spin_unlock_irq(&dev->event_lock);
3632
3633 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003634}
3635
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003636static void intel_update_pipe_config(struct intel_crtc *crtc,
3637 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003638{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003639 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003640 struct intel_crtc_state *pipe_config =
3641 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003642
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003643 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3644 crtc->base.mode = crtc->base.state->mode;
3645
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003646 /*
3647 * Update pipe size and adjust fitter if needed: the reason for this is
3648 * that in compute_mode_changes we check the native mode (not the pfit
3649 * mode) to see if we can flip rather than do a full mode set. In the
3650 * fastboot case, we'll flip, but if we don't update the pipesrc and
3651 * pfit state, we'll end up with a big fb scanned out into the wrong
3652 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003653 */
3654
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003655 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003656 ((pipe_config->pipe_src_w - 1) << 16) |
3657 (pipe_config->pipe_src_h - 1));
3658
3659 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003660 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003661 skl_detach_scalers(crtc);
3662
3663 if (pipe_config->pch_pfit.enabled)
3664 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003665 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003666 if (pipe_config->pch_pfit.enabled)
3667 ironlake_pfit_enable(crtc);
3668 else if (old_crtc_state->pch_pfit.enabled)
3669 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003670 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003671}
3672
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003673static void intel_fdi_normal_train(struct intel_crtc *crtc)
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003674{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003675 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003676 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003677 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003678 i915_reg_t reg;
3679 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003680
3681 /* enable normal train */
3682 reg = FDI_TX_CTL(pipe);
3683 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003684 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003685 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3686 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003687 } else {
3688 temp &= ~FDI_LINK_TRAIN_NONE;
3689 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003690 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003691 I915_WRITE(reg, temp);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003695 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003696 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3697 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3698 } else {
3699 temp &= ~FDI_LINK_TRAIN_NONE;
3700 temp |= FDI_LINK_TRAIN_NONE;
3701 }
3702 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3703
3704 /* wait one idle pattern time */
3705 POSTING_READ(reg);
3706 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003707
3708 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003709 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003710 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3711 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003712}
3713
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003714/* The FDI link training functions for ILK/Ibexpeak. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003715static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3716 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003717{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003718 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003719 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003720 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003721 i915_reg_t reg;
3722 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003723
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003724 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003725 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003726
Adam Jacksone1a44742010-06-25 15:32:14 -04003727 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3728 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003729 reg = FDI_RX_IMR(pipe);
3730 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003731 temp &= ~FDI_RX_SYMBOL_LOCK;
3732 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003733 I915_WRITE(reg, temp);
3734 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003735 udelay(150);
3736
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003737 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003740 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003741 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003742 temp &= ~FDI_LINK_TRAIN_NONE;
3743 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003744 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003745
Chris Wilson5eddb702010-09-11 13:48:45 +01003746 reg = FDI_RX_CTL(pipe);
3747 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003748 temp &= ~FDI_LINK_TRAIN_NONE;
3749 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003750 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3751
3752 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003753 udelay(150);
3754
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003755 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003756 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3757 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3758 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003759
Chris Wilson5eddb702010-09-11 13:48:45 +01003760 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003761 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003762 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003763 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3764
3765 if ((temp & FDI_RX_BIT_LOCK)) {
3766 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003767 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003768 break;
3769 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003770 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003771 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003772 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003773
3774 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003779 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003780
Chris Wilson5eddb702010-09-11 13:48:45 +01003781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003783 temp &= ~FDI_LINK_TRAIN_NONE;
3784 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003785 I915_WRITE(reg, temp);
3786
3787 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003788 udelay(150);
3789
Chris Wilson5eddb702010-09-11 13:48:45 +01003790 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003791 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003792 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003793 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3794
3795 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003796 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003797 DRM_DEBUG_KMS("FDI train 2 done.\n");
3798 break;
3799 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003800 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003801 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003802 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003803
3804 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003805
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003806}
3807
Akshay Joshi0206e352011-08-16 15:34:10 -04003808static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003809 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3810 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3811 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3812 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3813};
3814
3815/* The FDI link training functions for SNB/Cougarpoint. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003816static void gen6_fdi_link_train(struct intel_crtc *crtc,
3817 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003818{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003819 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003820 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003821 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003822 i915_reg_t reg;
3823 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003824
Adam Jacksone1a44742010-06-25 15:32:14 -04003825 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3826 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003827 reg = FDI_RX_IMR(pipe);
3828 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003829 temp &= ~FDI_RX_SYMBOL_LOCK;
3830 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003831 I915_WRITE(reg, temp);
3832
3833 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003834 udelay(150);
3835
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003836 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003837 reg = FDI_TX_CTL(pipe);
3838 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003839 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003840 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003841 temp &= ~FDI_LINK_TRAIN_NONE;
3842 temp |= FDI_LINK_TRAIN_PATTERN_1;
3843 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3844 /* SNB-B */
3845 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003846 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003847
Daniel Vetterd74cf322012-10-26 10:58:13 +02003848 I915_WRITE(FDI_RX_MISC(pipe),
3849 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3850
Chris Wilson5eddb702010-09-11 13:48:45 +01003851 reg = FDI_RX_CTL(pipe);
3852 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003853 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003854 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3855 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3856 } else {
3857 temp &= ~FDI_LINK_TRAIN_NONE;
3858 temp |= FDI_LINK_TRAIN_PATTERN_1;
3859 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003860 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3861
3862 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003863 udelay(150);
3864
Akshay Joshi0206e352011-08-16 15:34:10 -04003865 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003866 reg = FDI_TX_CTL(pipe);
3867 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003868 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3869 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003870 I915_WRITE(reg, temp);
3871
3872 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003873 udelay(500);
3874
Sean Paulfa37d392012-03-02 12:53:39 -05003875 for (retry = 0; retry < 5; retry++) {
3876 reg = FDI_RX_IIR(pipe);
3877 temp = I915_READ(reg);
3878 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3879 if (temp & FDI_RX_BIT_LOCK) {
3880 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3881 DRM_DEBUG_KMS("FDI train 1 done.\n");
3882 break;
3883 }
3884 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003885 }
Sean Paulfa37d392012-03-02 12:53:39 -05003886 if (retry < 5)
3887 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003888 }
3889 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003890 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003891
3892 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003893 reg = FDI_TX_CTL(pipe);
3894 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003895 temp &= ~FDI_LINK_TRAIN_NONE;
3896 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003897 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3899 /* SNB-B */
3900 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3901 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003902 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003903
Chris Wilson5eddb702010-09-11 13:48:45 +01003904 reg = FDI_RX_CTL(pipe);
3905 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003906 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003907 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3908 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3909 } else {
3910 temp &= ~FDI_LINK_TRAIN_NONE;
3911 temp |= FDI_LINK_TRAIN_PATTERN_2;
3912 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003913 I915_WRITE(reg, temp);
3914
3915 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003916 udelay(150);
3917
Akshay Joshi0206e352011-08-16 15:34:10 -04003918 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003919 reg = FDI_TX_CTL(pipe);
3920 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003921 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3922 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003923 I915_WRITE(reg, temp);
3924
3925 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003926 udelay(500);
3927
Sean Paulfa37d392012-03-02 12:53:39 -05003928 for (retry = 0; retry < 5; retry++) {
3929 reg = FDI_RX_IIR(pipe);
3930 temp = I915_READ(reg);
3931 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3932 if (temp & FDI_RX_SYMBOL_LOCK) {
3933 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3934 DRM_DEBUG_KMS("FDI train 2 done.\n");
3935 break;
3936 }
3937 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003938 }
Sean Paulfa37d392012-03-02 12:53:39 -05003939 if (retry < 5)
3940 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003941 }
3942 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003943 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003944
3945 DRM_DEBUG_KMS("FDI train done.\n");
3946}
3947
Jesse Barnes357555c2011-04-28 15:09:55 -07003948/* Manual link training for Ivy Bridge A0 parts */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003949static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
3950 const struct intel_crtc_state *crtc_state)
Jesse Barnes357555c2011-04-28 15:09:55 -07003951{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003952 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003953 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003954 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003955 i915_reg_t reg;
3956 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003957
3958 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3959 for train result */
3960 reg = FDI_RX_IMR(pipe);
3961 temp = I915_READ(reg);
3962 temp &= ~FDI_RX_SYMBOL_LOCK;
3963 temp &= ~FDI_RX_BIT_LOCK;
3964 I915_WRITE(reg, temp);
3965
3966 POSTING_READ(reg);
3967 udelay(150);
3968
Daniel Vetter01a415f2012-10-27 15:58:40 +02003969 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3970 I915_READ(FDI_RX_IIR(pipe)));
3971
Jesse Barnes139ccd32013-08-19 11:04:55 -07003972 /* Try each vswing and preemphasis setting twice before moving on */
3973 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3974 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003975 reg = FDI_TX_CTL(pipe);
3976 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003977 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3978 temp &= ~FDI_TX_ENABLE;
3979 I915_WRITE(reg, temp);
3980
3981 reg = FDI_RX_CTL(pipe);
3982 temp = I915_READ(reg);
3983 temp &= ~FDI_LINK_TRAIN_AUTO;
3984 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3985 temp &= ~FDI_RX_ENABLE;
3986 I915_WRITE(reg, temp);
3987
3988 /* enable CPU FDI TX and PCH FDI RX */
3989 reg = FDI_TX_CTL(pipe);
3990 temp = I915_READ(reg);
3991 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003992 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003993 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003994 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003995 temp |= snb_b_fdi_train_param[j/2];
3996 temp |= FDI_COMPOSITE_SYNC;
3997 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3998
3999 I915_WRITE(FDI_RX_MISC(pipe),
4000 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4001
4002 reg = FDI_RX_CTL(pipe);
4003 temp = I915_READ(reg);
4004 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4005 temp |= FDI_COMPOSITE_SYNC;
4006 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4007
4008 POSTING_READ(reg);
4009 udelay(1); /* should be 0.5us */
4010
4011 for (i = 0; i < 4; i++) {
4012 reg = FDI_RX_IIR(pipe);
4013 temp = I915_READ(reg);
4014 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4015
4016 if (temp & FDI_RX_BIT_LOCK ||
4017 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4018 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4019 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4020 i);
4021 break;
4022 }
4023 udelay(1); /* should be 0.5us */
4024 }
4025 if (i == 4) {
4026 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4027 continue;
4028 }
4029
4030 /* Train 2 */
4031 reg = FDI_TX_CTL(pipe);
4032 temp = I915_READ(reg);
4033 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4034 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4035 I915_WRITE(reg, temp);
4036
4037 reg = FDI_RX_CTL(pipe);
4038 temp = I915_READ(reg);
4039 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4040 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004041 I915_WRITE(reg, temp);
4042
4043 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004044 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004045
Jesse Barnes139ccd32013-08-19 11:04:55 -07004046 for (i = 0; i < 4; i++) {
4047 reg = FDI_RX_IIR(pipe);
4048 temp = I915_READ(reg);
4049 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004050
Jesse Barnes139ccd32013-08-19 11:04:55 -07004051 if (temp & FDI_RX_SYMBOL_LOCK ||
4052 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4053 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4054 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4055 i);
4056 goto train_done;
4057 }
4058 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004059 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004060 if (i == 4)
4061 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004062 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004063
Jesse Barnes139ccd32013-08-19 11:04:55 -07004064train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004065 DRM_DEBUG_KMS("FDI train done.\n");
4066}
4067
Daniel Vetter88cefb62012-08-12 19:27:14 +02004068static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004069{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004070 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004071 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004072 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004073 i915_reg_t reg;
4074 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004075
Jesse Barnes0e23b992010-09-10 11:10:00 -07004076 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004077 reg = FDI_RX_CTL(pipe);
4078 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004079 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004080 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004081 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004082 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4083
4084 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004085 udelay(200);
4086
4087 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004088 temp = I915_READ(reg);
4089 I915_WRITE(reg, temp | FDI_PCDCLK);
4090
4091 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004092 udelay(200);
4093
Paulo Zanoni20749732012-11-23 15:30:38 -02004094 /* Enable CPU FDI TX PLL, always on for Ironlake */
4095 reg = FDI_TX_CTL(pipe);
4096 temp = I915_READ(reg);
4097 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4098 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004099
Paulo Zanoni20749732012-11-23 15:30:38 -02004100 POSTING_READ(reg);
4101 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004102 }
4103}
4104
Daniel Vetter88cefb62012-08-12 19:27:14 +02004105static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4106{
4107 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004108 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004109 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004110 i915_reg_t reg;
4111 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004112
4113 /* Switch from PCDclk to Rawclk */
4114 reg = FDI_RX_CTL(pipe);
4115 temp = I915_READ(reg);
4116 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4117
4118 /* Disable CPU FDI TX PLL */
4119 reg = FDI_TX_CTL(pipe);
4120 temp = I915_READ(reg);
4121 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4122
4123 POSTING_READ(reg);
4124 udelay(100);
4125
4126 reg = FDI_RX_CTL(pipe);
4127 temp = I915_READ(reg);
4128 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4129
4130 /* Wait for the clocks to turn off. */
4131 POSTING_READ(reg);
4132 udelay(100);
4133}
4134
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004135static void ironlake_fdi_disable(struct drm_crtc *crtc)
4136{
4137 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004138 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4140 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004141 i915_reg_t reg;
4142 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004143
4144 /* disable CPU FDI tx and PCH FDI rx */
4145 reg = FDI_TX_CTL(pipe);
4146 temp = I915_READ(reg);
4147 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4148 POSTING_READ(reg);
4149
4150 reg = FDI_RX_CTL(pipe);
4151 temp = I915_READ(reg);
4152 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004153 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004154 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4155
4156 POSTING_READ(reg);
4157 udelay(100);
4158
4159 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004160 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004161 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004162
4163 /* still set train pattern 1 */
4164 reg = FDI_TX_CTL(pipe);
4165 temp = I915_READ(reg);
4166 temp &= ~FDI_LINK_TRAIN_NONE;
4167 temp |= FDI_LINK_TRAIN_PATTERN_1;
4168 I915_WRITE(reg, temp);
4169
4170 reg = FDI_RX_CTL(pipe);
4171 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004172 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004173 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4174 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4175 } else {
4176 temp &= ~FDI_LINK_TRAIN_NONE;
4177 temp |= FDI_LINK_TRAIN_PATTERN_1;
4178 }
4179 /* BPC in FDI rx is consistent with that in PIPECONF */
4180 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004181 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004182 I915_WRITE(reg, temp);
4183
4184 POSTING_READ(reg);
4185 udelay(100);
4186}
4187
Chris Wilson49d73912016-11-29 09:50:08 +00004188bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004189{
4190 struct intel_crtc *crtc;
4191
4192 /* Note that we don't need to be called with mode_config.lock here
4193 * as our list of CRTC objects is static for the lifetime of the
4194 * device and so cannot disappear as we iterate. Similarly, we can
4195 * happily treat the predicates as racy, atomic checks as userspace
4196 * cannot claim and pin a new fb without at least acquring the
4197 * struct_mutex and so serialising with us.
4198 */
Chris Wilson49d73912016-11-29 09:50:08 +00004199 for_each_intel_crtc(&dev_priv->drm, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004200 if (atomic_read(&crtc->unpin_work_count) == 0)
4201 continue;
4202
Daniel Vetter5a21b662016-05-24 17:13:53 +02004203 if (crtc->flip_work)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004204 intel_wait_for_vblank(dev_priv, crtc->pipe);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004205
4206 return true;
4207 }
4208
4209 return false;
4210}
4211
Daniel Vetter5a21b662016-05-24 17:13:53 +02004212static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004213{
4214 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004215 struct intel_flip_work *work = intel_crtc->flip_work;
4216
4217 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004218
4219 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004220 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004221
4222 drm_crtc_vblank_put(&intel_crtc->base);
4223
Daniel Vetter5a21b662016-05-24 17:13:53 +02004224 wake_up_all(&dev_priv->pending_flip_queue);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004225 trace_i915_flip_complete(intel_crtc->plane,
4226 work->pending_flip_obj);
Andrey Ryabinin05c41f92017-01-26 17:32:11 +03004227
4228 queue_work(dev_priv->wq, &work->unpin_work);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004229}
4230
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004231static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004232{
Chris Wilson0f911282012-04-17 10:05:38 +01004233 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004234 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004235 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004236
Daniel Vetter2c10d572012-12-20 21:24:07 +01004237 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004238
4239 ret = wait_event_interruptible_timeout(
4240 dev_priv->pending_flip_queue,
4241 !intel_crtc_has_pending_flip(crtc),
4242 60*HZ);
4243
4244 if (ret < 0)
4245 return ret;
4246
Daniel Vetter5a21b662016-05-24 17:13:53 +02004247 if (ret == 0) {
4248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4249 struct intel_flip_work *work;
4250
4251 spin_lock_irq(&dev->event_lock);
4252 work = intel_crtc->flip_work;
4253 if (work && !is_mmio_work(work)) {
4254 WARN_ONCE(1, "Removing stuck page flip\n");
4255 page_flip_completed(intel_crtc);
4256 }
4257 spin_unlock_irq(&dev->event_lock);
4258 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004259
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004260 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004261}
4262
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004263void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004264{
4265 u32 temp;
4266
4267 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4268
4269 mutex_lock(&dev_priv->sb_lock);
4270
4271 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4272 temp |= SBI_SSCCTL_DISABLE;
4273 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4274
4275 mutex_unlock(&dev_priv->sb_lock);
4276}
4277
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004278/* Program iCLKIP clock to the desired frequency */
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004279static void lpt_program_iclkip(struct intel_crtc *crtc)
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004280{
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004281 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4282 int clock = crtc->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004283 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4284 u32 temp;
4285
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004286 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004287
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004288 /* The iCLK virtual clock root frequency is in MHz,
4289 * but the adjusted_mode->crtc_clock in in KHz. To get the
4290 * divisors, it is necessary to divide one by another, so we
4291 * convert the virtual clock precision to KHz here for higher
4292 * precision.
4293 */
4294 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004295 u32 iclk_virtual_root_freq = 172800 * 1000;
4296 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004297 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004298
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004299 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4300 clock << auxdiv);
4301 divsel = (desired_divisor / iclk_pi_range) - 2;
4302 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004303
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004304 /*
4305 * Near 20MHz is a corner case which is
4306 * out of range for the 7-bit divisor
4307 */
4308 if (divsel <= 0x7f)
4309 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004310 }
4311
4312 /* This should not happen with any sane values */
4313 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4314 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4315 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4316 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4317
4318 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 +03004319 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004320 auxdiv,
4321 divsel,
4322 phasedir,
4323 phaseinc);
4324
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004325 mutex_lock(&dev_priv->sb_lock);
4326
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004327 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004328 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004329 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4330 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4331 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4332 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4333 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4334 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004335 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004336
4337 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004338 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004339 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4340 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004341 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004342
4343 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004344 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004345 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004346 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004347
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004348 mutex_unlock(&dev_priv->sb_lock);
4349
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004350 /* Wait for initialization time */
4351 udelay(24);
4352
4353 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4354}
4355
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004356int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4357{
4358 u32 divsel, phaseinc, auxdiv;
4359 u32 iclk_virtual_root_freq = 172800 * 1000;
4360 u32 iclk_pi_range = 64;
4361 u32 desired_divisor;
4362 u32 temp;
4363
4364 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4365 return 0;
4366
4367 mutex_lock(&dev_priv->sb_lock);
4368
4369 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4370 if (temp & SBI_SSCCTL_DISABLE) {
4371 mutex_unlock(&dev_priv->sb_lock);
4372 return 0;
4373 }
4374
4375 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4376 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4377 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4378 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4379 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4380
4381 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4382 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4383 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4384
4385 mutex_unlock(&dev_priv->sb_lock);
4386
4387 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4388
4389 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4390 desired_divisor << auxdiv);
4391}
4392
Daniel Vetter275f01b22013-05-03 11:49:47 +02004393static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4394 enum pipe pch_transcoder)
4395{
4396 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004397 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004398 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004399
4400 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4401 I915_READ(HTOTAL(cpu_transcoder)));
4402 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4403 I915_READ(HBLANK(cpu_transcoder)));
4404 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4405 I915_READ(HSYNC(cpu_transcoder)));
4406
4407 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4408 I915_READ(VTOTAL(cpu_transcoder)));
4409 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4410 I915_READ(VBLANK(cpu_transcoder)));
4411 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4412 I915_READ(VSYNC(cpu_transcoder)));
4413 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4414 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4415}
4416
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004417static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004418{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004419 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004420 uint32_t temp;
4421
4422 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004423 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004424 return;
4425
4426 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4427 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4428
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004429 temp &= ~FDI_BC_BIFURCATION_SELECT;
4430 if (enable)
4431 temp |= FDI_BC_BIFURCATION_SELECT;
4432
4433 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004434 I915_WRITE(SOUTH_CHICKEN1, temp);
4435 POSTING_READ(SOUTH_CHICKEN1);
4436}
4437
4438static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4439{
4440 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004441
4442 switch (intel_crtc->pipe) {
4443 case PIPE_A:
4444 break;
4445 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004446 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004447 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004448 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004449 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004450
4451 break;
4452 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004453 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004454
4455 break;
4456 default:
4457 BUG();
4458 }
4459}
4460
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004461/* Return which DP Port should be selected for Transcoder DP control */
4462static enum port
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004463intel_trans_dp_port_sel(struct intel_crtc *crtc)
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004464{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004465 struct drm_device *dev = crtc->base.dev;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004466 struct intel_encoder *encoder;
4467
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004468 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004469 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004470 encoder->type == INTEL_OUTPUT_EDP)
4471 return enc_to_dig_port(&encoder->base)->port;
4472 }
4473
4474 return -1;
4475}
4476
Jesse Barnesf67a5592011-01-05 10:31:48 -08004477/*
4478 * Enable PCH resources required for PCH ports:
4479 * - PCH PLLs
4480 * - FDI training & RX/TX
4481 * - update transcoder timings
4482 * - DP transcoding bits
4483 * - transcoder
4484 */
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004485static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08004486{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004487 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004488 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004489 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004490 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004491 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004492
Daniel Vetterab9412b2013-05-03 11:49:46 +02004493 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004494
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004495 if (IS_IVYBRIDGE(dev_priv))
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004496 ivybridge_update_fdi_bc_bifurcation(crtc);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004497
Daniel Vettercd986ab2012-10-26 10:58:12 +02004498 /* Write the TU size bits before fdi link training, so that error
4499 * detection works. */
4500 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4501 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4502
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004503 /* For PCH output, training FDI link */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004504 dev_priv->display.fdi_link_train(crtc, crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004505
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004506 /* We need to program the right clock selection before writing the pixel
4507 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004508 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004509 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004510
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004511 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004512 temp |= TRANS_DPLL_ENABLE(pipe);
4513 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004514 if (crtc_state->shared_dpll ==
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004515 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004516 temp |= sel;
4517 else
4518 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004519 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004520 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004521
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004522 /* XXX: pch pll's can be enabled any time before we enable the PCH
4523 * transcoder, and we actually should do this to not upset any PCH
4524 * transcoder that already use the clock when we share it.
4525 *
4526 * Note that enable_shared_dpll tries to do the right thing, but
4527 * get_shared_dpll unconditionally resets the pll - we need that to have
4528 * the right LVDS enable sequence. */
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004529 intel_enable_shared_dpll(crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004530
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004531 /* set transcoder timing, panel must allow it */
4532 assert_panel_unlocked(dev_priv, pipe);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004533 ironlake_pch_transcoder_set_timings(crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004534
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004535 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004536
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004537 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004538 if (HAS_PCH_CPT(dev_priv) &&
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004539 intel_crtc_has_dp_encoder(crtc_state)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004540 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004541 &crtc_state->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004542 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004543 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004544 temp = I915_READ(reg);
4545 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004546 TRANS_DP_SYNC_MASK |
4547 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004548 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004549 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004550
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004551 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004552 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004553 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004554 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004555
4556 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004557 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004558 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004559 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004560 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004561 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004562 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004563 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004564 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004565 break;
4566 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004567 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004568 }
4569
Chris Wilson5eddb702010-09-11 13:48:45 +01004570 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004571 }
4572
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004573 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004574}
4575
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004576static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004577{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004578 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004579 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004580 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004581
Daniel Vetterab9412b2013-05-03 11:49:46 +02004582 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004583
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004584 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004585
Paulo Zanoni0540e482012-10-31 18:12:40 -02004586 /* Set transcoder timing. */
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004587 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004588
Paulo Zanoni937bb612012-10-31 18:12:47 -02004589 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004590}
4591
Daniel Vettera1520312013-05-03 11:49:50 +02004592static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004593{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004594 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004595 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004596 u32 temp;
4597
4598 temp = I915_READ(dslreg);
4599 udelay(500);
4600 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004601 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004602 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004603 }
4604}
4605
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004606static int
4607skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004608 unsigned int scaler_user, int *scaler_id,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004609 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004610{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004611 struct intel_crtc_scaler_state *scaler_state =
4612 &crtc_state->scaler_state;
4613 struct intel_crtc *intel_crtc =
4614 to_intel_crtc(crtc_state->base.crtc);
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304615 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4616 const struct drm_display_mode *adjusted_mode =
4617 &crtc_state->base.adjusted_mode;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004618 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004619
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004620 /*
4621 * Src coordinates are already rotated by 270 degrees for
4622 * the 90/270 degree plane rotation cases (to match the
4623 * GTT mapping), hence no need to account for rotation here.
4624 */
4625 need_scaling = src_w != dst_w || src_h != dst_h;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004626
4627 /*
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304628 * Scaling/fitting not supported in IF-ID mode in GEN9+
4629 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4630 * Once NV12 is enabled, handle it here while allocating scaler
4631 * for NV12.
4632 */
4633 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4634 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4635 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4636 return -EINVAL;
4637 }
4638
4639 /*
Chandra Kondurua1b22782015-04-07 15:28:45 -07004640 * if plane is being disabled or scaler is no more required or force detach
4641 * - free scaler binded to this plane/crtc
4642 * - in order to do this, update crtc->scaler_usage
4643 *
4644 * Here scaler state in crtc_state is set free so that
4645 * scaler can be assigned to other user. Actual register
4646 * update to free the scaler is done in plane/panel-fit programming.
4647 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4648 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004649 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004650 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004651 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004652 scaler_state->scalers[*scaler_id].in_use = 0;
4653
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004654 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4655 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4656 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004657 scaler_state->scaler_users);
4658 *scaler_id = -1;
4659 }
4660 return 0;
4661 }
4662
4663 /* range checks */
4664 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4665 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4666
4667 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4668 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004669 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004670 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004671 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004672 return -EINVAL;
4673 }
4674
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004675 /* mark this plane as a scaler user in crtc_state */
4676 scaler_state->scaler_users |= (1 << scaler_user);
4677 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4678 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4679 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4680 scaler_state->scaler_users);
4681
4682 return 0;
4683}
4684
4685/**
4686 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4687 *
4688 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004689 *
4690 * Return
4691 * 0 - scaler_usage updated successfully
4692 * error - requested scaling cannot be supported or other error condition
4693 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004694int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004695{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004696 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004697
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004698 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004699 &state->scaler_state.scaler_id,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004700 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004701 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004702}
4703
4704/**
4705 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4706 *
4707 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004708 * @plane_state: atomic plane state to update
4709 *
4710 * Return
4711 * 0 - scaler_usage updated successfully
4712 * error - requested scaling cannot be supported or other error condition
4713 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004714static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4715 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004716{
4717
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004718 struct intel_plane *intel_plane =
4719 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004720 struct drm_framebuffer *fb = plane_state->base.fb;
4721 int ret;
4722
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004723 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004724
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004725 ret = skl_update_scaler(crtc_state, force_detach,
4726 drm_plane_index(&intel_plane->base),
4727 &plane_state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004728 drm_rect_width(&plane_state->base.src) >> 16,
4729 drm_rect_height(&plane_state->base.src) >> 16,
4730 drm_rect_width(&plane_state->base.dst),
4731 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004732
4733 if (ret || plane_state->scaler_id < 0)
4734 return ret;
4735
Chandra Kondurua1b22782015-04-07 15:28:45 -07004736 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004737 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004738 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4739 intel_plane->base.base.id,
4740 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004741 return -EINVAL;
4742 }
4743
4744 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004745 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004746 case DRM_FORMAT_RGB565:
4747 case DRM_FORMAT_XBGR8888:
4748 case DRM_FORMAT_XRGB8888:
4749 case DRM_FORMAT_ABGR8888:
4750 case DRM_FORMAT_ARGB8888:
4751 case DRM_FORMAT_XRGB2101010:
4752 case DRM_FORMAT_XBGR2101010:
4753 case DRM_FORMAT_YUYV:
4754 case DRM_FORMAT_YVYU:
4755 case DRM_FORMAT_UYVY:
4756 case DRM_FORMAT_VYUY:
4757 break;
4758 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004759 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4760 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004761 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004762 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004763 }
4764
Chandra Kondurua1b22782015-04-07 15:28:45 -07004765 return 0;
4766}
4767
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004768static void skylake_scaler_disable(struct intel_crtc *crtc)
4769{
4770 int i;
4771
4772 for (i = 0; i < crtc->num_scalers; i++)
4773 skl_detach_scaler(crtc, i);
4774}
4775
4776static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004777{
4778 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004779 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004780 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004781 struct intel_crtc_scaler_state *scaler_state =
4782 &crtc->config->scaler_state;
4783
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004784 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004785 int id;
4786
Ville Syrjäläc3f8ad52017-03-07 22:54:19 +02004787 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
Chandra Kondurua1b22782015-04-07 15:28:45 -07004788 return;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004789
4790 id = scaler_state->scaler_id;
4791 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4792 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4793 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4794 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004795 }
4796}
4797
Jesse Barnesb074cec2013-04-25 12:55:02 -07004798static void ironlake_pfit_enable(struct intel_crtc *crtc)
4799{
4800 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004801 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004802 int pipe = crtc->pipe;
4803
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004804 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004805 /* Force use of hard-coded filter coefficients
4806 * as some pre-programmed values are broken,
4807 * e.g. x201.
4808 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004809 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004810 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4811 PF_PIPE_SEL_IVB(pipe));
4812 else
4813 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004814 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4815 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004816 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004817}
4818
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004819void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004820{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004821 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004822 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004823
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004824 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004825 return;
4826
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004827 /*
4828 * We can only enable IPS after we enable a plane and wait for a vblank
4829 * This function is called from post_plane_update, which is run after
4830 * a vblank wait.
4831 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004832
Paulo Zanonid77e4532013-09-24 13:52:55 -03004833 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004834 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004835 mutex_lock(&dev_priv->rps.hw_lock);
4836 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4837 mutex_unlock(&dev_priv->rps.hw_lock);
4838 /* Quoting Art Runyan: "its not safe to expect any particular
4839 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004840 * mailbox." Moreover, the mailbox may return a bogus state,
4841 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004842 */
4843 } else {
4844 I915_WRITE(IPS_CTL, IPS_ENABLE);
4845 /* The bit only becomes 1 in the next vblank, so this wait here
4846 * is essentially intel_wait_for_vblank. If we don't have this
4847 * and don't wait for vblanks until the end of crtc_enable, then
4848 * the HW state readout code will complain that the expected
4849 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004850 if (intel_wait_for_register(dev_priv,
4851 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4852 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004853 DRM_ERROR("Timed out waiting for IPS enable\n");
4854 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004855}
4856
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004857void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004858{
4859 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004860 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004861
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004862 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004863 return;
4864
4865 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004866 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004867 mutex_lock(&dev_priv->rps.hw_lock);
4868 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4869 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004870 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004871 if (intel_wait_for_register(dev_priv,
4872 IPS_CTL, IPS_ENABLE, 0,
4873 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004874 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004875 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004876 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004877 POSTING_READ(IPS_CTL);
4878 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004879
4880 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004881 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004882}
4883
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004884static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004885{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004886 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004887 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004888
4889 mutex_lock(&dev->struct_mutex);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004890 (void) intel_overlay_switch_off(intel_crtc->overlay);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004891 mutex_unlock(&dev->struct_mutex);
4892 }
4893
4894 /* Let userspace switch the overlay on again. In most cases userspace
4895 * has to recompute where to put it anyway.
4896 */
4897}
4898
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004899/**
4900 * intel_post_enable_primary - Perform operations after enabling primary plane
4901 * @crtc: the CRTC whose primary plane was just enabled
4902 *
4903 * Performs potentially sleeping operations that must be done after the primary
4904 * plane is enabled, such as updating FBC and IPS. Note that this may be
4905 * called due to an explicit primary plane update, or due to an implicit
4906 * re-enable that is caused when a sprite plane is updated to no longer
4907 * completely hide the primary plane.
4908 */
4909static void
4910intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004911{
4912 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004913 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4915 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004916
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004917 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004918 * FIXME IPS should be fine as long as one plane is
4919 * enabled, but in practice it seems to have problems
4920 * when going from primary only to sprite only and vice
4921 * versa.
4922 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004923 hsw_enable_ips(intel_crtc);
4924
Daniel Vetterf99d7062014-06-19 16:01:59 +02004925 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004926 * Gen2 reports pipe underruns whenever all planes are disabled.
4927 * So don't enable underrun reporting before at least some planes
4928 * are enabled.
4929 * FIXME: Need to fix the logic to work when we turn off all planes
4930 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004931 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004932 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4934
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004935 /* Underruns don't always raise interrupts, so check manually. */
4936 intel_check_cpu_fifo_underruns(dev_priv);
4937 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004938}
4939
Ville Syrjälä2622a082016-03-09 19:07:26 +02004940/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004941static void
4942intel_pre_disable_primary(struct drm_crtc *crtc)
4943{
4944 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004945 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4947 int pipe = intel_crtc->pipe;
4948
4949 /*
4950 * Gen2 reports pipe underruns whenever all planes are disabled.
4951 * So diasble underrun reporting before all the planes get disabled.
4952 * FIXME: Need to fix the logic to work when we turn off all planes
4953 * but leave the pipe running.
4954 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004955 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004956 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4957
4958 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004959 * FIXME IPS should be fine as long as one plane is
4960 * enabled, but in practice it seems to have problems
4961 * when going from primary only to sprite only and vice
4962 * versa.
4963 */
4964 hsw_disable_ips(intel_crtc);
4965}
4966
4967/* FIXME get rid of this and use pre_plane_update */
4968static void
4969intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4970{
4971 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004972 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4974 int pipe = intel_crtc->pipe;
4975
4976 intel_pre_disable_primary(crtc);
4977
4978 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004979 * Vblank time updates from the shadow to live plane control register
4980 * are blocked if the memory self-refresh mode is active at that
4981 * moment. So to make sure the plane gets truly disabled, disable
4982 * first the self-refresh mode. The self-refresh enable bit in turn
4983 * will be checked/applied by the HW only at the next frame start
4984 * event which is after the vblank start event, so we need to have a
4985 * wait-for-vblank between disabling the plane and the pipe.
4986 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02004987 if (HAS_GMCH_DISPLAY(dev_priv) &&
4988 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004989 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004990}
4991
Daniel Vetter5a21b662016-05-24 17:13:53 +02004992static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4993{
4994 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4995 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4996 struct intel_crtc_state *pipe_config =
4997 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004998 struct drm_plane *primary = crtc->base.primary;
4999 struct drm_plane_state *old_pri_state =
5000 drm_atomic_get_existing_plane_state(old_state, primary);
5001
Chris Wilson5748b6a2016-08-04 16:32:38 +01005002 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005003
Daniel Vetter5a21b662016-05-24 17:13:53 +02005004 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005005 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005006
5007 if (old_pri_state) {
5008 struct intel_plane_state *primary_state =
5009 to_intel_plane_state(primary->state);
5010 struct intel_plane_state *old_primary_state =
5011 to_intel_plane_state(old_pri_state);
5012
5013 intel_fbc_post_update(crtc);
5014
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005015 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005016 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005017 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02005018 intel_post_enable_primary(&crtc->base);
5019 }
5020}
5021
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005022static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5023 struct intel_crtc_state *pipe_config)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005024{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005025 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005026 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005027 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005028 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5029 struct drm_plane *primary = crtc->base.primary;
5030 struct drm_plane_state *old_pri_state =
5031 drm_atomic_get_existing_plane_state(old_state, primary);
5032 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005033 struct intel_atomic_state *old_intel_state =
5034 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005035
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005036 if (old_pri_state) {
5037 struct intel_plane_state *primary_state =
5038 to_intel_plane_state(primary->state);
5039 struct intel_plane_state *old_primary_state =
5040 to_intel_plane_state(old_pri_state);
5041
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005042 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005043
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005044 if (old_primary_state->base.visible &&
5045 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005046 intel_pre_disable_primary(&crtc->base);
5047 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005048
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02005049 /*
5050 * Vblank time updates from the shadow to live plane control register
5051 * are blocked if the memory self-refresh mode is active at that
5052 * moment. So to make sure the plane gets truly disabled, disable
5053 * first the self-refresh mode. The self-refresh enable bit in turn
5054 * will be checked/applied by the HW only at the next frame start
5055 * event which is after the vblank start event, so we need to have a
5056 * wait-for-vblank between disabling the plane and the pipe.
5057 */
5058 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5059 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5060 intel_wait_for_vblank(dev_priv, crtc->pipe);
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005061
Matt Ropered4a6a72016-02-23 17:20:13 -08005062 /*
5063 * IVB workaround: must disable low power watermarks for at least
5064 * one frame before enabling scaling. LP watermarks can be re-enabled
5065 * when scaling is disabled.
5066 *
5067 * WaCxSRDisabledForSpriteScaling:ivb
5068 */
Ville Syrjäläddd2b792016-11-28 19:37:04 +02005069 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005070 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005071
5072 /*
5073 * If we're doing a modeset, we're done. No need to do any pre-vblank
5074 * watermark programming here.
5075 */
5076 if (needs_modeset(&pipe_config->base))
5077 return;
5078
5079 /*
5080 * For platforms that support atomic watermarks, program the
5081 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5082 * will be the intermediate values that are safe for both pre- and
5083 * post- vblank; when vblank happens, the 'active' values will be set
5084 * to the final 'target' values and we'll do this again to get the
5085 * optimal watermarks. For gen9+ platforms, the values we program here
5086 * will be the final target values which will get automatically latched
5087 * at vblank time; no further programming will be necessary.
5088 *
5089 * If a platform hasn't been transitioned to atomic watermarks yet,
5090 * we'll continue to update watermarks the old way, if flags tell
5091 * us to.
5092 */
5093 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005094 dev_priv->display.initial_watermarks(old_intel_state,
5095 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005096 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005097 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005098}
5099
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005100static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005101{
5102 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005104 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005105 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005106
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005107 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005108
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005109 drm_for_each_plane_mask(p, dev, plane_mask)
Ville Syrjälä282dbf92017-03-27 21:55:33 +03005110 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005111
Daniel Vetterf99d7062014-06-19 16:01:59 +02005112 /*
5113 * FIXME: Once we grow proper nuclear flip support out of this we need
5114 * to compute the mask of flip planes precisely. For the time being
5115 * consider this a flip to a NULL plane.
5116 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005117 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005118}
5119
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005120static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005121 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005122 struct drm_atomic_state *old_state)
5123{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005124 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005125 struct drm_connector *conn;
5126 int i;
5127
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005128 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005129 struct intel_encoder *encoder =
5130 to_intel_encoder(conn_state->best_encoder);
5131
5132 if (conn_state->crtc != crtc)
5133 continue;
5134
5135 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005136 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005137 }
5138}
5139
5140static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005141 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005142 struct drm_atomic_state *old_state)
5143{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005144 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005145 struct drm_connector *conn;
5146 int i;
5147
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005148 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005149 struct intel_encoder *encoder =
5150 to_intel_encoder(conn_state->best_encoder);
5151
5152 if (conn_state->crtc != crtc)
5153 continue;
5154
5155 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005156 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005157 }
5158}
5159
5160static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005161 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005162 struct drm_atomic_state *old_state)
5163{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005164 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005165 struct drm_connector *conn;
5166 int i;
5167
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005168 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005169 struct intel_encoder *encoder =
5170 to_intel_encoder(conn_state->best_encoder);
5171
5172 if (conn_state->crtc != crtc)
5173 continue;
5174
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005175 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005176 intel_opregion_notify_encoder(encoder, true);
5177 }
5178}
5179
5180static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005181 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005182 struct drm_atomic_state *old_state)
5183{
5184 struct drm_connector_state *old_conn_state;
5185 struct drm_connector *conn;
5186 int i;
5187
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005188 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005189 struct intel_encoder *encoder =
5190 to_intel_encoder(old_conn_state->best_encoder);
5191
5192 if (old_conn_state->crtc != crtc)
5193 continue;
5194
5195 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005196 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005197 }
5198}
5199
5200static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005201 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005202 struct drm_atomic_state *old_state)
5203{
5204 struct drm_connector_state *old_conn_state;
5205 struct drm_connector *conn;
5206 int i;
5207
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005208 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005209 struct intel_encoder *encoder =
5210 to_intel_encoder(old_conn_state->best_encoder);
5211
5212 if (old_conn_state->crtc != crtc)
5213 continue;
5214
5215 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005216 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005217 }
5218}
5219
5220static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005221 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005222 struct drm_atomic_state *old_state)
5223{
5224 struct drm_connector_state *old_conn_state;
5225 struct drm_connector *conn;
5226 int i;
5227
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005228 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005229 struct intel_encoder *encoder =
5230 to_intel_encoder(old_conn_state->best_encoder);
5231
5232 if (old_conn_state->crtc != crtc)
5233 continue;
5234
5235 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005236 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005237 }
5238}
5239
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005240static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5241 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005242{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005243 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005244 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005245 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5247 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005248 struct intel_atomic_state *old_intel_state =
5249 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005250
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005251 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005252 return;
5253
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005254 /*
5255 * Sometimes spurious CPU pipe underruns happen during FDI
5256 * training, at least with VGA+HDMI cloning. Suppress them.
5257 *
5258 * On ILK we get an occasional spurious CPU pipe underruns
5259 * between eDP port A enable and vdd enable. Also PCH port
5260 * enable seems to result in the occasional CPU pipe underrun.
5261 *
5262 * Spurious PCH underruns also occur during PCH enabling.
5263 */
5264 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5265 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005266 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005267 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5268
5269 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005270 intel_prepare_shared_dpll(intel_crtc);
5271
Ville Syrjälä37a56502016-06-22 21:57:04 +03005272 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305273 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005274
5275 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005276 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005277
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005278 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005279 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005280 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005281 }
5282
5283 ironlake_set_pipeconf(crtc);
5284
Jesse Barnesf67a5592011-01-05 10:31:48 -08005285 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005286
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005287 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005288
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005289 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005290 /* Note: FDI PLL enabling _must_ be done before we enable the
5291 * cpu pipes, hence this is separate from all the other fdi/pch
5292 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005293 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005294 } else {
5295 assert_fdi_tx_disabled(dev_priv, pipe);
5296 assert_fdi_rx_disabled(dev_priv, pipe);
5297 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005298
Jesse Barnesb074cec2013-04-25 12:55:02 -07005299 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005300
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005301 /*
5302 * On ILK+ LUT must be loaded before the pipe is running but with
5303 * clocks enabled
5304 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005305 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005306
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005307 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005308 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005309 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005310
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005311 if (intel_crtc->config->has_pch_encoder)
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02005312 ironlake_pch_enable(pipe_config);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005313
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005314 assert_vblank_disabled(crtc);
5315 drm_crtc_vblank_on(crtc);
5316
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005317 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005318
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005319 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005320 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005321
5322 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5323 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005324 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005325 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005326 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005327}
5328
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005329/* IPS only exists on ULT machines and is tied to pipe A. */
5330static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5331{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005332 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005333}
5334
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005335static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5336 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005337{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005338 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005339 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005341 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005342 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005343 struct intel_atomic_state *old_intel_state =
5344 to_intel_atomic_state(old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005345
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005346 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005347 return;
5348
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005349 if (intel_crtc->config->has_pch_encoder)
5350 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5351 false);
5352
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005353 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005354
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005355 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005356 intel_enable_shared_dpll(intel_crtc);
5357
Ville Syrjälä37a56502016-06-22 21:57:04 +03005358 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305359 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005360
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005361 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005362 intel_set_pipe_timings(intel_crtc);
5363
Jani Nikulabc58be62016-03-18 17:05:39 +02005364 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005365
Jani Nikula4d1de972016-03-18 17:05:42 +02005366 if (cpu_transcoder != TRANSCODER_EDP &&
5367 !transcoder_is_dsi(cpu_transcoder)) {
5368 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005369 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005370 }
5371
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005372 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005373 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005374 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005375 }
5376
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005377 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005378 haswell_set_pipeconf(crtc);
5379
Jani Nikula391bf042016-03-18 17:05:40 +02005380 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005381
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005382 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005383
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005384 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005385
Daniel Vetter6b698512015-11-28 11:05:39 +01005386 if (intel_crtc->config->has_pch_encoder)
5387 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5388 else
5389 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5390
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005391 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005392
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005393 if (intel_crtc->config->has_pch_encoder)
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02005394 dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
Imre Deak4fe94672014-06-25 22:01:49 +03005395
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005396 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005397 intel_ddi_enable_pipe_clock(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005398
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005399 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005400 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005401 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005402 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005403
5404 /*
5405 * On ILK+ LUT must be loaded before the pipe is running but with
5406 * clocks enabled
5407 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005408 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005409
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005410 intel_ddi_set_pipe_settings(pipe_config);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005411 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005412 intel_ddi_enable_transcoder_func(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005413
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005414 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005415 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005416
5417 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005418 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005419 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005420
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005421 if (intel_crtc->config->has_pch_encoder)
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02005422 lpt_pch_enable(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005423
Ville Syrjälä00370712016-11-14 19:44:06 +02005424 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005425 intel_ddi_set_vc_payload_alloc(pipe_config, true);
Dave Airlie0e32b392014-05-02 14:02:48 +10005426
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005427 assert_vblank_disabled(crtc);
5428 drm_crtc_vblank_on(crtc);
5429
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005430 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005431
Daniel Vetter6b698512015-11-28 11:05:39 +01005432 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005433 intel_wait_for_vblank(dev_priv, pipe);
5434 intel_wait_for_vblank(dev_priv, pipe);
Daniel Vetter6b698512015-11-28 11:05:39 +01005435 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005436 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5437 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005438 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005439
Paulo Zanonie4916942013-09-20 16:21:19 -03005440 /* If we change the relative order between pipe/planes enabling, we need
5441 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005442 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005443 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005444 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5445 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005446 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005447}
5448
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005449static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005450{
5451 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005452 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005453 int pipe = crtc->pipe;
5454
5455 /* To avoid upsetting the power well on haswell only disable the pfit if
5456 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005457 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005458 I915_WRITE(PF_CTL(pipe), 0);
5459 I915_WRITE(PF_WIN_POS(pipe), 0);
5460 I915_WRITE(PF_WIN_SZ(pipe), 0);
5461 }
5462}
5463
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005464static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5465 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005466{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005467 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005468 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005469 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5471 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005472
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005473 /*
5474 * Sometimes spurious CPU pipe underruns happen when the
5475 * pipe is already disabled, but FDI RX/TX is still enabled.
5476 * Happens at least with VGA+HDMI cloning. Suppress them.
5477 */
5478 if (intel_crtc->config->has_pch_encoder) {
5479 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005480 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005481 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005482
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005483 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005484
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005485 drm_crtc_vblank_off(crtc);
5486 assert_vblank_disabled(crtc);
5487
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005488 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005489
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005490 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005491
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005492 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005493 ironlake_fdi_disable(crtc);
5494
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005495 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005496
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005497 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005498 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005499
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005500 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005501 i915_reg_t reg;
5502 u32 temp;
5503
Daniel Vetterd925c592013-06-05 13:34:04 +02005504 /* disable TRANS_DP_CTL */
5505 reg = TRANS_DP_CTL(pipe);
5506 temp = I915_READ(reg);
5507 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5508 TRANS_DP_PORT_SEL_MASK);
5509 temp |= TRANS_DP_PORT_SEL_NONE;
5510 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005511
Daniel Vetterd925c592013-06-05 13:34:04 +02005512 /* disable DPLL_SEL */
5513 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005514 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005515 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005516 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005517
Daniel Vetterd925c592013-06-05 13:34:04 +02005518 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005519 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005520
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005521 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005522 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005523}
5524
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005525static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5526 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005527{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005528 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005529 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005531 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005532
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005533 if (intel_crtc->config->has_pch_encoder)
5534 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5535 false);
5536
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005537 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005538
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005539 drm_crtc_vblank_off(crtc);
5540 assert_vblank_disabled(crtc);
5541
Jani Nikula4d1de972016-03-18 17:05:42 +02005542 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005543 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005544 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005545
Ville Syrjälä00370712016-11-14 19:44:06 +02005546 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005547 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005548
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005549 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305550 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005551
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005552 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005553 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005554 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005555 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005556
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005557 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005558 intel_ddi_disable_pipe_clock(intel_crtc->config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005559
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005560 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005561
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005562 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005563 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5564 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005565}
5566
Jesse Barnes2dd24552013-04-25 12:55:01 -07005567static void i9xx_pfit_enable(struct intel_crtc *crtc)
5568{
5569 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005570 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005571 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005572
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005573 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005574 return;
5575
Daniel Vetterc0b03412013-05-28 12:05:54 +02005576 /*
5577 * The panel fitter should only be adjusted whilst the pipe is disabled,
5578 * according to register description and PRM.
5579 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005580 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5581 assert_pipe_disabled(dev_priv, crtc->pipe);
5582
Jesse Barnesb074cec2013-04-25 12:55:02 -07005583 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5584 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005585
5586 /* Border color in case we don't scale up to the full screen. Black by
5587 * default, change to something else for debugging. */
5588 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005589}
5590
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005591enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005592{
5593 switch (port) {
5594 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005595 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005596 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005597 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005598 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005599 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005600 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005601 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005602 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005603 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005604 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005605 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005606 return POWER_DOMAIN_PORT_OTHER;
5607 }
5608}
5609
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005610static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5611 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005612{
5613 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005614 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005615 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5617 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005618 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005619 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005620
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005621 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005622 return 0;
5623
Imre Deak77d22dc2014-03-05 16:20:52 +02005624 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5625 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005626 if (crtc_state->pch_pfit.enabled ||
5627 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005628 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005629
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005630 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5631 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5632
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005633 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005634 }
Imre Deak319be8a2014-03-04 19:22:57 +02005635
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005636 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5637 mask |= BIT(POWER_DOMAIN_AUDIO);
5638
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005639 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005640 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005641
Imre Deak77d22dc2014-03-05 16:20:52 +02005642 return mask;
5643}
5644
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005645static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005646modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5647 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005648{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005649 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5651 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005652 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005653
5654 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005655 intel_crtc->enabled_power_domains = new_domains =
5656 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005657
Daniel Vetter5a21b662016-05-24 17:13:53 +02005658 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005659
5660 for_each_power_domain(domain, domains)
5661 intel_display_power_get(dev_priv, domain);
5662
Daniel Vetter5a21b662016-05-24 17:13:53 +02005663 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005664}
5665
5666static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005667 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005668{
5669 enum intel_display_power_domain domain;
5670
5671 for_each_power_domain(domain, domains)
5672 intel_display_power_put(dev_priv, domain);
5673}
5674
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005675static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5676 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005677{
Ville Syrjäläff32c542017-03-02 19:14:57 +02005678 struct intel_atomic_state *old_intel_state =
5679 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005680 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005681 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005682 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005684 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005685
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005686 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005687 return;
5688
Ville Syrjälä37a56502016-06-22 21:57:04 +03005689 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305690 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005691
5692 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005693 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005694
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005695 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01005696 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005697
5698 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5699 I915_WRITE(CHV_CANVAS(pipe), 0);
5700 }
5701
Daniel Vetter5b18e572014-04-24 23:55:06 +02005702 i9xx_set_pipeconf(intel_crtc);
5703
Jesse Barnes89b667f2013-04-18 14:51:36 -07005704 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005705
Daniel Vettera72e4c92014-09-30 10:56:47 +02005706 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005707
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005708 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005709
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005710 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005711 chv_prepare_pll(intel_crtc, intel_crtc->config);
5712 chv_enable_pll(intel_crtc, intel_crtc->config);
5713 } else {
5714 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5715 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005716 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005717
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005718 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005719
Jesse Barnes2dd24552013-04-25 12:55:01 -07005720 i9xx_pfit_enable(intel_crtc);
5721
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005722 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005723
Ville Syrjäläff32c542017-03-02 19:14:57 +02005724 dev_priv->display.initial_watermarks(old_intel_state,
5725 pipe_config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005726 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005727
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005728 assert_vblank_disabled(crtc);
5729 drm_crtc_vblank_on(crtc);
5730
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005731 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005732}
5733
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005734static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5735{
5736 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005737 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005739 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5740 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005741}
5742
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005743static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5744 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005745{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005746 struct intel_atomic_state *old_intel_state =
5747 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005748 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005749 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005750 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005752 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005753
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005754 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005755 return;
5756
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005757 i9xx_set_pll_dividers(intel_crtc);
5758
Ville Syrjälä37a56502016-06-22 21:57:04 +03005759 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305760 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005761
5762 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005763 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005764
Daniel Vetter5b18e572014-04-24 23:55:06 +02005765 i9xx_set_pipeconf(intel_crtc);
5766
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005767 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005768
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005769 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005770 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005771
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005772 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005773
Daniel Vetterf6736a12013-06-05 13:34:30 +02005774 i9xx_enable_pll(intel_crtc);
5775
Jesse Barnes2dd24552013-04-25 12:55:01 -07005776 i9xx_pfit_enable(intel_crtc);
5777
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005778 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005779
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005780 if (dev_priv->display.initial_watermarks != NULL)
5781 dev_priv->display.initial_watermarks(old_intel_state,
5782 intel_crtc->config);
5783 else
5784 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005785 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005786
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005787 assert_vblank_disabled(crtc);
5788 drm_crtc_vblank_on(crtc);
5789
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005790 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005791}
5792
Daniel Vetter87476d62013-04-11 16:29:06 +02005793static void i9xx_pfit_disable(struct intel_crtc *crtc)
5794{
5795 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005796 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02005797
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005798 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005799 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005800
5801 assert_pipe_disabled(dev_priv, crtc->pipe);
5802
Daniel Vetter328d8e82013-05-08 10:36:31 +02005803 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5804 I915_READ(PFIT_CONTROL));
5805 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005806}
5807
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005808static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5809 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005810{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005811 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005812 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005813 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5815 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005816
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005817 /*
5818 * On gen2 planes are double buffered but the pipe isn't, so we must
5819 * wait for planes to fully turn off before disabling the pipe.
5820 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005821 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005822 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005823
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005824 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005825
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005826 drm_crtc_vblank_off(crtc);
5827 assert_vblank_disabled(crtc);
5828
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005829 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005830
Daniel Vetter87476d62013-04-11 16:29:06 +02005831 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005832
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005833 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005834
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005835 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005836 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005837 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005838 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005839 vlv_disable_pll(dev_priv, pipe);
5840 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005841 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005842 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005843
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005844 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03005845
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005846 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005847 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005848
5849 if (!dev_priv->display.initial_watermarks)
5850 intel_update_watermarks(intel_crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +03005851
5852 /* clock the pipe down to 640x480@60 to potentially save power */
5853 if (IS_I830(dev_priv))
5854 i830_enable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005855}
5856
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03005857static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
5858 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005859{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005860 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005862 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005863 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005864 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005865 struct drm_atomic_state *state;
5866 struct intel_crtc_state *crtc_state;
5867 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005868
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005869 if (!intel_crtc->active)
5870 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005871
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +01005872 if (crtc->primary->state->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02005873 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02005874
Ville Syrjälä2622a082016-03-09 19:07:26 +02005875 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01005876
5877 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +01005878 crtc->primary->state->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02005879 }
5880
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005881 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02005882 if (!state) {
5883 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5884 crtc->base.id, crtc->name);
5885 return;
5886 }
5887
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03005888 state->acquire_ctx = ctx;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005889
5890 /* Everything's already locked, -EDEADLK can't happen. */
5891 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5892 ret = drm_atomic_add_affected_connectors(state, crtc);
5893
5894 WARN_ON(IS_ERR(crtc_state) || ret);
5895
5896 dev_priv->display.crtc_disable(crtc_state, state);
5897
Chris Wilson08536952016-10-14 13:18:18 +01005898 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005899
Ville Syrjälä78108b72016-05-27 20:59:19 +03005900 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5901 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005902
5903 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5904 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07005905 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005906 crtc->enabled = false;
5907 crtc->state->connector_mask = 0;
5908 crtc->state->encoder_mask = 0;
5909
5910 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5911 encoder->base.crtc = NULL;
5912
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02005913 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005914 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02005915 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005916
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005917 domains = intel_crtc->enabled_power_domains;
5918 for_each_power_domain(domain, domains)
5919 intel_display_power_put(dev_priv, domain);
5920 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005921
5922 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5923 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005924}
5925
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005926/*
5927 * turn all crtc's off, but do not adjust state
5928 * This has to be paired with a call to intel_modeset_setup_hw_state.
5929 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005930int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005931{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005932 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005933 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005934 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005935
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005936 state = drm_atomic_helper_suspend(dev);
5937 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005938 if (ret)
5939 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005940 else
5941 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005942 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005943}
5944
Chris Wilsonea5b2132010-08-04 13:50:23 +01005945void intel_encoder_destroy(struct drm_encoder *encoder)
5946{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005947 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005948
Chris Wilsonea5b2132010-08-04 13:50:23 +01005949 drm_encoder_cleanup(encoder);
5950 kfree(intel_encoder);
5951}
5952
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005953/* Cross check the actual hw state with our own modeset state tracking (and it's
5954 * internal consistency). */
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005955static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
5956 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005957{
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005958 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005959
5960 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5961 connector->base.base.id,
5962 connector->base.name);
5963
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005964 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005965 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005966
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005967 I915_STATE_WARN(!crtc_state,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005968 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005969
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005970 if (!crtc_state)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005971 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005972
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005973 I915_STATE_WARN(!crtc_state->active,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005974 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005975
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005976 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005977 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005978
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005979 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005980 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10005981
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005982 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005983 "attached encoder crtc differs from connector crtc\n");
5984 } else {
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005985 I915_STATE_WARN(crtc_state && crtc_state->active,
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02005986 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02005987 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005988 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005989 }
5990}
5991
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005992int intel_connector_init(struct intel_connector *connector)
5993{
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02005994 struct intel_digital_connector_state *conn_state;
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005995
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02005996 /*
5997 * Allocate enough memory to hold intel_digital_connector_state,
5998 * This might be a few bytes too many, but for connectors that don't
5999 * need it we'll free the state and allocate a smaller one on the first
6000 * succesful commit anyway.
6001 */
6002 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6003 if (!conn_state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006004 return -ENOMEM;
6005
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006006 __drm_atomic_helper_connector_reset(&connector->base,
6007 &conn_state->base);
6008
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006009 return 0;
6010}
6011
6012struct intel_connector *intel_connector_alloc(void)
6013{
6014 struct intel_connector *connector;
6015
6016 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6017 if (!connector)
6018 return NULL;
6019
6020 if (intel_connector_init(connector) < 0) {
6021 kfree(connector);
6022 return NULL;
6023 }
6024
6025 return connector;
6026}
6027
Daniel Vetterf0947c32012-07-02 13:10:34 +02006028/* Simple connector->get_hw_state implementation for encoders that support only
6029 * one connector and no cloning and hence the encoder state determines the state
6030 * of the connector. */
6031bool intel_connector_get_hw_state(struct intel_connector *connector)
6032{
Daniel Vetter24929352012-07-02 20:28:59 +02006033 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006034 struct intel_encoder *encoder = connector->encoder;
6035
6036 return encoder->get_hw_state(encoder, &pipe);
6037}
6038
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006039static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006040{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006041 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6042 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006043
6044 return 0;
6045}
6046
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006047static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006048 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006049{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006050 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006051 struct drm_atomic_state *state = pipe_config->base.state;
6052 struct intel_crtc *other_crtc;
6053 struct intel_crtc_state *other_crtc_state;
6054
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006055 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6056 pipe_name(pipe), pipe_config->fdi_lanes);
6057 if (pipe_config->fdi_lanes > 4) {
6058 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6059 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006060 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006061 }
6062
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006063 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006064 if (pipe_config->fdi_lanes > 2) {
6065 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6066 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006067 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006068 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006069 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006070 }
6071 }
6072
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006073 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006074 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006075
6076 /* Ivybridge 3 pipe is really complicated */
6077 switch (pipe) {
6078 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006079 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006080 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006081 if (pipe_config->fdi_lanes <= 2)
6082 return 0;
6083
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006084 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006085 other_crtc_state =
6086 intel_atomic_get_crtc_state(state, other_crtc);
6087 if (IS_ERR(other_crtc_state))
6088 return PTR_ERR(other_crtc_state);
6089
6090 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006091 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6092 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006093 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006094 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006095 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006096 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006097 if (pipe_config->fdi_lanes > 2) {
6098 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6099 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006100 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006101 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006102
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006103 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006104 other_crtc_state =
6105 intel_atomic_get_crtc_state(state, other_crtc);
6106 if (IS_ERR(other_crtc_state))
6107 return PTR_ERR(other_crtc_state);
6108
6109 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006110 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006111 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006112 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006113 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006114 default:
6115 BUG();
6116 }
6117}
6118
Daniel Vettere29c22c2013-02-21 00:00:16 +01006119#define RETRY 1
6120static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006121 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006122{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006123 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006124 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006125 int lane, link_bw, fdi_dotclock, ret;
6126 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006127
Daniel Vettere29c22c2013-02-21 00:00:16 +01006128retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006129 /* FDI is a binary signal running at ~2.7GHz, encoding
6130 * each output octet as 10 bits. The actual frequency
6131 * is stored as a divider into a 100MHz clock, and the
6132 * mode pixel clock is stored in units of 1KHz.
6133 * Hence the bw of each lane in terms of the mode signal
6134 * is:
6135 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006136 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006137
Damien Lespiau241bfc32013-09-25 16:45:37 +01006138 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006139
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006140 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006141 pipe_config->pipe_bpp);
6142
6143 pipe_config->fdi_lanes = lane;
6144
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006145 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006146 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006147
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006148 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006149 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006150 pipe_config->pipe_bpp -= 2*3;
6151 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6152 pipe_config->pipe_bpp);
6153 needs_recompute = true;
6154 pipe_config->bw_constrained = true;
6155
6156 goto retry;
6157 }
6158
6159 if (needs_recompute)
6160 return RETRY;
6161
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006162 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006163}
6164
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006165static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6166 struct intel_crtc_state *pipe_config)
6167{
6168 if (pipe_config->pipe_bpp > 24)
6169 return false;
6170
6171 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006172 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006173 return true;
6174
6175 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006176 * We compare against max which means we must take
6177 * the increased cdclk requirement into account when
6178 * calculating the new cdclk.
6179 *
6180 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006181 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006182 return pipe_config->pixel_rate <=
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006183 dev_priv->max_cdclk_freq * 95 / 100;
6184}
6185
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006186static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006187 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006188{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006189 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006190 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006191
Jani Nikulad330a952014-01-21 11:24:25 +02006192 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006193 hsw_crtc_supports_ips(crtc) &&
6194 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006195}
6196
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006197static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6198{
6199 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6200
6201 /* GDG double wide on either pipe, otherwise pipe A only */
6202 return INTEL_INFO(dev_priv)->gen < 4 &&
6203 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6204}
6205
Ville Syrjäläceb99322017-01-20 20:22:05 +02006206static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6207{
6208 uint32_t pixel_rate;
6209
6210 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6211
6212 /*
6213 * We only use IF-ID interlacing. If we ever use
6214 * PF-ID we'll need to adjust the pixel_rate here.
6215 */
6216
6217 if (pipe_config->pch_pfit.enabled) {
6218 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6219 uint32_t pfit_size = pipe_config->pch_pfit.size;
6220
6221 pipe_w = pipe_config->pipe_src_w;
6222 pipe_h = pipe_config->pipe_src_h;
6223
6224 pfit_w = (pfit_size >> 16) & 0xFFFF;
6225 pfit_h = pfit_size & 0xFFFF;
6226 if (pipe_w < pfit_w)
6227 pipe_w = pfit_w;
6228 if (pipe_h < pfit_h)
6229 pipe_h = pfit_h;
6230
6231 if (WARN_ON(!pfit_w || !pfit_h))
6232 return pixel_rate;
6233
6234 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6235 pfit_w * pfit_h);
6236 }
6237
6238 return pixel_rate;
6239}
6240
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006241static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6242{
6243 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6244
6245 if (HAS_GMCH_DISPLAY(dev_priv))
6246 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6247 crtc_state->pixel_rate =
6248 crtc_state->base.adjusted_mode.crtc_clock;
6249 else
6250 crtc_state->pixel_rate =
6251 ilk_pipe_pixel_rate(crtc_state);
6252}
6253
Daniel Vettera43f6e02013-06-07 23:10:32 +02006254static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006255 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006256{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006257 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006258 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006259 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006260 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006261
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006262 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006263 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006264
6265 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006266 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006267 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006268 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006269 if (intel_crtc_supports_double_wide(crtc) &&
6270 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006271 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006272 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006273 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006274 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006275
Ville Syrjäläf3261152016-05-24 21:34:18 +03006276 if (adjusted_mode->crtc_clock > clock_limit) {
6277 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6278 adjusted_mode->crtc_clock, clock_limit,
6279 yesno(pipe_config->double_wide));
6280 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006281 }
Chris Wilson89749352010-09-12 18:25:19 +01006282
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006283 /*
6284 * Pipe horizontal size must be even in:
6285 * - DVO ganged mode
6286 * - LVDS dual channel mode
6287 * - Double wide pipe
6288 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006289 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006290 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6291 pipe_config->pipe_src_w &= ~1;
6292
Damien Lespiau8693a822013-05-03 18:48:11 +01006293 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6294 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006295 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006296 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006297 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006298 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006299
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006300 intel_crtc_compute_pixel_rate(pipe_config);
6301
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006302 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006303 hsw_compute_ips_config(crtc, pipe_config);
6304
Daniel Vetter877d48d2013-04-19 11:24:43 +02006305 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006306 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006307
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006308 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006309}
6310
Zhenyu Wang2c072452009-06-05 15:38:42 +08006311static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006312intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006313{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006314 while (*num > DATA_LINK_M_N_MASK ||
6315 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006316 *num >>= 1;
6317 *den >>= 1;
6318 }
6319}
6320
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006321static void compute_m_n(unsigned int m, unsigned int n,
6322 uint32_t *ret_m, uint32_t *ret_n)
6323{
Jani Nikula9a86cda2017-03-27 14:33:25 +03006324 /*
6325 * Reduce M/N as much as possible without loss in precision. Several DP
6326 * dongles in particular seem to be fussy about too large *link* M/N
6327 * values. The passed in values are more likely to have the least
6328 * significant bits zero than M after rounding below, so do this first.
6329 */
6330 while ((m & 1) == 0 && (n & 1) == 0) {
6331 m >>= 1;
6332 n >>= 1;
6333 }
6334
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006335 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6336 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6337 intel_reduce_m_n_ratio(ret_m, ret_n);
6338}
6339
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006340void
6341intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6342 int pixel_clock, int link_clock,
6343 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006344{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006345 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006346
6347 compute_m_n(bits_per_pixel * pixel_clock,
6348 link_clock * nlanes * 8,
6349 &m_n->gmch_m, &m_n->gmch_n);
6350
6351 compute_m_n(pixel_clock, link_clock,
6352 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006353}
6354
Chris Wilsona7615032011-01-12 17:04:08 +00006355static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6356{
Jani Nikulad330a952014-01-21 11:24:25 +02006357 if (i915.panel_use_ssc >= 0)
6358 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006359 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006360 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006361}
6362
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006363static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006364{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006365 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006366}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006367
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006368static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6369{
6370 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006371}
6372
Daniel Vetterf47709a2013-03-28 10:42:02 +01006373static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006374 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006375 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006376{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006377 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006378 u32 fp, fp2 = 0;
6379
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006380 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006381 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006382 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006383 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006384 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006385 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006386 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006387 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006388 }
6389
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006390 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006391
Daniel Vetterf47709a2013-03-28 10:42:02 +01006392 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006393 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006394 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006395 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006396 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006397 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006398 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006399 }
6400}
6401
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006402static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6403 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006404{
6405 u32 reg_val;
6406
6407 /*
6408 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6409 * and set it to a reasonable value instead.
6410 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006411 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006412 reg_val &= 0xffffff00;
6413 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006415
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006416 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Imre Deaked585702017-05-10 12:21:47 +03006417 reg_val &= 0x00ffffff;
6418 reg_val |= 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006419 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006420
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006421 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006422 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006423 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006424
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006425 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006426 reg_val &= 0x00ffffff;
6427 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006428 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006429}
6430
Daniel Vetterb5518422013-05-03 11:49:48 +02006431static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6432 struct intel_link_m_n *m_n)
6433{
6434 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006435 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006436 int pipe = crtc->pipe;
6437
Daniel Vettere3b95f12013-05-03 11:49:49 +02006438 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6439 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6440 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6441 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006442}
6443
6444static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006445 struct intel_link_m_n *m_n,
6446 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006447{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006448 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006449 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006450 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006451
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006452 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006453 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6454 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6455 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6456 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006457 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6458 * for gen < 8) and if DRRS is supported (to make sure the
6459 * registers are not unnecessarily accessed).
6460 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006461 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6462 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006463 I915_WRITE(PIPE_DATA_M2(transcoder),
6464 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6465 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6466 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6467 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6468 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006469 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006470 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6471 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6472 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6473 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006474 }
6475}
6476
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306477void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006478{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306479 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6480
6481 if (m_n == M1_N1) {
6482 dp_m_n = &crtc->config->dp_m_n;
6483 dp_m2_n2 = &crtc->config->dp_m2_n2;
6484 } else if (m_n == M2_N2) {
6485
6486 /*
6487 * M2_N2 registers are not supported. Hence m2_n2 divider value
6488 * needs to be programmed into M1_N1.
6489 */
6490 dp_m_n = &crtc->config->dp_m2_n2;
6491 } else {
6492 DRM_ERROR("Unsupported divider value\n");
6493 return;
6494 }
6495
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006496 if (crtc->config->has_pch_encoder)
6497 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006498 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306499 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006500}
6501
Daniel Vetter251ac862015-06-18 10:30:24 +02006502static void vlv_compute_dpll(struct intel_crtc *crtc,
6503 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006504{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006505 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006506 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006507 if (crtc->pipe != PIPE_A)
6508 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006509
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006510 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006511 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006512 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6513 DPLL_EXT_BUFFER_ENABLE_VLV;
6514
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006515 pipe_config->dpll_hw_state.dpll_md =
6516 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6517}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006518
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006519static void chv_compute_dpll(struct intel_crtc *crtc,
6520 struct intel_crtc_state *pipe_config)
6521{
6522 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006523 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006524 if (crtc->pipe != PIPE_A)
6525 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6526
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006527 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006528 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006529 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6530
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006531 pipe_config->dpll_hw_state.dpll_md =
6532 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006533}
6534
Ville Syrjäläd288f652014-10-28 13:20:22 +02006535static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006536 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006537{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006538 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006539 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006540 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006541 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006542 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006543 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006544
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006545 /* Enable Refclk */
6546 I915_WRITE(DPLL(pipe),
6547 pipe_config->dpll_hw_state.dpll &
6548 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6549
6550 /* No need to actually set up the DPLL with DSI */
6551 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6552 return;
6553
Ville Syrjäläa5805162015-05-26 20:42:30 +03006554 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006555
Ville Syrjäläd288f652014-10-28 13:20:22 +02006556 bestn = pipe_config->dpll.n;
6557 bestm1 = pipe_config->dpll.m1;
6558 bestm2 = pipe_config->dpll.m2;
6559 bestp1 = pipe_config->dpll.p1;
6560 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006561
Jesse Barnes89b667f2013-04-18 14:51:36 -07006562 /* See eDP HDMI DPIO driver vbios notes doc */
6563
6564 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006565 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006566 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006567
6568 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006569 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006570
6571 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006572 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006573 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006575
6576 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006577 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006578
6579 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006580 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6581 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6582 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006583 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006584
6585 /*
6586 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6587 * but we don't support that).
6588 * Note: don't use the DAC post divider as it seems unstable.
6589 */
6590 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006591 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006592
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006593 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006594 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006595
Jesse Barnes89b667f2013-04-18 14:51:36 -07006596 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006597 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006598 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6599 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006600 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006601 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006602 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006603 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006604 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006605
Ville Syrjälä37a56502016-06-22 21:57:04 +03006606 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006607 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006608 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006609 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006610 0x0df40000);
6611 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006612 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006613 0x0df70000);
6614 } else { /* HDMI or VGA */
6615 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006616 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006617 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006618 0x0df70000);
6619 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006620 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006621 0x0df40000);
6622 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006623
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006624 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006625 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03006626 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006627 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006628 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006629
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006630 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006631 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006632}
6633
Ville Syrjäläd288f652014-10-28 13:20:22 +02006634static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006635 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006636{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006637 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006638 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006639 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006640 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306641 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006642 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306643 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306644 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006645
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006646 /* Enable Refclk and SSC */
6647 I915_WRITE(DPLL(pipe),
6648 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6649
6650 /* No need to actually set up the DPLL with DSI */
6651 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6652 return;
6653
Ville Syrjäläd288f652014-10-28 13:20:22 +02006654 bestn = pipe_config->dpll.n;
6655 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6656 bestm1 = pipe_config->dpll.m1;
6657 bestm2 = pipe_config->dpll.m2 >> 22;
6658 bestp1 = pipe_config->dpll.p1;
6659 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306660 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306661 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306662 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006663
Ville Syrjäläa5805162015-05-26 20:42:30 +03006664 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006665
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006666 /* p1 and p2 divider */
6667 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6668 5 << DPIO_CHV_S1_DIV_SHIFT |
6669 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6670 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6671 1 << DPIO_CHV_K_DIV_SHIFT);
6672
6673 /* Feedback post-divider - m2 */
6674 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6675
6676 /* Feedback refclk divider - n and m1 */
6677 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6678 DPIO_CHV_M1_DIV_BY_2 |
6679 1 << DPIO_CHV_N_DIV_SHIFT);
6680
6681 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03006682 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006683
6684 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306685 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6686 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6687 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6688 if (bestm2_frac)
6689 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6690 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006691
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306692 /* Program digital lock detect threshold */
6693 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6694 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6695 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6696 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6697 if (!bestm2_frac)
6698 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6699 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6700
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006701 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306702 if (vco == 5400000) {
6703 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6704 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6705 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6706 tribuf_calcntr = 0x9;
6707 } else if (vco <= 6200000) {
6708 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6709 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6710 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6711 tribuf_calcntr = 0x9;
6712 } else if (vco <= 6480000) {
6713 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6714 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6715 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6716 tribuf_calcntr = 0x8;
6717 } else {
6718 /* Not supported. Apply the same limits as in the max case */
6719 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6720 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6721 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6722 tribuf_calcntr = 0;
6723 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006724 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6725
Ville Syrjälä968040b2015-03-11 22:52:08 +02006726 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306727 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6728 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6729 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6730
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006731 /* AFC Recal */
6732 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6733 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6734 DPIO_AFC_RECAL);
6735
Ville Syrjäläa5805162015-05-26 20:42:30 +03006736 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006737}
6738
Ville Syrjäläd288f652014-10-28 13:20:22 +02006739/**
6740 * vlv_force_pll_on - forcibly enable just the PLL
6741 * @dev_priv: i915 private structure
6742 * @pipe: pipe PLL to enable
6743 * @dpll: PLL configuration
6744 *
6745 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6746 * in cases where we need the PLL enabled even when @pipe is not going to
6747 * be enabled.
6748 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006749int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006750 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006751{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006752 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006753 struct intel_crtc_state *pipe_config;
6754
6755 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6756 if (!pipe_config)
6757 return -ENOMEM;
6758
6759 pipe_config->base.crtc = &crtc->base;
6760 pipe_config->pixel_multiplier = 1;
6761 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006762
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006763 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006764 chv_compute_dpll(crtc, pipe_config);
6765 chv_prepare_pll(crtc, pipe_config);
6766 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006767 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006768 vlv_compute_dpll(crtc, pipe_config);
6769 vlv_prepare_pll(crtc, pipe_config);
6770 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006771 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006772
6773 kfree(pipe_config);
6774
6775 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006776}
6777
6778/**
6779 * vlv_force_pll_off - forcibly disable just the PLL
6780 * @dev_priv: i915 private structure
6781 * @pipe: pipe PLL to disable
6782 *
6783 * Disable the PLL for @pipe. To be used in cases where we need
6784 * the PLL enabled even when @pipe is not going to be enabled.
6785 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006786void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006787{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006788 if (IS_CHERRYVIEW(dev_priv))
6789 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006790 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006791 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006792}
6793
Daniel Vetter251ac862015-06-18 10:30:24 +02006794static void i9xx_compute_dpll(struct intel_crtc *crtc,
6795 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006796 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006797{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006798 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006799 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006800 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006801
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006802 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306803
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006804 dpll = DPLL_VGA_MODE_DIS;
6805
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006806 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006807 dpll |= DPLLB_MODE_LVDS;
6808 else
6809 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006810
Jani Nikula73f67aa2016-12-07 22:48:09 +02006811 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6812 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006813 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006814 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006815 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006816
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03006817 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6818 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006819 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006820
Ville Syrjälä37a56502016-06-22 21:57:04 +03006821 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006822 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006823
6824 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006825 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006826 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6827 else {
6828 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006829 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006830 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6831 }
6832 switch (clock->p2) {
6833 case 5:
6834 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6835 break;
6836 case 7:
6837 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6838 break;
6839 case 10:
6840 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6841 break;
6842 case 14:
6843 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6844 break;
6845 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006846 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006847 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6848
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006849 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006850 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006851 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02006852 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006853 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6854 else
6855 dpll |= PLL_REF_INPUT_DREFCLK;
6856
6857 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006858 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006859
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006860 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006861 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006862 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006863 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006864 }
6865}
6866
Daniel Vetter251ac862015-06-18 10:30:24 +02006867static void i8xx_compute_dpll(struct intel_crtc *crtc,
6868 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006869 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006870{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006871 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006872 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006873 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006874 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006875
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006876 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306877
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006878 dpll = DPLL_VGA_MODE_DIS;
6879
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006880 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006881 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6882 } else {
6883 if (clock->p1 == 2)
6884 dpll |= PLL_P1_DIVIDE_BY_TWO;
6885 else
6886 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6887 if (clock->p2 == 4)
6888 dpll |= PLL_P2_DIVIDE_BY_4;
6889 }
6890
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006891 if (!IS_I830(dev_priv) &&
6892 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006893 dpll |= DPLL_DVO_2X_MODE;
6894
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006895 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02006896 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006897 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6898 else
6899 dpll |= PLL_REF_INPUT_DREFCLK;
6900
6901 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006902 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006903}
6904
Daniel Vetter8a654f32013-06-01 17:16:22 +02006905static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006906{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006907 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006908 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006909 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006910 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006911 uint32_t crtc_vtotal, crtc_vblank_end;
6912 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006913
6914 /* We need to be careful not to changed the adjusted mode, for otherwise
6915 * the hw state checker will get angry at the mismatch. */
6916 crtc_vtotal = adjusted_mode->crtc_vtotal;
6917 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006918
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006919 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006920 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006921 crtc_vtotal -= 1;
6922 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006923
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006924 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006925 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6926 else
6927 vsyncshift = adjusted_mode->crtc_hsync_start -
6928 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006929 if (vsyncshift < 0)
6930 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006931 }
6932
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006933 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006934 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006935
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006936 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006937 (adjusted_mode->crtc_hdisplay - 1) |
6938 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006939 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006940 (adjusted_mode->crtc_hblank_start - 1) |
6941 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006942 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006943 (adjusted_mode->crtc_hsync_start - 1) |
6944 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6945
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006946 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006947 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006948 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006949 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006950 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006951 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006952 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006953 (adjusted_mode->crtc_vsync_start - 1) |
6954 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6955
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006956 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6957 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6958 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6959 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01006960 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006961 (pipe == PIPE_B || pipe == PIPE_C))
6962 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6963
Jani Nikulabc58be62016-03-18 17:05:39 +02006964}
6965
6966static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6967{
6968 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006969 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02006970 enum pipe pipe = intel_crtc->pipe;
6971
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006972 /* pipesrc controls the size that is scaled from, which should
6973 * always be the user's requested size.
6974 */
6975 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006976 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6977 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006978}
6979
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006980static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006981 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006982{
6983 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006984 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006985 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6986 uint32_t tmp;
6987
6988 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006989 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6990 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006991 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006992 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6993 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006994 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006995 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6996 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006997
6998 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006999 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7000 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007001 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007002 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7003 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007004 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007005 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7006 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007007
7008 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007009 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7010 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7011 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007012 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007013}
7014
7015static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7016 struct intel_crtc_state *pipe_config)
7017{
7018 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007019 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007020 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007021
7022 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007023 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7024 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7025
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007026 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7027 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007028}
7029
Daniel Vetterf6a83282014-02-11 15:28:57 -08007030void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007031 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007032{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007033 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7034 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7035 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7036 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007037
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007038 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7039 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7040 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7041 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007042
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007043 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007044 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007045
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007046 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007047
7048 mode->hsync = drm_mode_hsync(mode);
7049 mode->vrefresh = drm_mode_vrefresh(mode);
7050 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007051}
7052
Daniel Vetter84b046f2013-02-19 18:48:54 +01007053static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7054{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007055 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007056 uint32_t pipeconf;
7057
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007058 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007059
Ville Syrjäläe56134b2017-06-01 17:36:19 +03007060 /* we keep both pipes enabled on 830 */
7061 if (IS_I830(dev_priv))
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007062 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007063
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007064 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007065 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007066
Daniel Vetterff9ce462013-04-24 14:57:17 +02007067 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007068 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7069 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007070 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007071 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007072 pipeconf |= PIPECONF_DITHER_EN |
7073 PIPECONF_DITHER_TYPE_SP;
7074
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007075 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007076 case 18:
7077 pipeconf |= PIPECONF_6BPC;
7078 break;
7079 case 24:
7080 pipeconf |= PIPECONF_8BPC;
7081 break;
7082 case 30:
7083 pipeconf |= PIPECONF_10BPC;
7084 break;
7085 default:
7086 /* Case prevented by intel_choose_pipe_bpp_dither. */
7087 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007088 }
7089 }
7090
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +00007091 if (HAS_PIPE_CXSR(dev_priv)) {
Daniel Vetter84b046f2013-02-19 18:48:54 +01007092 if (intel_crtc->lowfreq_avail) {
7093 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7094 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7095 } else {
7096 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007097 }
7098 }
7099
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007100 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007101 if (INTEL_GEN(dev_priv) < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007102 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007103 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7104 else
7105 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7106 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007107 pipeconf |= PIPECONF_PROGRESSIVE;
7108
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007109 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007110 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007111 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007112
Daniel Vetter84b046f2013-02-19 18:48:54 +01007113 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7114 POSTING_READ(PIPECONF(intel_crtc->pipe));
7115}
7116
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007117static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7118 struct intel_crtc_state *crtc_state)
7119{
7120 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007121 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007122 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007123 int refclk = 48000;
7124
7125 memset(&crtc_state->dpll_hw_state, 0,
7126 sizeof(crtc_state->dpll_hw_state));
7127
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007128 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007129 if (intel_panel_use_ssc(dev_priv)) {
7130 refclk = dev_priv->vbt.lvds_ssc_freq;
7131 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7132 }
7133
7134 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007135 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007136 limit = &intel_limits_i8xx_dvo;
7137 } else {
7138 limit = &intel_limits_i8xx_dac;
7139 }
7140
7141 if (!crtc_state->clock_set &&
7142 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7143 refclk, NULL, &crtc_state->dpll)) {
7144 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7145 return -EINVAL;
7146 }
7147
7148 i8xx_compute_dpll(crtc, crtc_state, NULL);
7149
7150 return 0;
7151}
7152
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007153static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7154 struct intel_crtc_state *crtc_state)
7155{
7156 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007157 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007158 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007159 int refclk = 96000;
7160
7161 memset(&crtc_state->dpll_hw_state, 0,
7162 sizeof(crtc_state->dpll_hw_state));
7163
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007164 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007165 if (intel_panel_use_ssc(dev_priv)) {
7166 refclk = dev_priv->vbt.lvds_ssc_freq;
7167 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7168 }
7169
7170 if (intel_is_dual_link_lvds(dev))
7171 limit = &intel_limits_g4x_dual_channel_lvds;
7172 else
7173 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007174 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7175 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007176 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007177 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007178 limit = &intel_limits_g4x_sdvo;
7179 } else {
7180 /* The option is for other outputs */
7181 limit = &intel_limits_i9xx_sdvo;
7182 }
7183
7184 if (!crtc_state->clock_set &&
7185 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7186 refclk, NULL, &crtc_state->dpll)) {
7187 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7188 return -EINVAL;
7189 }
7190
7191 i9xx_compute_dpll(crtc, crtc_state, NULL);
7192
7193 return 0;
7194}
7195
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007196static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7197 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007198{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007199 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007200 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007201 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007202 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007203
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007204 memset(&crtc_state->dpll_hw_state, 0,
7205 sizeof(crtc_state->dpll_hw_state));
7206
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007207 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007208 if (intel_panel_use_ssc(dev_priv)) {
7209 refclk = dev_priv->vbt.lvds_ssc_freq;
7210 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7211 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007212
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007213 limit = &intel_limits_pineview_lvds;
7214 } else {
7215 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007216 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007217
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007218 if (!crtc_state->clock_set &&
7219 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7220 refclk, NULL, &crtc_state->dpll)) {
7221 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7222 return -EINVAL;
7223 }
7224
7225 i9xx_compute_dpll(crtc, crtc_state, NULL);
7226
7227 return 0;
7228}
7229
7230static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7231 struct intel_crtc_state *crtc_state)
7232{
7233 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007234 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007235 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007236 int refclk = 96000;
7237
7238 memset(&crtc_state->dpll_hw_state, 0,
7239 sizeof(crtc_state->dpll_hw_state));
7240
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007241 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007242 if (intel_panel_use_ssc(dev_priv)) {
7243 refclk = dev_priv->vbt.lvds_ssc_freq;
7244 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007245 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007246
7247 limit = &intel_limits_i9xx_lvds;
7248 } else {
7249 limit = &intel_limits_i9xx_sdvo;
7250 }
7251
7252 if (!crtc_state->clock_set &&
7253 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7254 refclk, NULL, &crtc_state->dpll)) {
7255 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7256 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007257 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007258
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007259 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007260
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007261 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007262}
7263
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007264static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7265 struct intel_crtc_state *crtc_state)
7266{
7267 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007268 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007269
7270 memset(&crtc_state->dpll_hw_state, 0,
7271 sizeof(crtc_state->dpll_hw_state));
7272
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007273 if (!crtc_state->clock_set &&
7274 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7275 refclk, NULL, &crtc_state->dpll)) {
7276 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7277 return -EINVAL;
7278 }
7279
7280 chv_compute_dpll(crtc, crtc_state);
7281
7282 return 0;
7283}
7284
7285static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7286 struct intel_crtc_state *crtc_state)
7287{
7288 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007289 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007290
7291 memset(&crtc_state->dpll_hw_state, 0,
7292 sizeof(crtc_state->dpll_hw_state));
7293
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007294 if (!crtc_state->clock_set &&
7295 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7296 refclk, NULL, &crtc_state->dpll)) {
7297 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7298 return -EINVAL;
7299 }
7300
7301 vlv_compute_dpll(crtc, crtc_state);
7302
7303 return 0;
7304}
7305
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007306static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007307 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007308{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007309 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007310 uint32_t tmp;
7311
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007312 if (INTEL_GEN(dev_priv) <= 3 &&
7313 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007314 return;
7315
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007316 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007317 if (!(tmp & PFIT_ENABLE))
7318 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007319
Daniel Vetter06922822013-07-11 13:35:40 +02007320 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007321 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007322 if (crtc->pipe != PIPE_B)
7323 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007324 } else {
7325 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7326 return;
7327 }
7328
Daniel Vetter06922822013-07-11 13:35:40 +02007329 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007330 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007331}
7332
Jesse Barnesacbec812013-09-20 11:29:32 -07007333static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007334 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007335{
7336 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007337 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007338 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007339 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007340 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007341 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007342
Ville Syrjäläb5219732016-03-15 16:40:01 +02007343 /* In case of DSI, DPLL will not be used */
7344 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307345 return;
7346
Ville Syrjäläa5805162015-05-26 20:42:30 +03007347 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007348 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007349 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007350
7351 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7352 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7353 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7354 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7355 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7356
Imre Deakdccbea32015-06-22 23:35:51 +03007357 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007358}
7359
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007360static void
7361i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7362 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007363{
7364 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007365 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007366 u32 val, base, offset;
7367 int pipe = crtc->pipe, plane = crtc->plane;
7368 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007369 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007370 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007371 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007372
Damien Lespiau42a7b082015-02-05 19:35:13 +00007373 val = I915_READ(DSPCNTR(plane));
7374 if (!(val & DISPLAY_PLANE_ENABLE))
7375 return;
7376
Damien Lespiaud9806c92015-01-21 14:07:19 +00007377 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007378 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007379 DRM_DEBUG_KMS("failed to alloc fb\n");
7380 return;
7381 }
7382
Damien Lespiau1b842c82015-01-21 13:50:54 +00007383 fb = &intel_fb->base;
7384
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007385 fb->dev = dev;
7386
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007387 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007388 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007389 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007390 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007391 }
7392 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007393
7394 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007395 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007396 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007397
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007398 if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007399 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007400 offset = I915_READ(DSPTILEOFF(plane));
7401 else
7402 offset = I915_READ(DSPLINOFF(plane));
7403 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7404 } else {
7405 base = I915_READ(DSPADDR(plane));
7406 }
7407 plane_config->base = base;
7408
7409 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007410 fb->width = ((val >> 16) & 0xfff) + 1;
7411 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007412
7413 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007414 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007415
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02007416 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007417
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007418 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007419
Damien Lespiau2844a922015-01-20 12:51:48 +00007420 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7421 pipe_name(pipe), plane, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007422 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007423 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007424
Damien Lespiau2d140302015-02-05 17:22:18 +00007425 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007426}
7427
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007428static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007429 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007430{
7431 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007432 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007433 int pipe = pipe_config->cpu_transcoder;
7434 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007435 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007436 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007437 int refclk = 100000;
7438
Ville Syrjäläb5219732016-03-15 16:40:01 +02007439 /* In case of DSI, DPLL will not be used */
7440 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7441 return;
7442
Ville Syrjäläa5805162015-05-26 20:42:30 +03007443 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007444 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7445 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7446 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7447 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007448 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007449 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007450
7451 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007452 clock.m2 = (pll_dw0 & 0xff) << 22;
7453 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7454 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007455 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7456 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7457 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7458
Imre Deakdccbea32015-06-22 23:35:51 +03007459 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007460}
7461
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007462static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007463 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007464{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007465 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007466 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007467 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007468 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007469
Imre Deak17290502016-02-12 18:55:11 +02007470 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7471 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007472 return false;
7473
Daniel Vettere143a212013-07-04 12:01:15 +02007474 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007475 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007476
Imre Deak17290502016-02-12 18:55:11 +02007477 ret = false;
7478
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007479 tmp = I915_READ(PIPECONF(crtc->pipe));
7480 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007481 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007482
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007483 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7484 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007485 switch (tmp & PIPECONF_BPC_MASK) {
7486 case PIPECONF_6BPC:
7487 pipe_config->pipe_bpp = 18;
7488 break;
7489 case PIPECONF_8BPC:
7490 pipe_config->pipe_bpp = 24;
7491 break;
7492 case PIPECONF_10BPC:
7493 pipe_config->pipe_bpp = 30;
7494 break;
7495 default:
7496 break;
7497 }
7498 }
7499
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007500 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007501 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007502 pipe_config->limited_color_range = true;
7503
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007504 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007505 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7506
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007507 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007508 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007509
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007510 i9xx_get_pfit_config(crtc, pipe_config);
7511
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007512 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007513 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007514 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007515 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7516 else
7517 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007518 pipe_config->pixel_multiplier =
7519 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7520 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007521 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007522 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007523 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007524 tmp = I915_READ(DPLL(crtc->pipe));
7525 pipe_config->pixel_multiplier =
7526 ((tmp & SDVO_MULTIPLIER_MASK)
7527 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7528 } else {
7529 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7530 * port and will be fixed up in the encoder->get_config
7531 * function. */
7532 pipe_config->pixel_multiplier = 1;
7533 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007534 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007535 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007536 /*
7537 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7538 * on 830. Filter it out here so that we don't
7539 * report errors due to that.
7540 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007541 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007542 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7543
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007544 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7545 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007546 } else {
7547 /* Mask out read-only status bits. */
7548 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7549 DPLL_PORTC_READY_MASK |
7550 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007551 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007552
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007553 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007554 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007555 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007556 vlv_crtc_clock_get(crtc, pipe_config);
7557 else
7558 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007559
Ville Syrjälä0f646142015-08-26 19:39:18 +03007560 /*
7561 * Normally the dotclock is filled in by the encoder .get_config()
7562 * but in case the pipe is enabled w/o any ports we need a sane
7563 * default.
7564 */
7565 pipe_config->base.adjusted_mode.crtc_clock =
7566 pipe_config->port_clock / pipe_config->pixel_multiplier;
7567
Imre Deak17290502016-02-12 18:55:11 +02007568 ret = true;
7569
7570out:
7571 intel_display_power_put(dev_priv, power_domain);
7572
7573 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007574}
7575
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007576static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007577{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007578 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007579 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007580 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007581 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007582 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007583 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007584 bool has_ck505 = false;
7585 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007586 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007587
7588 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007589 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007590 switch (encoder->type) {
7591 case INTEL_OUTPUT_LVDS:
7592 has_panel = true;
7593 has_lvds = true;
7594 break;
7595 case INTEL_OUTPUT_EDP:
7596 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007597 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007598 has_cpu_edp = true;
7599 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007600 default:
7601 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007602 }
7603 }
7604
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007605 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007606 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007607 can_ssc = has_ck505;
7608 } else {
7609 has_ck505 = false;
7610 can_ssc = true;
7611 }
7612
Lyude1c1a24d2016-06-14 11:04:09 -04007613 /* Check if any DPLLs are using the SSC source */
7614 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7615 u32 temp = I915_READ(PCH_DPLL(i));
7616
7617 if (!(temp & DPLL_VCO_ENABLE))
7618 continue;
7619
7620 if ((temp & PLL_REF_INPUT_MASK) ==
7621 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7622 using_ssc_source = true;
7623 break;
7624 }
7625 }
7626
7627 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7628 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007629
7630 /* Ironlake: try to setup display ref clock before DPLL
7631 * enabling. This is only under driver's control after
7632 * PCH B stepping, previous chipset stepping should be
7633 * ignoring this setting.
7634 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007635 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007636
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007637 /* As we must carefully and slowly disable/enable each source in turn,
7638 * compute the final state we want first and check if we need to
7639 * make any changes at all.
7640 */
7641 final = val;
7642 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007643 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007644 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007645 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007646 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7647
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007648 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007649 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007650 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007651
Keith Packard199e5d72011-09-22 12:01:57 -07007652 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007653 final |= DREF_SSC_SOURCE_ENABLE;
7654
7655 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7656 final |= DREF_SSC1_ENABLE;
7657
7658 if (has_cpu_edp) {
7659 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7660 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7661 else
7662 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7663 } else
7664 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04007665 } else if (using_ssc_source) {
7666 final |= DREF_SSC_SOURCE_ENABLE;
7667 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007668 }
7669
7670 if (final == val)
7671 return;
7672
7673 /* Always enable nonspread source */
7674 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7675
7676 if (has_ck505)
7677 val |= DREF_NONSPREAD_CK505_ENABLE;
7678 else
7679 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7680
7681 if (has_panel) {
7682 val &= ~DREF_SSC_SOURCE_MASK;
7683 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007684
Keith Packard199e5d72011-09-22 12:01:57 -07007685 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007686 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007687 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007688 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007689 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007690 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007691
7692 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007693 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007694 POSTING_READ(PCH_DREF_CONTROL);
7695 udelay(200);
7696
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007697 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007698
7699 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007700 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007701 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007702 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007703 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007704 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007705 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007706 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007707 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007708
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007709 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007710 POSTING_READ(PCH_DREF_CONTROL);
7711 udelay(200);
7712 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04007713 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07007714
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007715 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007716
7717 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007718 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007719
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007720 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007721 POSTING_READ(PCH_DREF_CONTROL);
7722 udelay(200);
7723
Lyude1c1a24d2016-06-14 11:04:09 -04007724 if (!using_ssc_source) {
7725 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07007726
Lyude1c1a24d2016-06-14 11:04:09 -04007727 /* Turn off the SSC source */
7728 val &= ~DREF_SSC_SOURCE_MASK;
7729 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007730
Lyude1c1a24d2016-06-14 11:04:09 -04007731 /* Turn off SSC1 */
7732 val &= ~DREF_SSC1_ENABLE;
7733
7734 I915_WRITE(PCH_DREF_CONTROL, val);
7735 POSTING_READ(PCH_DREF_CONTROL);
7736 udelay(200);
7737 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07007738 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007739
7740 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007741}
7742
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007743static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007744{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007745 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007746
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007747 tmp = I915_READ(SOUTH_CHICKEN2);
7748 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7749 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007750
Imre Deakcf3598c2016-06-28 13:37:31 +03007751 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7752 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007753 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007754
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007755 tmp = I915_READ(SOUTH_CHICKEN2);
7756 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7757 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007758
Imre Deakcf3598c2016-06-28 13:37:31 +03007759 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7760 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007761 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007762}
7763
7764/* WaMPhyProgramming:hsw */
7765static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7766{
7767 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007768
7769 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7770 tmp &= ~(0xFF << 24);
7771 tmp |= (0x12 << 24);
7772 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7773
Paulo Zanonidde86e22012-12-01 12:04:25 -02007774 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7775 tmp |= (1 << 11);
7776 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7777
7778 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7779 tmp |= (1 << 11);
7780 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7781
Paulo Zanonidde86e22012-12-01 12:04:25 -02007782 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7783 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7784 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7785
7786 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7787 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7788 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7789
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007790 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7791 tmp &= ~(7 << 13);
7792 tmp |= (5 << 13);
7793 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007794
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007795 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7796 tmp &= ~(7 << 13);
7797 tmp |= (5 << 13);
7798 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007799
7800 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7801 tmp &= ~0xFF;
7802 tmp |= 0x1C;
7803 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7804
7805 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7806 tmp &= ~0xFF;
7807 tmp |= 0x1C;
7808 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7809
7810 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7811 tmp &= ~(0xFF << 16);
7812 tmp |= (0x1C << 16);
7813 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7814
7815 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7816 tmp &= ~(0xFF << 16);
7817 tmp |= (0x1C << 16);
7818 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7819
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007820 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7821 tmp |= (1 << 27);
7822 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007823
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007824 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7825 tmp |= (1 << 27);
7826 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007827
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007828 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7829 tmp &= ~(0xF << 28);
7830 tmp |= (4 << 28);
7831 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007832
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007833 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7834 tmp &= ~(0xF << 28);
7835 tmp |= (4 << 28);
7836 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007837}
7838
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007839/* Implements 3 different sequences from BSpec chapter "Display iCLK
7840 * Programming" based on the parameters passed:
7841 * - Sequence to enable CLKOUT_DP
7842 * - Sequence to enable CLKOUT_DP without spread
7843 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7844 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007845static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7846 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007847{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007848 uint32_t reg, tmp;
7849
7850 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7851 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007852 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7853 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007854 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007855
Ville Syrjäläa5805162015-05-26 20:42:30 +03007856 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007857
7858 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7859 tmp &= ~SBI_SSCCTL_DISABLE;
7860 tmp |= SBI_SSCCTL_PATHALT;
7861 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7862
7863 udelay(24);
7864
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007865 if (with_spread) {
7866 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7867 tmp &= ~SBI_SSCCTL_PATHALT;
7868 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007869
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007870 if (with_fdi) {
7871 lpt_reset_fdi_mphy(dev_priv);
7872 lpt_program_fdi_mphy(dev_priv);
7873 }
7874 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007875
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007876 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007877 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7878 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7879 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007880
Ville Syrjäläa5805162015-05-26 20:42:30 +03007881 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007882}
7883
Paulo Zanoni47701c32013-07-23 11:19:25 -03007884/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007885static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03007886{
Paulo Zanoni47701c32013-07-23 11:19:25 -03007887 uint32_t reg, tmp;
7888
Ville Syrjäläa5805162015-05-26 20:42:30 +03007889 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03007890
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007891 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03007892 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7893 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7894 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7895
7896 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7897 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7898 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7899 tmp |= SBI_SSCCTL_PATHALT;
7900 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7901 udelay(32);
7902 }
7903 tmp |= SBI_SSCCTL_DISABLE;
7904 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7905 }
7906
Ville Syrjäläa5805162015-05-26 20:42:30 +03007907 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03007908}
7909
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007910#define BEND_IDX(steps) ((50 + (steps)) / 5)
7911
7912static const uint16_t sscdivintphase[] = {
7913 [BEND_IDX( 50)] = 0x3B23,
7914 [BEND_IDX( 45)] = 0x3B23,
7915 [BEND_IDX( 40)] = 0x3C23,
7916 [BEND_IDX( 35)] = 0x3C23,
7917 [BEND_IDX( 30)] = 0x3D23,
7918 [BEND_IDX( 25)] = 0x3D23,
7919 [BEND_IDX( 20)] = 0x3E23,
7920 [BEND_IDX( 15)] = 0x3E23,
7921 [BEND_IDX( 10)] = 0x3F23,
7922 [BEND_IDX( 5)] = 0x3F23,
7923 [BEND_IDX( 0)] = 0x0025,
7924 [BEND_IDX( -5)] = 0x0025,
7925 [BEND_IDX(-10)] = 0x0125,
7926 [BEND_IDX(-15)] = 0x0125,
7927 [BEND_IDX(-20)] = 0x0225,
7928 [BEND_IDX(-25)] = 0x0225,
7929 [BEND_IDX(-30)] = 0x0325,
7930 [BEND_IDX(-35)] = 0x0325,
7931 [BEND_IDX(-40)] = 0x0425,
7932 [BEND_IDX(-45)] = 0x0425,
7933 [BEND_IDX(-50)] = 0x0525,
7934};
7935
7936/*
7937 * Bend CLKOUT_DP
7938 * steps -50 to 50 inclusive, in steps of 5
7939 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7940 * change in clock period = -(steps / 10) * 5.787 ps
7941 */
7942static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7943{
7944 uint32_t tmp;
7945 int idx = BEND_IDX(steps);
7946
7947 if (WARN_ON(steps % 5 != 0))
7948 return;
7949
7950 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7951 return;
7952
7953 mutex_lock(&dev_priv->sb_lock);
7954
7955 if (steps % 10 != 0)
7956 tmp = 0xAAAAAAAB;
7957 else
7958 tmp = 0x00000000;
7959 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7960
7961 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7962 tmp &= 0xffff0000;
7963 tmp |= sscdivintphase[idx];
7964 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7965
7966 mutex_unlock(&dev_priv->sb_lock);
7967}
7968
7969#undef BEND_IDX
7970
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007971static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007972{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007973 struct intel_encoder *encoder;
7974 bool has_vga = false;
7975
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007976 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007977 switch (encoder->type) {
7978 case INTEL_OUTPUT_ANALOG:
7979 has_vga = true;
7980 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007981 default:
7982 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007983 }
7984 }
7985
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007986 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007987 lpt_bend_clkout_dp(dev_priv, 0);
7988 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007989 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007990 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007991 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007992}
7993
Paulo Zanonidde86e22012-12-01 12:04:25 -02007994/*
7995 * Initialize reference clocks when the driver loads
7996 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007997void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007998{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007999 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008000 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008001 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008002 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008003}
8004
Daniel Vetter6ff93602013-04-19 11:24:36 +02008005static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008006{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008007 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8009 int pipe = intel_crtc->pipe;
8010 uint32_t val;
8011
Daniel Vetter78114072013-06-13 00:54:57 +02008012 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008014 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008015 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008016 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008017 break;
8018 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008019 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008020 break;
8021 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008022 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008023 break;
8024 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008025 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008026 break;
8027 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008028 /* Case prevented by intel_choose_pipe_bpp_dither. */
8029 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008030 }
8031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008032 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008033 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008035 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008036 val |= PIPECONF_INTERLACED_ILK;
8037 else
8038 val |= PIPECONF_PROGRESSIVE;
8039
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008040 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008041 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008042
Paulo Zanonic8203562012-09-12 10:06:29 -03008043 I915_WRITE(PIPECONF(pipe), val);
8044 POSTING_READ(PIPECONF(pipe));
8045}
8046
Daniel Vetter6ff93602013-04-19 11:24:36 +02008047static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008048{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008049 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008051 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008052 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008053
Jani Nikula391bf042016-03-18 17:05:40 +02008054 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008055 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8056
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008057 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008058 val |= PIPECONF_INTERLACED_ILK;
8059 else
8060 val |= PIPECONF_PROGRESSIVE;
8061
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008062 I915_WRITE(PIPECONF(cpu_transcoder), val);
8063 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008064}
8065
Jani Nikula391bf042016-03-18 17:05:40 +02008066static void haswell_set_pipemisc(struct drm_crtc *crtc)
8067{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008068 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8070
8071 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8072 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008074 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008075 case 18:
8076 val |= PIPEMISC_DITHER_6_BPC;
8077 break;
8078 case 24:
8079 val |= PIPEMISC_DITHER_8_BPC;
8080 break;
8081 case 30:
8082 val |= PIPEMISC_DITHER_10_BPC;
8083 break;
8084 case 36:
8085 val |= PIPEMISC_DITHER_12_BPC;
8086 break;
8087 default:
8088 /* Case prevented by pipe_config_set_bpp. */
8089 BUG();
8090 }
8091
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008092 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008093 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8094
Jani Nikula391bf042016-03-18 17:05:40 +02008095 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008096 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008097}
8098
Paulo Zanonid4b19312012-11-29 11:29:32 -02008099int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8100{
8101 /*
8102 * Account for spread spectrum to avoid
8103 * oversubscribing the link. Max center spread
8104 * is 2.5%; use 5% for safety's sake.
8105 */
8106 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008107 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008108}
8109
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008110static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008111{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008112 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008113}
8114
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008115static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8116 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008117 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008118{
8119 struct drm_crtc *crtc = &intel_crtc->base;
8120 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008121 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008122 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008123 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008124
Chris Wilsonc1858122010-12-03 21:35:48 +00008125 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008126 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008127 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008128 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008129 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008130 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008131 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008132 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008133 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008134
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008135 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008136
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008137 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8138 fp |= FP_CB_TUNE;
8139
8140 if (reduced_clock) {
8141 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8142
8143 if (reduced_clock->m < factor * reduced_clock->n)
8144 fp2 |= FP_CB_TUNE;
8145 } else {
8146 fp2 = fp;
8147 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008148
Chris Wilson5eddb702010-09-11 13:48:45 +01008149 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008150
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008151 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008152 dpll |= DPLLB_MODE_LVDS;
8153 else
8154 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008155
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008156 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008157 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008158
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008159 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8160 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008161 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008162
Ville Syrjälä37a56502016-06-22 21:57:04 +03008163 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008164 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008165
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008166 /*
8167 * The high speed IO clock is only really required for
8168 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8169 * possible to share the DPLL between CRT and HDMI. Enabling
8170 * the clock needlessly does no real harm, except use up a
8171 * bit of power potentially.
8172 *
8173 * We'll limit this to IVB with 3 pipes, since it has only two
8174 * DPLLs and so DPLL sharing is the only way to get three pipes
8175 * driving PCH ports at the same time. On SNB we could do this,
8176 * and potentially avoid enabling the second DPLL, but it's not
8177 * clear if it''s a win or loss power wise. No point in doing
8178 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8179 */
8180 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8181 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8182 dpll |= DPLL_SDVO_HIGH_SPEED;
8183
Eric Anholta07d6782011-03-30 13:01:08 -07008184 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008185 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008186 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008187 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008188
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008189 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008190 case 5:
8191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8192 break;
8193 case 7:
8194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8195 break;
8196 case 10:
8197 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8198 break;
8199 case 14:
8200 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8201 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008202 }
8203
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008204 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8205 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008206 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008207 else
8208 dpll |= PLL_REF_INPUT_DREFCLK;
8209
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008210 dpll |= DPLL_VCO_ENABLE;
8211
8212 crtc_state->dpll_hw_state.dpll = dpll;
8213 crtc_state->dpll_hw_state.fp0 = fp;
8214 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008215}
8216
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008217static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8218 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008219{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008220 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008221 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008222 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008223 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008224
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008225 memset(&crtc_state->dpll_hw_state, 0,
8226 sizeof(crtc_state->dpll_hw_state));
8227
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008228 crtc->lowfreq_avail = false;
8229
8230 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8231 if (!crtc_state->has_pch_encoder)
8232 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008233
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008234 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008235 if (intel_panel_use_ssc(dev_priv)) {
8236 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8237 dev_priv->vbt.lvds_ssc_freq);
8238 refclk = dev_priv->vbt.lvds_ssc_freq;
8239 }
8240
8241 if (intel_is_dual_link_lvds(dev)) {
8242 if (refclk == 100000)
8243 limit = &intel_limits_ironlake_dual_lvds_100m;
8244 else
8245 limit = &intel_limits_ironlake_dual_lvds;
8246 } else {
8247 if (refclk == 100000)
8248 limit = &intel_limits_ironlake_single_lvds_100m;
8249 else
8250 limit = &intel_limits_ironlake_single_lvds;
8251 }
8252 } else {
8253 limit = &intel_limits_ironlake_dac;
8254 }
8255
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008256 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008257 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8258 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008259 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8260 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008261 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008262
Gustavo A. R. Silvacbaa3312017-05-15 16:56:05 -05008263 ironlake_compute_dpll(crtc, crtc_state, NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008264
Gustavo A. R. Silvaefd38b62017-05-15 17:00:28 -05008265 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008266 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8267 pipe_name(crtc->pipe));
8268 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008269 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008270
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008271 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008272}
8273
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008274static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8275 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008276{
8277 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008278 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008279 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008280
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008281 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8282 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8283 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8284 & ~TU_SIZE_MASK;
8285 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8286 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8287 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8288}
8289
8290static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8291 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008292 struct intel_link_m_n *m_n,
8293 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008294{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008295 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008296 enum pipe pipe = crtc->pipe;
8297
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008298 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008299 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8300 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8301 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8302 & ~TU_SIZE_MASK;
8303 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8304 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8305 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008306 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8307 * gen < 8) and if DRRS is supported (to make sure the
8308 * registers are not unnecessarily read).
8309 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008310 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008311 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008312 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8313 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8314 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8315 & ~TU_SIZE_MASK;
8316 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8317 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8318 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8319 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008320 } else {
8321 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8322 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8323 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8324 & ~TU_SIZE_MASK;
8325 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8326 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8327 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8328 }
8329}
8330
8331void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008332 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008333{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008334 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008335 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8336 else
8337 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008338 &pipe_config->dp_m_n,
8339 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008340}
8341
Daniel Vetter72419202013-04-04 13:28:53 +02008342static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008343 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008344{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008345 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008346 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008347}
8348
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008349static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008350 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008351{
8352 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008353 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008354 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8355 uint32_t ps_ctrl = 0;
8356 int id = -1;
8357 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008358
Chandra Kondurua1b22782015-04-07 15:28:45 -07008359 /* find scaler attached to this pipe */
8360 for (i = 0; i < crtc->num_scalers; i++) {
8361 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8362 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8363 id = i;
8364 pipe_config->pch_pfit.enabled = true;
8365 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8366 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8367 break;
8368 }
8369 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008370
Chandra Kondurua1b22782015-04-07 15:28:45 -07008371 scaler_state->scaler_id = id;
8372 if (id >= 0) {
8373 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8374 } else {
8375 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008376 }
8377}
8378
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008379static void
8380skylake_get_initial_plane_config(struct intel_crtc *crtc,
8381 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008382{
8383 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008384 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00008385 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008386 int pipe = crtc->pipe;
8387 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008388 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008389 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008390 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008391
Damien Lespiaud9806c92015-01-21 14:07:19 +00008392 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008393 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008394 DRM_DEBUG_KMS("failed to alloc fb\n");
8395 return;
8396 }
8397
Damien Lespiau1b842c82015-01-21 13:50:54 +00008398 fb = &intel_fb->base;
8399
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008400 fb->dev = dev;
8401
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008402 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008403 if (!(val & PLANE_CTL_ENABLE))
8404 goto error;
8405
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008406 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8407 fourcc = skl_format_to_fourcc(pixel_format,
8408 val & PLANE_CTL_ORDER_RGBX,
8409 val & PLANE_CTL_ALPHA_MASK);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008410 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008411
Damien Lespiau40f46282015-02-27 11:15:21 +00008412 tiling = val & PLANE_CTL_TILED_MASK;
8413 switch (tiling) {
8414 case PLANE_CTL_TILED_LINEAR:
Ben Widawsky2f075562017-03-24 14:29:48 -07008415 fb->modifier = DRM_FORMAT_MOD_LINEAR;
Damien Lespiau40f46282015-02-27 11:15:21 +00008416 break;
8417 case PLANE_CTL_TILED_X:
8418 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008419 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008420 break;
8421 case PLANE_CTL_TILED_Y:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008422 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008423 break;
8424 case PLANE_CTL_TILED_YF:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008425 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008426 break;
8427 default:
8428 MISSING_CASE(tiling);
8429 goto error;
8430 }
8431
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008432 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8433 plane_config->base = base;
8434
8435 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8436
8437 val = I915_READ(PLANE_SIZE(pipe, 0));
8438 fb->height = ((val >> 16) & 0xfff) + 1;
8439 fb->width = ((val >> 0) & 0x1fff) + 1;
8440
8441 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008442 stride_mult = intel_fb_stride_alignment(fb, 0);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008443 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8444
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008445 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008446
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008447 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008448
8449 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8450 pipe_name(pipe), fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008451 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008452 plane_config->size);
8453
Damien Lespiau2d140302015-02-05 17:22:18 +00008454 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008455 return;
8456
8457error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008458 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008459}
8460
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008461static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008462 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008463{
8464 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008465 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008466 uint32_t tmp;
8467
8468 tmp = I915_READ(PF_CTL(crtc->pipe));
8469
8470 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008471 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008472 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8473 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008474
8475 /* We currently do not free assignements of panel fitters on
8476 * ivb/hsw (since we don't use the higher upscaling modes which
8477 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008478 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008479 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8480 PF_PIPE_SEL_IVB(crtc->pipe));
8481 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008482 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008483}
8484
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008485static void
8486ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8487 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008488{
8489 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008490 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008491 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008492 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008493 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008494 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008495 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008496 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008497
Damien Lespiau42a7b082015-02-05 19:35:13 +00008498 val = I915_READ(DSPCNTR(pipe));
8499 if (!(val & DISPLAY_PLANE_ENABLE))
8500 return;
8501
Damien Lespiaud9806c92015-01-21 14:07:19 +00008502 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008503 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008504 DRM_DEBUG_KMS("failed to alloc fb\n");
8505 return;
8506 }
8507
Damien Lespiau1b842c82015-01-21 13:50:54 +00008508 fb = &intel_fb->base;
8509
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008510 fb->dev = dev;
8511
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008512 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00008513 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008514 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008515 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00008516 }
8517 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008518
8519 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008520 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008521 fb->format = drm_format_info(fourcc);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008522
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008523 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01008524 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008525 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008526 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008527 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008528 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008529 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008530 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008531 }
8532 plane_config->base = base;
8533
8534 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008535 fb->width = ((val >> 16) & 0xfff) + 1;
8536 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008537
8538 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008539 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008540
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008541 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008542
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008543 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008544
Damien Lespiau2844a922015-01-20 12:51:48 +00008545 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8546 pipe_name(pipe), fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008547 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00008548 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008549
Damien Lespiau2d140302015-02-05 17:22:18 +00008550 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008551}
8552
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008553static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008554 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008555{
8556 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008557 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008558 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008559 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008560 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008561
Imre Deak17290502016-02-12 18:55:11 +02008562 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8563 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008564 return false;
8565
Daniel Vettere143a212013-07-04 12:01:15 +02008566 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008567 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008568
Imre Deak17290502016-02-12 18:55:11 +02008569 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008570 tmp = I915_READ(PIPECONF(crtc->pipe));
8571 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008572 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008573
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008574 switch (tmp & PIPECONF_BPC_MASK) {
8575 case PIPECONF_6BPC:
8576 pipe_config->pipe_bpp = 18;
8577 break;
8578 case PIPECONF_8BPC:
8579 pipe_config->pipe_bpp = 24;
8580 break;
8581 case PIPECONF_10BPC:
8582 pipe_config->pipe_bpp = 30;
8583 break;
8584 case PIPECONF_12BPC:
8585 pipe_config->pipe_bpp = 36;
8586 break;
8587 default:
8588 break;
8589 }
8590
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008591 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8592 pipe_config->limited_color_range = true;
8593
Daniel Vetterab9412b2013-05-03 11:49:46 +02008594 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008595 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008596 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008597
Daniel Vetter88adfff2013-03-28 10:42:01 +01008598 pipe_config->has_pch_encoder = true;
8599
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008600 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8601 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8602 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008603
8604 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008605
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008606 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008607 /*
8608 * The pipe->pch transcoder and pch transcoder->pll
8609 * mapping is fixed.
8610 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008611 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008612 } else {
8613 tmp = I915_READ(PCH_DPLL_SEL);
8614 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008615 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008616 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008617 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008618 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008619
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008620 pipe_config->shared_dpll =
8621 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8622 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008623
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02008624 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8625 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008626
8627 tmp = pipe_config->dpll_hw_state.dpll;
8628 pipe_config->pixel_multiplier =
8629 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8630 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008631
8632 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008633 } else {
8634 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008635 }
8636
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008637 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008638 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008639
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008640 ironlake_get_pfit_config(crtc, pipe_config);
8641
Imre Deak17290502016-02-12 18:55:11 +02008642 ret = true;
8643
8644out:
8645 intel_display_power_put(dev_priv, power_domain);
8646
8647 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008648}
8649
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008650static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8651{
Chris Wilson91c8a322016-07-05 10:40:23 +01008652 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008653 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008654
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008655 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008656 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008657 pipe_name(crtc->pipe));
8658
Rob Clarke2c719b2014-12-15 13:56:32 -05008659 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8660 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03008661 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8662 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03008663 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008664 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008665 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008666 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05008667 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008668 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008669 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008670 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008671 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008672 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008673 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008674
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008675 /*
8676 * In theory we can still leave IRQs enabled, as long as only the HPD
8677 * interrupts remain enabled. We used to check for that, but since it's
8678 * gen-specific and since we only disable LCPLL after we fully disable
8679 * the interrupts, the check below should be enough.
8680 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008681 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008682}
8683
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008684static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8685{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008686 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008687 return I915_READ(D_COMP_HSW);
8688 else
8689 return I915_READ(D_COMP_BDW);
8690}
8691
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008692static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8693{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008694 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008695 mutex_lock(&dev_priv->rps.hw_lock);
8696 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8697 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01008698 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008699 mutex_unlock(&dev_priv->rps.hw_lock);
8700 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008701 I915_WRITE(D_COMP_BDW, val);
8702 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008703 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008704}
8705
8706/*
8707 * This function implements pieces of two sequences from BSpec:
8708 * - Sequence for display software to disable LCPLL
8709 * - Sequence for display software to allow package C8+
8710 * The steps implemented here are just the steps that actually touch the LCPLL
8711 * register. Callers should take care of disabling all the display engine
8712 * functions, doing the mode unset, fixing interrupts, etc.
8713 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008714static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8715 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008716{
8717 uint32_t val;
8718
8719 assert_can_disable_lcpll(dev_priv);
8720
8721 val = I915_READ(LCPLL_CTL);
8722
8723 if (switch_to_fclk) {
8724 val |= LCPLL_CD_SOURCE_FCLK;
8725 I915_WRITE(LCPLL_CTL, val);
8726
Imre Deakf53dd632016-06-28 13:37:32 +03008727 if (wait_for_us(I915_READ(LCPLL_CTL) &
8728 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008729 DRM_ERROR("Switching to FCLK failed\n");
8730
8731 val = I915_READ(LCPLL_CTL);
8732 }
8733
8734 val |= LCPLL_PLL_DISABLE;
8735 I915_WRITE(LCPLL_CTL, val);
8736 POSTING_READ(LCPLL_CTL);
8737
Chris Wilson24d84412016-06-30 15:33:07 +01008738 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008739 DRM_ERROR("LCPLL still locked\n");
8740
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008741 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008742 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008743 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008744 ndelay(100);
8745
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008746 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8747 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008748 DRM_ERROR("D_COMP RCOMP still in progress\n");
8749
8750 if (allow_power_down) {
8751 val = I915_READ(LCPLL_CTL);
8752 val |= LCPLL_POWER_DOWN_ALLOW;
8753 I915_WRITE(LCPLL_CTL, val);
8754 POSTING_READ(LCPLL_CTL);
8755 }
8756}
8757
8758/*
8759 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8760 * source.
8761 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008762static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008763{
8764 uint32_t val;
8765
8766 val = I915_READ(LCPLL_CTL);
8767
8768 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8769 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8770 return;
8771
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008772 /*
8773 * Make sure we're not on PC8 state before disabling PC8, otherwise
8774 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008775 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008776 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008777
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008778 if (val & LCPLL_POWER_DOWN_ALLOW) {
8779 val &= ~LCPLL_POWER_DOWN_ALLOW;
8780 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008781 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008782 }
8783
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008784 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008785 val |= D_COMP_COMP_FORCE;
8786 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008787 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008788
8789 val = I915_READ(LCPLL_CTL);
8790 val &= ~LCPLL_PLL_DISABLE;
8791 I915_WRITE(LCPLL_CTL, val);
8792
Chris Wilson93220c02016-06-30 15:33:08 +01008793 if (intel_wait_for_register(dev_priv,
8794 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8795 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008796 DRM_ERROR("LCPLL not locked yet\n");
8797
8798 if (val & LCPLL_CD_SOURCE_FCLK) {
8799 val = I915_READ(LCPLL_CTL);
8800 val &= ~LCPLL_CD_SOURCE_FCLK;
8801 I915_WRITE(LCPLL_CTL, val);
8802
Imre Deakf53dd632016-06-28 13:37:32 +03008803 if (wait_for_us((I915_READ(LCPLL_CTL) &
8804 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008805 DRM_ERROR("Switching back to LCPLL failed\n");
8806 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008807
Mika Kuoppala59bad942015-01-16 11:34:40 +02008808 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjälä4c75b942016-10-31 22:37:12 +02008809 intel_update_cdclk(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008810}
8811
Paulo Zanoni765dab672014-03-07 20:08:18 -03008812/*
8813 * Package states C8 and deeper are really deep PC states that can only be
8814 * reached when all the devices on the system allow it, so even if the graphics
8815 * device allows PC8+, it doesn't mean the system will actually get to these
8816 * states. Our driver only allows PC8+ when going into runtime PM.
8817 *
8818 * The requirements for PC8+ are that all the outputs are disabled, the power
8819 * well is disabled and most interrupts are disabled, and these are also
8820 * requirements for runtime PM. When these conditions are met, we manually do
8821 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8822 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8823 * hang the machine.
8824 *
8825 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8826 * the state of some registers, so when we come back from PC8+ we need to
8827 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8828 * need to take care of the registers kept by RC6. Notice that this happens even
8829 * if we don't put the device in PCI D3 state (which is what currently happens
8830 * because of the runtime PM support).
8831 *
8832 * For more, read "Display Sequences for Package C8" on the hardware
8833 * documentation.
8834 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008835void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008836{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008837 uint32_t val;
8838
Paulo Zanonic67a4702013-08-19 13:18:09 -03008839 DRM_DEBUG_KMS("Enabling package C8+\n");
8840
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008841 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03008842 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8843 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8844 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8845 }
8846
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008847 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008848 hsw_disable_lcpll(dev_priv, true, true);
8849}
8850
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008851void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008852{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008853 uint32_t val;
8854
Paulo Zanonic67a4702013-08-19 13:18:09 -03008855 DRM_DEBUG_KMS("Disabling package C8+\n");
8856
8857 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008858 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008859
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008860 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03008861 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8862 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8863 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8864 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03008865}
8866
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008867static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8868 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008869{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03008870 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Paulo Zanoni44a126b2017-03-22 15:58:45 -03008871 struct intel_encoder *encoder =
8872 intel_ddi_get_crtc_new_encoder(crtc_state);
8873
8874 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
8875 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8876 pipe_name(crtc->pipe));
Mika Kaholaaf3997b2016-02-05 13:29:28 +02008877 return -EINVAL;
Paulo Zanoni44a126b2017-03-22 15:58:45 -03008878 }
Mika Kaholaaf3997b2016-02-05 13:29:28 +02008879 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03008880
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008881 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008882
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008883 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008884}
8885
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07008886static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
8887 enum port port,
8888 struct intel_crtc_state *pipe_config)
8889{
8890 enum intel_dpll_id id;
8891 u32 temp;
8892
8893 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
8894 id = temp >> (port * 2);
8895
8896 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
8897 return;
8898
8899 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
8900}
8901
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308902static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8903 enum port port,
8904 struct intel_crtc_state *pipe_config)
8905{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008906 enum intel_dpll_id id;
8907
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308908 switch (port) {
8909 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +02008910 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308911 break;
8912 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +02008913 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308914 break;
8915 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +02008916 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308917 break;
8918 default:
8919 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008920 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308921 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008922
8923 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308924}
8925
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008926static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8927 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008928 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008929{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008930 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02008931 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008932
8933 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008934 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008935
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008936 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008937 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008938
8939 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008940}
8941
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008942static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8943 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008944 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008945{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008946 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008947 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008948
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008949 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008950 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008951 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008952 break;
8953 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008954 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008955 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01008956 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008957 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02008958 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02008959 case PORT_CLK_SEL_LCPLL_810:
8960 id = DPLL_ID_LCPLL_810;
8961 break;
8962 case PORT_CLK_SEL_LCPLL_1350:
8963 id = DPLL_ID_LCPLL_1350;
8964 break;
8965 case PORT_CLK_SEL_LCPLL_2700:
8966 id = DPLL_ID_LCPLL_2700;
8967 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008968 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008969 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008970 /* fall through */
8971 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008972 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008973 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008974
8975 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008976}
8977
Jani Nikulacf304292016-03-18 17:05:41 +02008978static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8979 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02008980 u64 *power_domain_mask)
Jani Nikulacf304292016-03-18 17:05:41 +02008981{
8982 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008983 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02008984 enum intel_display_power_domain power_domain;
8985 u32 tmp;
8986
Imre Deakd9a7bc62016-05-12 16:18:50 +03008987 /*
8988 * The pipe->transcoder mapping is fixed with the exception of the eDP
8989 * transcoder handled below.
8990 */
Jani Nikulacf304292016-03-18 17:05:41 +02008991 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8992
8993 /*
8994 * XXX: Do intel_display_power_get_if_enabled before reading this (for
8995 * consistency and less surprising code; it's in always on power).
8996 */
8997 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8998 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8999 enum pipe trans_edp_pipe;
9000 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9001 default:
9002 WARN(1, "unknown pipe linked to edp transcoder\n");
9003 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9004 case TRANS_DDI_EDP_INPUT_A_ON:
9005 trans_edp_pipe = PIPE_A;
9006 break;
9007 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9008 trans_edp_pipe = PIPE_B;
9009 break;
9010 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9011 trans_edp_pipe = PIPE_C;
9012 break;
9013 }
9014
9015 if (trans_edp_pipe == crtc->pipe)
9016 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9017 }
9018
9019 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9020 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9021 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009022 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikulacf304292016-03-18 17:05:41 +02009023
9024 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9025
9026 return tmp & PIPECONF_ENABLE;
9027}
9028
Jani Nikula4d1de972016-03-18 17:05:42 +02009029static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9030 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009031 u64 *power_domain_mask)
Jani Nikula4d1de972016-03-18 17:05:42 +02009032{
9033 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009034 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +02009035 enum intel_display_power_domain power_domain;
9036 enum port port;
9037 enum transcoder cpu_transcoder;
9038 u32 tmp;
9039
Jani Nikula4d1de972016-03-18 17:05:42 +02009040 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9041 if (port == PORT_A)
9042 cpu_transcoder = TRANSCODER_DSI_A;
9043 else
9044 cpu_transcoder = TRANSCODER_DSI_C;
9045
9046 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9047 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9048 continue;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009049 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikula4d1de972016-03-18 17:05:42 +02009050
Imre Deakdb18b6a2016-03-24 12:41:40 +02009051 /*
9052 * The PLL needs to be enabled with a valid divider
9053 * configuration, otherwise accessing DSI registers will hang
9054 * the machine. See BSpec North Display Engine
9055 * registers/MIPI[BXT]. We can break out here early, since we
9056 * need the same DSI PLL to be enabled for both DSI ports.
9057 */
9058 if (!intel_dsi_pll_is_enabled(dev_priv))
9059 break;
9060
Jani Nikula4d1de972016-03-18 17:05:42 +02009061 /* XXX: this works for video mode only */
9062 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9063 if (!(tmp & DPI_ENABLE))
9064 continue;
9065
9066 tmp = I915_READ(MIPI_CTRL(port));
9067 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9068 continue;
9069
9070 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +02009071 break;
9072 }
9073
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009074 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +02009075}
9076
Daniel Vetter26804af2014-06-25 22:01:55 +03009077static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009078 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009079{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009080 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009081 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009082 enum port port;
9083 uint32_t tmp;
9084
9085 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9086
9087 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9088
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009089 if (IS_CANNONLAKE(dev_priv))
9090 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9091 else if (IS_GEN9_BC(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009092 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009093 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309094 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009095 else
9096 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009097
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009098 pll = pipe_config->shared_dpll;
9099 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009100 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9101 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009102 }
9103
Daniel Vetter26804af2014-06-25 22:01:55 +03009104 /*
9105 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9106 * DDI E. So just check whether this pipe is wired to DDI E and whether
9107 * the PCH transcoder is on.
9108 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009109 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +00009110 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009111 pipe_config->has_pch_encoder = true;
9112
9113 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9114 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9115 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9116
9117 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9118 }
9119}
9120
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009121static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009122 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009123{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009124 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02009125 enum intel_display_power_domain power_domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009126 u64 power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009127 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009128
Imre Deak17290502016-02-12 18:55:11 +02009129 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9130 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009131 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009132 power_domain_mask = BIT_ULL(power_domain);
Imre Deak17290502016-02-12 18:55:11 +02009133
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009134 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009135
Jani Nikulacf304292016-03-18 17:05:41 +02009136 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009137
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009138 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009139 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9140 WARN_ON(active);
9141 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +02009142 }
9143
Jani Nikulacf304292016-03-18 17:05:41 +02009144 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009145 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009146
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009147 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +02009148 haswell_get_ddi_port_state(crtc, pipe_config);
9149 intel_get_pipe_timings(crtc, pipe_config);
9150 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009151
Jani Nikulabc58be62016-03-18 17:05:39 +02009152 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009153
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009154 pipe_config->gamma_mode =
9155 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9156
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009157 if (INTEL_GEN(dev_priv) >= 9) {
Nabendu Maiti1c74eea2016-11-29 11:23:14 +05309158 intel_crtc_init_scalers(crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009159
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009160 pipe_config->scaler_state.scaler_id = -1;
9161 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9162 }
9163
Imre Deak17290502016-02-12 18:55:11 +02009164 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9165 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009166 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009167 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009168 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009169 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009170 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009171 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009172
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009173 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -08009174 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9175 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009176
Jani Nikula4d1de972016-03-18 17:05:42 +02009177 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9178 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009179 pipe_config->pixel_multiplier =
9180 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9181 } else {
9182 pipe_config->pixel_multiplier = 1;
9183 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009184
Imre Deak17290502016-02-12 18:55:11 +02009185out:
9186 for_each_power_domain(power_domain, power_domain_mask)
9187 intel_display_power_put(dev_priv, power_domain);
9188
Jani Nikulacf304292016-03-18 17:05:41 +02009189 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009190}
9191
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009192static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009193{
9194 struct drm_i915_private *dev_priv =
9195 to_i915(plane_state->base.plane->dev);
9196 const struct drm_framebuffer *fb = plane_state->base.fb;
9197 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9198 u32 base;
9199
9200 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9201 base = obj->phys_handle->busaddr;
9202 else
9203 base = intel_plane_ggtt_offset(plane_state);
9204
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009205 base += plane_state->main.offset;
9206
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009207 /* ILK+ do this automagically */
9208 if (HAS_GMCH_DISPLAY(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009209 plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009210 base += (plane_state->base.crtc_h *
9211 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9212
9213 return base;
9214}
9215
Ville Syrjäläed270222017-03-27 21:55:36 +03009216static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9217{
9218 int x = plane_state->base.crtc_x;
9219 int y = plane_state->base.crtc_y;
9220 u32 pos = 0;
9221
9222 if (x < 0) {
9223 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9224 x = -x;
9225 }
9226 pos |= x << CURSOR_X_SHIFT;
9227
9228 if (y < 0) {
9229 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9230 y = -y;
9231 }
9232 pos |= y << CURSOR_Y_SHIFT;
9233
9234 return pos;
9235}
9236
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009237static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9238{
9239 const struct drm_mode_config *config =
9240 &plane_state->base.plane->dev->mode_config;
9241 int width = plane_state->base.crtc_w;
9242 int height = plane_state->base.crtc_h;
9243
9244 return width > 0 && width <= config->cursor_width &&
9245 height > 0 && height <= config->cursor_height;
9246}
9247
Ville Syrjälä659056f2017-03-27 21:55:39 +03009248static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9249 struct intel_plane_state *plane_state)
9250{
9251 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009252 int src_x, src_y;
9253 u32 offset;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009254 int ret;
9255
9256 ret = drm_plane_helper_check_state(&plane_state->base,
9257 &plane_state->clip,
9258 DRM_PLANE_HELPER_NO_SCALING,
9259 DRM_PLANE_HELPER_NO_SCALING,
9260 true, true);
9261 if (ret)
9262 return ret;
9263
9264 if (!fb)
9265 return 0;
9266
9267 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9268 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9269 return -EINVAL;
9270 }
9271
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009272 src_x = plane_state->base.src_x >> 16;
9273 src_y = plane_state->base.src_y >> 16;
9274
9275 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9276 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9277
9278 if (src_x != 0 || src_y != 0) {
9279 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9280 return -EINVAL;
9281 }
9282
9283 plane_state->main.offset = offset;
9284
Ville Syrjälä659056f2017-03-27 21:55:39 +03009285 return 0;
9286}
9287
Ville Syrjälä292889e2017-03-17 23:18:01 +02009288static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9289 const struct intel_plane_state *plane_state)
9290{
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009291 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009292
Ville Syrjälä292889e2017-03-17 23:18:01 +02009293 return CURSOR_ENABLE |
9294 CURSOR_GAMMA_ENABLE |
9295 CURSOR_FORMAT_ARGB |
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009296 CURSOR_STRIDE(fb->pitches[0]);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009297}
9298
Ville Syrjälä659056f2017-03-27 21:55:39 +03009299static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9300{
Ville Syrjälä659056f2017-03-27 21:55:39 +03009301 int width = plane_state->base.crtc_w;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009302
9303 /*
9304 * 845g/865g are only limited by the width of their cursors,
9305 * the height is arbitrary up to the precision of the register.
9306 */
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009307 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009308}
9309
9310static int i845_check_cursor(struct intel_plane *plane,
9311 struct intel_crtc_state *crtc_state,
9312 struct intel_plane_state *plane_state)
9313{
9314 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009315 int ret;
9316
9317 ret = intel_check_cursor(crtc_state, plane_state);
9318 if (ret)
9319 return ret;
9320
9321 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009322 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009323 return 0;
9324
9325 /* Check for which cursor types we support */
9326 if (!i845_cursor_size_ok(plane_state)) {
9327 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9328 plane_state->base.crtc_w,
9329 plane_state->base.crtc_h);
9330 return -EINVAL;
9331 }
9332
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009333 switch (fb->pitches[0]) {
Chris Wilson560b85b2010-08-07 11:01:38 +01009334 case 256:
9335 case 512:
9336 case 1024:
9337 case 2048:
Ville Syrjälädc41c152014-08-13 11:57:05 +03009338 break;
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009339 default:
9340 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9341 fb->pitches[0]);
9342 return -EINVAL;
Chris Wilson560b85b2010-08-07 11:01:38 +01009343 }
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009344
Ville Syrjälä659056f2017-03-27 21:55:39 +03009345 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9346
9347 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009348}
9349
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009350static void i845_update_cursor(struct intel_plane *plane,
9351 const struct intel_crtc_state *crtc_state,
Chris Wilson560b85b2010-08-07 11:01:38 +01009352 const struct intel_plane_state *plane_state)
9353{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009354 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009355 u32 cntl = 0, base = 0, pos = 0, size = 0;
9356 unsigned long irqflags;
Chris Wilson560b85b2010-08-07 11:01:38 +01009357
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009358 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009359 unsigned int width = plane_state->base.crtc_w;
9360 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009361
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009362 cntl = plane_state->ctl;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009363 size = (height << 12) | width;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009364
9365 base = intel_cursor_base(plane_state);
9366 pos = intel_cursor_position(plane_state);
Chris Wilson4b0e3332014-05-30 16:35:26 +03009367 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009368
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009369 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9370
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009371 /* On these chipsets we can only modify the base/size/stride
9372 * whilst the cursor is disabled.
9373 */
9374 if (plane->cursor.base != base ||
9375 plane->cursor.size != size ||
9376 plane->cursor.cntl != cntl) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009377 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009378 I915_WRITE_FW(CURBASE(PIPE_A), base);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009379 I915_WRITE_FW(CURSIZE, size);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009380 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009381 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009382
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009383 plane->cursor.base = base;
9384 plane->cursor.size = size;
9385 plane->cursor.cntl = cntl;
9386 } else {
9387 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009388 }
9389
Ville Syrjälä75343a42017-03-27 21:55:38 +03009390 POSTING_READ_FW(CURCNTR(PIPE_A));
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009391
9392 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9393}
9394
9395static void i845_disable_cursor(struct intel_plane *plane,
9396 struct intel_crtc *crtc)
9397{
9398 i845_update_cursor(plane, NULL, NULL);
Chris Wilson560b85b2010-08-07 11:01:38 +01009399}
9400
Ville Syrjälä292889e2017-03-17 23:18:01 +02009401static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9402 const struct intel_plane_state *plane_state)
9403{
9404 struct drm_i915_private *dev_priv =
9405 to_i915(plane_state->base.plane->dev);
9406 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009407 u32 cntl;
9408
9409 cntl = MCURSOR_GAMMA_ENABLE;
9410
9411 if (HAS_DDI(dev_priv))
9412 cntl |= CURSOR_PIPE_CSC_ENABLE;
9413
Ville Syrjäläd509e282017-03-27 21:55:32 +03009414 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009415
9416 switch (plane_state->base.crtc_w) {
9417 case 64:
9418 cntl |= CURSOR_MODE_64_ARGB_AX;
9419 break;
9420 case 128:
9421 cntl |= CURSOR_MODE_128_ARGB_AX;
9422 break;
9423 case 256:
9424 cntl |= CURSOR_MODE_256_ARGB_AX;
9425 break;
9426 default:
9427 MISSING_CASE(plane_state->base.crtc_w);
9428 return 0;
9429 }
9430
Robert Fossc2c446a2017-05-19 16:50:17 -04009431 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä292889e2017-03-17 23:18:01 +02009432 cntl |= CURSOR_ROTATE_180;
9433
9434 return cntl;
9435}
9436
Ville Syrjälä659056f2017-03-27 21:55:39 +03009437static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009438{
Ville Syrjälä024faac2017-03-27 21:55:42 +03009439 struct drm_i915_private *dev_priv =
9440 to_i915(plane_state->base.plane->dev);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009441 int width = plane_state->base.crtc_w;
9442 int height = plane_state->base.crtc_h;
Chris Wilson560b85b2010-08-07 11:01:38 +01009443
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009444 if (!intel_cursor_size_ok(plane_state))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009445 return false;
9446
Ville Syrjälä024faac2017-03-27 21:55:42 +03009447 /* Cursor width is limited to a few power-of-two sizes */
9448 switch (width) {
Ville Syrjälä659056f2017-03-27 21:55:39 +03009449 case 256:
9450 case 128:
Ville Syrjälä659056f2017-03-27 21:55:39 +03009451 case 64:
9452 break;
9453 default:
9454 return false;
9455 }
9456
Ville Syrjälädc41c152014-08-13 11:57:05 +03009457 /*
Ville Syrjälä024faac2017-03-27 21:55:42 +03009458 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9459 * height from 8 lines up to the cursor width, when the
9460 * cursor is not rotated. Everything else requires square
9461 * cursors.
Ville Syrjälädc41c152014-08-13 11:57:05 +03009462 */
Ville Syrjälä024faac2017-03-27 21:55:42 +03009463 if (HAS_CUR_FBC(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009464 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009465 if (height < 8 || height > width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009466 return false;
9467 } else {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009468 if (height != width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009469 return false;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009470 }
9471
9472 return true;
9473}
9474
Ville Syrjälä659056f2017-03-27 21:55:39 +03009475static int i9xx_check_cursor(struct intel_plane *plane,
9476 struct intel_crtc_state *crtc_state,
9477 struct intel_plane_state *plane_state)
9478{
9479 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9480 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009481 enum pipe pipe = plane->pipe;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009482 int ret;
9483
9484 ret = intel_check_cursor(crtc_state, plane_state);
9485 if (ret)
9486 return ret;
9487
9488 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009489 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009490 return 0;
9491
9492 /* Check for which cursor types we support */
9493 if (!i9xx_cursor_size_ok(plane_state)) {
9494 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9495 plane_state->base.crtc_w,
9496 plane_state->base.crtc_h);
9497 return -EINVAL;
9498 }
9499
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009500 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9501 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9502 fb->pitches[0], plane_state->base.crtc_w);
9503 return -EINVAL;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009504 }
9505
9506 /*
9507 * There's something wrong with the cursor on CHV pipe C.
9508 * If it straddles the left edge of the screen then
9509 * moving it away from the edge or disabling it often
9510 * results in a pipe underrun, and often that can lead to
9511 * dead pipe (constant underrun reported, and it scans
9512 * out just a solid color). To recover from that, the
9513 * display power well must be turned off and on again.
9514 * Refuse the put the cursor into that compromised position.
9515 */
9516 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9517 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9518 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9519 return -EINVAL;
9520 }
9521
9522 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
9523
9524 return 0;
9525}
9526
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009527static void i9xx_update_cursor(struct intel_plane *plane,
9528 const struct intel_crtc_state *crtc_state,
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309529 const struct intel_plane_state *plane_state)
9530{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009531 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9532 enum pipe pipe = plane->pipe;
Ville Syrjälä024faac2017-03-27 21:55:42 +03009533 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009534 unsigned long irqflags;
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309535
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009536 if (plane_state && plane_state->base.visible) {
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009537 cntl = plane_state->ctl;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009538
Ville Syrjälä024faac2017-03-27 21:55:42 +03009539 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9540 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
9541
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009542 base = intel_cursor_base(plane_state);
9543 pos = intel_cursor_position(plane_state);
9544 }
9545
9546 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9547
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009548 /*
9549 * On some platforms writing CURCNTR first will also
9550 * cause CURPOS to be armed by the CURBASE write.
9551 * Without the CURCNTR write the CURPOS write would
9552 * arm itself.
9553 *
9554 * CURCNTR and CUR_FBC_CTL are always
9555 * armed by the CURBASE write only.
9556 */
9557 if (plane->cursor.base != base ||
Ville Syrjälä024faac2017-03-27 21:55:42 +03009558 plane->cursor.size != fbc_ctl ||
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009559 plane->cursor.cntl != cntl) {
9560 I915_WRITE_FW(CURCNTR(pipe), cntl);
9561 if (HAS_CUR_FBC(dev_priv))
9562 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
9563 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009564 I915_WRITE_FW(CURBASE(pipe), base);
9565
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009566 plane->cursor.base = base;
9567 plane->cursor.size = fbc_ctl;
9568 plane->cursor.cntl = cntl;
9569 } else {
9570 I915_WRITE_FW(CURPOS(pipe), pos);
9571 }
9572
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309573 POSTING_READ_FW(CURBASE(pipe));
9574
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009575 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009576}
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009577
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009578static void i9xx_disable_cursor(struct intel_plane *plane,
9579 struct intel_crtc *crtc)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009580{
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009581 i9xx_update_cursor(plane, NULL, NULL);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009582}
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009583
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009584
Jesse Barnes79e53942008-11-07 14:24:08 -08009585/* VESA 640x480x72Hz mode to set on the pipe */
9586static struct drm_display_mode load_detect_mode = {
9587 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9588 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9589};
9590
Daniel Vettera8bb6812014-02-10 18:00:39 +01009591struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +00009592intel_framebuffer_create(struct drm_i915_gem_object *obj,
9593 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +01009594{
9595 struct intel_framebuffer *intel_fb;
9596 int ret;
9597
9598 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009599 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009600 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +01009601
Chris Wilson24dbf512017-02-15 10:59:18 +00009602 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009603 if (ret)
9604 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009605
9606 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009607
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009608err:
9609 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009610 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009611}
9612
9613static u32
9614intel_framebuffer_pitch_for_width(int width, int bpp)
9615{
9616 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9617 return ALIGN(pitch, 64);
9618}
9619
9620static u32
9621intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9622{
9623 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009624 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009625}
9626
9627static struct drm_framebuffer *
9628intel_framebuffer_create_for_mode(struct drm_device *dev,
9629 struct drm_display_mode *mode,
9630 int depth, int bpp)
9631{
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009632 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009633 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009634 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009635
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00009636 obj = i915_gem_object_create(to_i915(dev),
Chris Wilsond2dff872011-04-19 08:36:26 +01009637 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +01009638 if (IS_ERR(obj))
9639 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009640
9641 mode_cmd.width = mode->hdisplay;
9642 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009643 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9644 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009645 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009646
Chris Wilson24dbf512017-02-15 10:59:18 +00009647 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009648 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +01009649 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009650
9651 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009652}
9653
9654static struct drm_framebuffer *
9655mode_fits_in_fbdev(struct drm_device *dev,
9656 struct drm_display_mode *mode)
9657{
Daniel Vetter06957262015-08-10 13:34:08 +02009658#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +01009659 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01009660 struct drm_i915_gem_object *obj;
9661 struct drm_framebuffer *fb;
9662
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009663 if (!dev_priv->fbdev)
9664 return NULL;
9665
9666 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009667 return NULL;
9668
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009669 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009670 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009671
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009672 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009673 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
Ville Syrjälä272725c2016-12-14 23:32:20 +02009674 fb->format->cpp[0] * 8))
Chris Wilsond2dff872011-04-19 08:36:26 +01009675 return NULL;
9676
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009677 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009678 return NULL;
9679
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009680 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +01009681 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009682#else
9683 return NULL;
9684#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009685}
9686
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009687static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9688 struct drm_crtc *crtc,
9689 struct drm_display_mode *mode,
9690 struct drm_framebuffer *fb,
9691 int x, int y)
9692{
9693 struct drm_plane_state *plane_state;
9694 int hdisplay, vdisplay;
9695 int ret;
9696
9697 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9698 if (IS_ERR(plane_state))
9699 return PTR_ERR(plane_state);
9700
9701 if (mode)
Daniel Vetter196cd5d2017-01-25 07:26:56 +01009702 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009703 else
9704 hdisplay = vdisplay = 0;
9705
9706 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9707 if (ret)
9708 return ret;
9709 drm_atomic_set_fb_for_plane(plane_state, fb);
9710 plane_state->crtc_x = 0;
9711 plane_state->crtc_y = 0;
9712 plane_state->crtc_w = hdisplay;
9713 plane_state->crtc_h = vdisplay;
9714 plane_state->src_x = x << 16;
9715 plane_state->src_y = y << 16;
9716 plane_state->src_w = hdisplay << 16;
9717 plane_state->src_h = vdisplay << 16;
9718
9719 return 0;
9720}
9721
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009722int intel_get_load_detect_pipe(struct drm_connector *connector,
9723 struct drm_display_mode *mode,
9724 struct intel_load_detect_pipe *old,
9725 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009726{
9727 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009728 struct intel_encoder *intel_encoder =
9729 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009730 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009731 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009732 struct drm_crtc *crtc = NULL;
9733 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02009734 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94352cf2012-07-05 22:51:56 +02009735 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009736 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009737 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009738 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009739 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009740 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009741
Chris Wilsond2dff872011-04-19 08:36:26 +01009742 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009743 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009744 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009745
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009746 old->restore_state = NULL;
9747
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009748 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009749
Jesse Barnes79e53942008-11-07 14:24:08 -08009750 /*
9751 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009752 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009753 * - if the connector already has an assigned crtc, use it (but make
9754 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009755 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009756 * - try to find the first unused crtc that can drive this connector,
9757 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009758 */
9759
9760 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009761 if (connector->state->crtc) {
9762 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009763
Rob Clark51fd3712013-11-19 12:10:12 -05009764 ret = drm_modeset_lock(&crtc->mutex, ctx);
9765 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009766 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +01009767
9768 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009769 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -08009770 }
9771
9772 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009773 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009774 i++;
9775 if (!(encoder->possible_crtcs & (1 << i)))
9776 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009777
9778 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9779 if (ret)
9780 goto fail;
9781
9782 if (possible_crtc->state->enable) {
9783 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +03009784 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009785 }
Ville Syrjäläa4592492014-08-11 13:15:36 +03009786
9787 crtc = possible_crtc;
9788 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009789 }
9790
9791 /*
9792 * If we didn't find an unused CRTC, don't use any.
9793 */
9794 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009795 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +03009796 ret = -ENODEV;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009797 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009798 }
9799
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009800found:
9801 intel_crtc = to_intel_crtc(crtc);
9802
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009803 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9804 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009805 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009806
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009807 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009808 restore_state = drm_atomic_state_alloc(dev);
9809 if (!state || !restore_state) {
9810 ret = -ENOMEM;
9811 goto fail;
9812 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009813
9814 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009815 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009816
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009817 connector_state = drm_atomic_get_connector_state(state, connector);
9818 if (IS_ERR(connector_state)) {
9819 ret = PTR_ERR(connector_state);
9820 goto fail;
9821 }
9822
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009823 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9824 if (ret)
9825 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009826
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009827 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9828 if (IS_ERR(crtc_state)) {
9829 ret = PTR_ERR(crtc_state);
9830 goto fail;
9831 }
9832
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009833 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009834
Chris Wilson64927112011-04-20 07:25:26 +01009835 if (!mode)
9836 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009837
Chris Wilsond2dff872011-04-19 08:36:26 +01009838 /* We need a framebuffer large enough to accommodate all accesses
9839 * that the plane may generate whilst we perform load detection.
9840 * We can not rely on the fbcon either being present (we get called
9841 * during its initialisation to detect all boot displays, or it may
9842 * not even exist) or that it is large enough to satisfy the
9843 * requested mode.
9844 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009845 fb = mode_fits_in_fbdev(dev, mode);
9846 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009847 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009848 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +01009849 } else
9850 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009851 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009852 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +03009853 ret = PTR_ERR(fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009854 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009855 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009856
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009857 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9858 if (ret)
9859 goto fail;
9860
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009861 drm_framebuffer_unreference(fb);
9862
9863 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9864 if (ret)
9865 goto fail;
9866
9867 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9868 if (!ret)
9869 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9870 if (!ret)
9871 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9872 if (ret) {
9873 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9874 goto fail;
9875 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +03009876
Maarten Lankhorst3ba86072016-02-29 09:18:57 +01009877 ret = drm_atomic_commit(state);
9878 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +01009879 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009880 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009881 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009882
9883 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +00009884 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +01009885
Jesse Barnes79e53942008-11-07 14:24:08 -08009886 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02009887 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009888 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009889
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009890fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +01009891 if (state) {
9892 drm_atomic_state_put(state);
9893 state = NULL;
9894 }
9895 if (restore_state) {
9896 drm_atomic_state_put(restore_state);
9897 restore_state = NULL;
9898 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009899
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009900 if (ret == -EDEADLK)
9901 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -05009902
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009903 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009904}
9905
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009906void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009907 struct intel_load_detect_pipe *old,
9908 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009909{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009910 struct intel_encoder *intel_encoder =
9911 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009912 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009913 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009914 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009915
Chris Wilsond2dff872011-04-19 08:36:26 +01009916 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009917 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009918 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009919
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009920 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +01009921 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009922
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01009923 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Chris Wilson08536952016-10-14 13:18:18 +01009924 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009925 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +01009926 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009927}
9928
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009929static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009930 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009931{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009932 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009933 u32 dpll = pipe_config->dpll_hw_state.dpll;
9934
9935 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009936 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009937 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009938 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009939 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009940 return 96000;
9941 else
9942 return 48000;
9943}
9944
Jesse Barnes79e53942008-11-07 14:24:08 -08009945/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009946static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009947 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009948{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009949 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009950 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009951 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009952 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009953 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009954 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +03009955 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009956 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009957
9958 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009959 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009960 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009961 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009962
9963 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009964 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009965 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9966 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009967 } else {
9968 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9969 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9970 }
9971
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009972 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009973 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009974 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9975 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009976 else
9977 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009978 DPLL_FPA01_P1_POST_DIV_SHIFT);
9979
9980 switch (dpll & DPLL_MODE_MASK) {
9981 case DPLLB_MODE_DAC_SERIAL:
9982 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9983 5 : 10;
9984 break;
9985 case DPLLB_MODE_LVDS:
9986 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9987 7 : 14;
9988 break;
9989 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009990 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009991 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009992 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009993 }
9994
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009995 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +03009996 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009997 else
Imre Deakdccbea32015-06-22 23:35:51 +03009998 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009999 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010000 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010001 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010002
10003 if (is_lvds) {
10004 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10005 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010006
10007 if (lvds & LVDS_CLKB_POWER_UP)
10008 clock.p2 = 7;
10009 else
10010 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010011 } else {
10012 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10013 clock.p1 = 2;
10014 else {
10015 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10016 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10017 }
10018 if (dpll & PLL_P2_DIVIDE_BY_4)
10019 clock.p2 = 4;
10020 else
10021 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010022 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010023
Imre Deakdccbea32015-06-22 23:35:51 +030010024 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010025 }
10026
Ville Syrjälä18442d02013-09-13 16:00:08 +030010027 /*
10028 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010029 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010030 * encoder's get_config() function.
10031 */
Imre Deakdccbea32015-06-22 23:35:51 +030010032 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010033}
10034
Ville Syrjälä6878da02013-09-13 15:59:11 +030010035int intel_dotclock_calculate(int link_freq,
10036 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010037{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010038 /*
10039 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010040 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010041 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010042 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010043 *
10044 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010045 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010046 */
10047
Ville Syrjälä6878da02013-09-13 15:59:11 +030010048 if (!m_n->link_n)
10049 return 0;
10050
10051 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10052}
10053
Ville Syrjälä18442d02013-09-13 16:00:08 +030010054static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010055 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010056{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010057 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010058
10059 /* read out port_clock from the DPLL */
10060 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010061
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010062 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010063 * In case there is an active pipe without active ports,
10064 * we may need some idea for the dotclock anyway.
10065 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010066 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010067 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010068 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010069 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010070}
10071
10072/** Returns the currently programmed mode of the given pipe. */
10073struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10074 struct drm_crtc *crtc)
10075{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010076 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010078 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010079 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010080 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010081 int htot = I915_READ(HTOTAL(cpu_transcoder));
10082 int hsync = I915_READ(HSYNC(cpu_transcoder));
10083 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10084 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010085 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010086
10087 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10088 if (!mode)
10089 return NULL;
10090
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010091 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10092 if (!pipe_config) {
10093 kfree(mode);
10094 return NULL;
10095 }
10096
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010097 /*
10098 * Construct a pipe_config sufficient for getting the clock info
10099 * back out of crtc_clock_get.
10100 *
10101 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10102 * to use a real value here instead.
10103 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010104 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10105 pipe_config->pixel_multiplier = 1;
10106 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10107 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10108 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10109 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010110
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010111 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010112 mode->hdisplay = (htot & 0xffff) + 1;
10113 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10114 mode->hsync_start = (hsync & 0xffff) + 1;
10115 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10116 mode->vdisplay = (vtot & 0xffff) + 1;
10117 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10118 mode->vsync_start = (vsync & 0xffff) + 1;
10119 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10120
10121 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010122
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010123 kfree(pipe_config);
10124
Jesse Barnes79e53942008-11-07 14:24:08 -080010125 return mode;
10126}
10127
10128static void intel_crtc_destroy(struct drm_crtc *crtc)
10129{
10130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010131 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010132 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010133
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010134 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010135 work = intel_crtc->flip_work;
10136 intel_crtc->flip_work = NULL;
10137 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010138
Daniel Vetter5a21b662016-05-24 17:13:53 +020010139 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010140 cancel_work_sync(&work->mmio_work);
10141 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010142 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010143 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010144
10145 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010146
Jesse Barnes79e53942008-11-07 14:24:08 -080010147 kfree(intel_crtc);
10148}
10149
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010150static void intel_unpin_work_fn(struct work_struct *__work)
10151{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010152 struct intel_flip_work *work =
10153 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010154 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10155 struct drm_device *dev = crtc->base.dev;
10156 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010157
Daniel Vetter5a21b662016-05-24 17:13:53 +020010158 if (is_mmio_work(work))
10159 flush_work(&work->mmio_work);
10160
10161 mutex_lock(&dev->struct_mutex);
Chris Wilsonbe1e3412017-01-16 15:21:27 +000010162 intel_unpin_fb_vma(work->old_vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010010163 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010164 mutex_unlock(&dev->struct_mutex);
10165
Chris Wilsone8a261e2016-07-20 13:31:49 +010010166 i915_gem_request_put(work->flip_queued_req);
10167
Chris Wilson5748b6a2016-08-04 16:32:38 +010010168 intel_frontbuffer_flip_complete(to_i915(dev),
10169 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010170 intel_fbc_post_update(crtc);
10171 drm_framebuffer_unreference(work->old_fb);
10172
10173 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10174 atomic_dec(&crtc->unpin_work_count);
10175
10176 kfree(work);
10177}
10178
10179/* Is 'a' after or equal to 'b'? */
10180static bool g4x_flip_count_after_eq(u32 a, u32 b)
10181{
10182 return !((a - b) & 0x80000000);
10183}
10184
10185static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10186 struct intel_flip_work *work)
10187{
10188 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010189 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010190
Chris Wilson8af29b02016-09-09 14:11:47 +010010191 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010192 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010193
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010194 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010195 * The relevant registers doen't exist on pre-ctg.
10196 * As the flip done interrupt doesn't trigger for mmio
10197 * flips on gmch platforms, a flip count check isn't
10198 * really needed there. But since ctg has the registers,
10199 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010200 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010201 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010202 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010203
Daniel Vetter5a21b662016-05-24 17:13:53 +020010204 /*
10205 * BDW signals flip done immediately if the plane
10206 * is disabled, even if the plane enable is already
10207 * armed to occur at the next vblank :(
10208 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010209
Daniel Vetter5a21b662016-05-24 17:13:53 +020010210 /*
10211 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10212 * used the same base address. In that case the mmio flip might
10213 * have completed, but the CS hasn't even executed the flip yet.
10214 *
10215 * A flip count check isn't enough as the CS might have updated
10216 * the base address just after start of vblank, but before we
10217 * managed to process the interrupt. This means we'd complete the
10218 * CS flip too soon.
10219 *
10220 * Combining both checks should get us a good enough result. It may
10221 * still happen that the CS flip has been executed, but has not
10222 * yet actually completed. But in case the base address is the same
10223 * anyway, we don't really care.
10224 */
10225 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10226 crtc->flip_work->gtt_offset &&
10227 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10228 crtc->flip_work->flip_count);
10229}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010230
Daniel Vetter5a21b662016-05-24 17:13:53 +020010231static bool
10232__pageflip_finished_mmio(struct intel_crtc *crtc,
10233 struct intel_flip_work *work)
10234{
10235 /*
10236 * MMIO work completes when vblank is different from
10237 * flip_queued_vblank.
10238 *
10239 * Reset counter value doesn't matter, this is handled by
10240 * i915_wait_request finishing early, so no need to handle
10241 * reset here.
10242 */
10243 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010244}
10245
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010246
10247static bool pageflip_finished(struct intel_crtc *crtc,
10248 struct intel_flip_work *work)
10249{
10250 if (!atomic_read(&work->pending))
10251 return false;
10252
10253 smp_rmb();
10254
Daniel Vetter5a21b662016-05-24 17:13:53 +020010255 if (is_mmio_work(work))
10256 return __pageflip_finished_mmio(crtc, work);
10257 else
10258 return __pageflip_finished_cs(crtc, work);
10259}
10260
10261void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
10262{
Chris Wilson91c8a322016-07-05 10:40:23 +010010263 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010264 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010265 struct intel_flip_work *work;
10266 unsigned long flags;
10267
10268 /* Ignore early vblank irqs */
10269 if (!crtc)
10270 return;
10271
Daniel Vetterf3260382014-09-15 14:55:23 +020010272 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010273 * This is called both by irq handlers and the reset code (to complete
10274 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000010275 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010276 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010277 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010278
10279 if (work != NULL &&
10280 !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010281 pageflip_finished(crtc, work))
10282 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010283
10284 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010285}
10286
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010287void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010288{
Chris Wilson91c8a322016-07-05 10:40:23 +010010289 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010290 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010291 struct intel_flip_work *work;
10292 unsigned long flags;
10293
10294 /* Ignore early vblank irqs */
10295 if (!crtc)
10296 return;
10297
10298 /*
10299 * This is called both by irq handlers and the reset code (to complete
10300 * lost pageflips) so needs the full irqsave spinlocks.
10301 */
10302 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010303 work = crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010304
Daniel Vetter5a21b662016-05-24 17:13:53 +020010305 if (work != NULL &&
10306 is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010307 pageflip_finished(crtc, work))
10308 page_flip_completed(crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020010309
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010310 spin_unlock_irqrestore(&dev->event_lock, flags);
10311}
10312
Daniel Vetter5a21b662016-05-24 17:13:53 +020010313static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10314 struct intel_flip_work *work)
10315{
10316 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
10317
10318 /* Ensure that the work item is consistent when activating it ... */
10319 smp_mb__before_atomic();
10320 atomic_set(&work->pending, 1);
10321}
10322
10323static int intel_gen2_queue_flip(struct drm_device *dev,
10324 struct drm_crtc *crtc,
10325 struct drm_framebuffer *fb,
10326 struct drm_i915_gem_object *obj,
10327 struct drm_i915_gem_request *req,
10328 uint32_t flags)
10329{
Daniel Vetter5a21b662016-05-24 17:13:53 +020010330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010331 u32 flip_mask, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010332
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010333 cs = intel_ring_begin(req, 6);
10334 if (IS_ERR(cs))
10335 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010336
10337 /* Can't queue multiple flips, so wait for the previous
10338 * one to finish before executing the next.
10339 */
10340 if (intel_crtc->plane)
10341 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10342 else
10343 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010344 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10345 *cs++ = MI_NOOP;
10346 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10347 *cs++ = fb->pitches[0];
10348 *cs++ = intel_crtc->flip_work->gtt_offset;
10349 *cs++ = 0; /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010350
10351 return 0;
10352}
10353
10354static int intel_gen3_queue_flip(struct drm_device *dev,
10355 struct drm_crtc *crtc,
10356 struct drm_framebuffer *fb,
10357 struct drm_i915_gem_object *obj,
10358 struct drm_i915_gem_request *req,
10359 uint32_t flags)
10360{
Daniel Vetter5a21b662016-05-24 17:13:53 +020010361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010362 u32 flip_mask, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010363
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010364 cs = intel_ring_begin(req, 6);
10365 if (IS_ERR(cs))
10366 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010367
10368 if (intel_crtc->plane)
10369 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10370 else
10371 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010372 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10373 *cs++ = MI_NOOP;
10374 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10375 *cs++ = fb->pitches[0];
10376 *cs++ = intel_crtc->flip_work->gtt_offset;
10377 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010378
10379 return 0;
10380}
10381
10382static int intel_gen4_queue_flip(struct drm_device *dev,
10383 struct drm_crtc *crtc,
10384 struct drm_framebuffer *fb,
10385 struct drm_i915_gem_object *obj,
10386 struct drm_i915_gem_request *req,
10387 uint32_t flags)
10388{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010389 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010391 u32 pf, pipesrc, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010392
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010393 cs = intel_ring_begin(req, 4);
10394 if (IS_ERR(cs))
10395 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010396
10397 /* i965+ uses the linear or tiled offsets from the
10398 * Display Registers (which do not change across a page-flip)
10399 * so we need only reprogram the base address.
10400 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010401 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10402 *cs++ = fb->pitches[0];
10403 *cs++ = intel_crtc->flip_work->gtt_offset |
10404 intel_fb_modifier_to_tiling(fb->modifier);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010405
10406 /* XXX Enabling the panel-fitter across page-flip is so far
10407 * untested on non-native modes, so ignore it for now.
10408 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10409 */
10410 pf = 0;
10411 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010412 *cs++ = pf | pipesrc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010413
10414 return 0;
10415}
10416
10417static int intel_gen6_queue_flip(struct drm_device *dev,
10418 struct drm_crtc *crtc,
10419 struct drm_framebuffer *fb,
10420 struct drm_i915_gem_object *obj,
10421 struct drm_i915_gem_request *req,
10422 uint32_t flags)
10423{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010424 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010426 u32 pf, pipesrc, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010427
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010428 cs = intel_ring_begin(req, 4);
10429 if (IS_ERR(cs))
10430 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010431
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010432 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10433 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10434 *cs++ = intel_crtc->flip_work->gtt_offset;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010435
10436 /* Contrary to the suggestions in the documentation,
10437 * "Enable Panel Fitter" does not seem to be required when page
10438 * flipping with a non-native mode, and worse causes a normal
10439 * modeset to fail.
10440 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10441 */
10442 pf = 0;
10443 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010444 *cs++ = pf | pipesrc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010445
10446 return 0;
10447}
10448
10449static int intel_gen7_queue_flip(struct drm_device *dev,
10450 struct drm_crtc *crtc,
10451 struct drm_framebuffer *fb,
10452 struct drm_i915_gem_object *obj,
10453 struct drm_i915_gem_request *req,
10454 uint32_t flags)
10455{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010456 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010458 u32 *cs, plane_bit = 0;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010459 int len, ret;
10460
10461 switch (intel_crtc->plane) {
10462 case PLANE_A:
10463 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10464 break;
10465 case PLANE_B:
10466 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10467 break;
10468 case PLANE_C:
10469 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10470 break;
10471 default:
10472 WARN_ONCE(1, "unknown plane in flip command\n");
10473 return -ENODEV;
10474 }
10475
10476 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010010477 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010478 len += 6;
10479 /*
10480 * On Gen 8, SRM is now taking an extra dword to accommodate
10481 * 48bits addresses, and we need a NOOP for the batch size to
10482 * stay even.
10483 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010484 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010485 len += 2;
10486 }
10487
10488 /*
10489 * BSpec MI_DISPLAY_FLIP for IVB:
10490 * "The full packet must be contained within the same cache line."
10491 *
10492 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10493 * cacheline, if we ever start emitting more commands before
10494 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10495 * then do the cacheline alignment, and finally emit the
10496 * MI_DISPLAY_FLIP.
10497 */
10498 ret = intel_ring_cacheline_align(req);
10499 if (ret)
10500 return ret;
10501
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010502 cs = intel_ring_begin(req, len);
10503 if (IS_ERR(cs))
10504 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010505
10506 /* Unmask the flip-done completion message. Note that the bspec says that
10507 * we should do this for both the BCS and RCS, and that we must not unmask
10508 * more than one flip event at any time (or ensure that one flip message
10509 * can be sent by waiting for flip-done prior to queueing new flips).
10510 * Experimentation says that BCS works despite DERRMR masking all
10511 * flip-done completion events and that unmasking all planes at once
10512 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10513 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10514 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010010515 if (req->engine->id == RCS) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010516 *cs++ = MI_LOAD_REGISTER_IMM(1);
10517 *cs++ = i915_mmio_reg_offset(DERRMR);
10518 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10519 DERRMR_PIPEB_PRI_FLIP_DONE |
10520 DERRMR_PIPEC_PRI_FLIP_DONE);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010521 if (IS_GEN8(dev_priv))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010522 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10523 MI_SRM_LRM_GLOBAL_GTT;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010524 else
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010525 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10526 *cs++ = i915_mmio_reg_offset(DERRMR);
10527 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010528 if (IS_GEN8(dev_priv)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010529 *cs++ = 0;
10530 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010531 }
10532 }
10533
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010534 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10535 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10536 *cs++ = intel_crtc->flip_work->gtt_offset;
10537 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010538
10539 return 0;
10540}
10541
10542static bool use_mmio_flip(struct intel_engine_cs *engine,
10543 struct drm_i915_gem_object *obj)
10544{
10545 /*
10546 * This is not being used for older platforms, because
10547 * non-availability of flip done interrupt forces us to use
10548 * CS flips. Older platforms derive flip done using some clever
10549 * tricks involving the flip_pending status bits and vblank irqs.
10550 * So using MMIO flips there would disrupt this mechanism.
10551 */
10552
10553 if (engine == NULL)
10554 return true;
10555
10556 if (INTEL_GEN(engine->i915) < 5)
10557 return false;
10558
10559 if (i915.use_mmio_flip < 0)
10560 return false;
10561 else if (i915.use_mmio_flip > 0)
10562 return true;
10563 else if (i915.enable_execlists)
10564 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010010565
Chris Wilsond07f0e52016-10-28 13:58:44 +010010566 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010567}
10568
10569static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10570 unsigned int rotation,
10571 struct intel_flip_work *work)
10572{
10573 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010574 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010575 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10576 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020010577 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010578
10579 ctl = I915_READ(PLANE_CTL(pipe, 0));
10580 ctl &= ~PLANE_CTL_TILED_MASK;
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010581 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -070010582 case DRM_FORMAT_MOD_LINEAR:
Daniel Vetter5a21b662016-05-24 17:13:53 +020010583 break;
10584 case I915_FORMAT_MOD_X_TILED:
10585 ctl |= PLANE_CTL_TILED_X;
10586 break;
10587 case I915_FORMAT_MOD_Y_TILED:
10588 ctl |= PLANE_CTL_TILED_Y;
10589 break;
10590 case I915_FORMAT_MOD_Yf_TILED:
10591 ctl |= PLANE_CTL_TILED_YF;
10592 break;
10593 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010594 MISSING_CASE(fb->modifier);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010595 }
10596
10597 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010598 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10599 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10600 */
10601 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10602 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10603
10604 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10605 POSTING_READ(PLANE_SURF(pipe, 0));
10606}
10607
10608static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10609 struct intel_flip_work *work)
10610{
10611 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010612 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020010613 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010614 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10615 u32 dspcntr;
10616
10617 dspcntr = I915_READ(reg);
10618
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010619 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010620 dspcntr |= DISPPLANE_TILED;
10621 else
10622 dspcntr &= ~DISPPLANE_TILED;
10623
10624 I915_WRITE(reg, dspcntr);
10625
10626 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10627 POSTING_READ(DSPSURF(intel_crtc->plane));
10628}
10629
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010630static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000010631{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010632 struct intel_flip_work *work =
10633 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010634 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10635 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10636 struct intel_framebuffer *intel_fb =
10637 to_intel_framebuffer(crtc->base.primary->fb);
10638 struct drm_i915_gem_object *obj = intel_fb->obj;
10639
Chris Wilsond07f0e52016-10-28 13:58:44 +010010640 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010641
10642 intel_pipe_update_start(crtc);
10643
10644 if (INTEL_GEN(dev_priv) >= 9)
10645 skl_do_mmio_flip(crtc, work->rotation, work);
10646 else
10647 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10648 ilk_do_mmio_flip(crtc, work);
10649
10650 intel_pipe_update_end(crtc, work);
10651}
10652
10653static int intel_default_queue_flip(struct drm_device *dev,
10654 struct drm_crtc *crtc,
10655 struct drm_framebuffer *fb,
10656 struct drm_i915_gem_object *obj,
10657 struct drm_i915_gem_request *req,
10658 uint32_t flags)
10659{
10660 return -ENODEV;
10661}
10662
10663static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10664 struct intel_crtc *intel_crtc,
10665 struct intel_flip_work *work)
10666{
10667 u32 addr, vblank;
10668
10669 if (!atomic_read(&work->pending))
10670 return false;
10671
10672 smp_rmb();
10673
10674 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10675 if (work->flip_ready_vblank == 0) {
10676 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010010677 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010678 return false;
10679
10680 work->flip_ready_vblank = vblank;
10681 }
10682
10683 if (vblank - work->flip_ready_vblank < 3)
10684 return false;
10685
10686 /* Potential stall - if we see that the flip has happened,
10687 * assume a missed interrupt. */
10688 if (INTEL_GEN(dev_priv) >= 4)
10689 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10690 else
10691 addr = I915_READ(DSPADDR(intel_crtc->plane));
10692
10693 /* There is a potential issue here with a false positive after a flip
10694 * to the same address. We could address this by checking for a
10695 * non-incrementing frame counter.
10696 */
10697 return addr == work->gtt_offset;
10698}
10699
10700void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10701{
Chris Wilson91c8a322016-07-05 10:40:23 +010010702 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010703 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010704 struct intel_flip_work *work;
10705
10706 WARN_ON(!in_interrupt());
10707
10708 if (crtc == NULL)
10709 return;
10710
10711 spin_lock(&dev->event_lock);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010712 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010713
10714 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010715 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010716 WARN_ONCE(1,
10717 "Kicking stuck page flip: queued at %d, now %d\n",
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010718 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10719 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010720 work = NULL;
10721 }
10722
10723 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010724 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010725 intel_queue_rps_boost_for_request(work->flip_queued_req);
10726 spin_unlock(&dev->event_lock);
10727}
10728
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010010729__maybe_unused
Daniel Vetter5a21b662016-05-24 17:13:53 +020010730static int intel_crtc_page_flip(struct drm_crtc *crtc,
10731 struct drm_framebuffer *fb,
10732 struct drm_pending_vblank_event *event,
10733 uint32_t page_flip_flags)
10734{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010735 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010736 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010737 struct drm_framebuffer *old_fb = crtc->primary->fb;
10738 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10740 struct drm_plane *primary = crtc->primary;
10741 enum pipe pipe = intel_crtc->pipe;
10742 struct intel_flip_work *work;
10743 struct intel_engine_cs *engine;
10744 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010010745 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010010746 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010747 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010748
Daniel Vetter5a21b662016-05-24 17:13:53 +020010749 /*
10750 * drm_mode_page_flip_ioctl() should already catch this, but double
10751 * check to be safe. In the future we may enable pageflipping from
10752 * a disabled primary plane.
10753 */
10754 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10755 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010756
Daniel Vetter5a21b662016-05-24 17:13:53 +020010757 /* Can't change pixel format via MI display flips. */
Ville Syrjälädbd4d572016-11-18 21:53:10 +020010758 if (fb->format != crtc->primary->fb->format)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010759 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010760
Daniel Vetter5a21b662016-05-24 17:13:53 +020010761 /*
10762 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10763 * Note that pitch changes could also affect these register.
10764 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010765 if (INTEL_GEN(dev_priv) > 3 &&
Daniel Vetter5a21b662016-05-24 17:13:53 +020010766 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10767 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10768 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010769
Daniel Vetter5a21b662016-05-24 17:13:53 +020010770 if (i915_terminally_wedged(&dev_priv->gpu_error))
10771 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010772
Daniel Vetter5a21b662016-05-24 17:13:53 +020010773 work = kzalloc(sizeof(*work), GFP_KERNEL);
10774 if (work == NULL)
10775 return -ENOMEM;
10776
10777 work->event = event;
10778 work->crtc = crtc;
10779 work->old_fb = old_fb;
10780 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010781
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010782 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010783 if (ret)
10784 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010785
Daniel Vetter5a21b662016-05-24 17:13:53 +020010786 /* We borrow the event spin lock for protecting flip_work */
10787 spin_lock_irq(&dev->event_lock);
10788 if (intel_crtc->flip_work) {
10789 /* Before declaring the flip queue wedged, check if
10790 * the hardware completed the operation behind our backs.
10791 */
10792 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10793 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10794 page_flip_completed(intel_crtc);
10795 } else {
10796 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10797 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010798
Daniel Vetter5a21b662016-05-24 17:13:53 +020010799 drm_crtc_vblank_put(crtc);
10800 kfree(work);
10801 return -EBUSY;
10802 }
10803 }
10804 intel_crtc->flip_work = work;
10805 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080010806
Daniel Vetter5a21b662016-05-24 17:13:53 +020010807 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10808 flush_workqueue(dev_priv->wq);
10809
10810 /* Reference the objects for the scheduled work. */
10811 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010812
10813 crtc->primary->fb = fb;
10814 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020010815
Chris Wilson25dc5562016-07-20 13:31:52 +010010816 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010817
10818 ret = i915_mutex_lock_interruptible(dev);
10819 if (ret)
10820 goto cleanup;
10821
Chris Wilson8af29b02016-09-09 14:11:47 +010010822 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
Chris Wilson8c185ec2017-03-16 17:13:02 +000010823 if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010824 ret = -EIO;
Matthew Auldddbb2712016-11-28 10:36:48 +000010825 goto unlock;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010826 }
10827
10828 atomic_inc(&intel_crtc->unpin_work_count);
10829
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010830 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010831 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10832
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010010833 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053010834 engine = dev_priv->engine[BCS];
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010835 if (fb->modifier != old_fb->modifier)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010836 /* vlv: DISPLAY_FLIP fails to change tiling */
10837 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010010838 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053010839 engine = dev_priv->engine[BCS];
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010840 } else if (INTEL_GEN(dev_priv) >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010010841 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010842 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053010843 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020010844 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053010845 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020010846 }
10847
10848 mmio_flip = use_mmio_flip(engine, obj);
10849
Chris Wilson058d88c2016-08-15 10:49:06 +010010850 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10851 if (IS_ERR(vma)) {
10852 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010853 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010010854 }
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010855
Chris Wilsonbe1e3412017-01-16 15:21:27 +000010856 work->old_vma = to_intel_plane_state(primary->state)->vma;
10857 to_intel_plane_state(primary->state)->vma = vma;
10858
10859 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010860 work->rotation = crtc->primary->state->rotation;
10861
Paulo Zanoni1f0613162016-08-17 16:41:44 -030010862 /*
10863 * There's the potential that the next frame will not be compatible with
10864 * FBC, so we want to call pre_update() before the actual page flip.
10865 * The problem is that pre_update() caches some information about the fb
10866 * object, so we want to do this only after the object is pinned. Let's
10867 * be on the safe side and do this immediately before scheduling the
10868 * flip.
10869 */
10870 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10871 to_intel_plane_state(primary->state));
10872
Daniel Vetter5a21b662016-05-24 17:13:53 +020010873 if (mmio_flip) {
10874 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030010875 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010876 } else {
Chris Wilsone8a9c582016-12-18 15:37:20 +000010877 request = i915_gem_request_alloc(engine,
10878 dev_priv->kernel_context);
Chris Wilson8e637172016-08-02 22:50:26 +010010879 if (IS_ERR(request)) {
10880 ret = PTR_ERR(request);
10881 goto cleanup_unpin;
10882 }
10883
Chris Wilsona2bc4692016-09-09 14:11:56 +010010884 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010010885 if (ret)
10886 goto cleanup_request;
10887
Daniel Vetter5a21b662016-05-24 17:13:53 +020010888 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10889 page_flip_flags);
10890 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010010891 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010892
10893 intel_mark_page_flip_active(intel_crtc, work);
10894
Chris Wilson8e637172016-08-02 22:50:26 +010010895 work->flip_queued_req = i915_gem_request_get(request);
Chris Wilsone642c852017-03-17 11:47:09 +000010896 i915_add_request(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010897 }
10898
Chris Wilson92117f02016-11-28 14:36:48 +000010899 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010900 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10901 to_intel_plane(primary)->frontbuffer_bit);
10902 mutex_unlock(&dev->struct_mutex);
10903
Chris Wilson5748b6a2016-08-04 16:32:38 +010010904 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020010905 to_intel_plane(primary)->frontbuffer_bit);
10906
10907 trace_i915_flip_request(intel_crtc->plane, obj);
10908
10909 return 0;
10910
Chris Wilson8e637172016-08-02 22:50:26 +010010911cleanup_request:
Chris Wilsone642c852017-03-17 11:47:09 +000010912 i915_add_request(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010913cleanup_unpin:
Chris Wilsonbe1e3412017-01-16 15:21:27 +000010914 to_intel_plane_state(primary->state)->vma = work->old_vma;
10915 intel_unpin_fb_vma(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010916cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020010917 atomic_dec(&intel_crtc->unpin_work_count);
Matthew Auldddbb2712016-11-28 10:36:48 +000010918unlock:
Daniel Vetter5a21b662016-05-24 17:13:53 +020010919 mutex_unlock(&dev->struct_mutex);
10920cleanup:
10921 crtc->primary->fb = old_fb;
10922 update_state_fb(crtc->primary);
10923
Chris Wilsonf0cd5182016-10-28 13:58:43 +010010924 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010925 drm_framebuffer_unreference(work->old_fb);
10926
10927 spin_lock_irq(&dev->event_lock);
10928 intel_crtc->flip_work = NULL;
10929 spin_unlock_irq(&dev->event_lock);
10930
10931 drm_crtc_vblank_put(crtc);
10932free_work:
10933 kfree(work);
10934
10935 if (ret == -EIO) {
10936 struct drm_atomic_state *state;
10937 struct drm_plane_state *plane_state;
10938
10939out_hang:
10940 state = drm_atomic_state_alloc(dev);
10941 if (!state)
10942 return -ENOMEM;
Daniel Vetterb260ac32017-04-03 10:32:52 +020010943 state->acquire_ctx = dev->mode_config.acquire_ctx;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010944
10945retry:
10946 plane_state = drm_atomic_get_plane_state(state, primary);
10947 ret = PTR_ERR_OR_ZERO(plane_state);
10948 if (!ret) {
10949 drm_atomic_set_fb_for_plane(plane_state, fb);
10950
10951 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10952 if (!ret)
10953 ret = drm_atomic_commit(state);
10954 }
10955
10956 if (ret == -EDEADLK) {
10957 drm_modeset_backoff(state->acquire_ctx);
10958 drm_atomic_state_clear(state);
10959 goto retry;
10960 }
10961
Chris Wilson08536952016-10-14 13:18:18 +010010962 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010963
10964 if (ret == 0 && event) {
10965 spin_lock_irq(&dev->event_lock);
10966 drm_crtc_send_vblank_event(crtc, event);
10967 spin_unlock_irq(&dev->event_lock);
10968 }
10969 }
10970 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971}
10972
Daniel Vetter5a21b662016-05-24 17:13:53 +020010973
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010974/**
10975 * intel_wm_need_update - Check whether watermarks need updating
10976 * @plane: drm plane
10977 * @state: new plane state
10978 *
10979 * Check current plane state versus the new one to determine whether
10980 * watermarks need to be recalculated.
10981 *
10982 * Returns true or false.
10983 */
10984static bool intel_wm_need_update(struct drm_plane *plane,
10985 struct drm_plane_state *state)
10986{
Matt Roperd21fbe82015-09-24 15:53:12 -070010987 struct intel_plane_state *new = to_intel_plane_state(state);
10988 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10989
10990 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010991 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010992 return true;
10993
10994 if (!cur->base.fb || !new->base.fb)
10995 return false;
10996
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010997 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010998 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010999 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
11000 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
11001 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
11002 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011003 return true;
11004
11005 return false;
11006}
11007
Matt Roperd21fbe82015-09-24 15:53:12 -070011008static bool needs_scaling(struct intel_plane_state *state)
11009{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030011010 int src_w = drm_rect_width(&state->base.src) >> 16;
11011 int src_h = drm_rect_height(&state->base.src) >> 16;
11012 int dst_w = drm_rect_width(&state->base.dst);
11013 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070011014
11015 return (src_w != dst_w || src_h != dst_h);
11016}
11017
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011018int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11019 struct drm_plane_state *plane_state)
11020{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011021 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011022 struct drm_crtc *crtc = crtc_state->crtc;
11023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011024 struct intel_plane *plane = to_intel_plane(plane_state->plane);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011025 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011026 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011027 struct intel_plane_state *old_plane_state =
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011028 to_intel_plane_state(plane->base.state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011029 bool mode_changed = needs_modeset(crtc_state);
11030 bool was_crtc_enabled = crtc->state->active;
11031 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011032 bool turn_off, turn_on, visible, was_visible;
11033 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011034 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011035
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011036 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011037 ret = skl_update_scaler_plane(
11038 to_intel_crtc_state(crtc_state),
11039 to_intel_plane_state(plane_state));
11040 if (ret)
11041 return ret;
11042 }
11043
Ville Syrjälä936e71e2016-07-26 19:06:59 +030011044 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010011045 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011046
11047 if (!was_crtc_enabled && WARN_ON(was_visible))
11048 was_visible = false;
11049
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011050 /*
11051 * Visibility is calculated as if the crtc was on, but
11052 * after scaler setup everything depends on it being off
11053 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011054 *
11055 * FIXME this is wrong for watermarks. Watermarks should also
11056 * be computed as if the pipe would be active. Perhaps move
11057 * per-plane wm computation to the .check_plane() hook, and
11058 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011059 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011060 if (!is_crtc_enabled) {
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010011061 plane_state->visible = visible = false;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011062 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
11063 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011064
11065 if (!was_visible && !visible)
11066 return 0;
11067
Maarten Lankhorste8861672016-02-24 11:24:26 +010011068 if (fb != old_plane_state->base.fb)
11069 pipe_config->fb_changed = true;
11070
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011071 turn_off = was_visible && (!visible || mode_changed);
11072 turn_on = visible && (!was_visible || mode_changed);
11073
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011074 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011075 intel_crtc->base.base.id, intel_crtc->base.name,
11076 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011077 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011078
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011079 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011080 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011081 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011082 turn_off, turn_on, mode_changed);
11083
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011084 if (turn_on) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011085 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020011086 pipe_config->update_wm_pre = true;
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011087
11088 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011089 if (plane->id != PLANE_CURSOR)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011090 pipe_config->disable_cxsr = true;
11091 } else if (turn_off) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011092 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020011093 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011094
Ville Syrjälä852eb002015-06-24 22:00:07 +030011095 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011096 if (plane->id != PLANE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011097 pipe_config->disable_cxsr = true;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011098 } else if (intel_wm_need_update(&plane->base, plane_state)) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011099 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020011100 /* FIXME bollocks */
11101 pipe_config->update_wm_pre = true;
11102 pipe_config->update_wm_post = true;
11103 }
Ville Syrjälä852eb002015-06-24 22:00:07 +030011104 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011105
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011106 if (visible || was_visible)
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011107 pipe_config->fb_bits |= plane->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011108
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011109 /*
11110 * WaCxSRDisabledForSpriteScaling:ivb
11111 *
11112 * cstate->update_wm was already set above, so this flag will
11113 * take effect when we commit and program watermarks.
11114 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020011115 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011116 needs_scaling(to_intel_plane_state(plane_state)) &&
11117 !needs_scaling(old_plane_state))
11118 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011119
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011120 return 0;
11121}
11122
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011123static bool encoders_cloneable(const struct intel_encoder *a,
11124 const struct intel_encoder *b)
11125{
11126 /* masks could be asymmetric, so check both ways */
11127 return a == b || (a->cloneable & (1 << b->type) &&
11128 b->cloneable & (1 << a->type));
11129}
11130
11131static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11132 struct intel_crtc *crtc,
11133 struct intel_encoder *encoder)
11134{
11135 struct intel_encoder *source_encoder;
11136 struct drm_connector *connector;
11137 struct drm_connector_state *connector_state;
11138 int i;
11139
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011140 for_each_new_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011141 if (connector_state->crtc != &crtc->base)
11142 continue;
11143
11144 source_encoder =
11145 to_intel_encoder(connector_state->best_encoder);
11146 if (!encoders_cloneable(encoder, source_encoder))
11147 return false;
11148 }
11149
11150 return true;
11151}
11152
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011153static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11154 struct drm_crtc_state *crtc_state)
11155{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011156 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011157 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011159 struct intel_crtc_state *pipe_config =
11160 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011161 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011162 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011163 bool mode_changed = needs_modeset(crtc_state);
11164
Ville Syrjälä852eb002015-06-24 22:00:07 +030011165 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011166 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011167
Maarten Lankhorstad421372015-06-15 12:33:42 +020011168 if (mode_changed && crtc_state->enable &&
11169 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011170 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011171 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11172 pipe_config);
11173 if (ret)
11174 return ret;
11175 }
11176
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011177 if (crtc_state->color_mgmt_changed) {
11178 ret = intel_color_check(crtc, crtc_state);
11179 if (ret)
11180 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010011181
11182 /*
11183 * Changing color management on Intel hardware is
11184 * handled as part of planes update.
11185 */
11186 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011187 }
11188
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011189 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011190 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011191 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011192 if (ret) {
11193 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011194 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011195 }
11196 }
11197
11198 if (dev_priv->display.compute_intermediate_wm &&
11199 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11200 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11201 return 0;
11202
11203 /*
11204 * Calculate 'intermediate' watermarks that satisfy both the
11205 * old state and the new state. We can program these
11206 * immediately.
11207 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011208 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080011209 intel_crtc,
11210 pipe_config);
11211 if (ret) {
11212 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11213 return ret;
11214 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070011215 } else if (dev_priv->display.compute_intermediate_wm) {
11216 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11217 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011218 }
11219
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011220 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011221 if (mode_changed)
11222 ret = skl_update_scaler_crtc(pipe_config);
11223
11224 if (!ret)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +053011225 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
11226 pipe_config);
11227 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020011228 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011229 pipe_config);
11230 }
11231
11232 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011233}
11234
Jani Nikula65b38e02015-04-13 11:26:56 +030011235static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011236 .atomic_begin = intel_begin_crtc_commit,
11237 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011238 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011239};
11240
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011241static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11242{
11243 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010011244 struct drm_connector_list_iter conn_iter;
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011245
Daniel Vetterf9e905c2017-03-01 10:52:25 +010011246 drm_connector_list_iter_begin(dev, &conn_iter);
11247 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020011248 if (connector->base.state->crtc)
11249 drm_connector_unreference(&connector->base);
11250
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011251 if (connector->base.encoder) {
11252 connector->base.state->best_encoder =
11253 connector->base.encoder;
11254 connector->base.state->crtc =
11255 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020011256
11257 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011258 } else {
11259 connector->base.state->best_encoder = NULL;
11260 connector->base.state->crtc = NULL;
11261 }
11262 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010011263 drm_connector_list_iter_end(&conn_iter);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011264}
11265
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011266static void
Robin Schroereba905b2014-05-18 02:24:50 +020011267connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011268 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011269{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011270 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011271 int bpp = pipe_config->pipe_bpp;
11272
11273 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011274 connector->base.base.id,
11275 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011276
11277 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011278 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011279 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011280 bpp, info->bpc * 3);
11281 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011282 }
11283
Mario Kleiner196f9542016-07-06 12:05:45 +020011284 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011285 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020011286 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11287 bpp);
11288 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011289 }
11290}
11291
11292static int
11293compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011294 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011295{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011296 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011297 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011298 struct drm_connector *connector;
11299 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011300 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011301
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011302 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11303 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011304 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011305 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011306 bpp = 12*3;
11307 else
11308 bpp = 8*3;
11309
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011310
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011311 pipe_config->pipe_bpp = bpp;
11312
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011313 state = pipe_config->base.state;
11314
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011315 /* Clamp display bpp to EDID value */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011316 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011317 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011318 continue;
11319
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011320 connected_sink_compute_bpp(to_intel_connector(connector),
11321 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011322 }
11323
11324 return bpp;
11325}
11326
Daniel Vetter644db712013-09-19 14:53:58 +020011327static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11328{
11329 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11330 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011331 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011332 mode->crtc_hdisplay, mode->crtc_hsync_start,
11333 mode->crtc_hsync_end, mode->crtc_htotal,
11334 mode->crtc_vdisplay, mode->crtc_vsync_start,
11335 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11336}
11337
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011338static inline void
11339intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011340 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011341{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011342 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11343 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011344 m_n->gmch_m, m_n->gmch_n,
11345 m_n->link_m, m_n->link_n, m_n->tu);
11346}
11347
Daniel Vetterc0b03412013-05-28 12:05:54 +020011348static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011349 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011350 const char *context)
11351{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011352 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011353 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011354 struct drm_plane *plane;
11355 struct intel_plane *intel_plane;
11356 struct intel_plane_state *state;
11357 struct drm_framebuffer *fb;
11358
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000011359 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11360 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011361
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011362 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11363 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020011364 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011365
11366 if (pipe_config->has_pch_encoder)
11367 intel_dump_m_n_config(pipe_config, "fdi",
11368 pipe_config->fdi_lanes,
11369 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011370
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011371 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011372 intel_dump_m_n_config(pipe_config, "dp m_n",
11373 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000011374 if (pipe_config->has_drrs)
11375 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11376 pipe_config->lane_count,
11377 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011378 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011379
Daniel Vetter55072d12014-11-20 16:10:28 +010011380 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011381 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010011382
Daniel Vetterc0b03412013-05-28 12:05:54 +020011383 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011384 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011385 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011386 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11387 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011388 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011389 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011390 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11391 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011392
11393 if (INTEL_GEN(dev_priv) >= 9)
11394 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11395 crtc->num_scalers,
11396 pipe_config->scaler_state.scaler_users,
11397 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011398
11399 if (HAS_GMCH_DISPLAY(dev_priv))
11400 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11401 pipe_config->gmch_pfit.control,
11402 pipe_config->gmch_pfit.pgm_ratios,
11403 pipe_config->gmch_pfit.lvds_border_bits);
11404 else
11405 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11406 pipe_config->pch_pfit.pos,
11407 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000011408 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011409
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011410 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11411 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011412
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020011413 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011414
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011415 DRM_DEBUG_KMS("planes on this crtc\n");
11416 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011417 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011418 intel_plane = to_intel_plane(plane);
11419 if (intel_plane->pipe != crtc->pipe)
11420 continue;
11421
11422 state = to_intel_plane_state(plane->state);
11423 fb = state->base.fb;
11424 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030011425 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11426 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011427 continue;
11428 }
11429
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011430 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11431 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011432 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020011433 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011434 if (INTEL_GEN(dev_priv) >= 9)
11435 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11436 state->scaler_id,
11437 state->base.src.x1 >> 16,
11438 state->base.src.y1 >> 16,
11439 drm_rect_width(&state->base.src) >> 16,
11440 drm_rect_height(&state->base.src) >> 16,
11441 state->base.dst.x1, state->base.dst.y1,
11442 drm_rect_width(&state->base.dst),
11443 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011444 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011445}
11446
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011447static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011448{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011449 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011450 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011451 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011452 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011453
11454 /*
11455 * Walk the connector list instead of the encoder
11456 * list to detect the problem on ddi platforms
11457 * where there's just one encoder per digital port.
11458 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011459 drm_for_each_connector(connector, dev) {
11460 struct drm_connector_state *connector_state;
11461 struct intel_encoder *encoder;
11462
11463 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11464 if (!connector_state)
11465 connector_state = connector->state;
11466
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011467 if (!connector_state->best_encoder)
11468 continue;
11469
11470 encoder = to_intel_encoder(connector_state->best_encoder);
11471
11472 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011473
11474 switch (encoder->type) {
11475 unsigned int port_mask;
11476 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011477 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011478 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030011479 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011480 case INTEL_OUTPUT_HDMI:
11481 case INTEL_OUTPUT_EDP:
11482 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11483
11484 /* the same port mustn't appear more than once */
11485 if (used_ports & port_mask)
11486 return false;
11487
11488 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011489 break;
11490 case INTEL_OUTPUT_DP_MST:
11491 used_mst_ports |=
11492 1 << enc_to_mst(&encoder->base)->primary->port;
11493 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011494 default:
11495 break;
11496 }
11497 }
11498
Ville Syrjälä477321e2016-07-28 17:50:40 +030011499 /* can't mix MST and SST/HDMI on the same port */
11500 if (used_ports & used_mst_ports)
11501 return false;
11502
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011503 return true;
11504}
11505
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011506static void
11507clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11508{
Ville Syrjäläff32c542017-03-02 19:14:57 +020011509 struct drm_i915_private *dev_priv =
11510 to_i915(crtc_state->base.crtc->dev);
Chandra Konduru663a3642015-04-07 15:28:41 -070011511 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011512 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011513 struct intel_shared_dpll *shared_dpll;
Ville Syrjäläff32c542017-03-02 19:14:57 +020011514 struct intel_crtc_wm_state wm_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011515 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011516
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011517 /* FIXME: before the switch to atomic started, a new pipe_config was
11518 * kzalloc'd. Code that depends on any field being zero should be
11519 * fixed, so that the crtc_state can be safely duplicated. For now,
11520 * only fields that are know to not cause problems are preserved. */
11521
Chandra Konduru663a3642015-04-07 15:28:41 -070011522 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011523 shared_dpll = crtc_state->shared_dpll;
11524 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011525 force_thru = crtc_state->pch_pfit.force_thru;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011526 if (IS_G4X(dev_priv) ||
11527 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011528 wm_state = crtc_state->wm;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011529
Chris Wilsond2fa80a2017-03-03 15:46:44 +000011530 /* Keep base drm_crtc_state intact, only clear our extended struct */
11531 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11532 memset(&crtc_state->base + 1, 0,
11533 sizeof(*crtc_state) - sizeof(crtc_state->base));
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011534
Chandra Konduru663a3642015-04-07 15:28:41 -070011535 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011536 crtc_state->shared_dpll = shared_dpll;
11537 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011538 crtc_state->pch_pfit.force_thru = force_thru;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011539 if (IS_G4X(dev_priv) ||
11540 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011541 crtc_state->wm = wm_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011542}
11543
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011544static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011545intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011546 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011547{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011548 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011549 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011550 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011551 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011552 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011553 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011554 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011555
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011556 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011557
Daniel Vettere143a212013-07-04 12:01:15 +020011558 pipe_config->cpu_transcoder =
11559 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011560
Imre Deak2960bc92013-07-30 13:36:32 +030011561 /*
11562 * Sanitize sync polarity flags based on requested ones. If neither
11563 * positive or negative polarity is requested, treat this as meaning
11564 * negative polarity.
11565 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011566 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011567 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011568 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011569
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011570 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011571 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011572 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011573
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011574 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11575 pipe_config);
11576 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011577 goto fail;
11578
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011579 /*
11580 * Determine the real pipe dimensions. Note that stereo modes can
11581 * increase the actual pipe size due to the frame doubling and
11582 * insertion of additional space for blanks between the frame. This
11583 * is stored in the crtc timings. We use the requested mode to do this
11584 * computation to clearly distinguish it from the adjusted mode, which
11585 * can be changed by the connectors in the below retry loop.
11586 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010011587 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011588 &pipe_config->pipe_src_w,
11589 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011590
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011591 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011592 if (connector_state->crtc != crtc)
11593 continue;
11594
11595 encoder = to_intel_encoder(connector_state->best_encoder);
11596
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011597 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11598 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11599 goto fail;
11600 }
11601
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011602 /*
11603 * Determine output_types before calling the .compute_config()
11604 * hooks so that the hooks can use this information safely.
11605 */
11606 pipe_config->output_types |= 1 << encoder->type;
11607 }
11608
Daniel Vettere29c22c2013-02-21 00:00:16 +010011609encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011610 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011611 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011612 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011613
Daniel Vetter135c81b2013-07-21 21:37:09 +020011614 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011615 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11616 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011617
Daniel Vetter7758a112012-07-08 19:40:39 +020011618 /* Pass our mode to the connectors and the CRTC to give them a chance to
11619 * adjust it according to limitations or connector properties, and also
11620 * a chance to reject the mode entirely.
11621 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011622 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011623 if (connector_state->crtc != crtc)
11624 continue;
11625
11626 encoder = to_intel_encoder(connector_state->best_encoder);
11627
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011628 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011629 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011630 goto fail;
11631 }
11632 }
11633
Daniel Vetterff9a6752013-06-01 17:16:21 +020011634 /* Set default port clock if not overwritten by the encoder. Needs to be
11635 * done afterwards in case the encoder adjusts the mode. */
11636 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011637 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011638 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011639
Daniel Vettera43f6e02013-06-07 23:10:32 +020011640 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011641 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011642 DRM_DEBUG_KMS("CRTC fixup failed\n");
11643 goto fail;
11644 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011645
11646 if (ret == RETRY) {
11647 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11648 ret = -EINVAL;
11649 goto fail;
11650 }
11651
11652 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11653 retry = false;
11654 goto encoder_retry;
11655 }
11656
Daniel Vettere8fa4272015-08-12 11:43:34 +020011657 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011658 * only enable it on 6bpc panels and when its not a compliance
11659 * test requesting 6bpc video pattern.
11660 */
11661 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11662 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011663 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011664 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011665
Daniel Vetter7758a112012-07-08 19:40:39 +020011666fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011667 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011668}
11669
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011670static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020011671intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011672{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011673 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011674 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020011675 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011676
Ville Syrjälä76688512014-01-10 11:28:06 +020011677 /* Double check state. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011678 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11679 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020011680
Maarten Lankhorst61067a52015-09-23 16:29:36 +020011681 /*
11682 * Update legacy state to satisfy fbc code. This can
11683 * be removed when fbc uses the atomic state.
11684 */
11685 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11686 struct drm_plane_state *plane_state = crtc->primary->state;
11687
11688 crtc->primary->fb = plane_state->fb;
11689 crtc->x = plane_state->src_x >> 16;
11690 crtc->y = plane_state->src_y >> 16;
11691 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011692 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011693}
11694
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011695static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011696{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011697 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011698
11699 if (clock1 == clock2)
11700 return true;
11701
11702 if (!clock1 || !clock2)
11703 return false;
11704
11705 diff = abs(clock1 - clock2);
11706
11707 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11708 return true;
11709
11710 return false;
11711}
11712
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011713static bool
11714intel_compare_m_n(unsigned int m, unsigned int n,
11715 unsigned int m2, unsigned int n2,
11716 bool exact)
11717{
11718 if (m == m2 && n == n2)
11719 return true;
11720
11721 if (exact || !m || !n || !m2 || !n2)
11722 return false;
11723
11724 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11725
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011726 if (n > n2) {
11727 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011728 m2 <<= 1;
11729 n2 <<= 1;
11730 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011731 } else if (n < n2) {
11732 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011733 m <<= 1;
11734 n <<= 1;
11735 }
11736 }
11737
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011738 if (n != n2)
11739 return false;
11740
11741 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011742}
11743
11744static bool
11745intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11746 struct intel_link_m_n *m2_n2,
11747 bool adjust)
11748{
11749 if (m_n->tu == m2_n2->tu &&
11750 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11751 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11752 intel_compare_m_n(m_n->link_m, m_n->link_n,
11753 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11754 if (adjust)
11755 *m2_n2 = *m_n;
11756
11757 return true;
11758 }
11759
11760 return false;
11761}
11762
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011763static void __printf(3, 4)
11764pipe_config_err(bool adjust, const char *name, const char *format, ...)
11765{
11766 char *level;
11767 unsigned int category;
11768 struct va_format vaf;
11769 va_list args;
11770
11771 if (adjust) {
11772 level = KERN_DEBUG;
11773 category = DRM_UT_KMS;
11774 } else {
11775 level = KERN_ERR;
11776 category = DRM_UT_NONE;
11777 }
11778
11779 va_start(args, format);
11780 vaf.fmt = format;
11781 vaf.va = &args;
11782
11783 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11784
11785 va_end(args);
11786}
11787
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011788static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011789intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011790 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011791 struct intel_crtc_state *pipe_config,
11792 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011793{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011794 bool ret = true;
11795
Daniel Vetter66e985c2013-06-05 13:34:20 +020011796#define PIPE_CONF_CHECK_X(name) \
11797 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011798 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011799 "(expected 0x%08x, found 0x%08x)\n", \
11800 current_config->name, \
11801 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011802 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011803 }
11804
Daniel Vetter08a24032013-04-19 11:25:34 +020011805#define PIPE_CONF_CHECK_I(name) \
11806 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011807 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011808 "(expected %i, found %i)\n", \
11809 current_config->name, \
11810 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011811 ret = false; \
11812 }
11813
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011814#define PIPE_CONF_CHECK_P(name) \
11815 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011816 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011817 "(expected %p, found %p)\n", \
11818 current_config->name, \
11819 pipe_config->name); \
11820 ret = false; \
11821 }
11822
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011823#define PIPE_CONF_CHECK_M_N(name) \
11824 if (!intel_compare_link_m_n(&current_config->name, \
11825 &pipe_config->name,\
11826 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011827 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011828 "(expected tu %i gmch %i/%i link %i/%i, " \
11829 "found tu %i, gmch %i/%i link %i/%i)\n", \
11830 current_config->name.tu, \
11831 current_config->name.gmch_m, \
11832 current_config->name.gmch_n, \
11833 current_config->name.link_m, \
11834 current_config->name.link_n, \
11835 pipe_config->name.tu, \
11836 pipe_config->name.gmch_m, \
11837 pipe_config->name.gmch_n, \
11838 pipe_config->name.link_m, \
11839 pipe_config->name.link_n); \
11840 ret = false; \
11841 }
11842
Daniel Vetter55c561a2016-03-30 11:34:36 +020011843/* This is required for BDW+ where there is only one set of registers for
11844 * switching between high and low RR.
11845 * This macro can be used whenever a comparison has to be made between one
11846 * hw state and multiple sw state variables.
11847 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011848#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11849 if (!intel_compare_link_m_n(&current_config->name, \
11850 &pipe_config->name, adjust) && \
11851 !intel_compare_link_m_n(&current_config->alt_name, \
11852 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011853 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011854 "(expected tu %i gmch %i/%i link %i/%i, " \
11855 "or tu %i gmch %i/%i link %i/%i, " \
11856 "found tu %i, gmch %i/%i link %i/%i)\n", \
11857 current_config->name.tu, \
11858 current_config->name.gmch_m, \
11859 current_config->name.gmch_n, \
11860 current_config->name.link_m, \
11861 current_config->name.link_n, \
11862 current_config->alt_name.tu, \
11863 current_config->alt_name.gmch_m, \
11864 current_config->alt_name.gmch_n, \
11865 current_config->alt_name.link_m, \
11866 current_config->alt_name.link_n, \
11867 pipe_config->name.tu, \
11868 pipe_config->name.gmch_m, \
11869 pipe_config->name.gmch_n, \
11870 pipe_config->name.link_m, \
11871 pipe_config->name.link_n); \
11872 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011873 }
11874
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011875#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11876 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011877 pipe_config_err(adjust, __stringify(name), \
11878 "(%x) (expected %i, found %i)\n", \
11879 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011880 current_config->name & (mask), \
11881 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011882 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011883 }
11884
Ville Syrjälä5e550652013-09-06 23:29:07 +030011885#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11886 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011887 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011888 "(expected %i, found %i)\n", \
11889 current_config->name, \
11890 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011891 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011892 }
11893
Daniel Vetterbb760062013-06-06 14:55:52 +020011894#define PIPE_CONF_QUIRK(quirk) \
11895 ((current_config->quirks | pipe_config->quirks) & (quirk))
11896
Daniel Vettereccb1402013-05-22 00:50:22 +020011897 PIPE_CONF_CHECK_I(cpu_transcoder);
11898
Daniel Vetter08a24032013-04-19 11:25:34 +020011899 PIPE_CONF_CHECK_I(has_pch_encoder);
11900 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011901 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011902
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011903 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011904 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011905
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011906 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011907 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011908
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011909 if (current_config->has_drrs)
11910 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11911 } else
11912 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011913
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011914 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011915
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011916 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11917 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11918 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11919 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11920 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11921 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011922
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011923 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11924 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11925 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11926 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11927 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11928 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011929
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011930 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020011931 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011932 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011933 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011934 PIPE_CONF_CHECK_I(limited_color_range);
Shashank Sharma15953632017-03-13 16:54:03 +053011935
11936 PIPE_CONF_CHECK_I(hdmi_scrambling);
11937 PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
Jesse Barnese43823e2014-11-05 14:26:08 -080011938 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011939
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011940 PIPE_CONF_CHECK_I(has_audio);
11941
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011942 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011943 DRM_MODE_FLAG_INTERLACE);
11944
Daniel Vetterbb760062013-06-06 14:55:52 +020011945 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011946 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011947 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011948 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011949 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011950 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011951 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011952 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011953 DRM_MODE_FLAG_NVSYNC);
11954 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011955
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011956 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011957 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011958 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011959 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011960 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011961
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011962 if (!adjust) {
11963 PIPE_CONF_CHECK_I(pipe_src_w);
11964 PIPE_CONF_CHECK_I(pipe_src_h);
11965
11966 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11967 if (current_config->pch_pfit.enabled) {
11968 PIPE_CONF_CHECK_X(pch_pfit.pos);
11969 PIPE_CONF_CHECK_X(pch_pfit.size);
11970 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011971
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011972 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011973 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011974 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011975
Jesse Barnese59150d2014-01-07 13:30:45 -080011976 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011977 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080011978 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011979
Ville Syrjälä282740f2013-09-04 18:30:03 +030011980 PIPE_CONF_CHECK_I(double_wide);
11981
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011982 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011983 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011984 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011985 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11986 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011987 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011988 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011989 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11990 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11991 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011992
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011993 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11994 PIPE_CONF_CHECK_X(dsi_pll.div);
11995
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011996 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011997 PIPE_CONF_CHECK_I(pipe_bpp);
11998
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011999 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012000 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012001
Daniel Vetter66e985c2013-06-05 13:34:20 +020012002#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012003#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012004#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012005#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012006#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012007#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012008
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012009 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012010}
12011
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012012static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12013 const struct intel_crtc_state *pipe_config)
12014{
12015 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012016 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012017 &pipe_config->fdi_m_n);
12018 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12019
12020 /*
12021 * FDI already provided one idea for the dotclock.
12022 * Yell if the encoder disagrees.
12023 */
12024 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12025 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12026 fdi_dotclock, dotclock);
12027 }
12028}
12029
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012030static void verify_wm_state(struct drm_crtc *crtc,
12031 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012032{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012033 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012034 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012035 struct skl_pipe_wm hw_wm, *sw_wm;
12036 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
12037 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12039 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012040 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000012041
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012042 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012043 return;
12044
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012045 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020012046 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012047
Damien Lespiau08db6652014-11-04 17:06:52 +000012048 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12049 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12050
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012051 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070012052 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012053 hw_plane_wm = &hw_wm.planes[plane];
12054 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012055
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012056 /* Watermarks */
12057 for (level = 0; level <= max_level; level++) {
12058 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12059 &sw_plane_wm->wm[level]))
12060 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000012061
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012062 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12063 pipe_name(pipe), plane + 1, level,
12064 sw_plane_wm->wm[level].plane_en,
12065 sw_plane_wm->wm[level].plane_res_b,
12066 sw_plane_wm->wm[level].plane_res_l,
12067 hw_plane_wm->wm[level].plane_en,
12068 hw_plane_wm->wm[level].plane_res_b,
12069 hw_plane_wm->wm[level].plane_res_l);
12070 }
12071
12072 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12073 &sw_plane_wm->trans_wm)) {
12074 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12075 pipe_name(pipe), plane + 1,
12076 sw_plane_wm->trans_wm.plane_en,
12077 sw_plane_wm->trans_wm.plane_res_b,
12078 sw_plane_wm->trans_wm.plane_res_l,
12079 hw_plane_wm->trans_wm.plane_en,
12080 hw_plane_wm->trans_wm.plane_res_b,
12081 hw_plane_wm->trans_wm.plane_res_l);
12082 }
12083
12084 /* DDB */
12085 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
12086 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
12087
12088 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040012089 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012090 pipe_name(pipe), plane + 1,
12091 sw_ddb_entry->start, sw_ddb_entry->end,
12092 hw_ddb_entry->start, hw_ddb_entry->end);
12093 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012094 }
12095
Lyude27082492016-08-24 07:48:10 +020012096 /*
12097 * cursor
12098 * If the cursor plane isn't active, we may not have updated it's ddb
12099 * allocation. In that case since the ddb allocation will be updated
12100 * once the plane becomes visible, we can skip this check
12101 */
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030012102 if (1) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012103 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
12104 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012105
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012106 /* Watermarks */
12107 for (level = 0; level <= max_level; level++) {
12108 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12109 &sw_plane_wm->wm[level]))
12110 continue;
12111
12112 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12113 pipe_name(pipe), level,
12114 sw_plane_wm->wm[level].plane_en,
12115 sw_plane_wm->wm[level].plane_res_b,
12116 sw_plane_wm->wm[level].plane_res_l,
12117 hw_plane_wm->wm[level].plane_en,
12118 hw_plane_wm->wm[level].plane_res_b,
12119 hw_plane_wm->wm[level].plane_res_l);
12120 }
12121
12122 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12123 &sw_plane_wm->trans_wm)) {
12124 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12125 pipe_name(pipe),
12126 sw_plane_wm->trans_wm.plane_en,
12127 sw_plane_wm->trans_wm.plane_res_b,
12128 sw_plane_wm->trans_wm.plane_res_l,
12129 hw_plane_wm->trans_wm.plane_en,
12130 hw_plane_wm->trans_wm.plane_res_b,
12131 hw_plane_wm->trans_wm.plane_res_l);
12132 }
12133
12134 /* DDB */
12135 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12136 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12137
12138 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040012139 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020012140 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040012141 sw_ddb_entry->start, sw_ddb_entry->end,
12142 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020012143 }
Damien Lespiau08db6652014-11-04 17:06:52 +000012144 }
12145}
12146
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012147static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012148verify_connector_state(struct drm_device *dev,
12149 struct drm_atomic_state *state,
12150 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012151{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012152 struct drm_connector *connector;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012153 struct drm_connector_state *new_conn_state;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012154 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012155
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012156 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012157 struct drm_encoder *encoder = connector->encoder;
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020012158 struct drm_crtc_state *crtc_state = NULL;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012159
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012160 if (new_conn_state->crtc != crtc)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012161 continue;
12162
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020012163 if (crtc)
12164 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
12165
12166 intel_connector_verify_state(crtc_state, new_conn_state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012167
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012168 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012169 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012170 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012171}
12172
12173static void
Daniel Vetter86b04262017-03-01 10:52:26 +010012174verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012175{
12176 struct intel_encoder *encoder;
Daniel Vetter86b04262017-03-01 10:52:26 +010012177 struct drm_connector *connector;
12178 struct drm_connector_state *old_conn_state, *new_conn_state;
12179 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012180
Damien Lespiaub2784e12014-08-05 11:29:37 +010012181 for_each_intel_encoder(dev, encoder) {
Daniel Vetter86b04262017-03-01 10:52:26 +010012182 bool enabled = false, found = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012183 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012184
12185 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12186 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012187 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012188
Daniel Vetter86b04262017-03-01 10:52:26 +010012189 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
12190 new_conn_state, i) {
12191 if (old_conn_state->best_encoder == &encoder->base)
12192 found = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012193
Daniel Vetter86b04262017-03-01 10:52:26 +010012194 if (new_conn_state->best_encoder != &encoder->base)
12195 continue;
12196 found = enabled = true;
12197
12198 I915_STATE_WARN(new_conn_state->crtc !=
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012199 encoder->base.crtc,
12200 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012201 }
Daniel Vetter86b04262017-03-01 10:52:26 +010012202
12203 if (!found)
12204 continue;
Dave Airlie0e32b392014-05-02 14:02:48 +100012205
Rob Clarke2c719b2014-12-15 13:56:32 -050012206 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012207 "encoder's enabled state mismatch "
12208 "(expected %i, found %i)\n",
12209 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012210
12211 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012212 bool active;
12213
12214 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012215 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012216 "encoder detached but still enabled on pipe %c.\n",
12217 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012218 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012219 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012220}
12221
12222static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012223verify_crtc_state(struct drm_crtc *crtc,
12224 struct drm_crtc_state *old_crtc_state,
12225 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012226{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012227 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012228 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012229 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12231 struct intel_crtc_state *pipe_config, *sw_config;
12232 struct drm_atomic_state *old_state;
12233 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012234
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012235 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020012236 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012237 pipe_config = to_intel_crtc_state(old_crtc_state);
12238 memset(pipe_config, 0, sizeof(*pipe_config));
12239 pipe_config->base.crtc = crtc;
12240 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012241
Ville Syrjälä78108b72016-05-27 20:59:19 +030012242 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012243
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012244 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012245
Ville Syrjäläe56134b2017-06-01 17:36:19 +030012246 /* we keep both pipes enabled on 830 */
12247 if (IS_I830(dev_priv))
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012248 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012249
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012250 I915_STATE_WARN(new_crtc_state->active != active,
12251 "crtc active state doesn't match with hw state "
12252 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012253
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012254 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12255 "transitional active state does not match atomic hw state "
12256 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012257
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012258 for_each_encoder_on_crtc(dev, crtc, encoder) {
12259 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012260
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012261 active = encoder->get_hw_state(encoder, &pipe);
12262 I915_STATE_WARN(active != new_crtc_state->active,
12263 "[ENCODER:%i] active %i with crtc active %i\n",
12264 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012265
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012266 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12267 "Encoder connected to wrong pipe %c\n",
12268 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012269
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012270 if (active) {
12271 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012272 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012273 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012274 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012275
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020012276 intel_crtc_compute_pixel_rate(pipe_config);
12277
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012278 if (!new_crtc_state->active)
12279 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012280
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012281 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012282
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020012283 sw_config = to_intel_crtc_state(new_crtc_state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012284 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012285 pipe_config, false)) {
12286 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12287 intel_dump_pipe_config(intel_crtc, pipe_config,
12288 "[hw state]");
12289 intel_dump_pipe_config(intel_crtc, sw_config,
12290 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012291 }
12292}
12293
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012294static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012295verify_single_dpll_state(struct drm_i915_private *dev_priv,
12296 struct intel_shared_dpll *pll,
12297 struct drm_crtc *crtc,
12298 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012299{
12300 struct intel_dpll_hw_state dpll_hw_state;
12301 unsigned crtc_mask;
12302 bool active;
12303
12304 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12305
12306 DRM_DEBUG_KMS("%s\n", pll->name);
12307
12308 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12309
12310 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12311 I915_STATE_WARN(!pll->on && pll->active_mask,
12312 "pll in active use but not on in sw tracking\n");
12313 I915_STATE_WARN(pll->on && !pll->active_mask,
12314 "pll is on but not used by any active crtc\n");
12315 I915_STATE_WARN(pll->on != active,
12316 "pll on state mismatch (expected %i, found %i)\n",
12317 pll->on, active);
12318 }
12319
12320 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012321 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012322 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012323 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012324
12325 return;
12326 }
12327
12328 crtc_mask = 1 << drm_crtc_index(crtc);
12329
12330 if (new_state->active)
12331 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12332 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12333 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12334 else
12335 I915_STATE_WARN(pll->active_mask & crtc_mask,
12336 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12337 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12338
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012339 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012340 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012341 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012342
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012343 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012344 &dpll_hw_state,
12345 sizeof(dpll_hw_state)),
12346 "pll hw state mismatch\n");
12347}
12348
12349static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012350verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12351 struct drm_crtc_state *old_crtc_state,
12352 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012353{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012354 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012355 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12356 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12357
12358 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012359 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012360
12361 if (old_state->shared_dpll &&
12362 old_state->shared_dpll != new_state->shared_dpll) {
12363 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12364 struct intel_shared_dpll *pll = old_state->shared_dpll;
12365
12366 I915_STATE_WARN(pll->active_mask & crtc_mask,
12367 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12368 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012369 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012370 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12371 pipe_name(drm_crtc_index(crtc)));
12372 }
12373}
12374
12375static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012376intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012377 struct drm_atomic_state *state,
12378 struct drm_crtc_state *old_state,
12379 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012380{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012381 if (!needs_modeset(new_state) &&
12382 !to_intel_crtc_state(new_state)->update_pipe)
12383 return;
12384
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012385 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012386 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012387 verify_crtc_state(crtc, old_state, new_state);
12388 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012389}
12390
12391static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012392verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012393{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012394 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012395 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012396
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012397 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012398 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012399}
Daniel Vetter53589012013-06-05 13:34:16 +020012400
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012401static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012402intel_modeset_verify_disabled(struct drm_device *dev,
12403 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012404{
Daniel Vetter86b04262017-03-01 10:52:26 +010012405 verify_encoder_state(dev, state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012406 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012407 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020012408}
12409
Ville Syrjälä80715b22014-05-15 20:23:23 +030012410static void update_scanline_offset(struct intel_crtc *crtc)
12411{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012412 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012413
12414 /*
12415 * The scanline counter increments at the leading edge of hsync.
12416 *
12417 * On most platforms it starts counting from vtotal-1 on the
12418 * first active line. That means the scanline counter value is
12419 * always one less than what we would expect. Ie. just after
12420 * start of vblank, which also occurs at start of hsync (on the
12421 * last active line), the scanline counter will read vblank_start-1.
12422 *
12423 * On gen2 the scanline counter starts counting from 1 instead
12424 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12425 * to keep the value positive), instead of adding one.
12426 *
12427 * On HSW+ the behaviour of the scanline counter depends on the output
12428 * type. For DP ports it behaves like most other platforms, but on HDMI
12429 * there's an extra 1 line difference. So we need to add two instead of
12430 * one to the value.
Ville Syrjäläec1b4ee2016-12-15 19:47:34 +020012431 *
12432 * On VLV/CHV DSI the scanline counter would appear to increment
12433 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12434 * that means we can't tell whether we're in vblank or not while
12435 * we're on that particular line. We must still set scanline_offset
12436 * to 1 so that the vblank timestamps come out correct when we query
12437 * the scanline counter from within the vblank interrupt handler.
12438 * However if queried just before the start of vblank we'll get an
12439 * answer that's slightly in the future.
Ville Syrjälä80715b22014-05-15 20:23:23 +030012440 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012441 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012442 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012443 int vtotal;
12444
Ville Syrjälä124abe02015-09-08 13:40:45 +030012445 vtotal = adjusted_mode->crtc_vtotal;
12446 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012447 vtotal /= 2;
12448
12449 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012450 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030012451 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012452 crtc->scanline_offset = 2;
12453 } else
12454 crtc->scanline_offset = 1;
12455}
12456
Maarten Lankhorstad421372015-06-15 12:33:42 +020012457static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012458{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012459 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012460 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012461 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012462 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012463 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012464
12465 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012466 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012467
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012468 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012470 struct intel_shared_dpll *old_dpll =
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012471 to_intel_crtc_state(old_crtc_state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012472
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012473 if (!needs_modeset(new_crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012474 continue;
12475
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012476 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012477
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012478 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012479 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012480
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020012481 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012482 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012483}
12484
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012485/*
12486 * This implements the workaround described in the "notes" section of the mode
12487 * set sequence documentation. When going from no pipes or single pipe to
12488 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12489 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12490 */
12491static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12492{
12493 struct drm_crtc_state *crtc_state;
12494 struct intel_crtc *intel_crtc;
12495 struct drm_crtc *crtc;
12496 struct intel_crtc_state *first_crtc_state = NULL;
12497 struct intel_crtc_state *other_crtc_state = NULL;
12498 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12499 int i;
12500
12501 /* look at all crtc's that are going to be enabled in during modeset */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012502 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012503 intel_crtc = to_intel_crtc(crtc);
12504
12505 if (!crtc_state->active || !needs_modeset(crtc_state))
12506 continue;
12507
12508 if (first_crtc_state) {
12509 other_crtc_state = to_intel_crtc_state(crtc_state);
12510 break;
12511 } else {
12512 first_crtc_state = to_intel_crtc_state(crtc_state);
12513 first_pipe = intel_crtc->pipe;
12514 }
12515 }
12516
12517 /* No workaround needed? */
12518 if (!first_crtc_state)
12519 return 0;
12520
12521 /* w/a possibly needed, check how many crtc's are already enabled. */
12522 for_each_intel_crtc(state->dev, intel_crtc) {
12523 struct intel_crtc_state *pipe_config;
12524
12525 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12526 if (IS_ERR(pipe_config))
12527 return PTR_ERR(pipe_config);
12528
12529 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12530
12531 if (!pipe_config->base.active ||
12532 needs_modeset(&pipe_config->base))
12533 continue;
12534
12535 /* 2 or more enabled crtcs means no need for w/a */
12536 if (enabled_pipe != INVALID_PIPE)
12537 return 0;
12538
12539 enabled_pipe = intel_crtc->pipe;
12540 }
12541
12542 if (enabled_pipe != INVALID_PIPE)
12543 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12544 else if (other_crtc_state)
12545 other_crtc_state->hsw_workaround_pipe = first_pipe;
12546
12547 return 0;
12548}
12549
Ville Syrjälä8d965612016-11-14 18:35:10 +020012550static int intel_lock_all_pipes(struct drm_atomic_state *state)
12551{
12552 struct drm_crtc *crtc;
12553
12554 /* Add all pipes to the state */
12555 for_each_crtc(state->dev, crtc) {
12556 struct drm_crtc_state *crtc_state;
12557
12558 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12559 if (IS_ERR(crtc_state))
12560 return PTR_ERR(crtc_state);
12561 }
12562
12563 return 0;
12564}
12565
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012566static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12567{
12568 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012569
Ville Syrjälä8d965612016-11-14 18:35:10 +020012570 /*
12571 * Add all pipes to the state, and force
12572 * a modeset on all the active ones.
12573 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012574 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012575 struct drm_crtc_state *crtc_state;
12576 int ret;
12577
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012578 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12579 if (IS_ERR(crtc_state))
12580 return PTR_ERR(crtc_state);
12581
12582 if (!crtc_state->active || needs_modeset(crtc_state))
12583 continue;
12584
12585 crtc_state->mode_changed = true;
12586
12587 ret = drm_atomic_add_affected_connectors(state, crtc);
12588 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012589 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012590
12591 ret = drm_atomic_add_affected_planes(state, crtc);
12592 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012593 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012594 }
12595
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012596 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012597}
12598
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012599static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012600{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012601 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012602 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012603 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012604 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012605 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012606
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012607 if (!check_digital_port_conflicts(state)) {
12608 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12609 return -EINVAL;
12610 }
12611
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012612 intel_state->modeset = true;
12613 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012614 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12615 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012616
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012617 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12618 if (new_crtc_state->active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012619 intel_state->active_crtcs |= 1 << i;
12620 else
12621 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012622
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012623 if (old_crtc_state->active != new_crtc_state->active)
Matt Roper8b4a7d02016-05-12 07:06:00 -070012624 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012625 }
12626
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012627 /*
12628 * See if the config requires any additional preparation, e.g.
12629 * to adjust global state with pipes off. We need to do this
12630 * here so we can get the modeset_pipe updated config for the new
12631 * mode set on this crtc. For other crtcs we need to use the
12632 * adjusted_mode bits in the crtc directly.
12633 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012634 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012635 ret = dev_priv->display.modeset_calc_cdclk(state);
12636 if (ret < 0)
12637 return ret;
12638
Ville Syrjälä8d965612016-11-14 18:35:10 +020012639 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012640 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012641 * holding all the crtc locks, even if we don't end up
12642 * touching the hardware
12643 */
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012644 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12645 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012646 ret = intel_lock_all_pipes(state);
12647 if (ret < 0)
12648 return ret;
12649 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012650
Ville Syrjälä8d965612016-11-14 18:35:10 +020012651 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012652 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12653 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012654 ret = intel_modeset_all_pipes(state);
12655 if (ret < 0)
12656 return ret;
12657 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012658
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012659 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12660 intel_state->cdclk.logical.cdclk,
12661 intel_state->cdclk.actual.cdclk);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012662 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012663 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012664 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012665
Maarten Lankhorstad421372015-06-15 12:33:42 +020012666 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012667
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012668 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012669 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012670
Maarten Lankhorstad421372015-06-15 12:33:42 +020012671 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012672}
12673
Matt Roperaa363132015-09-24 15:53:18 -070012674/*
12675 * Handle calculation of various watermark data at the end of the atomic check
12676 * phase. The code here should be run after the per-crtc and per-plane 'check'
12677 * handlers to ensure that all derived state has been updated.
12678 */
Matt Roper55994c22016-05-12 07:06:08 -070012679static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012680{
12681 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012682 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012683
12684 /* Is there platform-specific watermark information to calculate? */
12685 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012686 return dev_priv->display.compute_global_watermarks(state);
12687
12688 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012689}
12690
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012691/**
12692 * intel_atomic_check - validate state object
12693 * @dev: drm device
12694 * @state: state to validate
12695 */
12696static int intel_atomic_check(struct drm_device *dev,
12697 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012698{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012699 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012700 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012701 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012702 struct drm_crtc_state *old_crtc_state, *crtc_state;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012703 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012704 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012705
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012706 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012707 if (ret)
12708 return ret;
12709
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012710 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012711 struct intel_crtc_state *pipe_config =
12712 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012713
12714 /* Catch I915_MODE_FLAG_INHERITED */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012715 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012716 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012717
Daniel Vetter26495482015-07-15 14:15:52 +020012718 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012719 continue;
12720
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012721 if (!crtc_state->enable) {
12722 any_ms = true;
12723 continue;
12724 }
12725
Daniel Vetter26495482015-07-15 14:15:52 +020012726 /* FIXME: For only active_changed we shouldn't need to do any
12727 * state recomputation at all. */
12728
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012729 ret = drm_atomic_add_affected_connectors(state, crtc);
12730 if (ret)
12731 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012732
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012733 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012734 if (ret) {
12735 intel_dump_pipe_config(to_intel_crtc(crtc),
12736 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012737 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012738 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012739
Jani Nikula73831232015-11-19 10:26:30 +020012740 if (i915.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012741 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012742 to_intel_crtc_state(old_crtc_state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012743 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012744 crtc_state->mode_changed = false;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012745 pipe_config->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012746 }
12747
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012748 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012749 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012750
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012751 ret = drm_atomic_add_affected_planes(state, crtc);
12752 if (ret)
12753 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012754
Daniel Vetter26495482015-07-15 14:15:52 +020012755 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12756 needs_modeset(crtc_state) ?
12757 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012758 }
12759
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012760 if (any_ms) {
12761 ret = intel_modeset_checks(state);
12762
12763 if (ret)
12764 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012765 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012766 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012767 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012768
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012769 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012770 if (ret)
12771 return ret;
12772
Paulo Zanonif51be2e2016-01-19 11:35:50 -020012773 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070012774 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012775}
12776
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012777static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012778 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012779{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012780 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012781 struct drm_crtc_state *crtc_state;
12782 struct drm_crtc *crtc;
12783 int i, ret;
12784
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012785 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012786 if (state->legacy_cursor_update)
12787 continue;
12788
12789 ret = intel_crtc_wait_for_pending_flips(crtc);
12790 if (ret)
12791 return ret;
12792
12793 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12794 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012795 }
12796
Maarten Lankhorstf9356752015-08-18 13:40:05 +020012797 ret = mutex_lock_interruptible(&dev->struct_mutex);
12798 if (ret)
12799 return ret;
12800
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012801 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010012802 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012803
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012804 return ret;
12805}
12806
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012807u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12808{
12809 struct drm_device *dev = crtc->base.dev;
12810
12811 if (!dev->max_vblank_count)
12812 return drm_accurate_vblank_count(&crtc->base);
12813
12814 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12815}
12816
Daniel Vetter5a21b662016-05-24 17:13:53 +020012817static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12818 struct drm_i915_private *dev_priv,
12819 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010012820{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012821 unsigned last_vblank_count[I915_MAX_PIPES];
12822 enum pipe pipe;
12823 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012824
Daniel Vetter5a21b662016-05-24 17:13:53 +020012825 if (!crtc_mask)
12826 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012827
Daniel Vetter5a21b662016-05-24 17:13:53 +020012828 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020012829 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12830 pipe);
Maarten Lankhorste8861672016-02-24 11:24:26 +010012831
Daniel Vetter5a21b662016-05-24 17:13:53 +020012832 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010012833 continue;
12834
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012835 ret = drm_crtc_vblank_get(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012836 if (WARN_ON(ret != 0)) {
12837 crtc_mask &= ~(1 << pipe);
12838 continue;
12839 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012840
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012841 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012842 }
12843
12844 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020012845 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12846 pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012847 long lret;
12848
12849 if (!((1 << pipe) & crtc_mask))
12850 continue;
12851
12852 lret = wait_event_timeout(dev->vblank[pipe].queue,
12853 last_vblank_count[pipe] !=
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012854 drm_crtc_vblank_count(&crtc->base),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012855 msecs_to_jiffies(50));
12856
12857 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
12858
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012859 drm_crtc_vblank_put(&crtc->base);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012860 }
12861}
12862
Daniel Vetter5a21b662016-05-24 17:13:53 +020012863static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012864{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012865 /* fb updated, need to unpin old fb */
12866 if (crtc_state->fb_changed)
12867 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012868
Daniel Vetter5a21b662016-05-24 17:13:53 +020012869 /* wm changes, need vblank before final wm's */
12870 if (crtc_state->update_wm_post)
12871 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012872
Ville Syrjälä5eeb7982017-03-02 19:15:00 +020012873 if (crtc_state->wm.need_postvbl_update)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012874 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012875
Daniel Vetter5a21b662016-05-24 17:13:53 +020012876 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012877}
12878
Lyude896e5bb2016-08-24 07:48:09 +020012879static void intel_update_crtc(struct drm_crtc *crtc,
12880 struct drm_atomic_state *state,
12881 struct drm_crtc_state *old_crtc_state,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012882 struct drm_crtc_state *new_crtc_state,
Lyude896e5bb2016-08-24 07:48:09 +020012883 unsigned int *crtc_vblank_mask)
12884{
12885 struct drm_device *dev = crtc->dev;
12886 struct drm_i915_private *dev_priv = to_i915(dev);
12887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012888 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12889 bool modeset = needs_modeset(new_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012890
12891 if (modeset) {
12892 update_scanline_offset(intel_crtc);
12893 dev_priv->display.crtc_enable(pipe_config, state);
12894 } else {
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012895 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12896 pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012897 }
12898
12899 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12900 intel_fbc_enable(
12901 intel_crtc, pipe_config,
12902 to_intel_plane_state(crtc->primary->state));
12903 }
12904
12905 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12906
12907 if (needs_vblank_wait(pipe_config))
12908 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12909}
12910
12911static void intel_update_crtcs(struct drm_atomic_state *state,
12912 unsigned int *crtc_vblank_mask)
12913{
12914 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012915 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyude896e5bb2016-08-24 07:48:09 +020012916 int i;
12917
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012918 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12919 if (!new_crtc_state->active)
Lyude896e5bb2016-08-24 07:48:09 +020012920 continue;
12921
12922 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012923 new_crtc_state, crtc_vblank_mask);
Lyude896e5bb2016-08-24 07:48:09 +020012924 }
12925}
12926
Lyude27082492016-08-24 07:48:10 +020012927static void skl_update_crtcs(struct drm_atomic_state *state,
12928 unsigned int *crtc_vblank_mask)
12929{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012930 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012931 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12932 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012933 struct intel_crtc *intel_crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012934 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012935 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012936 unsigned int updated = 0;
12937 bool progress;
12938 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012939 int i;
12940
12941 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12942
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012943 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012944 /* ignore allocations for crtc's that have been turned off. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012945 if (new_crtc_state->active)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012946 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012947
12948 /*
12949 * Whenever the number of active pipes changes, we need to make sure we
12950 * update the pipes in the right order so that their ddb allocations
12951 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12952 * cause pipe underruns and other bad stuff.
12953 */
12954 do {
Lyude27082492016-08-24 07:48:10 +020012955 progress = false;
12956
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012957 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Lyude27082492016-08-24 07:48:10 +020012958 bool vbl_wait = false;
12959 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012960
12961 intel_crtc = to_intel_crtc(crtc);
12962 cstate = to_intel_crtc_state(crtc->state);
12963 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012964
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012965 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012966 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012967
12968 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
Lyude27082492016-08-24 07:48:10 +020012969 continue;
12970
12971 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012972 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012973
12974 /*
12975 * If this is an already active pipe, it's DDB changed,
12976 * and this isn't the last pipe that needs updating
12977 * then we need to wait for a vblank to pass for the
12978 * new ddb allocation to take effect.
12979 */
Lyudece0ba282016-09-15 10:46:35 -040012980 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012981 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012982 !new_crtc_state->active_changed &&
Lyude27082492016-08-24 07:48:10 +020012983 intel_state->wm_results.dirty_pipes != updated)
12984 vbl_wait = true;
12985
12986 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012987 new_crtc_state, crtc_vblank_mask);
Lyude27082492016-08-24 07:48:10 +020012988
12989 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012990 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012991
12992 progress = true;
12993 }
12994 } while (progress);
12995}
12996
Chris Wilsonba318c62017-02-02 20:47:41 +000012997static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12998{
12999 struct intel_atomic_state *state, *next;
13000 struct llist_node *freed;
13001
13002 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
13003 llist_for_each_entry_safe(state, next, freed, freed)
13004 drm_atomic_state_put(&state->base);
13005}
13006
13007static void intel_atomic_helper_free_state_worker(struct work_struct *work)
13008{
13009 struct drm_i915_private *dev_priv =
13010 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
13011
13012 intel_atomic_helper_free_state(dev_priv);
13013}
13014
Daniel Vetter94f05022016-06-14 18:01:00 +020013015static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013016{
Daniel Vetter94f05022016-06-14 18:01:00 +020013017 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013018 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013019 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013020 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013021 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013022 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013023 bool hw_check = intel_state->modeset;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020013024 u64 put_domains[I915_MAX_PIPES] = {};
Daniel Vetter5a21b662016-05-24 17:13:53 +020013025 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010013026 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020013027
Daniel Vetterea0000f2016-06-13 16:13:46 +020013028 drm_atomic_helper_wait_for_dependencies(state);
13029
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013030 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013031 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013032
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013033 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13035
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013036 if (needs_modeset(new_crtc_state) ||
13037 to_intel_crtc_state(new_crtc_state)->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020013038 hw_check = true;
13039
13040 put_domains[to_intel_crtc(crtc)->pipe] =
13041 modeset_get_crtc_power_domains(crtc,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013042 to_intel_crtc_state(new_crtc_state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020013043 }
13044
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013045 if (!needs_modeset(new_crtc_state))
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013046 continue;
13047
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013048 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
13049 to_intel_crtc_state(new_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013050
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013051 if (old_crtc_state->active) {
13052 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020013053 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013054 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013055 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013056 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013057
13058 /*
13059 * Underruns don't always raise
13060 * interrupts, so check manually.
13061 */
13062 intel_check_cpu_fifo_underruns(dev_priv);
13063 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013064
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013065 if (!crtc->state->active) {
13066 /*
13067 * Make sure we don't call initial_watermarks
13068 * for ILK-style watermark updates.
Ville Syrjäläff32c542017-03-02 19:14:57 +020013069 *
13070 * No clue what this is supposed to achieve.
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013071 */
Ville Syrjäläff32c542017-03-02 19:14:57 +020013072 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013073 dev_priv->display.initial_watermarks(intel_state,
13074 to_intel_crtc_state(crtc->state));
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013075 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013076 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013077 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013078
Daniel Vetterea9d7582012-07-10 10:42:52 +020013079 /* Only after disabling all output pipelines that will be changed can we
13080 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013081 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013082
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013083 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013084 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013085
Ville Syrjäläb0587e42017-01-26 21:52:01 +020013086 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013087
Lyude656d1b82016-08-17 15:55:54 -040013088 /*
13089 * SKL workaround: bspec recommends we disable the SAGV when we
13090 * have more then one pipe enabled
13091 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030013092 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030013093 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040013094
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013095 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013096 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013097
Lyude896e5bb2016-08-24 07:48:09 +020013098 /* Complete the events for pipes that have now been disabled */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013099 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13100 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013101
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013102 /* Complete events for now disable pipes here. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013103 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013104 spin_lock_irq(&dev->event_lock);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013105 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013106 spin_unlock_irq(&dev->event_lock);
13107
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013108 new_crtc_state->event = NULL;
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013109 }
Matt Ropered4a6a72016-02-23 17:20:13 -080013110 }
13111
Lyude896e5bb2016-08-24 07:48:09 +020013112 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
13113 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
13114
Daniel Vetter94f05022016-06-14 18:01:00 +020013115 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13116 * already, but still need the state for the delayed optimization. To
13117 * fix this:
13118 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13119 * - schedule that vblank worker _before_ calling hw_done
13120 * - at the start of commit_tail, cancel it _synchrously
13121 * - switch over to the vblank wait helper in the core after that since
13122 * we don't need out special handling any more.
13123 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013124 if (!state->legacy_cursor_update)
13125 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13126
13127 /*
13128 * Now that the vblank has passed, we can go ahead and program the
13129 * optimal watermarks on platforms that need two-step watermark
13130 * programming.
13131 *
13132 * TODO: Move this (and other cleanup) to an async worker eventually.
13133 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013134 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
13135 intel_cstate = to_intel_crtc_state(new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013136
13137 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013138 dev_priv->display.optimize_watermarks(intel_state,
13139 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013140 }
13141
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013142 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020013143 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13144
13145 if (put_domains[i])
13146 modeset_put_power_domains(dev_priv, put_domains[i]);
13147
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013148 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013149 }
13150
Paulo Zanoni56feca92016-09-22 18:00:28 -030013151 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030013152 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040013153
Daniel Vetter94f05022016-06-14 18:01:00 +020013154 drm_atomic_helper_commit_hw_done(state);
13155
Chris Wilsond5553c02017-05-04 12:55:08 +010013156 if (intel_state->modeset) {
13157 /* As one of the primary mmio accessors, KMS has a high
13158 * likelihood of triggering bugs in unclaimed access. After we
13159 * finish modesetting, see if an error has been flagged, and if
13160 * so enable debugging for the next modeset - and hope we catch
13161 * the culprit.
13162 */
13163 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013164 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Chris Wilsond5553c02017-05-04 12:55:08 +010013165 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013166
13167 mutex_lock(&dev->struct_mutex);
13168 drm_atomic_helper_cleanup_planes(dev, state);
13169 mutex_unlock(&dev->struct_mutex);
13170
Daniel Vetterea0000f2016-06-13 16:13:46 +020013171 drm_atomic_helper_commit_cleanup_done(state);
13172
Chris Wilson08536952016-10-14 13:18:18 +010013173 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013174
Chris Wilsonba318c62017-02-02 20:47:41 +000013175 intel_atomic_helper_free_state(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013176}
13177
13178static void intel_atomic_commit_work(struct work_struct *work)
13179{
Chris Wilsonc004a902016-10-28 13:58:45 +010013180 struct drm_atomic_state *state =
13181 container_of(work, struct drm_atomic_state, commit_work);
13182
Daniel Vetter94f05022016-06-14 18:01:00 +020013183 intel_atomic_commit_tail(state);
13184}
13185
Chris Wilsonc004a902016-10-28 13:58:45 +010013186static int __i915_sw_fence_call
13187intel_atomic_commit_ready(struct i915_sw_fence *fence,
13188 enum i915_sw_fence_notify notify)
13189{
13190 struct intel_atomic_state *state =
13191 container_of(fence, struct intel_atomic_state, commit_ready);
13192
13193 switch (notify) {
13194 case FENCE_COMPLETE:
13195 if (state->base.commit_work.func)
13196 queue_work(system_unbound_wq, &state->base.commit_work);
13197 break;
13198
13199 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000013200 {
13201 struct intel_atomic_helper *helper =
13202 &to_i915(state->base.dev)->atomic_helper;
13203
13204 if (llist_add(&state->freed, &helper->free_list))
13205 schedule_work(&helper->free_work);
13206 break;
13207 }
Chris Wilsonc004a902016-10-28 13:58:45 +010013208 }
13209
13210 return NOTIFY_DONE;
13211}
13212
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013213static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13214{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013215 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013216 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013217 int i;
13218
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013219 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010013220 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010013221 intel_fb_obj(new_plane_state->fb),
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010013222 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013223}
13224
Daniel Vetter94f05022016-06-14 18:01:00 +020013225/**
13226 * intel_atomic_commit - commit validated state object
13227 * @dev: DRM device
13228 * @state: the top-level driver state object
13229 * @nonblock: nonblocking commit
13230 *
13231 * This function commits a top-level state object that has been validated
13232 * with drm_atomic_helper_check().
13233 *
Daniel Vetter94f05022016-06-14 18:01:00 +020013234 * RETURNS
13235 * Zero for success or -errno.
13236 */
13237static int intel_atomic_commit(struct drm_device *dev,
13238 struct drm_atomic_state *state,
13239 bool nonblock)
13240{
13241 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013242 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013243 int ret = 0;
13244
Daniel Vetter94f05022016-06-14 18:01:00 +020013245 ret = drm_atomic_helper_setup_commit(state, nonblock);
13246 if (ret)
13247 return ret;
13248
Chris Wilsonc004a902016-10-28 13:58:45 +010013249 drm_atomic_state_get(state);
13250 i915_sw_fence_init(&intel_state->commit_ready,
13251 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013252
Chris Wilsond07f0e52016-10-28 13:58:44 +010013253 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013254 if (ret) {
13255 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Chris Wilsonc004a902016-10-28 13:58:45 +010013256 i915_sw_fence_commit(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013257 return ret;
13258 }
13259
Ville Syrjälä440df932017-03-29 17:21:23 +030013260 /*
13261 * The intel_legacy_cursor_update() fast path takes care
13262 * of avoiding the vblank waits for simple cursor
13263 * movement and flips. For cursor on/off and size changes,
13264 * we want to perform the vblank waits so that watermark
13265 * updates happen during the correct frames. Gen9+ have
13266 * double buffered watermarks and so shouldn't need this.
13267 *
13268 * Do this after drm_atomic_helper_setup_commit() and
13269 * intel_atomic_prepare_commit() because we still want
13270 * to skip the flip and fb cleanup waits. Although that
13271 * does risk yanking the mapping from under the display
13272 * engine.
13273 *
13274 * FIXME doing watermarks and fb cleanup from a vblank worker
13275 * (assuming we had any) would solve these problems.
13276 */
13277 if (INTEL_GEN(dev_priv) < 9)
13278 state->legacy_cursor_update = false;
13279
Daniel Vetter94f05022016-06-14 18:01:00 +020013280 drm_atomic_helper_swap_state(state, true);
13281 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020013282 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013283 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013284
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013285 if (intel_state->modeset) {
13286 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13287 sizeof(intel_state->min_pixclk));
13288 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020013289 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13290 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013291 }
13292
Chris Wilson08536952016-10-14 13:18:18 +010013293 drm_atomic_state_get(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010013294 INIT_WORK(&state->commit_work,
13295 nonblock ? intel_atomic_commit_work : NULL);
13296
13297 i915_sw_fence_commit(&intel_state->commit_ready);
13298 if (!nonblock) {
13299 i915_sw_fence_wait(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013300 intel_atomic_commit_tail(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010013301 }
Mika Kuoppala75714942015-12-16 09:26:48 +020013302
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013303 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013304}
13305
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013306static const struct drm_crtc_funcs intel_crtc_funcs = {
Daniel Vetter3fab2f02017-04-03 10:32:57 +020013307 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013308 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013309 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013310 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010013311 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013312 .atomic_duplicate_state = intel_crtc_duplicate_state,
13313 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010013314 .set_crc_source = intel_crtc_set_crc_source,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013315};
13316
Matt Roper6beb8c232014-12-01 15:40:14 -080013317/**
13318 * intel_prepare_plane_fb - Prepare fb for usage on plane
13319 * @plane: drm plane to prepare for
13320 * @fb: framebuffer to prepare for presentation
13321 *
13322 * Prepares a framebuffer for usage on a display plane. Generally this
13323 * involves pinning the underlying object and updating the frontbuffer tracking
13324 * bits. Some older platforms need special physical address handling for
13325 * cursor planes.
13326 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013327 * Must be called with struct_mutex held.
13328 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013329 * Returns 0 on success, negative error code on failure.
13330 */
13331int
13332intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013333 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013334{
Chris Wilsonc004a902016-10-28 13:58:45 +010013335 struct intel_atomic_state *intel_state =
13336 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013337 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013338 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013340 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010013341 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013342
Chris Wilson57822dc2017-02-22 11:40:48 +000013343 if (obj) {
13344 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13345 INTEL_INFO(dev_priv)->cursor_needs_physical) {
Ville Syrjäläfabac482017-03-27 21:55:43 +030013346 const int align = intel_cursor_alignment(dev_priv);
Chris Wilson57822dc2017-02-22 11:40:48 +000013347
13348 ret = i915_gem_object_attach_phys(obj, align);
13349 if (ret) {
13350 DRM_DEBUG_KMS("failed to attach phys object\n");
13351 return ret;
13352 }
13353 } else {
13354 struct i915_vma *vma;
13355
13356 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13357 if (IS_ERR(vma)) {
13358 DRM_DEBUG_KMS("failed to pin object\n");
13359 return PTR_ERR(vma);
13360 }
13361
13362 to_intel_plane_state(new_state)->vma = vma;
13363 }
13364 }
13365
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013366 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013367 return 0;
13368
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013369 if (old_obj) {
13370 struct drm_crtc_state *crtc_state =
Chris Wilsonc004a902016-10-28 13:58:45 +010013371 drm_atomic_get_existing_crtc_state(new_state->state,
13372 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013373
13374 /* Big Hammer, we also need to ensure that any pending
13375 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13376 * current scanout is retired before unpinning the old
13377 * framebuffer. Note that we rely on userspace rendering
13378 * into the buffer attached to the pipe they are waiting
13379 * on. If not, userspace generates a GPU hang with IPEHR
13380 * point to the MI_WAIT_FOR_EVENT.
13381 *
13382 * This should only fail upon a hung GPU, in which case we
13383 * can safely continue.
13384 */
Chris Wilsonc004a902016-10-28 13:58:45 +010013385 if (needs_modeset(crtc_state)) {
13386 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13387 old_obj->resv, NULL,
13388 false, 0,
13389 GFP_KERNEL);
13390 if (ret < 0)
13391 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013392 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013393 }
13394
Chris Wilsonc004a902016-10-28 13:58:45 +010013395 if (new_state->fence) { /* explicit fencing */
13396 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13397 new_state->fence,
13398 I915_FENCE_TIMEOUT,
13399 GFP_KERNEL);
13400 if (ret < 0)
13401 return ret;
13402 }
13403
Chris Wilsonc37efb92016-06-17 08:28:47 +010013404 if (!obj)
13405 return 0;
13406
Chris Wilsonc004a902016-10-28 13:58:45 +010013407 if (!new_state->fence) { /* implicit fencing */
13408 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13409 obj->resv, NULL,
13410 false, I915_FENCE_TIMEOUT,
13411 GFP_KERNEL);
13412 if (ret < 0)
13413 return ret;
Chris Wilson6b5e90f2016-11-14 20:41:05 +000013414
13415 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Chris Wilsonc004a902016-10-28 13:58:45 +010013416 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013417
Chris Wilsond07f0e52016-10-28 13:58:44 +010013418 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080013419}
13420
Matt Roper38f3ce32014-12-02 07:45:25 -080013421/**
13422 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13423 * @plane: drm plane to clean up for
13424 * @fb: old framebuffer that was on plane
13425 *
13426 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013427 *
13428 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013429 */
13430void
13431intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013432 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013433{
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013434 struct i915_vma *vma;
Matt Roper38f3ce32014-12-02 07:45:25 -080013435
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013436 /* Should only be called after a successful intel_prepare_plane_fb()! */
13437 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13438 if (vma)
13439 intel_unpin_fb_vma(vma);
Matt Roper465c1202014-05-29 08:06:54 -070013440}
13441
Chandra Konduru6156a452015-04-27 13:48:39 -070013442int
13443skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13444{
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013445 struct drm_i915_private *dev_priv;
Chandra Konduru6156a452015-04-27 13:48:39 -070013446 int max_scale;
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013447 int crtc_clock, max_dotclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013448
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013449 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013450 return DRM_PLANE_HELPER_NO_SCALING;
13451
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013452 dev_priv = to_i915(intel_crtc->base.dev);
Chandra Konduru6156a452015-04-27 13:48:39 -070013453
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013454 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13455 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13456
13457 if (IS_GEMINILAKE(dev_priv))
13458 max_dotclk *= 2;
13459
13460 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013461 return DRM_PLANE_HELPER_NO_SCALING;
13462
13463 /*
13464 * skl max scale is lower of:
13465 * close to 3 but not 3, -1 is for that purpose
13466 * or
13467 * cdclk/crtc_clock
13468 */
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013469 max_scale = min((1 << 16) * 3 - 1,
13470 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
Chandra Konduru6156a452015-04-27 13:48:39 -070013471
13472 return max_scale;
13473}
13474
Matt Roper465c1202014-05-29 08:06:54 -070013475static int
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013476intel_check_primary_plane(struct intel_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013477 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013478 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013479{
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013480 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Matt Roper2b875c22014-12-01 15:40:13 -080013481 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013482 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013483 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13484 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013485 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013486
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013487 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013488 /* use scaler when colorkey is not required */
13489 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13490 min_scale = 1;
13491 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13492 }
Sonika Jindald8106362015-04-10 14:37:28 +053013493 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013494 }
Sonika Jindald8106362015-04-10 14:37:28 +053013495
Daniel Vettercc926382016-08-15 10:41:47 +020013496 ret = drm_plane_helper_check_state(&state->base,
13497 &state->clip,
13498 min_scale, max_scale,
13499 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013500 if (ret)
13501 return ret;
13502
Daniel Vettercc926382016-08-15 10:41:47 +020013503 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013504 return 0;
13505
13506 if (INTEL_GEN(dev_priv) >= 9) {
13507 ret = skl_check_plane_surface(state);
13508 if (ret)
13509 return ret;
Ville Syrjäläa0864d52017-03-23 21:27:09 +020013510
13511 state->ctl = skl_plane_ctl(crtc_state, state);
13512 } else {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +020013513 ret = i9xx_check_plane_surface(state);
13514 if (ret)
13515 return ret;
13516
Ville Syrjäläa0864d52017-03-23 21:27:09 +020013517 state->ctl = i9xx_plane_ctl(crtc_state, state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013518 }
13519
13520 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013521}
13522
Daniel Vetter5a21b662016-05-24 17:13:53 +020013523static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13524 struct drm_crtc_state *old_crtc_state)
13525{
13526 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013527 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040013529 struct intel_crtc_state *intel_cstate =
13530 to_intel_crtc_state(crtc->state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013531 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013532 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013533 struct intel_atomic_state *old_intel_state =
13534 to_intel_atomic_state(old_crtc_state->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013535 bool modeset = needs_modeset(crtc->state);
13536
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013537 if (!modeset &&
13538 (intel_cstate->base.color_mgmt_changed ||
13539 intel_cstate->update_pipe)) {
13540 intel_color_set_csc(crtc->state);
13541 intel_color_load_luts(crtc->state);
13542 }
13543
Daniel Vetter5a21b662016-05-24 17:13:53 +020013544 /* Perform vblank evasion around commit operation */
13545 intel_pipe_update_start(intel_crtc);
13546
13547 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013548 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013549
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013550 if (intel_cstate->update_pipe)
13551 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13552 else if (INTEL_GEN(dev_priv) >= 9)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013553 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040013554
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013555out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013556 if (dev_priv->display.atomic_update_watermarks)
13557 dev_priv->display.atomic_update_watermarks(old_intel_state,
13558 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013559}
13560
13561static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13562 struct drm_crtc_state *old_crtc_state)
13563{
13564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13565
13566 intel_pipe_update_end(intel_crtc, NULL);
13567}
13568
Matt Ropercf4c7c12014-12-04 10:27:42 -080013569/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013570 * intel_plane_destroy - destroy a plane
13571 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013572 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013573 * Common destruction function for all types of planes (primary, cursor,
13574 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013575 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013576void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013577{
Matt Roper465c1202014-05-29 08:06:54 -070013578 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013579 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013580}
13581
Matt Roper65a3fea2015-01-21 16:35:42 -080013582const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013583 .update_plane = drm_atomic_helper_update_plane,
13584 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013585 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013586 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013587 .atomic_get_property = intel_plane_atomic_get_property,
13588 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013589 .atomic_duplicate_state = intel_plane_duplicate_state,
13590 .atomic_destroy_state = intel_plane_destroy_state,
Matt Roper465c1202014-05-29 08:06:54 -070013591};
13592
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013593static int
13594intel_legacy_cursor_update(struct drm_plane *plane,
13595 struct drm_crtc *crtc,
13596 struct drm_framebuffer *fb,
13597 int crtc_x, int crtc_y,
13598 unsigned int crtc_w, unsigned int crtc_h,
13599 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013600 uint32_t src_w, uint32_t src_h,
13601 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013602{
13603 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13604 int ret;
13605 struct drm_plane_state *old_plane_state, *new_plane_state;
13606 struct intel_plane *intel_plane = to_intel_plane(plane);
13607 struct drm_framebuffer *old_fb;
13608 struct drm_crtc_state *crtc_state = crtc->state;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013609 struct i915_vma *old_vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013610
13611 /*
13612 * When crtc is inactive or there is a modeset pending,
13613 * wait for it to complete in the slowpath
13614 */
13615 if (!crtc_state->active || needs_modeset(crtc_state) ||
13616 to_intel_crtc_state(crtc_state)->update_pipe)
13617 goto slow;
13618
13619 old_plane_state = plane->state;
13620
13621 /*
13622 * If any parameters change that may affect watermarks,
13623 * take the slowpath. Only changing fb or position should be
13624 * in the fastpath.
13625 */
13626 if (old_plane_state->crtc != crtc ||
13627 old_plane_state->src_w != src_w ||
13628 old_plane_state->src_h != src_h ||
13629 old_plane_state->crtc_w != crtc_w ||
13630 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013631 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013632 goto slow;
13633
13634 new_plane_state = intel_plane_duplicate_state(plane);
13635 if (!new_plane_state)
13636 return -ENOMEM;
13637
13638 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13639
13640 new_plane_state->src_x = src_x;
13641 new_plane_state->src_y = src_y;
13642 new_plane_state->src_w = src_w;
13643 new_plane_state->src_h = src_h;
13644 new_plane_state->crtc_x = crtc_x;
13645 new_plane_state->crtc_y = crtc_y;
13646 new_plane_state->crtc_w = crtc_w;
13647 new_plane_state->crtc_h = crtc_h;
13648
13649 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13650 to_intel_plane_state(new_plane_state));
13651 if (ret)
13652 goto out_free;
13653
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013654 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13655 if (ret)
13656 goto out_free;
13657
13658 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
Ville Syrjäläfabac482017-03-27 21:55:43 +030013659 int align = intel_cursor_alignment(dev_priv);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013660
13661 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13662 if (ret) {
13663 DRM_DEBUG_KMS("failed to attach phys object\n");
13664 goto out_unlock;
13665 }
13666 } else {
13667 struct i915_vma *vma;
13668
13669 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13670 if (IS_ERR(vma)) {
13671 DRM_DEBUG_KMS("failed to pin object\n");
13672
13673 ret = PTR_ERR(vma);
13674 goto out_unlock;
13675 }
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013676
13677 to_intel_plane_state(new_plane_state)->vma = vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013678 }
13679
13680 old_fb = old_plane_state->fb;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013681 old_vma = to_intel_plane_state(old_plane_state)->vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013682
13683 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13684 intel_plane->frontbuffer_bit);
13685
13686 /* Swap plane state */
13687 new_plane_state->fence = old_plane_state->fence;
13688 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13689 new_plane_state->fence = NULL;
13690 new_plane_state->fb = old_fb;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013691 to_intel_plane_state(new_plane_state)->vma = old_vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013692
Ville Syrjälä72259532017-03-02 19:15:05 +020013693 if (plane->state->visible) {
13694 trace_intel_update_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013695 intel_plane->update_plane(intel_plane,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013696 to_intel_crtc_state(crtc->state),
13697 to_intel_plane_state(plane->state));
Ville Syrjälä72259532017-03-02 19:15:05 +020013698 } else {
13699 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013700 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
Ville Syrjälä72259532017-03-02 19:15:05 +020013701 }
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013702
13703 intel_cleanup_plane_fb(plane, new_plane_state);
13704
13705out_unlock:
13706 mutex_unlock(&dev_priv->drm.struct_mutex);
13707out_free:
13708 intel_plane_destroy_state(plane, new_plane_state);
13709 return ret;
13710
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013711slow:
13712 return drm_atomic_helper_update_plane(plane, crtc, fb,
13713 crtc_x, crtc_y, crtc_w, crtc_h,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013714 src_x, src_y, src_w, src_h, ctx);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013715}
13716
13717static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13718 .update_plane = intel_legacy_cursor_update,
13719 .disable_plane = drm_atomic_helper_disable_plane,
13720 .destroy = intel_plane_destroy,
13721 .set_property = drm_atomic_helper_plane_set_property,
13722 .atomic_get_property = intel_plane_atomic_get_property,
13723 .atomic_set_property = intel_plane_atomic_set_property,
13724 .atomic_duplicate_state = intel_plane_duplicate_state,
13725 .atomic_destroy_state = intel_plane_destroy_state,
13726};
13727
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013728static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013729intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013730{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013731 struct intel_plane *primary = NULL;
13732 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013733 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013734 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020013735 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013736 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013737
13738 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013739 if (!primary) {
13740 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013741 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013742 }
Matt Roper465c1202014-05-29 08:06:54 -070013743
Matt Roper8e7d6882015-01-21 16:35:41 -080013744 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013745 if (!state) {
13746 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013747 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013748 }
13749
Matt Roper8e7d6882015-01-21 16:35:41 -080013750 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013751
Matt Roper465c1202014-05-29 08:06:54 -070013752 primary->can_scale = false;
13753 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013754 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070013755 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013756 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013757 }
Matt Roper465c1202014-05-29 08:06:54 -070013758 primary->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013759 /*
13760 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13761 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13762 */
13763 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13764 primary->plane = (enum plane) !pipe;
13765 else
13766 primary->plane = (enum plane) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013767 primary->id = PLANE_PRIMARY;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013768 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013769 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013770
Ville Syrjälä580503c2016-10-31 22:37:00 +020013771 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013772 intel_primary_formats = skl_primary_formats;
13773 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013774
13775 primary->update_plane = skylake_update_primary_plane;
13776 primary->disable_plane = skylake_disable_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013777 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013778 intel_primary_formats = i965_primary_formats;
13779 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013780
13781 primary->update_plane = i9xx_update_primary_plane;
13782 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013783 } else {
13784 intel_primary_formats = i8xx_primary_formats;
13785 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013786
13787 primary->update_plane = i9xx_update_primary_plane;
13788 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013789 }
13790
Ville Syrjälä580503c2016-10-31 22:37:00 +020013791 if (INTEL_GEN(dev_priv) >= 9)
13792 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13793 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013794 intel_primary_formats, num_formats,
13795 DRM_PLANE_TYPE_PRIMARY,
13796 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013797 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020013798 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13799 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013800 intel_primary_formats, num_formats,
13801 DRM_PLANE_TYPE_PRIMARY,
13802 "primary %c", pipe_name(pipe));
13803 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020013804 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13805 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013806 intel_primary_formats, num_formats,
13807 DRM_PLANE_TYPE_PRIMARY,
13808 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013809 if (ret)
13810 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013811
Dave Airlie5481e272016-10-25 16:36:13 +100013812 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013813 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013814 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13815 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013816 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13817 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013818 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13819 DRM_MODE_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013820 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013821 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013822 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013823 } else {
Robert Fossc2c446a2017-05-19 16:50:17 -040013824 supported_rotations = DRM_MODE_ROTATE_0;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013825 }
13826
Dave Airlie5481e272016-10-25 16:36:13 +100013827 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013828 drm_plane_create_rotation_property(&primary->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013829 DRM_MODE_ROTATE_0,
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013830 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013831
Matt Roperea2c67b2014-12-23 10:41:52 -080013832 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13833
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013834 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013835
13836fail:
13837 kfree(state);
13838 kfree(primary);
13839
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013840 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013841}
13842
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013843static struct intel_plane *
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013844intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13845 enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013846{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013847 struct intel_plane *cursor = NULL;
13848 struct intel_plane_state *state = NULL;
13849 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013850
13851 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013852 if (!cursor) {
13853 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013854 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013855 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013856
Matt Roper8e7d6882015-01-21 16:35:41 -080013857 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013858 if (!state) {
13859 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013860 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013861 }
13862
Matt Roper8e7d6882015-01-21 16:35:41 -080013863 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013864
Matt Roper3d7d6512014-06-10 08:28:13 -070013865 cursor->can_scale = false;
13866 cursor->max_downscale = 1;
13867 cursor->pipe = pipe;
13868 cursor->plane = pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013869 cursor->id = PLANE_CURSOR;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013870 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013871
13872 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13873 cursor->update_plane = i845_update_cursor;
13874 cursor->disable_plane = i845_disable_cursor;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013875 cursor->check_plane = i845_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013876 } else {
13877 cursor->update_plane = i9xx_update_cursor;
13878 cursor->disable_plane = i9xx_disable_cursor;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013879 cursor->check_plane = i9xx_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013880 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013881
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030013882 cursor->cursor.base = ~0;
13883 cursor->cursor.cntl = ~0;
Ville Syrjälä024faac2017-03-27 21:55:42 +030013884
13885 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13886 cursor->cursor.size = ~0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013887
Ville Syrjälä580503c2016-10-31 22:37:00 +020013888 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013889 0, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013890 intel_cursor_formats,
13891 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013892 DRM_PLANE_TYPE_CURSOR,
13893 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013894 if (ret)
13895 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013896
Dave Airlie5481e272016-10-25 16:36:13 +100013897 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013898 drm_plane_create_rotation_property(&cursor->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013899 DRM_MODE_ROTATE_0,
13900 DRM_MODE_ROTATE_0 |
13901 DRM_MODE_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013902
Ville Syrjälä580503c2016-10-31 22:37:00 +020013903 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013904 state->scaler_id = -1;
13905
Matt Roperea2c67b2014-12-23 10:41:52 -080013906 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13907
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013908 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013909
13910fail:
13911 kfree(state);
13912 kfree(cursor);
13913
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013914 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013915}
13916
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013917static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13918 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013919{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013920 struct intel_crtc_scaler_state *scaler_state =
13921 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013922 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013923 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013924
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013925 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13926 if (!crtc->num_scalers)
13927 return;
13928
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013929 for (i = 0; i < crtc->num_scalers; i++) {
13930 struct intel_scaler *scaler = &scaler_state->scalers[i];
13931
13932 scaler->in_use = 0;
13933 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013934 }
13935
13936 scaler_state->scaler_id = -1;
13937}
13938
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013939static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013940{
13941 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013942 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013943 struct intel_plane *primary = NULL;
13944 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013945 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013946
Daniel Vetter955382f2013-09-19 14:05:45 +020013947 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013948 if (!intel_crtc)
13949 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013950
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013951 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013952 if (!crtc_state) {
13953 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013954 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013955 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013956 intel_crtc->config = crtc_state;
13957 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013958 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013959
Ville Syrjälä580503c2016-10-31 22:37:00 +020013960 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013961 if (IS_ERR(primary)) {
13962 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013963 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013964 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013965 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013966
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013967 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013968 struct intel_plane *plane;
13969
Ville Syrjälä580503c2016-10-31 22:37:00 +020013970 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013971 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013972 ret = PTR_ERR(plane);
13973 goto fail;
13974 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013975 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013976 }
13977
Ville Syrjälä580503c2016-10-31 22:37:00 +020013978 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013979 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013980 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013981 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013982 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013983 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013984
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013985 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013986 &primary->base, &cursor->base,
13987 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013988 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013989 if (ret)
13990 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013991
Jesse Barnes80824002009-09-10 15:28:06 -070013992 intel_crtc->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013993 intel_crtc->plane = primary->plane;
Jesse Barnes80824002009-09-10 15:28:06 -070013994
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013995 /* initialize shared scalers */
13996 intel_crtc_init_scalers(intel_crtc, crtc_state);
13997
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013998 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13999 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014000 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
14001 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014002
Jesse Barnes79e53942008-11-07 14:24:08 -080014003 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014004
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014005 intel_color_init(&intel_crtc->base);
14006
Daniel Vetter87b6b102014-05-15 15:33:46 +020014007 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014008
14009 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070014010
14011fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014012 /*
14013 * drm_mode_config_cleanup() will free up any
14014 * crtcs/planes already initialized.
14015 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014016 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014017 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014018
14019 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014020}
14021
Jesse Barnes752aa882013-10-31 18:55:49 +020014022enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14023{
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014024 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014025
Rob Clark51fd3712013-11-19 12:10:12 -050014026 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014027
Daniel Vetter51ec53d2017-03-01 10:52:24 +010014028 if (!connector->base.state->crtc)
Jesse Barnes752aa882013-10-31 18:55:49 +020014029 return INVALID_PIPE;
14030
Daniel Vetter51ec53d2017-03-01 10:52:24 +010014031 return to_intel_crtc(connector->base.state->crtc)->pipe;
Jesse Barnes752aa882013-10-31 18:55:49 +020014032}
14033
Carl Worth08d7b3d2009-04-29 14:43:54 -070014034int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014035 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014036{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014037 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014038 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014039 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014040
Rob Clark7707e652014-07-17 23:30:04 -040014041 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014042 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014043 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014044
Rob Clark7707e652014-07-17 23:30:04 -040014045 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014046 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014047
Daniel Vetterc05422d2009-08-11 16:05:30 +020014048 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014049}
14050
Daniel Vetter66a92782012-07-12 20:08:18 +020014051static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014052{
Daniel Vetter66a92782012-07-12 20:08:18 +020014053 struct drm_device *dev = encoder->base.dev;
14054 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014055 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014056 int entry = 0;
14057
Damien Lespiaub2784e12014-08-05 11:29:37 +010014058 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014059 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014060 index_mask |= (1 << entry);
14061
Jesse Barnes79e53942008-11-07 14:24:08 -080014062 entry++;
14063 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014064
Jesse Barnes79e53942008-11-07 14:24:08 -080014065 return index_mask;
14066}
14067
Ville Syrjälä646d5772016-10-31 22:37:14 +020014068static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000014069{
Ville Syrjälä646d5772016-10-31 22:37:14 +020014070 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000014071 return false;
14072
14073 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14074 return false;
14075
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014076 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014077 return false;
14078
14079 return true;
14080}
14081
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014082static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014083{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014084 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000014085 return false;
14086
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014087 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014088 return false;
14089
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014090 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014091 return false;
14092
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014093 if (HAS_PCH_LPT_H(dev_priv) &&
14094 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014095 return false;
14096
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014097 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014098 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014099 return false;
14100
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014101 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014102 return false;
14103
14104 return true;
14105}
14106
Imre Deak8090ba82016-08-10 14:07:33 +030014107void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14108{
14109 int pps_num;
14110 int pps_idx;
14111
14112 if (HAS_DDI(dev_priv))
14113 return;
14114 /*
14115 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14116 * everywhere where registers can be write protected.
14117 */
14118 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14119 pps_num = 2;
14120 else
14121 pps_num = 1;
14122
14123 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14124 u32 val = I915_READ(PP_CONTROL(pps_idx));
14125
14126 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14127 I915_WRITE(PP_CONTROL(pps_idx), val);
14128 }
14129}
14130
Imre Deak44cb7342016-08-10 14:07:29 +030014131static void intel_pps_init(struct drm_i915_private *dev_priv)
14132{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014133 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030014134 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14135 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14136 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14137 else
14138 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014139
14140 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014141}
14142
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014143static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080014144{
Chris Wilson4ef69c72010-09-09 15:14:28 +010014145 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014146 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014147
Imre Deak44cb7342016-08-10 14:07:29 +030014148 intel_pps_init(dev_priv);
14149
Imre Deak97a824e12016-06-21 11:51:47 +030014150 /*
14151 * intel_edp_init_connector() depends on this completing first, to
14152 * prevent the registeration of both eDP and LVDS and the incorrect
14153 * sharing of the PPS.
14154 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014155 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014156
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014157 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014158 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014159
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014160 if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053014161 /*
14162 * FIXME: Broxton doesn't support port detection via the
14163 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14164 * detect the ports.
14165 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014166 intel_ddi_init(dev_priv, PORT_A);
14167 intel_ddi_init(dev_priv, PORT_B);
14168 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014169
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014170 intel_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014171 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014172 int found;
14173
Jesse Barnesde31fac2015-03-06 15:53:32 -080014174 /*
14175 * Haswell uses DDI functions to detect digital outputs.
14176 * On SKL pre-D0 the strap isn't connected, so we assume
14177 * it's there.
14178 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014179 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014180 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014181 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014182 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014183
14184 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14185 * register */
14186 found = I915_READ(SFUSE_STRAP);
14187
14188 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014189 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014190 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014191 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014192 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014193 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014194 /*
14195 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14196 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014197 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014198 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14199 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14200 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014201 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014202
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014203 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014204 int found;
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014205 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014206
Ville Syrjälä646d5772016-10-31 22:37:14 +020014207 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014208 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014209
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014210 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014211 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014212 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014213 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014214 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014215 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014216 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014217 }
14218
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014219 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014220 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014221
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014222 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014223 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014224
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014225 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014226 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014227
Daniel Vetter270b3042012-10-27 15:52:05 +020014228 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014229 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014230 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014231 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014232
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014233 /*
14234 * The DP_DETECTED bit is the latched state of the DDC
14235 * SDA pin at boot. However since eDP doesn't require DDC
14236 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14237 * eDP ports may have been muxed to an alternate function.
14238 * Thus we can't rely on the DP_DETECTED bit alone to detect
14239 * eDP ports. Consult the VBT as well as DP_DETECTED to
14240 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014241 *
14242 * Sadly the straps seem to be missing sometimes even for HDMI
14243 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14244 * and VBT for the presence of the port. Additionally we can't
14245 * trust the port type the VBT declares as we've seen at least
14246 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014247 */
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014248 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014249 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14250 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014251 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014252 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014253 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014254
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014255 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014256 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14257 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014258 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014259 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014260 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014261
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014262 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014263 /*
14264 * eDP not supported on port D,
14265 * so no need to worry about it
14266 */
14267 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14268 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014269 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014270 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014271 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014272 }
14273
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014274 intel_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014275 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014276 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014277
Paulo Zanonie2debe92013-02-18 19:00:27 -030014278 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014279 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014280 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014281 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014282 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014283 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014284 }
Ma Ling27185ae2009-08-24 13:50:23 +080014285
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014286 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014287 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014288 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014289
14290 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014291
Paulo Zanonie2debe92013-02-18 19:00:27 -030014292 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014293 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014294 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014295 }
Ma Ling27185ae2009-08-24 13:50:23 +080014296
Paulo Zanonie2debe92013-02-18 19:00:27 -030014297 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014298
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014299 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014300 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014301 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014302 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014303 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014304 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014305 }
Ma Ling27185ae2009-08-24 13:50:23 +080014306
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014307 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014308 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014309 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014310 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014311
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014312 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014313 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014314
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014315 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014316
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014317 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014318 encoder->base.possible_crtcs = encoder->crtc_mask;
14319 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014320 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014321 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014322
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014323 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014324
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014325 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014326}
14327
14328static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14329{
14330 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014331
Daniel Vetteref2d6332014-02-10 18:00:38 +010014332 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014333
Chris Wilsondd689282017-03-01 15:41:28 +000014334 i915_gem_object_lock(intel_fb->obj);
14335 WARN_ON(!intel_fb->obj->framebuffer_references--);
14336 i915_gem_object_unlock(intel_fb->obj);
14337
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014338 i915_gem_object_put(intel_fb->obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014339
Jesse Barnes79e53942008-11-07 14:24:08 -080014340 kfree(intel_fb);
14341}
14342
14343static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014344 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014345 unsigned int *handle)
14346{
14347 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014348 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014349
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014350 if (obj->userptr.mm) {
14351 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14352 return -EINVAL;
14353 }
14354
Chris Wilson05394f32010-11-08 19:18:58 +000014355 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014356}
14357
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014358static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14359 struct drm_file *file,
14360 unsigned flags, unsigned color,
14361 struct drm_clip_rect *clips,
14362 unsigned num_clips)
14363{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014364 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014365
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014366 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014367 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014368
14369 return 0;
14370}
14371
Jesse Barnes79e53942008-11-07 14:24:08 -080014372static const struct drm_framebuffer_funcs intel_fb_funcs = {
14373 .destroy = intel_user_framebuffer_destroy,
14374 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014375 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014376};
14377
Damien Lespiaub3218032015-02-27 11:15:18 +000014378static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014379u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14380 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000014381{
Chris Wilson24dbf512017-02-15 10:59:18 +000014382 u32 gen = INTEL_GEN(dev_priv);
Damien Lespiaub3218032015-02-27 11:15:18 +000014383
14384 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014385 int cpp = drm_format_plane_cpp(pixel_format, 0);
14386
Damien Lespiaub3218032015-02-27 11:15:18 +000014387 /* "The stride in bytes must not exceed the of the size of 8K
14388 * pixels and 32K bytes."
14389 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014390 return min(8192 * cpp, 32768);
Ville Syrjälä6401c372017-02-08 19:53:28 +020014391 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014392 return 32*1024;
14393 } else if (gen >= 4) {
14394 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14395 return 16*1024;
14396 else
14397 return 32*1024;
14398 } else if (gen >= 3) {
14399 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14400 return 8*1024;
14401 else
14402 return 16*1024;
14403 } else {
14404 /* XXX DSPC is limited to 4k tiled */
14405 return 8*1024;
14406 }
14407}
14408
Chris Wilson24dbf512017-02-15 10:59:18 +000014409static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14410 struct drm_i915_gem_object *obj,
14411 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014412{
Chris Wilson24dbf512017-02-15 10:59:18 +000014413 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014414 struct drm_format_name_buf format_name;
Chris Wilsondd689282017-03-01 15:41:28 +000014415 u32 pitch_limit, stride_alignment;
14416 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014417 int ret = -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -080014418
Chris Wilsondd689282017-03-01 15:41:28 +000014419 i915_gem_object_lock(obj);
14420 obj->framebuffer_references++;
14421 tiling = i915_gem_object_get_tiling(obj);
14422 stride = i915_gem_object_get_stride(obj);
14423 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014424
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014425 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014426 /*
14427 * If there's a fence, enforce that
14428 * the fb modifier and tiling mode match.
14429 */
14430 if (tiling != I915_TILING_NONE &&
14431 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014432 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014433 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014434 }
14435 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014436 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014437 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014438 } else if (tiling == I915_TILING_Y) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014439 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014440 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014441 }
14442 }
14443
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014444 /* Passed in modifier sanity checking. */
14445 switch (mode_cmd->modifier[0]) {
14446 case I915_FORMAT_MOD_Y_TILED:
14447 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014448 if (INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014449 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14450 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014451 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014452 }
Ben Widawsky2f075562017-03-24 14:29:48 -070014453 case DRM_FORMAT_MOD_LINEAR:
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014454 case I915_FORMAT_MOD_X_TILED:
14455 break;
14456 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014457 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14458 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014459 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014460 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014461
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014462 /*
14463 * gen2/3 display engine uses the fence if present,
14464 * so the tiling mode must match the fb modifier exactly.
14465 */
14466 if (INTEL_INFO(dev_priv)->gen < 4 &&
14467 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014468 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014469 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014470 }
14471
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014472 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014473 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014474 if (mode_cmd->pitches[0] > pitch_limit) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014475 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
Ben Widawsky2f075562017-03-24 14:29:48 -070014476 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014477 "tiled" : "linear",
14478 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014479 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014480 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014481
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014482 /*
14483 * If there's a fence, enforce that
14484 * the fb pitch and fence stride match.
14485 */
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014486 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14487 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14488 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014489 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014490 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014491
Ville Syrjälä57779d02012-10-31 17:50:14 +020014492 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014493 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014494 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014495 case DRM_FORMAT_RGB565:
14496 case DRM_FORMAT_XRGB8888:
14497 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014498 break;
14499 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014500 if (INTEL_GEN(dev_priv) > 3) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014501 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14502 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014503 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014504 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014505 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014506 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014507 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014508 INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014509 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14510 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014511 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014512 }
14513 break;
14514 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014515 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014516 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014517 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014518 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14519 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014520 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014521 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014522 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014523 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014524 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014525 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14526 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014527 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014528 }
14529 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014530 case DRM_FORMAT_YUYV:
14531 case DRM_FORMAT_UYVY:
14532 case DRM_FORMAT_YVYU:
14533 case DRM_FORMAT_VYUY:
Ville Syrjäläab330812017-04-21 21:14:32 +030014534 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014535 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14536 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014537 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014538 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014539 break;
14540 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014541 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14542 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014543 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014544 }
14545
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014546 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14547 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014548 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014549
Chris Wilson24dbf512017-02-15 10:59:18 +000014550 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14551 &intel_fb->base, mode_cmd);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014552
14553 stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
14554 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014555 DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
14556 mode_cmd->pitches[0], stride_alignment);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014557 goto err;
14558 }
14559
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014560 intel_fb->obj = obj;
14561
Ville Syrjälä6687c902015-09-15 13:16:41 +030014562 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14563 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014564 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014565
Chris Wilson24dbf512017-02-15 10:59:18 +000014566 ret = drm_framebuffer_init(obj->base.dev,
14567 &intel_fb->base,
14568 &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014569 if (ret) {
14570 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014571 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014572 }
14573
Jesse Barnes79e53942008-11-07 14:24:08 -080014574 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014575
14576err:
Chris Wilsondd689282017-03-01 15:41:28 +000014577 i915_gem_object_lock(obj);
14578 obj->framebuffer_references--;
14579 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014580 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014581}
14582
Jesse Barnes79e53942008-11-07 14:24:08 -080014583static struct drm_framebuffer *
14584intel_user_framebuffer_create(struct drm_device *dev,
14585 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014586 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014587{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014588 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014589 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014590 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014591
Chris Wilson03ac0642016-07-20 13:31:51 +010014592 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14593 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014594 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014595
Chris Wilson24dbf512017-02-15 10:59:18 +000014596 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014597 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014598 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014599
14600 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014601}
14602
Chris Wilson778e23a2016-12-05 14:29:39 +000014603static void intel_atomic_state_free(struct drm_atomic_state *state)
14604{
14605 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14606
14607 drm_atomic_state_default_release(state);
14608
14609 i915_sw_fence_fini(&intel_state->commit_ready);
14610
14611 kfree(state);
14612}
14613
Jesse Barnes79e53942008-11-07 14:24:08 -080014614static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014615 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014616 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014617 .atomic_check = intel_atomic_check,
14618 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014619 .atomic_state_alloc = intel_atomic_state_alloc,
14620 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014621 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014622};
14623
Imre Deak88212942016-03-16 13:38:53 +020014624/**
14625 * intel_init_display_hooks - initialize the display modesetting hooks
14626 * @dev_priv: device private
14627 */
14628void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014629{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014630 intel_init_cdclk_hooks(dev_priv);
14631
Imre Deak88212942016-03-16 13:38:53 +020014632 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014633 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014634 dev_priv->display.get_initial_plane_config =
14635 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014636 dev_priv->display.crtc_compute_clock =
14637 haswell_crtc_compute_clock;
14638 dev_priv->display.crtc_enable = haswell_crtc_enable;
14639 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014640 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014641 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014642 dev_priv->display.get_initial_plane_config =
14643 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014644 dev_priv->display.crtc_compute_clock =
14645 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014646 dev_priv->display.crtc_enable = haswell_crtc_enable;
14647 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014648 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014649 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014650 dev_priv->display.get_initial_plane_config =
14651 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014652 dev_priv->display.crtc_compute_clock =
14653 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014654 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14655 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014656 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014657 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014658 dev_priv->display.get_initial_plane_config =
14659 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014660 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14661 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14662 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14663 } else if (IS_VALLEYVIEW(dev_priv)) {
14664 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14665 dev_priv->display.get_initial_plane_config =
14666 i9xx_get_initial_plane_config;
14667 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014668 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14669 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014670 } else if (IS_G4X(dev_priv)) {
14671 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14672 dev_priv->display.get_initial_plane_config =
14673 i9xx_get_initial_plane_config;
14674 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14675 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14676 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014677 } else if (IS_PINEVIEW(dev_priv)) {
14678 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14679 dev_priv->display.get_initial_plane_config =
14680 i9xx_get_initial_plane_config;
14681 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14682 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14683 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014684 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014685 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014686 dev_priv->display.get_initial_plane_config =
14687 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014688 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014689 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14690 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014691 } else {
14692 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14693 dev_priv->display.get_initial_plane_config =
14694 i9xx_get_initial_plane_config;
14695 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14696 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14697 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014698 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014699
Imre Deak88212942016-03-16 13:38:53 +020014700 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014701 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014702 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014703 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014704 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014705 /* FIXME: detect B0+ stepping and use auto training */
14706 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014707 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014708 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014709 }
14710
Lyude27082492016-08-24 07:48:10 +020014711 if (dev_priv->info.gen >= 9)
14712 dev_priv->display.update_crtcs = skl_update_crtcs;
14713 else
14714 dev_priv->display.update_crtcs = intel_update_crtcs;
14715
Daniel Vetter5a21b662016-05-24 17:13:53 +020014716 switch (INTEL_INFO(dev_priv)->gen) {
14717 case 2:
14718 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14719 break;
14720
14721 case 3:
14722 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14723 break;
14724
14725 case 4:
14726 case 5:
14727 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14728 break;
14729
14730 case 6:
14731 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14732 break;
14733 case 7:
14734 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14735 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14736 break;
14737 case 9:
14738 /* Drop through - unsupported since execlist only. */
14739 default:
14740 /* Default just returns -ENODEV to indicate unsupported */
14741 dev_priv->display.queue_flip = intel_default_queue_flip;
14742 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014743}
14744
Jesse Barnesb690e962010-07-19 13:53:12 -070014745/*
Keith Packard435793d2011-07-12 14:56:22 -070014746 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14747 */
14748static void quirk_ssc_force_disable(struct drm_device *dev)
14749{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014750 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070014751 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014752 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014753}
14754
Carsten Emde4dca20e2012-03-15 15:56:26 +010014755/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014756 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14757 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014758 */
14759static void quirk_invert_brightness(struct drm_device *dev)
14760{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014761 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010014762 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014763 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014764}
14765
Scot Doyle9c72cc62014-07-03 23:27:50 +000014766/* Some VBT's incorrectly indicate no backlight is present */
14767static void quirk_backlight_present(struct drm_device *dev)
14768{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014769 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000014770 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14771 DRM_INFO("applying backlight present quirk\n");
14772}
14773
Manasi Navarec99a2592017-06-30 09:33:48 -070014774/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14775 * which is 300 ms greater than eDP spec T12 min.
14776 */
14777static void quirk_increase_t12_delay(struct drm_device *dev)
14778{
14779 struct drm_i915_private *dev_priv = to_i915(dev);
14780
14781 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14782 DRM_INFO("Applying T12 delay quirk\n");
14783}
14784
Jesse Barnesb690e962010-07-19 13:53:12 -070014785struct intel_quirk {
14786 int device;
14787 int subsystem_vendor;
14788 int subsystem_device;
14789 void (*hook)(struct drm_device *dev);
14790};
14791
Egbert Eich5f85f172012-10-14 15:46:38 +020014792/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14793struct intel_dmi_quirk {
14794 void (*hook)(struct drm_device *dev);
14795 const struct dmi_system_id (*dmi_id_list)[];
14796};
14797
14798static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14799{
14800 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14801 return 1;
14802}
14803
14804static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14805 {
14806 .dmi_id_list = &(const struct dmi_system_id[]) {
14807 {
14808 .callback = intel_dmi_reverse_brightness,
14809 .ident = "NCR Corporation",
14810 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14811 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14812 },
14813 },
14814 { } /* terminating entry */
14815 },
14816 .hook = quirk_invert_brightness,
14817 },
14818};
14819
Ben Widawskyc43b5632012-04-16 14:07:40 -070014820static struct intel_quirk intel_quirks[] = {
Keith Packard435793d2011-07-12 14:56:22 -070014821 /* Lenovo U160 cannot use SSC on LVDS */
14822 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014823
14824 /* Sony Vaio Y cannot use SSC on LVDS */
14825 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014826
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014827 /* Acer Aspire 5734Z must invert backlight brightness */
14828 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14829
14830 /* Acer/eMachines G725 */
14831 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14832
14833 /* Acer/eMachines e725 */
14834 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14835
14836 /* Acer/Packard Bell NCL20 */
14837 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14838
14839 /* Acer Aspire 4736Z */
14840 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014841
14842 /* Acer Aspire 5336 */
14843 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014844
14845 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14846 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014847
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014848 /* Acer C720 Chromebook (Core i3 4005U) */
14849 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14850
jens steinb2a96012014-10-28 20:25:53 +010014851 /* Apple Macbook 2,1 (Core 2 T7400) */
14852 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14853
Jani Nikula1b9448b2015-11-05 11:49:59 +020014854 /* Apple Macbook 4,1 */
14855 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14856
Scot Doyled4967d82014-07-03 23:27:52 +000014857 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14858 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014859
14860 /* HP Chromebook 14 (Celeron 2955U) */
14861 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014862
14863 /* Dell Chromebook 11 */
14864 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014865
14866 /* Dell Chromebook 11 (2015 version) */
14867 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Manasi Navarec99a2592017-06-30 09:33:48 -070014868
14869 /* Toshiba Satellite P50-C-18C */
14870 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
Jesse Barnesb690e962010-07-19 13:53:12 -070014871};
14872
14873static void intel_init_quirks(struct drm_device *dev)
14874{
14875 struct pci_dev *d = dev->pdev;
14876 int i;
14877
14878 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14879 struct intel_quirk *q = &intel_quirks[i];
14880
14881 if (d->device == q->device &&
14882 (d->subsystem_vendor == q->subsystem_vendor ||
14883 q->subsystem_vendor == PCI_ANY_ID) &&
14884 (d->subsystem_device == q->subsystem_device ||
14885 q->subsystem_device == PCI_ANY_ID))
14886 q->hook(dev);
14887 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014888 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14889 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14890 intel_dmi_quirks[i].hook(dev);
14891 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014892}
14893
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014894/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014895static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014896{
David Weinehall52a05c32016-08-22 13:32:44 +030014897 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014898 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014899 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014900
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014901 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014902 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014903 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014904 sr1 = inb(VGA_SR_DATA);
14905 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014906 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014907 udelay(300);
14908
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014909 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014910 POSTING_READ(vga_reg);
14911}
14912
Daniel Vetterf8175862012-04-10 15:50:11 +020014913void intel_modeset_init_hw(struct drm_device *dev)
14914{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014915 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014916
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014917 intel_update_cdclk(dev_priv);
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014918 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014919
Ville Syrjälä46f16e62016-10-31 22:37:22 +020014920 intel_init_clock_gating(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020014921}
14922
Matt Roperd93c0372015-12-03 11:37:41 -080014923/*
14924 * Calculate what we think the watermarks should be for the state we've read
14925 * out of the hardware and then immediately program those watermarks so that
14926 * we ensure the hardware settings match our internal state.
14927 *
14928 * We can calculate what we think WM's should be by creating a duplicate of the
14929 * current state (which was constructed during hardware readout) and running it
14930 * through the atomic check code to calculate new watermark values in the
14931 * state object.
14932 */
14933static void sanitize_watermarks(struct drm_device *dev)
14934{
14935 struct drm_i915_private *dev_priv = to_i915(dev);
14936 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014937 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014938 struct drm_crtc *crtc;
14939 struct drm_crtc_state *cstate;
14940 struct drm_modeset_acquire_ctx ctx;
14941 int ret;
14942 int i;
14943
14944 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014945 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014946 return;
14947
14948 /*
14949 * We need to hold connection_mutex before calling duplicate_state so
14950 * that the connector loop is protected.
14951 */
14952 drm_modeset_acquire_init(&ctx, 0);
14953retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014954 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014955 if (ret == -EDEADLK) {
14956 drm_modeset_backoff(&ctx);
14957 goto retry;
14958 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014959 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014960 }
14961
14962 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14963 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014964 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014965
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014966 intel_state = to_intel_atomic_state(state);
14967
Matt Ropered4a6a72016-02-23 17:20:13 -080014968 /*
14969 * Hardware readout is the only time we don't want to calculate
14970 * intermediate watermarks (since we don't trust the current
14971 * watermarks).
14972 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014973 if (!HAS_GMCH_DISPLAY(dev_priv))
14974 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014975
Matt Roperd93c0372015-12-03 11:37:41 -080014976 ret = intel_atomic_check(dev, state);
14977 if (ret) {
14978 /*
14979 * If we fail here, it means that the hardware appears to be
14980 * programmed in a way that shouldn't be possible, given our
14981 * understanding of watermark requirements. This might mean a
14982 * mistake in the hardware readout code or a mistake in the
14983 * watermark calculations for a given platform. Raise a WARN
14984 * so that this is noticeable.
14985 *
14986 * If this actually happens, we'll have to just leave the
14987 * BIOS-programmed watermarks untouched and hope for the best.
14988 */
14989 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014990 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014991 }
14992
14993 /* Write calculated watermark values back */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010014994 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roperd93c0372015-12-03 11:37:41 -080014995 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14996
Matt Ropered4a6a72016-02-23 17:20:13 -080014997 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014998 dev_priv->display.optimize_watermarks(intel_state, cs);
Matt Roperd93c0372015-12-03 11:37:41 -080014999 }
15000
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020015001put_state:
Chris Wilson08536952016-10-14 13:18:18 +010015002 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015003fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015004 drm_modeset_drop_locks(&ctx);
15005 drm_modeset_acquire_fini(&ctx);
15006}
15007
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015008int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080015009{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015010 struct drm_i915_private *dev_priv = to_i915(dev);
15011 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015012 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015013 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015014
15015 drm_mode_config_init(dev);
15016
15017 dev->mode_config.min_width = 0;
15018 dev->mode_config.min_height = 0;
15019
Dave Airlie019d96c2011-09-29 16:20:42 +010015020 dev->mode_config.preferred_depth = 24;
15021 dev->mode_config.prefer_shadow = 1;
15022
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015023 dev->mode_config.allow_fb_modifiers = true;
15024
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015025 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015026
Andrea Arcangeli400c19d2017-04-07 01:23:45 +020015027 init_llist_head(&dev_priv->atomic_helper.free_list);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015028 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000015029 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015030
Jesse Barnesb690e962010-07-19 13:53:12 -070015031 intel_init_quirks(dev);
15032
Ville Syrjälä62d75df2016-10-31 22:37:25 +020015033 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015034
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015035 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015036 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070015037
Lukas Wunner69f92f62015-07-15 13:57:35 +020015038 /*
15039 * There may be no VBT; and if the BIOS enabled SSC we can
15040 * just keep using it to avoid unnecessary flicker. Whereas if the
15041 * BIOS isn't using it, don't assume it will work even if the VBT
15042 * indicates as much.
15043 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015044 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020015045 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15046 DREF_SSC1_ENABLE);
15047
15048 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15049 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15050 bios_lvds_use_ssc ? "en" : "dis",
15051 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15052 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15053 }
15054 }
15055
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015056 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015057 dev->mode_config.max_width = 2048;
15058 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015059 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015060 dev->mode_config.max_width = 4096;
15061 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015062 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015063 dev->mode_config.max_width = 8192;
15064 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015065 }
Damien Lespiau068be562014-03-28 14:17:49 +000015066
Jani Nikula2a307c22016-11-30 17:43:04 +020015067 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15068 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015069 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015070 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015071 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15072 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15073 } else {
15074 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15075 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15076 }
15077
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015078 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015079
Zhao Yakui28c97732009-10-09 11:39:41 +080015080 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015081 INTEL_INFO(dev_priv)->num_pipes,
15082 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015083
Damien Lespiau055e3932014-08-18 13:49:10 +010015084 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015085 int ret;
15086
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015087 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015088 if (ret) {
15089 drm_mode_config_cleanup(dev);
15090 return ret;
15091 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015092 }
15093
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015094 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015095
Ville Syrjälä5be6e332017-02-20 16:04:43 +020015096 intel_update_czclk(dev_priv);
15097 intel_modeset_init_hw(dev);
15098
Ville Syrjäläb2045352016-05-13 23:41:27 +030015099 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020015100 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030015101
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015102 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015103 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015104 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000015105
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015106 drm_modeset_lock_all(dev);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015107 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015108 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015109
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015110 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015111 struct intel_initial_plane_config plane_config = {};
15112
Jesse Barnes46f297f2014-03-07 08:57:48 -080015113 if (!crtc->active)
15114 continue;
15115
Jesse Barnes46f297f2014-03-07 08:57:48 -080015116 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015117 * Note that reserving the BIOS fb up front prevents us
15118 * from stuffing other stolen allocations like the ring
15119 * on top. This prevents some ugliness at boot time, and
15120 * can even allow for smooth boot transitions if the BIOS
15121 * fb is large enough for the active pipe configuration.
15122 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015123 dev_priv->display.get_initial_plane_config(crtc,
15124 &plane_config);
15125
15126 /*
15127 * If the fb is shared between multiple heads, we'll
15128 * just get the first one.
15129 */
15130 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015131 }
Matt Roperd93c0372015-12-03 11:37:41 -080015132
15133 /*
15134 * Make sure hardware watermarks really match the state we read out.
15135 * Note that we need to do this after reconstructing the BIOS fb's
15136 * since the watermark calculation done here will use pstate->fb.
15137 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020015138 if (!HAS_GMCH_DISPLAY(dev_priv))
15139 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015140
15141 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010015142}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015143
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015144void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15145{
15146 /* 640x480@60Hz, ~25175 kHz */
15147 struct dpll clock = {
15148 .m1 = 18,
15149 .m2 = 7,
15150 .p1 = 13,
15151 .p2 = 4,
15152 .n = 2,
15153 };
15154 u32 dpll, fp;
15155 int i;
15156
15157 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15158
15159 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15160 pipe_name(pipe), clock.vco, clock.dot);
15161
15162 fp = i9xx_dpll_compute_fp(&clock);
15163 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15164 DPLL_VGA_MODE_DIS |
15165 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15166 PLL_P2_DIVIDE_BY_4 |
15167 PLL_REF_INPUT_DREFCLK |
15168 DPLL_VCO_ENABLE;
15169
15170 I915_WRITE(FP0(pipe), fp);
15171 I915_WRITE(FP1(pipe), fp);
15172
15173 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15174 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15175 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15176 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15177 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15178 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15179 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15180
15181 /*
15182 * Apparently we need to have VGA mode enabled prior to changing
15183 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15184 * dividers, even though the register value does change.
15185 */
15186 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15187 I915_WRITE(DPLL(pipe), dpll);
15188
15189 /* Wait for the clocks to stabilize. */
15190 POSTING_READ(DPLL(pipe));
15191 udelay(150);
15192
15193 /* The pixel multiplier can only be updated once the
15194 * DPLL is enabled and the clocks are stable.
15195 *
15196 * So write it again.
15197 */
15198 I915_WRITE(DPLL(pipe), dpll);
15199
15200 /* We do this three times for luck */
15201 for (i = 0; i < 3 ; i++) {
15202 I915_WRITE(DPLL(pipe), dpll);
15203 POSTING_READ(DPLL(pipe));
15204 udelay(150); /* wait for warmup */
15205 }
15206
15207 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15208 POSTING_READ(PIPECONF(pipe));
15209}
15210
15211void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15212{
15213 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15214 pipe_name(pipe));
15215
15216 assert_plane_disabled(dev_priv, PLANE_A);
15217 assert_plane_disabled(dev_priv, PLANE_B);
15218
15219 I915_WRITE(PIPECONF(pipe), 0);
15220 POSTING_READ(PIPECONF(pipe));
15221
15222 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
15223 DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
15224
15225 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15226 POSTING_READ(DPLL(pipe));
15227}
15228
Daniel Vetterfa555832012-10-10 23:14:00 +020015229static bool
15230intel_check_plane_mapping(struct intel_crtc *crtc)
15231{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015232 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015233 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015234
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015235 if (INTEL_INFO(dev_priv)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015236 return true;
15237
Ville Syrjälä649636e2015-09-22 19:50:01 +030015238 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015239
15240 if ((val & DISPLAY_PLANE_ENABLE) &&
15241 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15242 return false;
15243
15244 return true;
15245}
15246
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015247static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15248{
15249 struct drm_device *dev = crtc->base.dev;
15250 struct intel_encoder *encoder;
15251
15252 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15253 return true;
15254
15255 return false;
15256}
15257
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015258static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15259{
15260 struct drm_device *dev = encoder->base.dev;
15261 struct intel_connector *connector;
15262
15263 for_each_connector_on_encoder(dev, &encoder->base, connector)
15264 return connector;
15265
15266 return NULL;
15267}
15268
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015269static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15270 enum transcoder pch_transcoder)
15271{
15272 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15273 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15274}
15275
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015276static void intel_sanitize_crtc(struct intel_crtc *crtc,
15277 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter24929352012-07-02 20:28:59 +020015278{
15279 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015280 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015281 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015282
Daniel Vetter24929352012-07-02 20:28:59 +020015283 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015284 if (!transcoder_is_dsi(cpu_transcoder)) {
15285 i915_reg_t reg = PIPECONF(cpu_transcoder);
15286
15287 I915_WRITE(reg,
15288 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15289 }
Daniel Vetter24929352012-07-02 20:28:59 +020015290
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015291 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015292 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015293 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015294 struct intel_plane *plane;
15295
Daniel Vetter96256042015-02-13 21:03:42 +010015296 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015297
15298 /* Disable everything but the primary plane */
15299 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15300 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15301 continue;
15302
Ville Syrjälä72259532017-03-02 19:15:05 +020015303 trace_intel_disable_plane(&plane->base, crtc);
Ville Syrjälä282dbf92017-03-27 21:55:33 +030015304 plane->disable_plane(plane, crtc);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015305 }
Daniel Vetter96256042015-02-13 21:03:42 +010015306 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015307
Daniel Vetter24929352012-07-02 20:28:59 +020015308 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015309 * disable the crtc (and hence change the state) if it is wrong. Note
15310 * that gen4+ has a fixed plane -> pipe mapping. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015311 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015312 bool plane;
15313
Ville Syrjälä78108b72016-05-27 20:59:19 +030015314 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15315 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015316
15317 /* Pipe has the wrong plane attached and the plane is active.
15318 * Temporarily change the plane mapping and disable everything
15319 * ... */
15320 plane = crtc->plane;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010015321 crtc->base.primary->state->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015322 crtc->plane = !plane;
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015323 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015324 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015325 }
Daniel Vetter24929352012-07-02 20:28:59 +020015326
15327 /* Adjust the state of the output pipe according to whether we
15328 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015329 if (crtc->active && !intel_crtc_has_encoders(crtc))
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015330 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015331
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010015332 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015333 /*
15334 * We start out with underrun reporting disabled to avoid races.
15335 * For correct bookkeeping mark this on active crtcs.
15336 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015337 * Also on gmch platforms we dont have any hardware bits to
15338 * disable the underrun reporting. Which means we need to start
15339 * out with underrun reporting disabled also on inactive pipes,
15340 * since otherwise we'll complain about the garbage we read when
15341 * e.g. coming up after runtime pm.
15342 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015343 * No protection against concurrent access is required - at
15344 * worst a fifo underrun happens which also sets this to false.
15345 */
15346 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015347 /*
15348 * We track the PCH trancoder underrun reporting state
15349 * within the crtc. With crtc for pipe A housing the underrun
15350 * reporting state for PCH transcoder A, crtc for pipe B housing
15351 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15352 * and marking underrun reporting as disabled for the non-existing
15353 * PCH transcoders B and C would prevent enabling the south
15354 * error interrupt (see cpt_can_enable_serr_int()).
15355 */
15356 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15357 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015358 }
Daniel Vetter24929352012-07-02 20:28:59 +020015359}
15360
15361static void intel_sanitize_encoder(struct intel_encoder *encoder)
15362{
15363 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015364
15365 /* We need to check both for a crtc link (meaning that the
15366 * encoder is active and trying to read from a pipe) and the
15367 * pipe itself being active. */
15368 bool has_active_crtc = encoder->base.crtc &&
15369 to_intel_crtc(encoder->base.crtc)->active;
15370
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015371 connector = intel_encoder_find_connector(encoder);
15372 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015373 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15374 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015375 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015376
15377 /* Connector is active, but has no active pipe. This is
15378 * fallout from our resume register restoring. Disable
15379 * the encoder manually again. */
15380 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015381 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15382
Daniel Vetter24929352012-07-02 20:28:59 +020015383 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15384 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015385 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015386 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015387 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015388 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015389 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015390 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015391
15392 /* Inconsistent output/port/pipe state happens presumably due to
15393 * a bug in one of the get_hw_state functions. Or someplace else
15394 * in our code, like the register restore mess on resume. Clamp
15395 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015396
15397 connector->base.dpms = DRM_MODE_DPMS_OFF;
15398 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015399 }
15400 /* Enabled encoders without active connectors will be fixed in
15401 * the crtc fixup. */
15402}
15403
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015404void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015405{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015406 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015407
Imre Deak04098752014-02-18 00:02:16 +020015408 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15409 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015410 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015411 }
15412}
15413
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015414void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015415{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015416 /* This function can be called both from intel_modeset_setup_hw_state or
15417 * at a very early point in our resume sequence, where the power well
15418 * structures are not yet restored. Since this function is at a very
15419 * paranoid "someone might have enabled VGA while we were not looking"
15420 * level, just check if the power well is enabled instead of trying to
15421 * follow the "don't touch the power well if we don't need it" policy
15422 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015423 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015424 return;
15425
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015426 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015427
15428 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015429}
15430
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015431static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015432{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015433 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015434
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015435 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015436}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015437
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015438/* FIXME read out full plane state for all planes */
15439static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015440{
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020015441 struct intel_plane *primary = to_intel_plane(crtc->base.primary);
15442 bool visible;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015443
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020015444 visible = crtc->active && primary_get_hw_state(primary);
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015445
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020015446 intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
15447 to_intel_plane_state(primary->base.state),
15448 visible);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015449}
15450
Daniel Vetter30e984d2013-06-05 13:34:17 +020015451static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015452{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015453 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015454 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015455 struct intel_crtc *crtc;
15456 struct intel_encoder *encoder;
15457 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015458 struct drm_connector_list_iter conn_iter;
Daniel Vetter53589012013-06-05 13:34:16 +020015459 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015460
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015461 dev_priv->active_crtcs = 0;
15462
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015463 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015464 struct intel_crtc_state *crtc_state =
15465 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015466
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015467 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015468 memset(crtc_state, 0, sizeof(*crtc_state));
15469 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015470
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015471 crtc_state->base.active = crtc_state->base.enable =
15472 dev_priv->display.get_pipe_config(crtc, crtc_state);
15473
15474 crtc->base.enabled = crtc_state->base.enable;
15475 crtc->active = crtc_state->base.active;
15476
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015477 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015478 dev_priv->active_crtcs |= 1 << crtc->pipe;
15479
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015480 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015481
Ville Syrjälä78108b72016-05-27 20:59:19 +030015482 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15483 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015484 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015485 }
15486
Daniel Vetter53589012013-06-05 13:34:16 +020015487 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15488 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15489
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015490 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015491 &pll->state.hw_state);
15492 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015493 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015494 struct intel_crtc_state *crtc_state =
15495 to_intel_crtc_state(crtc->base.state);
15496
15497 if (crtc_state->base.active &&
15498 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015499 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015500 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015501 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015502
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015503 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015504 pll->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015505 }
15506
Damien Lespiaub2784e12014-08-05 11:29:37 +010015507 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015508 pipe = 0;
15509
15510 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015511 struct intel_crtc_state *crtc_state;
15512
Ville Syrjälä98187832016-10-31 22:37:10 +020015513 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015514 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015515
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015516 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015517 crtc_state->output_types |= 1 << encoder->type;
15518 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015519 } else {
15520 encoder->base.crtc = NULL;
15521 }
15522
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015523 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015524 encoder->base.base.id, encoder->base.name,
15525 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015526 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015527 }
15528
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015529 drm_connector_list_iter_begin(dev, &conn_iter);
15530 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter24929352012-07-02 20:28:59 +020015531 if (connector->get_hw_state(connector)) {
15532 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015533
15534 encoder = connector->encoder;
15535 connector->base.encoder = &encoder->base;
15536
15537 if (encoder->base.crtc &&
15538 encoder->base.crtc->state->active) {
15539 /*
15540 * This has to be done during hardware readout
15541 * because anything calling .crtc_disable may
15542 * rely on the connector_mask being accurate.
15543 */
15544 encoder->base.crtc->state->connector_mask |=
15545 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015546 encoder->base.crtc->state->encoder_mask |=
15547 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015548 }
15549
Daniel Vetter24929352012-07-02 20:28:59 +020015550 } else {
15551 connector->base.dpms = DRM_MODE_DPMS_OFF;
15552 connector->base.encoder = NULL;
15553 }
15554 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015555 connector->base.base.id, connector->base.name,
15556 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015557 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015558 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015559
15560 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015561 struct intel_crtc_state *crtc_state =
15562 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015563 int pixclk = 0;
15564
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015565 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015566 if (crtc_state->base.active) {
15567 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15568 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015569 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15570
15571 /*
15572 * The initial mode needs to be set in order to keep
15573 * the atomic core happy. It wants a valid mode if the
15574 * crtc's enabled, so we do the above call.
15575 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015576 * But we don't set all the derived state fully, hence
15577 * set a flag to indicate that a full recalculation is
15578 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015579 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015580 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015581
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015582 intel_crtc_compute_pixel_rate(crtc_state);
15583
15584 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15585 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15586 pixclk = crtc_state->pixel_rate;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015587 else
15588 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15589
15590 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015591 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015592 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15593
Daniel Vetter5caa0fe2017-05-09 16:03:29 +020015594 drm_calc_timestamping_constants(&crtc->base,
15595 &crtc_state->base.adjusted_mode);
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015596 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015597 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015598
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015599 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15600
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015601 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015602 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015603}
15604
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015605static void
15606get_encoder_power_domains(struct drm_i915_private *dev_priv)
15607{
15608 struct intel_encoder *encoder;
15609
15610 for_each_intel_encoder(&dev_priv->drm, encoder) {
15611 u64 get_domains;
15612 enum intel_display_power_domain domain;
15613
15614 if (!encoder->get_power_domains)
15615 continue;
15616
15617 get_domains = encoder->get_power_domains(encoder);
15618 for_each_power_domain(domain, get_domains)
15619 intel_display_power_get(dev_priv, domain);
15620 }
15621}
15622
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015623/* Scan out the current hw modeset state,
15624 * and sanitizes it to the current state
15625 */
15626static void
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015627intel_modeset_setup_hw_state(struct drm_device *dev,
15628 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015629{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015630 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015631 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015632 struct intel_crtc *crtc;
15633 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015634 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015635
15636 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015637
15638 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015639 get_encoder_power_domains(dev_priv);
15640
Damien Lespiaub2784e12014-08-05 11:29:37 +010015641 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015642 intel_sanitize_encoder(encoder);
15643 }
15644
Damien Lespiau055e3932014-08-18 13:49:10 +010015645 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020015646 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015647
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015648 intel_sanitize_crtc(crtc, ctx);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015649 intel_dump_pipe_config(crtc, crtc->config,
15650 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015651 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015652
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015653 intel_modeset_update_connector_atomic_state(dev);
15654
Daniel Vetter35c95372013-07-17 06:55:04 +020015655 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15656 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15657
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015658 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015659 continue;
15660
15661 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15662
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015663 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015664 pll->on = false;
15665 }
15666
Ville Syrjälä04548cb2017-04-21 21:14:29 +030015667 if (IS_G4X(dev_priv)) {
15668 g4x_wm_get_hw_state(dev);
15669 g4x_wm_sanitize(dev_priv);
15670 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015671 vlv_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015672 vlv_wm_sanitize(dev_priv);
15673 } else if (IS_GEN9(dev_priv)) {
Pradeep Bhat30789992014-11-04 17:06:45 +000015674 skl_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015675 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015676 ilk_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015677 }
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015678
15679 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015680 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015681
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015682 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015683 if (WARN_ON(put_domains))
15684 modeset_put_power_domains(dev_priv, put_domains);
15685 }
15686 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015687
Imre Deak8d8c3862017-02-17 17:39:46 +020015688 intel_power_domains_verify_state(dev_priv);
15689
Paulo Zanoni010cf732016-01-19 11:35:48 -020015690 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015691}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015692
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015693void intel_display_resume(struct drm_device *dev)
15694{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015695 struct drm_i915_private *dev_priv = to_i915(dev);
15696 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15697 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015698 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015699
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015700 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015701 if (state)
15702 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015703
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015704 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015705
Maarten Lankhorst73974892016-08-05 23:28:27 +030015706 while (1) {
15707 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15708 if (ret != -EDEADLK)
15709 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015710
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015711 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015712 }
15713
Maarten Lankhorst73974892016-08-05 23:28:27 +030015714 if (!ret)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010015715 ret = __intel_display_resume(dev, state, &ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +030015716
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015717 drm_modeset_drop_locks(&ctx);
15718 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015719
Chris Wilson08536952016-10-14 13:18:18 +010015720 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015721 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015722 if (state)
15723 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015724}
15725
15726void intel_modeset_gem_init(struct drm_device *dev)
15727{
Chris Wilsondc979972016-05-10 14:10:04 +010015728 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015729
Chris Wilsondc979972016-05-10 14:10:04 +010015730 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015731
Chris Wilson1ee8da62016-05-12 12:43:23 +010015732 intel_setup_overlay(dev_priv);
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015733}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015734
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015735int intel_connector_register(struct drm_connector *connector)
15736{
15737 struct intel_connector *intel_connector = to_intel_connector(connector);
15738 int ret;
15739
15740 ret = intel_backlight_device_register(intel_connector);
15741 if (ret)
15742 goto err;
15743
15744 return 0;
15745
15746err:
15747 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015748}
15749
Chris Wilsonc191eca2016-06-17 11:40:33 +010015750void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020015751{
Chris Wilsone63d87c2016-06-17 11:40:34 +010015752 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015753
Chris Wilsone63d87c2016-06-17 11:40:34 +010015754 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015755 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015756}
15757
Jesse Barnes79e53942008-11-07 14:24:08 -080015758void intel_modeset_cleanup(struct drm_device *dev)
15759{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015760 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015761
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015762 flush_work(&dev_priv->atomic_helper.free_work);
15763 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15764
Chris Wilsondc979972016-05-10 14:10:04 +010015765 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020015766
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015767 /*
15768 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015769 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015770 * experience fancy races otherwise.
15771 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015772 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015773
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015774 /*
15775 * Due to the hpd irq storm handling the hotplug work can re-arm the
15776 * poll handlers. Hence disable polling after hpd handling is shut down.
15777 */
Keith Packardf87ea762010-10-03 19:36:26 -070015778 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015779
Jesse Barnes723bfd72010-10-07 16:01:13 -070015780 intel_unregister_dsm_handler();
15781
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015782 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015783
Chris Wilson1630fe72011-07-08 12:22:42 +010015784 /* flush any delayed tasks or pending work */
15785 flush_scheduled_work();
15786
Jesse Barnes79e53942008-11-07 14:24:08 -080015787 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015788
Chris Wilson1ee8da62016-05-12 12:43:23 +010015789 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015790
Chris Wilsondc979972016-05-10 14:10:04 +010015791 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010015792
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015793 intel_teardown_gmbus(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015794}
15795
Chris Wilsondf0e9242010-09-09 16:20:55 +010015796void intel_connector_attach_encoder(struct intel_connector *connector,
15797 struct intel_encoder *encoder)
15798{
15799 connector->encoder = encoder;
15800 drm_mode_connector_attach_encoder(&connector->base,
15801 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015802}
Dave Airlie28d52042009-09-21 14:33:58 +100015803
15804/*
15805 * set vga decode state - true == enable VGA decode
15806 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015807int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015808{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015809 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015810 u16 gmch_ctrl;
15811
Chris Wilson75fa0412014-02-07 18:37:02 -020015812 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15813 DRM_ERROR("failed to read control word\n");
15814 return -EIO;
15815 }
15816
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015817 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15818 return 0;
15819
Dave Airlie28d52042009-09-21 14:33:58 +100015820 if (state)
15821 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15822 else
15823 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015824
15825 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15826 DRM_ERROR("failed to write control word\n");
15827 return -EIO;
15828 }
15829
Dave Airlie28d52042009-09-21 14:33:58 +100015830 return 0;
15831}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015832
Chris Wilson98a2f412016-10-12 10:05:18 +010015833#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15834
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015835struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015836
15837 u32 power_well_driver;
15838
Chris Wilson63b66e52013-08-08 15:12:06 +020015839 int num_transcoders;
15840
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015841 struct intel_cursor_error_state {
15842 u32 control;
15843 u32 position;
15844 u32 base;
15845 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015846 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015847
15848 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015849 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015850 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015851 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015852 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015853
15854 struct intel_plane_error_state {
15855 u32 control;
15856 u32 stride;
15857 u32 size;
15858 u32 pos;
15859 u32 addr;
15860 u32 surface;
15861 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015862 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015863
15864 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015865 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015866 enum transcoder cpu_transcoder;
15867
15868 u32 conf;
15869
15870 u32 htotal;
15871 u32 hblank;
15872 u32 hsync;
15873 u32 vtotal;
15874 u32 vblank;
15875 u32 vsync;
15876 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015877};
15878
15879struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015880intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015881{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015882 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015883 int transcoders[] = {
15884 TRANSCODER_A,
15885 TRANSCODER_B,
15886 TRANSCODER_C,
15887 TRANSCODER_EDP,
15888 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015889 int i;
15890
Chris Wilsonc0336662016-05-06 15:40:21 +010015891 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015892 return NULL;
15893
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015894 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015895 if (error == NULL)
15896 return NULL;
15897
Chris Wilsonc0336662016-05-06 15:40:21 +010015898 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015899 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15900
Damien Lespiau055e3932014-08-18 13:49:10 +010015901 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015902 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015903 __intel_display_power_is_enabled(dev_priv,
15904 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015905 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015906 continue;
15907
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015908 error->cursor[i].control = I915_READ(CURCNTR(i));
15909 error->cursor[i].position = I915_READ(CURPOS(i));
15910 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015911
15912 error->plane[i].control = I915_READ(DSPCNTR(i));
15913 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015914 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015915 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015916 error->plane[i].pos = I915_READ(DSPPOS(i));
15917 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015918 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015919 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015920 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015921 error->plane[i].surface = I915_READ(DSPSURF(i));
15922 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15923 }
15924
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015925 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015926
Chris Wilsonc0336662016-05-06 15:40:21 +010015927 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030015928 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015929 }
15930
Jani Nikula4d1de972016-03-18 17:05:42 +020015931 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015932 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015933 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015934 error->num_transcoders++; /* Account for eDP. */
15935
15936 for (i = 0; i < error->num_transcoders; i++) {
15937 enum transcoder cpu_transcoder = transcoders[i];
15938
Imre Deakddf9c532013-11-27 22:02:02 +020015939 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015940 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015941 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015942 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015943 continue;
15944
Chris Wilson63b66e52013-08-08 15:12:06 +020015945 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15946
15947 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15948 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15949 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15950 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15951 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15952 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15953 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015954 }
15955
15956 return error;
15957}
15958
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015959#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15960
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015961void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015962intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015963 struct intel_display_error_state *error)
15964{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000015965 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015966 int i;
15967
Chris Wilson63b66e52013-08-08 15:12:06 +020015968 if (!error)
15969 return;
15970
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015971 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010015972 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015973 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015974 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015975 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015976 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015977 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015978 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015979 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015980 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015981
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015982 err_printf(m, "Plane [%d]:\n", i);
15983 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15984 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015985 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015986 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15987 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015988 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010015989 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015990 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015991 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015992 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15993 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015994 }
15995
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015996 err_printf(m, "Cursor [%d]:\n", i);
15997 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15998 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15999 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016000 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016001
16002 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016003 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016004 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016005 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016006 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016007 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16008 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16009 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16010 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16011 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16012 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16013 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16014 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016015}
Chris Wilson98a2f412016-10-12 10:05:18 +010016016
16017#endif